| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.diff.display.internal; |
| 21 |
|
|
| 22 |
|
import java.lang.reflect.ParameterizedType; |
| 23 |
|
import java.util.HashMap; |
| 24 |
|
import java.util.List; |
| 25 |
|
import java.util.Map; |
| 26 |
|
|
| 27 |
|
import org.junit.Assert; |
| 28 |
|
import org.junit.Rule; |
| 29 |
|
import org.junit.Test; |
| 30 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
| 31 |
|
import org.xwiki.diff.DiffManager; |
| 32 |
|
import org.xwiki.diff.DiffResult; |
| 33 |
|
import org.xwiki.diff.display.InlineDiffChunk; |
| 34 |
|
import org.xwiki.diff.display.InlineDiffChunk.Type; |
| 35 |
|
import org.xwiki.diff.display.Splitter; |
| 36 |
|
import org.xwiki.diff.display.UnifiedDiffBlock; |
| 37 |
|
import org.xwiki.diff.display.UnifiedDiffConfiguration; |
| 38 |
|
import org.xwiki.diff.display.UnifiedDiffDisplayer; |
| 39 |
|
import org.xwiki.diff.display.UnifiedDiffElement; |
| 40 |
|
import org.xwiki.diff.internal.DefaultDiffManager; |
| 41 |
|
import org.xwiki.test.ComponentManagerRule; |
| 42 |
|
import org.xwiki.test.annotation.ComponentList; |
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
@version |
| 49 |
|
@since |
| 50 |
|
|
| 51 |
|
@ComponentList({ |
| 52 |
|
LineSplitter.class, |
| 53 |
|
CharSplitter.class, |
| 54 |
|
DefaultDiffManager.class, |
| 55 |
|
DefaultUnifiedDiffDisplayer.class, |
| 56 |
|
DefaultInlineDiffDisplayer.class |
| 57 |
|
}) |
| |
|
| 100% |
Uncovered Elements: 0 (43) |
Complexity: 8 |
Complexity Density: 0.24 |
|
| 58 |
|
public class ExtendedDiffDisplayerTest |
| 59 |
|
{ |
| 60 |
|
@Rule |
| 61 |
|
public final ComponentManagerRule componentManager = new ComponentManagerRule(); |
| 62 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 63 |
1 |
@Test... |
| 64 |
|
public void testLineAdded() throws Exception |
| 65 |
|
{ |
| 66 |
1 |
execute("one\nthree", "one\ntwo\nthree", "@@ -1,2 +1,3 @@\n one\n+two\n three\n"); |
| 67 |
|
} |
| 68 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 69 |
1 |
@Test... |
| 70 |
|
public void testLineRemoved() throws Exception |
| 71 |
|
{ |
| 72 |
1 |
execute("one\ntwo\nthree", "one\nthree", "@@ -1,3 +1,2 @@\n one\n-two\n three\n"); |
| 73 |
|
} |
| 74 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 75 |
1 |
@Test... |
| 76 |
|
public void testLineChanged() throws Exception |
| 77 |
|
{ |
| 78 |
1 |
execute("one\ntwo\nthree", "one\ntWo\nthree", "@@ -1,3 +1,3 @@\n one\n-t-w-o\n+t+W+o\n three\n"); |
| 79 |
|
} |
| 80 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 81 |
1 |
@Test... |
| 82 |
|
public void testLineReplaced() throws Exception |
| 83 |
|
{ |
| 84 |
1 |
execute("one\ntwo\nthree", "one\ntWo\nextra\nthree", "@@ -1,3 +1,4 @@\n one\n-two\n+tWo\n+extra\n three\n"); |
| 85 |
|
} |
| 86 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 87 |
1 |
@Test... |
| 88 |
|
public void testLinesChanges() throws Exception |
| 89 |
|
{ |
| 90 |
1 |
execute("one\ntwo\nthree\nfour", "one\ntWo\nthrEE\nfour", |
| 91 |
|
"@@ -1,4 +1,4 @@\n one\n-t-w-o\n-thr-ee-\n+t+W+o\n+thr+EE+\n four\n"); |
| 92 |
1 |
execute("one\ntwoo\nthre\nfour", "one\ntwo\nthree\nfour", |
| 93 |
|
"@@ -1,4 +1,4 @@\n one\n-two-o-\n-thre\n+two\n+thre+e+\n four\n"); |
| 94 |
|
} |
| 95 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 96 |
1 |
@Test... |
| 97 |
|
public void testNullInput() throws Exception |
| 98 |
|
{ |
| 99 |
1 |
execute(null, null, ""); |
| 100 |
|
} |
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
|
@param |
| 106 |
|
@param |
| 107 |
|
@param |
| 108 |
|
@throws |
| 109 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (29) |
Complexity: 2 |
Complexity Density: 0.07 |
|
| 110 |
7 |
private void execute(String previous, String next, String expected) throws Exception... |
| 111 |
|
{ |
| 112 |
7 |
ParameterizedType lineSplitterType = |
| 113 |
|
new DefaultParameterizedType(null, Splitter.class, String.class, String.class); |
| 114 |
7 |
Splitter<String, String> lineSplitter = this.componentManager.getInstance(lineSplitterType, "line"); |
| 115 |
7 |
List<String> previousLines = lineSplitter.split(previous); |
| 116 |
7 |
List<String> nextLines = lineSplitter.split(next); |
| 117 |
|
|
| 118 |
7 |
DiffManager diffManager = this.componentManager.getInstance(DiffManager.class); |
| 119 |
7 |
DiffResult<String> diffResult = diffManager.diff(previousLines, nextLines, null); |
| 120 |
|
|
| 121 |
7 |
ParameterizedType charSplitterType = |
| 122 |
|
new DefaultParameterizedType(null, Splitter.class, String.class, Character.class); |
| 123 |
7 |
Splitter<String, Character> charSplitter = this.componentManager.getInstance(charSplitterType); |
| 124 |
|
|
| 125 |
7 |
UnifiedDiffDisplayer unifiedDiffDisplayer = this.componentManager.getInstance(UnifiedDiffDisplayer.class); |
| 126 |
7 |
UnifiedDiffConfiguration<String, Character> config = unifiedDiffDisplayer.getDefaultConfiguration(); |
| 127 |
7 |
config.setSplitter(charSplitter); |
| 128 |
|
|
| 129 |
7 |
Map<Type, String> separators = new HashMap<Type, String>(); |
| 130 |
7 |
separators.put(Type.ADDED, "+"); |
| 131 |
7 |
separators.put(Type.DELETED, "-"); |
| 132 |
7 |
separators.put(Type.UNMODIFIED, ""); |
| 133 |
|
|
| 134 |
7 |
StringBuilder actual = new StringBuilder(); |
| 135 |
7 |
for (UnifiedDiffBlock<String, Character> block : unifiedDiffDisplayer.display(diffResult, config)) { |
| 136 |
6 |
actual.append(String.format("@@ -%s,%s +%s,%s @@\n", block.getPreviousStart() + 1, block.getPreviousSize(), |
| 137 |
|
block.getNextStart() + 1, block.getNextSize())); |
| 138 |
6 |
for (UnifiedDiffElement<String, Character> line : block) { |
| 139 |
27 |
if (line.getChunks() != null) { |
| 140 |
10 |
actual.append(line.getType().getSymbol()); |
| 141 |
10 |
for (InlineDiffChunk<Character> chunk : line.getChunks()) { |
| 142 |
22 |
String separator = separators.get(chunk.getType()); |
| 143 |
22 |
actual.append(separator).append(chunk).append(separator); |
| 144 |
|
} |
| 145 |
10 |
actual.append('\n'); |
| 146 |
|
} else { |
| 147 |
17 |
actual.append(line); |
| 148 |
|
} |
| 149 |
|
} |
| 150 |
|
} |
| 151 |
7 |
Assert.assertEquals(expected, actual.toString()); |
| 152 |
|
} |
| 153 |
|
} |