Search in sources :

Example 11 with FixReplacement

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");
}
Also used : TreeModification(com.google.gerrit.server.edit.tree.TreeModification) FixReplacement(com.google.gerrit.reviewdb.client.FixReplacement) Range(com.google.gerrit.reviewdb.client.Comment.Range) Test(org.junit.Test)

Example 12 with FixReplacement

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);
}
Also used : FixReplacement(com.google.gerrit.reviewdb.client.FixReplacement) Range(com.google.gerrit.reviewdb.client.Comment.Range) Test(org.junit.Test)

Example 13 with 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");
}
Also used : TreeModification(com.google.gerrit.server.edit.tree.TreeModification) FixReplacement(com.google.gerrit.reviewdb.client.FixReplacement) Range(com.google.gerrit.reviewdb.client.Comment.Range) Test(org.junit.Test)

Example 14 with FixReplacement

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);
}
Also used : FixReplacement(com.google.gerrit.reviewdb.client.FixReplacement) Range(com.google.gerrit.reviewdb.client.Comment.Range) Test(org.junit.Test)

Example 15 with 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();
}
Also used : Comment(com.google.gerrit.reviewdb.client.Comment) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ArrayList(java.util.ArrayList) FixReplacement(com.google.gerrit.reviewdb.client.FixReplacement)

Aggregations

FixReplacement (com.google.gerrit.reviewdb.client.FixReplacement)16 Range (com.google.gerrit.reviewdb.client.Comment.Range)13 Test (org.junit.Test)13 TreeModification (com.google.gerrit.server.edit.tree.TreeModification)9 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)3 Comment (com.google.gerrit.reviewdb.client.Comment)3 ArrayList (java.util.ArrayList)3 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 AutoValue (com.google.auto.value.AutoValue)1 MoreObjects (com.google.common.base.MoreObjects)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Strings (com.google.common.base.Strings)1 ListMultimap (com.google.common.collect.ListMultimap)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 Ordering (com.google.common.collect.Ordering)1 HashCode (com.google.common.hash.HashCode)1 Hashing (com.google.common.hash.Hashing)1 Nullable (com.google.gerrit.common.Nullable)1 RawInputUtil (com.google.gerrit.common.RawInputUtil)1