1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.diff.internal

File DefaultDiffManagerTest.java

 

Code metrics

2
119
9
1
377
213
10
0.08
13.22
9
1.11

Classes

Class Line # Actions
DefaultDiffManagerTest 38 119 0% 10 2
0.984615498.5%
 

Contributing tests

This file is covered by 7 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  345 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   
 
181  1 toggle @Test
182    public void testMergeCharList() throws Exception
183    {
184  1 MergeResult<Character> result;
185   
186    // New empty
187   
188  1 result = this.mocker.getComponentUnderTest()
189    .merge(toCharacters("a"), toCharacters(""), toCharacters("b"), null);
190   
191  1 Assert.assertEquals(1, result.getLog().getLogs(LogLevel.ERROR).size());
192  1 Assert.assertEquals(toCharacters("b"), result.getMerged());
193   
194    // New before
195   
196  1 result = this.mocker.getComponentUnderTest()
197    .merge(toCharacters("bc"), toCharacters("abc"), toCharacters("bc"), null);
198   
199  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
200  1 Assert.assertEquals(toCharacters("abc"), result.getMerged());
201   
202    // New after
203   
204  1 result = this.mocker.getComponentUnderTest()
205    .merge(toCharacters("ab"), toCharacters("abc"), toCharacters("ab"), null);
206   
207  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
208  1 Assert.assertEquals(toCharacters("abc"), result.getMerged());
209   
210    // New middle
211   
212  1 result = this.mocker.getComponentUnderTest()
213    .merge(toCharacters("ac"), toCharacters("abc"), toCharacters("ac"), null);
214   
215  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
216  1 Assert.assertEquals(toCharacters("abc"), result.getMerged());
217   
218    // Before and after
219   
220  1 result =
221    this.mocker.getComponentUnderTest().merge(toCharacters("b"), toCharacters("ab"), toCharacters("bc"), null);
222   
223  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
224  1 Assert.assertEquals(toCharacters("abc"), result.getMerged());
225   
226    // After and before
227   
228  1 result =
229    this.mocker.getComponentUnderTest().merge(toCharacters("b"), toCharacters("bc"), toCharacters("ab"), null);
230   
231  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
232  1 Assert.assertEquals(toCharacters("abc"), result.getMerged());
233   
234    // Insert current and next
235   
236  1 result = this.mocker.getComponentUnderTest()
237    .merge(toCharacters("abc"), toCharacters("aibc"), toCharacters("abcj"), null);
238   
239  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
240  1 Assert.assertEquals(toCharacters("aibcj"), result.getMerged());
241   
242  1 result = this.mocker.getComponentUnderTest()
243    .merge(toCharacters("abc"), toCharacters("abcj"), toCharacters("aibc"), null);
244   
245  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
246  1 Assert.assertEquals(toCharacters("aibcj"), result.getMerged());
247   
248  1 result = this.mocker.getComponentUnderTest()
249    .merge(toCharacters("abc"), toCharacters("ajbc"), toCharacters("aibc"), null);
250   
251  1 Assert.assertEquals(1, result.getLog().getLogs(LogLevel.ERROR).size());
252  1 Assert.assertEquals(toCharacters("ajibc"), result.getMerged());
253   
254  1 result = this.mocker.getComponentUnderTest()
255    .merge(toCharacters("ab"), toCharacters("aijb"), toCharacters("aib"), null);
256   
257  1 Assert.assertEquals(1, result.getLog().getLogs(LogLevel.ERROR).size());
258  1 Assert.assertEquals(toCharacters("aijb"), result.getMerged());
259   
260  1 result = this.mocker.getComponentUnderTest()
261    .merge(toCharacters("ab"), toCharacters("ajb"), toCharacters("aijb"), null);
262   
263  1 Assert.assertEquals(1, result.getLog().getLogs(LogLevel.ERROR).size());
264  1 Assert.assertEquals(toCharacters("aijb"), result.getMerged());
265   
266  1 result =
267    this.mocker.getComponentUnderTest().merge(toCharacters("d"), toCharacters("ab d"), toCharacters("abc d"), null);
268   
269  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
270  1 Assert.assertEquals(toCharacters("abc d"), result.getMerged());
271   
272  1 result =
273    this.mocker.getComponentUnderTest().merge(toCharacters("d"), toCharacters("abc d"), toCharacters("ab d"), null);
274   
275  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
276  1 Assert.assertEquals(toCharacters("abc d"), result.getMerged());
277   
278  1 result =
279    this.mocker.getComponentUnderTest().merge(toCharacters("d"), toCharacters("bcd"), toCharacters("abcd"), null);
280   
281  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
282  1 Assert.assertEquals(toCharacters("abcd"), result.getMerged());
283   
284  1 result =
285    this.mocker.getComponentUnderTest().merge(toCharacters("d"), toCharacters("abcd"), toCharacters("bcd"), null);
286   
287  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
288  1 Assert.assertEquals(toCharacters("abcd"), result.getMerged());
289   
290    // Misc
291   
292  1 result = this.mocker.getComponentUnderTest().merge(toCharacters("Alice Macro"),
293    toCharacters("Alice Wiki Macro (upgraded)"), toCharacters("Alice Extension"), null);
294   
295  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
296  1 Assert.assertEquals(toCharacters("Alice Wiki Extension (upgraded)"), result.getMerged());
297   
298  1 result = this.mocker.getComponentUnderTest()
299    .merge(toCharacters("$a(b)"), toCharacters("$c(d)e"), toCharacters("$c(d)e"), null);
300   
301  1 Assert.assertEquals(0, result.getLog().getLogs(LogLevel.ERROR).size());
302  1 Assert.assertEquals("$c(d)e", toString(result.getMerged()));
303    }
304   
 
305  1 toggle @Test
306    public void testMergeCharOnConflicts() throws Exception
307    {
308  1 MergeResult<Character> result;
309   
310    // Current and new at the same place
311  1 result =
312    this.mocker.getComponentUnderTest().merge(toCharacters("a"), toCharacters("b"), toCharacters("c"), null);
313   
314  1 Assert.assertEquals(1, result.getLog().getLogs(LogLevel.ERROR).size());
315  1 Assert.assertEquals(toCharacters("c"), result.getMerged());
316   
317    // Current and new in conflict at different indices
318  1 result = this.mocker.getComponentUnderTest()
319    .merge(toCharacters("abcd"), toCharacters("yycd"), toCharacters("azzd"), null);
320   
321  1 Assert.assertEquals(1, result.getLog().getLogs(LogLevel.ERROR).size());
322  1 Assert.assertEquals("yycd", toString(result.getMerged()));
323   
324    // Current and new in conflict at different indices
325  1 result = this.mocker.getComponentUnderTest()
326    .merge(toCharacters("abcd"), toCharacters("azzd"), toCharacters("yycd"), null);
327   
328  1 Assert.assertEquals(1, result.getLog().getLogs(LogLevel.ERROR).size());
329  1 Assert.assertEquals("yycd", toString(result.getMerged()));
330   
331    // New overlapping current
332  1 result = this.mocker.getComponentUnderTest()
333    .merge(toCharacters("aabbcc"), toCharacters("arrbcc"), toCharacters("ddddcc"), null);
334   
335  1 Assert.assertEquals(1, result.getLog().getLogs(LogLevel.ERROR).size());
336  1 Assert.assertEquals("ddddcc", toString(result.getMerged()));
337   
338    // current overlapping new
339  1 result = this.mocker.getComponentUnderTest()
340    .merge(toCharacters("aabbcc"), toCharacters("ddddcc"), toCharacters("arrbcc"), null);
341   
342  1 Assert.assertEquals(1, result.getLog().getLogs(LogLevel.ERROR).size());
343  1 Assert.assertEquals("ddddcc", toString(result.getMerged()));
344    }
345   
 
346  1 toggle @Test
347    public void testMergeWithEmptyNew() throws Exception
348    {
349  1 MergeResult<Character> result;
350   
351  1 result =
352    this.mocker.getComponentUnderTest().merge(toCharacters("a"), toCharacters(""), toCharacters("b"), null);
353   
354  1 Assert.assertEquals(1, result.getLog().getLogs(LogLevel.ERROR).size());
355  1 Assert.assertEquals(toCharacters("b"), result.getMerged());
356    }
357   
 
358  1 toggle @Test
359    public void testMergeWhenUserHasChangedAllContent() throws Exception
360    {
361  1 MergeResult<String> result;
362   
363    // Test 1: All content has changed between previous and current
364  1 result = mocker.getComponentUnderTest().merge(Arrays.asList("Line 1", "Line 2", "Line 3"),
365    Arrays.asList("Line 1", "Line 2 modified", "Line 3", "Line 4 Added"),
366    Arrays.asList("New content", "That is completely different"), null);
367   
368  1 Assert.assertEquals(Arrays.asList("New content", "That is completely different"), result.getMerged());
369   
370    // Test 2: All content has been deleted between previous and current
371  1 result = mocker.getComponentUnderTest().merge(Arrays.asList("Line 1", "Line 2", "Line 3"),
372    Arrays.asList("Line 1", "Line 2 modified", "Line 3", "Line 4 Added"),
373    Collections.emptyList(), null);
374   
375  1 Assert.assertEquals(Collections.emptyList(), result.getMerged());
376    }
377    }