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

File DefaultInlineDiffDisplayerTest.java

 

Code metrics

0
26
13
1
157
104
13
0.5
2
13
1

Classes

Class Line # Actions
DefaultInlineDiffDisplayerTest 50 26 0% 13 0
1.0100%
 

Contributing tests

This file is covered by 12 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.util.Arrays;
23    import java.util.HashMap;
24    import java.util.List;
25    import java.util.Map;
26   
27    import org.apache.commons.lang3.ArrayUtils;
28    import org.junit.Assert;
29    import org.junit.Rule;
30    import org.junit.Test;
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.InlineDiffDisplayer;
36    import org.xwiki.diff.internal.DefaultDiffManager;
37    import org.xwiki.test.ComponentManagerRule;
38    import org.xwiki.test.annotation.ComponentList;
39   
40    /**
41    * Unit tests for {@link DefaultInlineDiffDisplayer}.
42    *
43    * @version $Id: 1778b922094e12baa5fc94b3da4facaf3c10478b $
44    * @since 4.1M2
45    */
46    @ComponentList({
47    DefaultDiffManager.class,
48    DefaultInlineDiffDisplayer.class
49    })
 
50    public class DefaultInlineDiffDisplayerTest
51    {
52    @Rule
53    public final ComponentManagerRule componentManager = new ComponentManagerRule();
54   
 
55  1 toggle @Test
56    public void testBothEmpty() throws Exception
57    {
58  1 execute("", "", "");
59    }
60   
 
61  1 toggle @Test
62    public void testNoChange() throws Exception
63    {
64  1 execute("xwiki", "xwiki", "xwiki");
65    }
66   
 
67  1 toggle @Test
68    public void testOriginalEmpty() throws Exception
69    {
70  1 execute("", "xwiki", "+xwiki+");
71    }
72   
 
73  1 toggle @Test
74    public void testRevisedEmpty() throws Exception
75    {
76  1 execute("xwiki", "", "-xwiki-");
77    }
78   
 
79  1 toggle @Test
80    public void testAddCharacter() throws Exception
81    {
82  1 execute("xwki", "xwiki", "xw+i+ki");
83    }
84   
 
85  1 toggle @Test
86    public void testRemoveCharacter() throws Exception
87    {
88  1 execute("xwiki", "xwki", "xw-i-ki");
89    }
90   
 
91  1 toggle @Test
92    public void testChangeCharacter() throws Exception
93    {
94  1 execute("xwiki", "xwIki", "xw-i-+I+ki");
95    }
96   
 
97  1 toggle @Test
98    public void testEndPoints() throws Exception
99    {
100  1 execute("wiki", "xwik", "+x+wik-i-");
101    }
102   
 
103  1 toggle @Test
104    public void testAddWord() throws Exception
105    {
106  1 execute("123abc", "123XYZabc", "123+XYZ+abc");
107    }
108   
 
109  1 toggle @Test
110    public void testRemoveWord() throws Exception
111    {
112  1 execute("123xyzABC", "123ABC", "123-xyz-ABC");
113    }
114   
 
115  1 toggle @Test
116    public void testChangeWord() throws Exception
117    {
118  1 execute("123 xyz abc", "123 XYZ abc", "123 -xyz-+XYZ+ abc");
119    }
120   
 
121  1 toggle @Test
122    public void testChangeWords() throws Exception
123    {
124  1 execute("123 456 789", "abc 456 xyz", "-123-+abc+ 456 -789-+xyz+");
125    }
126   
127    /**
128    * Builds the in-line diff between the given versions and asserts if the result meets the expectation.
129    *
130    * @param previous the previous version
131    * @param next the next version
132    * @param expected the expected in-line diff
133    * @throws Exception if creating the diff fails
134    */
 
135  12 toggle private void execute(String previous, String next, String expected) throws Exception
136    {
137  12 List<Character> previousChars = Arrays.asList(ArrayUtils.toObject(previous.toCharArray()));
138  12 List<Character> nextChars = Arrays.asList(ArrayUtils.toObject(next.toCharArray()));
139   
140  12 DiffManager diffManager = this.componentManager.getInstance(DiffManager.class);
141  12 DiffResult<Character> diffResult = diffManager.diff(previousChars, nextChars, null);
142   
143  12 Map<Type, String> separators = new HashMap<Type, String>();
144  12 separators.put(Type.ADDED, "+");
145  12 separators.put(Type.DELETED, "-");
146  12 separators.put(Type.UNMODIFIED, "");
147   
148  12 StringBuilder actual = new StringBuilder();
149  12 InlineDiffDisplayer inlineDiffDisplayer = this.componentManager.getInstance(InlineDiffDisplayer.class);
150  12 for (InlineDiffChunk<Character> chunk : inlineDiffDisplayer.display(diffResult)) {
151  31 String separator = separators.get(chunk.getType());
152  31 actual.append(separator).append(chunk).append(separator);
153    }
154   
155  12 Assert.assertEquals(expected, actual.toString());
156    }
157    }