use of com.google.gerrit.reviewdb.client.FixReplacement in project gerrit by GerritCodeReview.
the class FixReplacementInterpreterTest method replacementsCanDeleteALine.
@Test
public void replacementsCanDeleteALine() throws Exception {
FixReplacement fixReplacement = new FixReplacement(filePath1, new Range(2, 0, 3, 0), "");
mockFileContent(filePath1, "First line\nSecond line\nThird line\n");
replay(fileContentUtil);
List<TreeModification> treeModifications = toTreeModifications(fixReplacement);
assertThatList(treeModifications).onlyElement().asChangeFileContentModification().newContent().isEqualTo("First line\nThird line\n");
}
use of com.google.gerrit.reviewdb.client.FixReplacement in project gerrit by GerritCodeReview.
the class FixReplacementInterpreterTest method replacementsMustNotReferToNotExistingOffsetOfIntermediateLine.
@Test
public void replacementsMustNotReferToNotExistingOffsetOfIntermediateLine() throws Exception {
FixReplacement fixReplacement = new FixReplacement(filePath1, new Range(1, 0, 1, 11), "modified");
mockFileContent(filePath1, "First line\nSecond line\nThird line\n");
replay(fileContentUtil);
expectedException.expect(ResourceConflictException.class);
toTreeModifications(fixReplacement);
}
use of com.google.gerrit.reviewdb.client.FixReplacement in project gerrit by GerritCodeReview.
the class FixReplacementInterpreterTest method replacementsMayOccurOnSameLine.
@Test
public void replacementsMayOccurOnSameLine() throws Exception {
FixReplacement fixReplacement1 = new FixReplacement(filePath1, new Range(2, 0, 2, 6), "A");
FixReplacement fixReplacement2 = new FixReplacement(filePath1, new Range(2, 7, 2, 11), "modification");
mockFileContent(filePath1, "First line\nSecond line\nThird line\n");
replay(fileContentUtil);
List<TreeModification> treeModifications = toTreeModifications(fixReplacement1, fixReplacement2);
assertThatList(treeModifications).onlyElement().asChangeFileContentModification().newContent().isEqualTo("First line\nA modification\nThird line\n");
}
use of com.google.gerrit.reviewdb.client.FixReplacement in project gerrit by GerritCodeReview.
the class FixReplacementInterpreterTest method replacementsMustNotReferToNotExistingLine.
@Test
public void replacementsMustNotReferToNotExistingLine() throws Exception {
FixReplacement fixReplacement = new FixReplacement(filePath1, new Range(5, 0, 5, 0), "A new line\n");
mockFileContent(filePath1, "First line\nSecond line\nThird line\n");
replay(fileContentUtil);
expectedException.expect(ResourceConflictException.class);
toTreeModifications(fixReplacement);
}
use of com.google.gerrit.reviewdb.client.FixReplacement in project gerrit by GerritCodeReview.
the class FixReplacementInterpreter method getNewFileContent.
private static String getNewFileContent(String fileContent, List<FixReplacement> fixReplacements) throws ResourceConflictException {
List<FixReplacement> sortedReplacements = new ArrayList<>(fixReplacements);
sortedReplacements.sort(ASC_RANGE_FIX_REPLACEMENT_COMPARATOR);
LineIdentifier lineIdentifier = new LineIdentifier(fileContent);
StringModifier fileContentModifier = new StringModifier(fileContent);
for (FixReplacement fixReplacement : sortedReplacements) {
Comment.Range range = fixReplacement.range;
try {
int startLineIndex = lineIdentifier.getStartIndexOfLine(range.startLine);
int startLineLength = lineIdentifier.getLengthOfLine(range.startLine);
int endLineIndex = lineIdentifier.getStartIndexOfLine(range.endLine);
int endLineLength = lineIdentifier.getLengthOfLine(range.endLine);
if (range.startChar > startLineLength || range.endChar > endLineLength) {
throw new ResourceConflictException(String.format("Range %s refers to a non-existent offset (start line length: %s," + " end line length: %s)", toString(range), startLineLength, endLineLength));
}
int startIndex = startLineIndex + range.startChar;
int endIndex = endLineIndex + range.endChar;
fileContentModifier.replace(startIndex, endIndex, fixReplacement.replacement);
} catch (StringIndexOutOfBoundsException e) {
// need to map this exception and thus provide a meaningful error.
throw new ResourceConflictException(String.format("Cannot apply fix replacement for range %s", toString(range)), e);
}
}
return fileContentModifier.getResult();
}
Aggregations