1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.diff.display.internal

File ExtendedDiffDisplayerTest.java

 

Code metrics

2
34
7
1
153
104
8
0.24
4.86
7
1.14

Classes

Class Line # Actions
ExtendedDiffDisplayerTest 58 34 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Tests how the unified and inline diff displayers can be mixed to generate a diff both at line (unified) and character
46    * (inline) level.
47    *
48    * @version $Id: d53e44c8d1795a8946385c87d755b00e799cddb7 $
49    * @since 4.1M2
50    */
51    @ComponentList({
52    LineSplitter.class,
53    CharSplitter.class,
54    DefaultDiffManager.class,
55    DefaultUnifiedDiffDisplayer.class,
56    DefaultInlineDiffDisplayer.class
57    })
 
58    public class ExtendedDiffDisplayerTest
59    {
60    @Rule
61    public final ComponentManagerRule componentManager = new ComponentManagerRule();
62   
 
63  1 toggle @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   
 
69  1 toggle @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   
 
75  1 toggle @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   
 
81  1 toggle @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   
 
87  1 toggle @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   
 
96  1 toggle @Test
97    public void testNullInput() throws Exception
98    {
99  1 execute(null, null, "");
100    }
101   
102    /**
103    * Generates the extended diff between the given versions and asserts if it meets the expectation.
104    *
105    * @param previous the previous version
106    * @param next the next version
107    * @param expected the expected extended diff
108    * @throws Exception if creating the diff fails
109    */
 
110  7 toggle 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    }