Skip to content

Commit

Permalink
Addresses issue #1402 - Possible to search and replace method/scenari…
Browse files Browse the repository at this point in the history
…o names with new name. (#1403)

* Addresses issue #1402 - Possible to search and replace method/scenario names with new name.

* Changed UI based on review comments.

* Review comment suggestion.

Co-authored-by: Fried Hoeben <github@hsac.nl>

* Review comment suggestion.

Co-authored-by: Fried Hoeben <github@hsac.nl>

* Review comment suggestion.

Co-authored-by: Fried Hoeben <github@hsac.nl>

* Review comment suggestion.

Co-authored-by: Fried Hoeben <github@hsac.nl>

Co-authored-by: Fried Hoeben <github@hsac.nl>
  • Loading branch information
suratdas and fhoeben committed Dec 26, 2022
1 parent 1226a00 commit 9994d29
Show file tree
Hide file tree
Showing 9 changed files with 796 additions and 12 deletions.
34 changes: 30 additions & 4 deletions src/fitnesse/resources/templates/refactorForm.vm
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
#if( !$type || $type == "replace" )
<h2>Replace</h2>
<form action="$refactoredRootPage" method="post">
<h2>Replace Text</h2>
<input type="hidden" name="responder" value="replace"/>
<fieldset>
<label for="searchString">Search String:</label>
<input type="text" id="searchString" name="searchString" value="#if ($request.hasInput("searchString"))$request.getInput("searchString")#end"/>
<input type="text" size="50" id="searchString" name="searchString" value="#if ($request.hasInput("searchString"))$request.getInput("searchString")#end"/>
</fieldset>
<fieldset>
<label for="replacementString">Replacement:</label>
<input type="text" id="replacementString" name="replacementString" value="#if ($request.hasInput("replacementString"))$request.getInput("replacementString")#end"/>
<input type="text" size="50" id="replacementString" name="replacementString" value="#if ($request.hasInput("replacementString"))$request.getInput("replacementString")#end"/>
</fieldset>
<fieldset class="buttons">
<input type="submit" name="replace" value="Replace!"/>
<a class="button" href="$viewLocation">Cancel</a>
</fieldset>

<p><strong>Search &amp; Replace: </strong>
Please note that this feature is experimental! It uses java-based regular expressions. For an introduction, take a look <a href="http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html" target="_new">here</a> (new window).
Please note that this feature is experimental! It uses java-based regular expressions. For an introduction, take a look <a href="http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html" target="_new">here</a> (new window).</p>
</form>

<form action="$refactoredRootPage" method="post">
<h2>Replace Method</h2>
<input type="hidden" name="responder" value="replace"/>
<input type="hidden" name="isMethodReplace"/>
<fieldset>
<label for="searchString">Search Method:</label>
<input type="text" size="50" id="searchStringMethod" name="searchString" value="#if ($request.hasInput("searchString"))$request.getInput("searchString")#end"/>
</fieldset>
<fieldset>
<label for="replacementString">Replacing Method:</label>
<input type="text" size="50" id="replacementString" name="replacementString" value="#if ($request.hasInput("replacementString"))$request.getInput("replacementString")#end"/>
</fieldset>
<fieldset class="buttons">
<input type="submit" name="replace" value="Replace Method!"/>
<a class="button" href="$viewLocation">Cancel</a>
</fieldset>
<p>
Reflect an updated method/scenario name across all existing test cases. To accomplish this, you can supply an existing line using that method from any test page. e.g. <strong>|Go to|url|</strong> can be changed to <strong>|Navigate to|url|</strong>. Keep the following in mind:
<ul>
<li>This will not change the parameters (if any) in the wiki.</li>
<li>This will replace any line which evaluates to a method name including a line in script table, a scenario or a decision table having a similar name. This will be desired in most cases. But if you have multiple classes with the same method of which you only changed one this will not work as desired, since all calls are replaced.</li>
<li>It is a good idea to review the updated lines locally (e.g. using Git) and undo any undesired changes before sharing/committing the tests. Most IDE have a version control comparison and selective undo feature.</li>
</ul>
</p>
</form>
#end

Expand Down
29 changes: 21 additions & 8 deletions src/fitnesse/responders/refactoring/SearchReplaceResponder.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package fitnesse.responders.refactoring;

import fitnesse.wiki.refactoring.ContentReplacingSearchObserver;
import fitnesse.wiki.search.PageFinder;
import fitnesse.wiki.search.RegularExpressionWikiPageFinder;
import fitnesse.components.TraversalListener;
import fitnesse.responders.search.ResultResponder;
import fitnesse.wiki.WikiPage;
import fitnesse.wiki.refactoring.ContentReplacingSearchObserver;
import fitnesse.wiki.refactoring.MethodReplacingSearchObserver;
import fitnesse.wiki.search.MethodWikiPageFinder;
import fitnesse.wiki.search.PageFinder;
import fitnesse.wiki.search.RegularExpressionWikiPageFinder;

public class SearchReplaceResponder extends ResultResponder {

Expand All @@ -21,19 +23,30 @@ protected String getTemplate() {
@Override
protected String getTitle() {
return String.format("Replacing matching content \"%s\" with content \"%s\"",
getSearchString(), getReplacementString());
getSearchString(), getReplacementString());
}

@Override
protected PageFinder getPageFinder(TraversalListener<WikiPage> webOutputObserver) {
String searchString = getSearchString();
String replacementString = getReplacementString();

ContentReplacingSearchObserver contentReplaceObserver =
new ContentReplacingSearchObserver(searchString, replacementString);
if (isMethodReplace()) {
MethodReplacingSearchObserver methodReplaceObserver =
new MethodReplacingSearchObserver(searchString, replacementString);

return new MethodWikiPageFinder(searchString,
new SearchReplaceTraverser(methodReplaceObserver, webOutputObserver));
} else {
ContentReplacingSearchObserver contentReplaceObserver =
new ContentReplacingSearchObserver(searchString, replacementString);
return new RegularExpressionWikiPageFinder(searchString,
new SearchReplaceTraverser(contentReplaceObserver, webOutputObserver));
}
}

return new RegularExpressionWikiPageFinder(searchString,
new SearchReplaceTraverser(contentReplaceObserver, webOutputObserver));
private boolean isMethodReplace() {
return request.hasInput("isMethodReplace");
}

private String getReplacementString() {
Expand Down
82 changes: 82 additions & 0 deletions src/fitnesse/util/WikiPageLineProcessingUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package fitnesse.util;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

public class WikiPageLineProcessingUtil {

public static boolean isColumnSpecialWikiKeyWord(String stringPassed) {
stringPassed = stringPassed.trim();
return stringPassed.isEmpty() || stringPassed.startsWith("$") || stringPassed.equals("ensure") || stringPassed.equals("reject") || stringPassed.equals("check") || stringPassed.equals("check not") || stringPassed.equals("show") || stringPassed.equals("note");
}

public static boolean doesLineNeedExtraLastColumn(String stringPassed) {
String firstColumn = Arrays.asList(stringPassed.split("\\|")).get(1).trim();
return firstColumn.equals("check") || firstColumn.equals("check not");
}

public static String getLastColumn(String stringPassed) {
if (isValidLine(stringPassed)) {
List<String> allColumns = Arrays.asList(stringPassed.split("\\|"));
return allColumns.get(allColumns.size() - 1);
}
return "";
}

private static boolean isValidLine(String textPassed) {
return (textPassed.startsWith("|") && textPassed.trim().endsWith("|"));
}

public static String getMethodNameFromLine(String textPassed) {
String methodNameToReturn = "";
if (isValidLine(textPassed)) {
List<String> methodSplit = new ArrayList<>();
methodSplit.addAll(Arrays.asList(textPassed.split("\\|")));
methodSplit.remove(0);//First one is always empty. We can discard it.
String firstColumn = methodSplit.get(0);
int counter = !isColumnSpecialWikiKeyWord(firstColumn) ? 0 : 1;
boolean lineHasExtraColumn = doesLineNeedExtraLastColumn(textPassed);
for (int i = counter; i < methodSplit.size(); i += 2) {
//Ensure last column is not included in the method name.
if (lineHasExtraColumn && methodSplit.size() == (i + 1)) {
} else {
List<String> currentCellWords = Arrays.asList(methodSplit.get(i).trim().split(" "));
for (String currentCellWord : currentCellWords) {
if (!currentCellWord.trim().isEmpty()) {
methodNameToReturn += org.apache.commons.lang3.StringUtils.capitalize(currentCellWord.trim());
}
}
}
}
methodNameToReturn = StringUtils.uncapitalize(methodNameToReturn);
}
return methodNameToReturn;
}

public static LinkedHashMap<Integer, String> getRowColumnsExcludingKeywordInFirstColumnIfPresent(String currentLine) {
LinkedHashMap<Integer, String> allColumnsOfCurrentLine = new LinkedHashMap<>();
String remainingText = currentLine;
int currentIndexOfPipe = 1;
boolean isFirstColumnSpecialKey = true;
while (remainingText.contains("|")) {
int nextIndexOfPipe = currentLine.indexOf("|", currentIndexOfPipe);
String currentColumnText = currentLine.substring(currentIndexOfPipe, nextIndexOfPipe);
if (isFirstColumnSpecialKey && !isColumnSpecialWikiKeyWord(currentColumnText)) {
isFirstColumnSpecialKey = false;
allColumnsOfCurrentLine.put(currentIndexOfPipe, currentColumnText);
} else if (!isFirstColumnSpecialKey) {
allColumnsOfCurrentLine.put(currentIndexOfPipe, currentColumnText);
}
currentIndexOfPipe = nextIndexOfPipe + 1;
remainingText = currentLine.substring(nextIndexOfPipe + 1);
}
if (doesLineNeedExtraLastColumn(currentLine))
allColumnsOfCurrentLine.remove(allColumnsOfCurrentLine.size() - 1);
return allColumnsOfCurrentLine;
}

}
80 changes: 80 additions & 0 deletions src/fitnesse/wiki/refactoring/MethodReplacingSearchObserver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package fitnesse.wiki.refactoring;

import fitnesse.components.TraversalListener;
import fitnesse.wiki.PageData;
import fitnesse.wiki.WikiPage;

import java.util.Iterator;
import java.util.LinkedHashMap;

import static fitnesse.util.WikiPageLineProcessingUtil.*;

public class MethodReplacingSearchObserver implements TraversalListener<WikiPage> {

private String searchMethodString;
private String replacingMethodString;

public MethodReplacingSearchObserver(String searchString, String replacement) {
this.searchMethodString = searchString;
this.replacingMethodString = replacement;
}

@Override
public void process(WikiPage page) {
PageData pageData = page.getData();

String content = pageData.getContent();
String[] lines = content.split(System.lineSeparator());
String newPageContent = "";
boolean isModified = false;
String targetMethod = getMethodNameFromLine(searchMethodString);
for (String eachLineOfFile : lines) {
if (!eachLineOfFile.startsWith("|") || !getMethodNameFromLine(eachLineOfFile).equals(targetMethod)) {
newPageContent += eachLineOfFile + System.lineSeparator();
} else {
isModified = true;
String modifiedLine = "";
LinkedHashMap<Integer, String> toBeReplacedText = getRowColumnsExcludingKeywordInFirstColumnIfPresent(eachLineOfFile);
Iterator<Integer> toBeReplacedKeys = toBeReplacedText.keySet().iterator();
LinkedHashMap<Integer, String> toReplaceText = getRowColumnsExcludingKeywordInFirstColumnIfPresent(replacingMethodString);
Iterator<Integer> toReplaceKeys = toReplaceText.keySet().iterator();
for (int i = 0; toBeReplacedKeys.hasNext() || toReplaceKeys.hasNext(); i++) {
int toBeReplacedIndex = toBeReplacedKeys.hasNext() ? toBeReplacedKeys.next() : -1;
int toReplaceIndex = toReplaceKeys.hasNext() ? toReplaceKeys.next() : -1;
modifiedLine = modifiedLine.isEmpty() ? eachLineOfFile.substring(0, toBeReplacedIndex - 1) : modifiedLine;
if (toBeReplacedIndex > 0 && toReplaceIndex > 0) {
if (i % 2 == 1) {
modifiedLine += "|" + eachLineOfFile.substring(toBeReplacedIndex, toBeReplacedIndex + toBeReplacedText.get(toBeReplacedIndex).length());
} else {
modifiedLine += "|" + toReplaceText.get(toReplaceIndex);
}
}
//Below case is when there are more columns in replacing text than in original text.
else if (toBeReplacedIndex == -1) {
//All remaining columns of replacing text should be appended.
modifiedLine += "|" + toReplaceText.get(toReplaceIndex);
while (toReplaceKeys.hasNext()) {
modifiedLine += "|" + toReplaceText.get(toReplaceKeys.next());
}
break;
}
//below case is when there are more columns in original text which need to be removed.
else if (toReplaceIndex == -1) {
modifiedLine += "|";
break;
}
}
if (doesLineNeedExtraLastColumn(eachLineOfFile)) {
modifiedLine += getLastColumn(eachLineOfFile);
}
modifiedLine = (!modifiedLine.isEmpty() && !modifiedLine.endsWith("|")) ? modifiedLine + "|" : modifiedLine;
newPageContent += modifiedLine + System.lineSeparator();
}
}
if (isModified) {
pageData.setContent(newPageContent);
}
page.commit(pageData);
}

}
34 changes: 34 additions & 0 deletions src/fitnesse/wiki/search/MethodWikiPageFinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package fitnesse.wiki.search;

import fitnesse.components.TraversalListener;
import fitnesse.wiki.WikiPage;

import static fitnesse.util.WikiPageLineProcessingUtil.getMethodNameFromLine;

public class MethodWikiPageFinder extends WikiPageFinder {

private String methodToFind;

public MethodWikiPageFinder(String methodToFind, TraversalListener<? super WikiPage> observer) {
super(observer);
this.methodToFind = methodToFind;
}

@Override
protected boolean pageMatches(WikiPage page) {
String pageContent = page.getData().getContent();
String targetMethod = getMethodNameFromLine(this.methodToFind);
String[] contentLines = pageContent.split(System.lineSeparator());

//Both the inputs should follow the correct format
if (!methodToFind.startsWith("|") || !methodToFind.endsWith("|"))
return false;

for (String eachLine : contentLines) {
if (getMethodNameFromLine(eachLine).equals(targetMethod))
return true;
}
return false;
}

}
Loading

0 comments on commit 9994d29

Please sign in to comment.