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

File DefaultDiffManagerTest.java

 

Code metrics

2
114
8
1
356
200
9
0.08
14.25
8
1.12

Classes

Class Line # Actions
DefaultDiffManagerTest 38 114 0% 9 2
0.98387198.4%
 

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.internal;
21   
22    import java.util.ArrayList;
23    import java.util.Arrays;
24    import java.util.Collections;
25    import java.util.List;
26   
27    import org.apache.commons.lang3.StringUtils;
28    import org.junit.Assert;
29    import org.junit.Rule;
30    import org.junit.Test;
31    import org.xwiki.diff.Delta.Type;
32    import org.xwiki.diff.DiffManager;
33    import org.xwiki.diff.DiffResult;
34    import org.xwiki.diff.MergeResult;
35    import org.xwiki.logging.LogLevel;
36    import org.xwiki.test.mockito.MockitoComponentMockingRule;
37   
 
38    public class DefaultDiffManagerTest
39    {
40    @Rule
41    public final MockitoComponentMockingRule<DiffManager> mocker = new MockitoComponentMockingRule<DiffManager>(
42    DefaultDiffManager.class);
43   
44    /**
45    * @param str the single line text
46    * @return the lines
47    */
 
48  87 toggle private static List<Character> toCharacters(String str)
49    {
50  87 List<Character> characters;
51   
52  87 if (str != null) {
53  87 characters = new ArrayList<Character>(str.length());
54   
55  87 for (char c : str.toCharArray()) {
56  323 characters.add(c);
57    }
58    } else {
59  0 characters = Collections.emptyList();
60    }
61   
62  87 return characters;
63    }
64   
 
65  5 toggle private static String toString(List<Character> characters)
66    {
67  5 return StringUtils.join(characters, null);
68    }
69   
70    // Tests
71   
 
72  1 toggle @Test
73    public void testDiffStringList() throws Exception
74    {
75    // Null
76   
77  1 DiffResult<String> result = this.mocker.getComponentUnderTest().diff(null, null, null);
78   
79  1 Assert.assertTrue(result.getPatch().isEmpty());
80   
81    // Empty
82   
83  1 result = this.mocker.getComponentUnderTest().diff(Collections.<String>emptyList(),
84    Collections.<String>emptyList(), null);
85   
86  1 Assert.assertTrue(result.getPatch().isEmpty());
87   
88    // Equals
89   
90  1 result = this.mocker.getComponentUnderTest().diff(Arrays.asList("equals"), Arrays.asList("equals"), null);
91   
92  1 Assert.assertTrue(result.getPatch().isEmpty());
93   
94    // Previous empty
95   
96  1 result = this.mocker.getComponentUnderTest().diff(Collections.<String>emptyList(), Arrays.asList("next"), null);
97   
98  1 Assert.assertEquals(1, result.getPatch().size());
99  1 Assert.assertEquals(Type.INSERT, result.getPatch().get(0).getType());
100  1 Assert.assertEquals(Arrays.asList("next"), result.getPatch().get(0).getNext().getElements());
101  1 Assert.assertEquals(0, result.getPatch().get(0).getNext().getIndex());
102   
103    // Next empty
104   
105  1 result =
106    this.mocker.getComponentUnderTest().diff(Arrays.asList("previous"), Collections.<String>emptyList(), null);
107   
108  1 Assert.assertEquals(1, result.getPatch().size());
109  1 Assert.assertEquals(Type.DELETE, result.getPatch().get(0).getType());
110  1 Assert.assertEquals(Arrays.asList("previous"), result.getPatch().get(0).getPrevious().getElements());
111  1 Assert.assertEquals(0, result.getPatch().get(0).getPrevious().getIndex());
112    }
113   
 
114  1 toggle @Test
115    public void testDiffCharList() throws Exception
116    {
117    // Equals
118   
119  1 DiffResult<Character> result =
120    this.mocker.getComponentUnderTest().diff(Arrays.asList('a'), Arrays.asList('a'), null);
121   
122  1 Assert.assertTrue(result.getPatch().isEmpty());
123   
124    // Changed
125   
126  1 result = this.mocker.getComponentUnderTest().diff(Arrays.asList('a'), Arrays.asList('b'), null);
127   
128  1 Assert.assertEquals(1, result.getPatch().size());
129  1 Assert.assertEquals(Type.CHANGE, result.getPatch().get(0).getType());
130    }
131   
 
132  1 toggle @Test
133    public void testMergeStringList() throws Exception
134    {
135  1 MergeResult<String> result;
136   
137    // Only new
138   
139  1 result = this.mocker.getComponentUnderTest().merge(
140    Arrays.asList("some content"), Arrays.asList("some new content"), Arrays.asList("some content"), null);
141   
142  1 Assert.assertEquals(Arrays.asList("some new content"), result.getMerged());
143   
144    // Only current
145   
146  1 result = this.mocker.getComponentUnderTest().merge(
147    Arrays.asList("some content"), Arrays.asList("some content"), Arrays.asList("some current content"), null);
148   
149  1 Assert.assertEquals(Arrays.asList("some current content"), result.getMerged());
150   
151    // New after
152   
153  1 result = this.mocker.getComponentUnderTest().merge(
154    Arrays.asList("some content"), Arrays.asList("some content", "after"), Arrays.asList("some content"), null);
155   
156  1 Assert.assertEquals(Arrays.asList("some content", "after"), result.getMerged());
157   
158    // Before and after
159   
160  1 result = this.mocker.getComponentUnderTest().merge(Arrays.asList("some content"),
161    Arrays.asList("before", "some content"), Arrays.asList("some content", "after"), null);
162   
163  1 Assert.assertEquals(Arrays.asList("before", "some content", "after"), result.getMerged());
164   
165    // After and before
166   
167  1 result = this.mocker.getComponentUnderTest().merge(Arrays.asList("some content"),
168    Arrays.asList("some content", "after"), Arrays.asList("before", "some content"), null);
169   
170  1 Assert.assertEquals(Arrays.asList("before", "some content", "after"), result.getMerged());
171   
172    // Same current and next
173   
174  1 result = this.mocker.getComponentUnderTest().merge(Arrays.asList("some content"),
175    Arrays.asList("some new content"), Arrays.asList("some new content"), null);
176   
177  1 Assert.assertEquals(Arrays.asList("some new content"), result.getMerged());
178    }
179   
 
180  1 toggle @Test
181    public void testMergeCharList() throws Exception
182    {
183  1 MergeResult<Character> result;
184   
185    // New empty
186   
187  1 result = this.mocker.getComponentUnderTest()
188    .merge(toCharacters("a"), toCharacters(""), toCharacters("b"), null);
189   
190  1 Assert.assertEquals(1, result.getLog().getLogs(LogLevel.ERROR).size());
191  1 Assert.assertEquals(toCharacters("b"), result.getMerged());
192   
193    // New before
194   
195  1 result = this.mocker.getComponentUnderTest()
196    .merge(toCharacters("bc"), toCharacters("abc"), toCharacters("bc"), null);
197   
198  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
199  1 Assert.assertEquals(toCharacters("abc"), result.getMerged());
200   
201    // New after
202   
203  1 result = this.mocker.getComponentUnderTest()
204    .merge(toCharacters("ab"), toCharacters("abc"), toCharacters("ab"), null);
205   
206  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
207  1 Assert.assertEquals(toCharacters("abc"), result.getMerged());
208   
209    // New middle
210   
211  1 result = this.mocker.getComponentUnderTest()
212    .merge(toCharacters("ac"), toCharacters("abc"), toCharacters("ac"), null);
213   
214  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
215  1 Assert.assertEquals(toCharacters("abc"), result.getMerged());
216   
217    // Before and after
218   
219  1 result =
220    this.mocker.getComponentUnderTest().merge(toCharacters("b"), toCharacters("ab"), toCharacters("bc"), null);
221   
222  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
223  1 Assert.assertEquals(toCharacters("abc"), result.getMerged());
224   
225    // After and before
226   
227  1 result =
228    this.mocker.getComponentUnderTest().merge(toCharacters("b"), toCharacters("bc"), toCharacters("ab"), null);
229   
230  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
231  1 Assert.assertEquals(toCharacters("abc"), result.getMerged());
232   
233    // Insert current and next
234   
235  1 result = this.mocker.getComponentUnderTest()
236    .merge(toCharacters("abc"), toCharacters("aibc"), toCharacters("abcj"), null);
237   
238  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
239  1 Assert.assertEquals(toCharacters("aibcj"), result.getMerged());
240   
241  1 result = this.mocker.getComponentUnderTest()
242    .merge(toCharacters("abc"), toCharacters("abcj"), toCharacters("aibc"), null);
243   
244  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
245  1 Assert.assertEquals(toCharacters("aibcj"), result.getMerged());
246   
247  1 result = this.mocker.getComponentUnderTest()
248    .merge(toCharacters("abc"), toCharacters("ajbc"), toCharacters("aibc"), null);
249   
250  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
251  1 Assert.assertEquals(toCharacters("ajibc"), result.getMerged());
252   
253  1 result = this.mocker.getComponentUnderTest()
254    .merge(toCharacters("ab"), toCharacters("aijb"), toCharacters("aib"), null);
255   
256  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
257  1 Assert.assertEquals(toCharacters("aijb"), result.getMerged());
258   
259  1 result = this.mocker.getComponentUnderTest()
260    .merge(toCharacters("ab"), toCharacters("ajb"), toCharacters("aijb"), null);
261   
262  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
263  1 Assert.assertEquals(toCharacters("aijb"), result.getMerged());
264   
265  1 result =
266    this.mocker.getComponentUnderTest().merge(toCharacters(""), toCharacters("ab"), toCharacters("abc"), null);
267   
268  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
269  1 Assert.assertEquals(toCharacters("abc"), result.getMerged());
270   
271  1 result =
272    this.mocker.getComponentUnderTest().merge(toCharacters(""), toCharacters("abc"), toCharacters("ab"), null);
273   
274  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
275  1 Assert.assertEquals(toCharacters("abc"), result.getMerged());
276   
277  1 result =
278    this.mocker.getComponentUnderTest().merge(toCharacters(""), toCharacters("bc"), toCharacters("abc"), null);
279   
280  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
281  1 Assert.assertEquals(toCharacters("abc"), result.getMerged());
282   
283  1 result =
284    this.mocker.getComponentUnderTest().merge(toCharacters(""), toCharacters("abc"), toCharacters("bc"), null);
285   
286  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
287  1 Assert.assertEquals(toCharacters("abc"), result.getMerged());
288   
289    // Misc
290   
291  1 result = this.mocker.getComponentUnderTest().merge(toCharacters("Alice Macro"),
292    toCharacters("Alice Wiki Macro (upgraded)"), toCharacters("Alice Extension"), null);
293   
294  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
295  1 Assert.assertEquals(toCharacters("Alice Wiki Extension (upgraded)"), result.getMerged());
296   
297  1 result = this.mocker.getComponentUnderTest()
298    .merge(toCharacters("$a(b)"), toCharacters("$c(d)e"), toCharacters("$c(d)e"), null);
299   
300  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
301  1 Assert.assertEquals("$c(d)e", toString(result.getMerged()));
302    }
303   
 
304  1 toggle @Test
305    public void testMergeCharOnConflicts() throws Exception
306    {
307  1 MergeResult<Character> result;
308   
309    // Current and new at the same place
310  1 result =
311    this.mocker.getComponentUnderTest().merge(toCharacters("a"), toCharacters("b"), toCharacters("c"), null);
312   
313  1 Assert.assertEquals(1, result.getLog().getLogs(LogLevel.ERROR).size());
314  1 Assert.assertEquals(toCharacters("c"), result.getMerged());
315   
316    // Current and new in conflict at different indices
317  1 result = this.mocker.getComponentUnderTest()
318    .merge(toCharacters("abcd"), toCharacters("yycd"), toCharacters("azzd"), null);
319   
320  1 Assert.assertEquals(1, result.getLog().getLogs(LogLevel.ERROR).size());
321  1 Assert.assertEquals("yycd", toString(result.getMerged()));
322   
323    // Current and new in conflict at different indices
324  1 result = this.mocker.getComponentUnderTest()
325    .merge(toCharacters("abcd"), toCharacters("azzd"), toCharacters("yycd"), null);
326   
327  1 Assert.assertEquals(1, result.getLog().getLogs(LogLevel.ERROR).size());
328  1 Assert.assertEquals("yycd", toString(result.getMerged()));
329   
330    // New overlapping current
331  1 result = this.mocker.getComponentUnderTest()
332    .merge(toCharacters("aabbcc"), toCharacters("arrbcc"), toCharacters("ddddcc"), null);
333   
334  1 Assert.assertEquals(1, result.getLog().getLogs(LogLevel.ERROR).size());
335  1 Assert.assertEquals("ddddcc", toString(result.getMerged()));
336   
337    // current overlapping new
338  1 result = this.mocker.getComponentUnderTest()
339    .merge(toCharacters("aabbcc"), toCharacters("ddddcc"), toCharacters("arrbcc"), null);
340   
341  1 Assert.assertEquals(1, result.getLog().getLogs(LogLevel.ERROR).size());
342  1 Assert.assertEquals("ddddcc", toString(result.getMerged()));
343    }
344   
 
345  1 toggle @Test
346    public void testMergeWithEmptyNew() throws Exception
347    {
348  1 MergeResult<Character> result;
349   
350  1 result =
351    this.mocker.getComponentUnderTest().merge(toCharacters("a"), toCharacters(""), toCharacters("b"), null);
352   
353  1 Assert.assertEquals(1, result.getLog().getLogs(LogLevel.ERROR).size());
354  1 Assert.assertEquals(toCharacters("b"), result.getMerged());
355    }
356    }