Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2008 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* Krzysztof Poglodzinski (intuicje@gmail.com) - initial API and implementation |
10 |
* Mariusz Tanski (mariusztanski@gmail.com) - initial API and implementation |
11 |
* Kacper Zdanowicz (kacper.zdanowicz@gmail.com) - initial API and implementation |
12 |
*******************************************************************************/ |
13 |
package org.eclipse.compare.tests; |
14 |
|
15 |
import java.io.BufferedReader; |
16 |
import java.io.File; |
17 |
import java.io.IOException; |
18 |
import java.io.InputStream; |
19 |
import java.io.InputStreamReader; |
20 |
import java.net.URL; |
21 |
|
22 |
import org.eclipse.compare.CompareConfiguration; |
23 |
import org.eclipse.compare.contentmergeviewer.ITokenComparator; |
24 |
import org.eclipse.compare.contentmergeviewer.TokenComparator; |
25 |
import org.eclipse.compare.internal.UnifiedDiffFormatter; |
26 |
import org.eclipse.compare.internal.merge.DocumentMerger; |
27 |
import org.eclipse.compare.internal.merge.DocumentMerger.IDocumentMergerInput; |
28 |
import org.eclipse.core.runtime.CoreException; |
29 |
import org.eclipse.core.runtime.IPath; |
30 |
import org.eclipse.core.runtime.Path; |
31 |
import org.eclipse.jface.text.Document; |
32 |
import org.eclipse.jface.text.IDocument; |
33 |
import org.eclipse.jface.text.Position; |
34 |
import org.osgi.framework.Bundle; |
35 |
|
36 |
import junit.framework.TestCase; |
37 |
|
38 |
public class UnifiedDiffFormatterTest extends TestCase { |
39 |
|
40 |
private static final String PATCHDATA = "patchdata"; |
41 |
private static final String TESTPATCHPATH = "patchdata/testPatch.txt"; |
42 |
private static final String TESTPATCHFILE = "testPatch.txt"; |
43 |
public static final char RIGHT_CONTRIBUTOR = 'R'; |
44 |
public static final char LEFT_CONTRIBUTOR = 'L'; |
45 |
|
46 |
protected void setUp() throws Exception { |
47 |
// empty |
48 |
} |
49 |
|
50 |
protected void tearDown() throws Exception { |
51 |
super.tearDown(); |
52 |
} |
53 |
|
54 |
public void testCreatePatch() throws CoreException, IOException { |
55 |
patch("addition.txt", "exp_addition.txt", "patch_additionA.txt"); |
56 |
} |
57 |
|
58 |
public void testUnterminatedCreatePatch() throws CoreException, IOException { |
59 |
patch("addition.txt", "exp_addition2.txt", "patch_addition2A.txt"); |
60 |
} |
61 |
|
62 |
public void testCreate2Patch()throws CoreException, IOException { |
63 |
patch("context.txt", "exp_context.txt", "patchAddition3.txt"); |
64 |
} |
65 |
|
66 |
private void patch(String fromFilePath, String toFilePath, String expectedPatch) throws CoreException, IOException{ |
67 |
String fromFileContent = readFileToString(fromFilePath); |
68 |
String toFileContent = readFileToString(toFilePath); |
69 |
|
70 |
final Document fromDoc = new Document(fromFileContent); |
71 |
final Document toDoc = new Document(toFileContent); |
72 |
|
73 |
DocumentMerger merger = new DocumentMerger(getDocumentMergerInput(fromDoc, toDoc)); |
74 |
|
75 |
merger.doDiff(); |
76 |
|
77 |
UnifiedDiffFormatter formatter = new UnifiedDiffFormatter(merger, |
78 |
fromDoc, toDoc, TESTPATCHPATH, false); |
79 |
|
80 |
File file = getFile(TESTPATCHPATH); |
81 |
|
82 |
formatter.generateDiff(file); |
83 |
|
84 |
String patchContent = readFileToString(TESTPATCHFILE); |
85 |
String expectedContent = readFileToString(expectedPatch); |
86 |
|
87 |
String[] patchContents = patchContent.split("\n"); |
88 |
String[] expectedContents = expectedContent.split("\n"); |
89 |
patchContent = new String(); |
90 |
expectedContent = new String(); |
91 |
|
92 |
for (int i = 4; i < patchContents.length; i++) { |
93 |
patchContent += patchContents[i]; |
94 |
} |
95 |
for (int i = 2; i < expectedContents.length; i++) { |
96 |
expectedContent += expectedContents[i]; |
97 |
} |
98 |
|
99 |
|
100 |
assertEquals(patchContent.length(), expectedContent.length()); |
101 |
|
102 |
assertEquals(patchContent, expectedContent); |
103 |
|
104 |
} |
105 |
|
106 |
private File getFile(String path) throws IOException { |
107 |
File result = new File(path); |
108 |
if(!result.exists()) { |
109 |
result.createNewFile(); |
110 |
} |
111 |
return result; |
112 |
} |
113 |
|
114 |
private IDocumentMergerInput getDocumentMergerInput(final Document fromDoc, final Document toDoc) { |
115 |
return new IDocumentMergerInput() { |
116 |
|
117 |
public ITokenComparator createTokenComparator(String line) { |
118 |
return new TokenComparator(line); |
119 |
} |
120 |
public CompareConfiguration getCompareConfiguration() { |
121 |
return new CompareConfiguration(); |
122 |
} |
123 |
public IDocument getDocument(char contributor) { |
124 |
switch (contributor) { |
125 |
case LEFT_CONTRIBUTOR: |
126 |
return fromDoc; |
127 |
case RIGHT_CONTRIBUTOR: |
128 |
return toDoc; |
129 |
default: |
130 |
return null; |
131 |
} |
132 |
} |
133 |
public int getHunkStart() { |
134 |
return 0; |
135 |
} |
136 |
public Position getRegion(char contributor) { |
137 |
switch (contributor) { |
138 |
case LEFT_CONTRIBUTOR: |
139 |
return new Position(0, fromDoc.getLength()); |
140 |
case RIGHT_CONTRIBUTOR: |
141 |
return new Position(0, toDoc.getLength()); |
142 |
} |
143 |
return null; |
144 |
} |
145 |
public boolean isHunkOnLeft() { |
146 |
return false; |
147 |
} |
148 |
public boolean isIgnoreAncestor() { |
149 |
return true; |
150 |
} |
151 |
public boolean isPatchHunk() { |
152 |
return false; |
153 |
} |
154 |
|
155 |
public boolean isShowPseudoConflicts() { |
156 |
return false; |
157 |
} |
158 |
public boolean isThreeWay() { |
159 |
// return TextMergeViewer.this.isThreeWay(); |
160 |
return false; |
161 |
} |
162 |
public boolean isPatchHunkOk() { |
163 |
return false; |
164 |
} |
165 |
|
166 |
}; |
167 |
} |
168 |
|
169 |
public String readFileToString(String path) throws IOException { |
170 |
BufferedReader reader = getReader(path); |
171 |
StringBuffer str = new StringBuffer(); |
172 |
int c; |
173 |
|
174 |
while (((c = reader.read()) != -1)){ |
175 |
str.append((char)c); |
176 |
} |
177 |
reader.close(); |
178 |
return str.toString(); |
179 |
|
180 |
} |
181 |
|
182 |
private BufferedReader getReader(String name) { |
183 |
InputStream resourceAsStream = asInputStream(name); |
184 |
InputStreamReader reader2= new InputStreamReader(resourceAsStream); |
185 |
return new BufferedReader(reader2); |
186 |
} |
187 |
|
188 |
private InputStream asInputStream(String name) { |
189 |
IPath path= new Path(PATCHDATA).append(name); |
190 |
try { |
191 |
URL url= new URL(getBundle().getEntry("/"), path.toString()); |
192 |
return url.openStream(); |
193 |
} catch (IOException e) { |
194 |
fail("Failed while reading " + name); |
195 |
return null; // never reached |
196 |
} |
197 |
} |
198 |
private Bundle getBundle() { |
199 |
return CompareTestPlugin.getDefault().getBundle(); |
200 |
} |
201 |
} |