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

File DefaultUnifiedDiffDisplayerTest.java

 

Code metrics

0
30
5
1
187
90
5
0.17
6
5
1

Classes

Class Line # Actions
DefaultUnifiedDiffDisplayerTest 58 30 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 9 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.io.BufferedReader;
23    import java.io.IOException;
24    import java.io.InputStream;
25    import java.io.InputStreamReader;
26    import java.util.ArrayList;
27    import java.util.Arrays;
28    import java.util.Collection;
29    import java.util.Collections;
30    import java.util.List;
31   
32    import org.apache.commons.io.IOUtils;
33    import org.junit.Assert;
34    import org.junit.Rule;
35    import org.junit.Test;
36    import org.junit.runner.RunWith;
37    import org.junit.runners.Parameterized;
38    import org.junit.runners.Parameterized.Parameters;
39    import org.xwiki.diff.DiffManager;
40    import org.xwiki.diff.display.UnifiedDiffBlock;
41    import org.xwiki.diff.display.UnifiedDiffDisplayer;
42    import org.xwiki.diff.internal.DefaultDiffManager;
43    import org.xwiki.test.ComponentManagerRule;
44    import org.xwiki.test.annotation.ComponentList;
45   
46    /**
47    * Unit tests for {@link DefaultUnifiedDiffDisplayer}.
48    *
49    * @version $Id: 633815cb490f6a52070842e8f89258b6d4efced7 $
50    * @since 4.1M2
51    */
52    @ComponentList({
53    DefaultDiffManager.class,
54    DefaultUnifiedDiffDisplayer.class,
55    DefaultInlineDiffDisplayer.class
56    })
57    @RunWith(Parameterized.class)
 
58    public class DefaultUnifiedDiffDisplayerTest
59    {
60    @Rule
61    public final ComponentManagerRule componentManager = new ComponentManagerRule();
62   
63    /**
64    * The previous version.
65    */
66    private final List<String> previous;
67   
68    /**
69    * The next version.
70    */
71    private final List<String> next;
72   
73    /**
74    * The expected unified diff.
75    */
76    private final String expected;
77   
78    /**
79    * Creates a new test with the given input and the specified expected output.
80    *
81    * @param previous the previous version
82    * @param next the next version
83    * @param expected the expected unified diff
84    */
 
85  9 toggle public DefaultUnifiedDiffDisplayerTest(List<String> previous, List<String> next, String expected)
86    {
87  9 this.previous = previous;
88  9 this.next = next;
89  9 this.expected = expected;
90    }
91   
92    /**
93    * The actual test.
94    */
 
95  9 toggle @Test
96    public void execute() throws Exception
97    {
98  9 DiffManager diffManager = this.componentManager.getInstance(DiffManager.class);
99  9 UnifiedDiffDisplayer unifiedDiffDisplayer = this.componentManager.getInstance(UnifiedDiffDisplayer.class);
100  9 List<UnifiedDiffBlock<String, Object>> blocks =
101    unifiedDiffDisplayer.display(diffManager.diff(this.previous, this.next, null));
102   
103  9 StringBuilder actual = new StringBuilder();
104  9 for (UnifiedDiffBlock<String, ?> block : blocks) {
105  8 actual.append(block);
106    }
107  9 Assert.assertEquals(this.expected, actual.toString());
108    }
109   
110    /**
111    * @return the collection of test parameters
112    */
 
113  2 toggle @Parameters
114    public static Collection<Object[]> data() throws IOException
115    {
116  2 Collection<Object[]> data = new ArrayList<Object[]>();
117   
118    //
119    // Add special tests.
120    //
121   
122    // Both previous and next are empty.
123  2 data.add(new Object[] { Collections.<String>emptyList(), Collections.<String>emptyList(), "" });
124   
125    // Previous and next are equal.
126  2 List<String> lines = Arrays.asList("one", "two", "three");
127  2 data.add(new Object[] { lines, lines, "" });
128   
129    // Previous is empty.
130  2 data.add(new Object[] { Collections.<String>emptyList(), lines, "@@ -1,0 +1,3 @@\n+one\n+two\n+three\n" });
131   
132    // Next is empty.
133  2 data.add(new Object[] { lines, Collections.<String>emptyList(), "@@ -1,3 +1,0 @@\n-one\n-two\n-three\n" });
134   
135    // Line removed.
136  2 data.add(new Object[] { lines, Arrays.asList(lines.get(0), lines.get(2)),
137    "@@ -1,3 +1,2 @@\n one\n-two\n three\n" });
138   
139    // Line added.
140  2 data.add(new Object[] { lines, Arrays.asList(lines.get(0), lines.get(1), "between", lines.get(2)),
141    "@@ -1,3 +1,4 @@\n one\n two\n+between\n three\n" });
142   
143    // Line changed.
144  2 data.add(new Object[] { lines, Arrays.asList(lines.get(0), "Two", lines.get(2)),
145    "@@ -1,3 +1,3 @@\n one\n-two\n+Two\n three\n" });
146   
147    //
148    // Add tests from files.
149    //
150   
151  2 List<String> previous = readLines("previous.txt");
152  2 String[] testNames = new String[] { "twoContexts", "sharedContext" };
153  2 for (String testName : testNames) {
154  4 data.add(new Object[] { previous, readLines(testName + ".txt"), readContent(testName + ".diff") });
155    }
156   
157  2 return data;
158    }
159   
160    /**
161    * Reads the lines from the specified file.
162    *
163    * @param fileName the file name
164    * @return the lines from the specified file
165    * @throws IOException if reading the file fails
166    */
 
167  6 toggle private static List<String> readLines(String fileName) throws IOException
168    {
169  6 InputStream stream = DefaultUnifiedDiffDisplayerTest.class.getResourceAsStream('/' + fileName);
170  6 BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
171  6 return IOUtils.readLines(reader);
172    }
173   
174    /**
175    * Reads the content of the specified file.
176    *
177    * @param fileName the file name
178    * @return the content of the specified file
179    * @throws IOException if reading the fail fails
180    */
 
181  4 toggle private static String readContent(String fileName) throws IOException
182    {
183  4 InputStream stream = DefaultUnifiedDiffDisplayerTest.class.getResourceAsStream('/' + fileName);
184  4 BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
185  4 return IOUtils.toString(reader);
186    }
187    }