View | Details | Raw Unified | Return to bug 71374 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/compare/tests/AllTests.java (+1 lines)
Lines 32-37 Link Here
32
		suite.addTestSuite(DiffTest.class);
32
		suite.addTestSuite(DiffTest.class);
33
		suite.addTestSuite(FileDiffResultTest.class);
33
		suite.addTestSuite(FileDiffResultTest.class);
34
		suite.addTestSuite(ContentMergeViewerTest.class);
34
		suite.addTestSuite(ContentMergeViewerTest.class);
35
		suite.addTestSuite(UnifiedDiffFormatterTest.class);
35
		//$JUnit-END$
36
		//$JUnit-END$
36
		return suite;
37
		return suite;
37
	}
38
	}
(-)patchdata/no_newline.txt (+8 lines)
Added Link Here
1
--- addition.txt	2005-05-09 12:14:04.000000000 +0200
Added Link Here
1
--- exp_addition.txt	2005-05-09 12:14:04.000000000 +0200
Added Link Here
1
[a]
2
[b]
3
[c]
4
[d]
5
[e]
6
[f]
(-)patchdata/exp_no_newline.txt (+11 lines)
Added Link Here
1
--- empty1.txt	24 Dec 2008 12:56:00 -0000
Added Link Here
1
--- addition.txt	2005-05-09 12:14:04.000000000 +0200
Added Link Here
1
Index: no_newline.txt
2
===================================================================
3
--- no_newline.txt	2009-01-08 21:48:55 -0000
Added Link Here
1
[a]
2
[b1]
3
[c1]
4
[d1]
5
[e]
6
[f]
(-)patchdata/exp_additionA.txt (+14 lines)
Added Link Here
1
--- no_newline.txt	2005-05-09 12:14:04.000000000 +0200
Added Link Here
1
--- exp_addition.txt	2005-05-09 12:14:04.000000000 +0200
Added Link Here
1
--- context.txt	20 Dec 2008 02:59:19 -0000
Added Link Here
1
--- addition.txt	2005-05-09 12:14:04.000000000 +0200
Added Link Here
1
--- addition.txt	2005-05-09 12:14:04.000000000 +0200
Added Link Here
1
[1]
2
[2]
3
[3]
4
[4]
5
[5]
6
[6]
7
[7]
8
[8]
9
[9]
(-)src/org/eclipse/compare/tests/UnifiedDiffFormatterTest.java (+222 lines)
Added Link Here
1
--- context.txt	20 Dec 2008 02:59:19 -0000
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
import org.eclipse.compare.CompareConfiguration;
22
import org.eclipse.compare.contentmergeviewer.ITokenComparator;
23
import org.eclipse.compare.contentmergeviewer.TokenComparator;
24
import org.eclipse.compare.internal.MergeViewerContentProvider;
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
import junit.framework.TestCase;
36
37
public class UnifiedDiffFormatterTest extends TestCase {
38
39
	private static final String PATCHDATA = "patchdata";
40
	private static final String TESTPATCHPATH = "patchdata/testPatch.txt";
41
	private static final String TESTPATCHFILE = "testPatch.txt";
42
	
43
	class FormatterTestDocMerger implements IDocumentMergerInput {
44
45
		private Document fromDoc;
46
		private Document toDoc;
47
		
48
		public FormatterTestDocMerger(Document fromDoc, Document toDoc) {
49
			this.fromDoc = fromDoc;
50
			this.toDoc = toDoc;
51
		}
52
		
53
		public ITokenComparator createTokenComparator(String line) {
54
	            return new TokenComparator(line);
55
	    }
56
	    public CompareConfiguration getCompareConfiguration() {
57
	            return new CompareConfiguration();
58
	    }
59
	    public IDocument getDocument(char contributor) {
60
	            switch (contributor) {
61
	            case MergeViewerContentProvider.LEFT_CONTRIBUTOR:
62
	                    return fromDoc;
63
	            case MergeViewerContentProvider.RIGHT_CONTRIBUTOR:
64
	                    return toDoc;
65
	            default:
66
	                    return null;
67
	            }
68
	    }
69
	    public int getHunkStart() {
70
	            return 0;
71
	    }
72
	    public Position getRegion(char contributor) {
73
	            switch (contributor) {
74
	            case MergeViewerContentProvider.LEFT_CONTRIBUTOR:
75
	                    return new Position(0, fromDoc.getLength());
76
	            case MergeViewerContentProvider.RIGHT_CONTRIBUTOR:
77
	                    return new Position(0, toDoc.getLength());
78
	            }
79
	            return null;
80
	    }
81
	    public boolean isHunkOnLeft() {
82
	    	return false;
83
	    }
84
	    public boolean isIgnoreAncestor() {
85
	    	return true;
86
	    }
87
	    public boolean isPatchHunk() {
88
	    	return false;
89
	    }
90
	
91
	    public boolean isShowPseudoConflicts() {
92
	    	return false;
93
	    }
94
	    public boolean isThreeWay() {
95
	//            return TextMergeViewer.this.isThreeWay();
96
	    	return false;
97
	    }
98
	    public boolean isPatchHunkOk() {
99
	    	return false;
100
	    }
101
	
102
	}
103
	
104
	protected void setUp() throws Exception {
105
		// empty
106
	}
107
108
	protected void tearDown() throws Exception {
109
		super.tearDown();
110
	}
111
	
112
	public void testLeftEmptyPatch() throws CoreException, IOException {
113
		patch("addition.txt", "exp_addition.txt", "patch_additionA2.txt");
114
	}
115
	
116
	public void testRightEmptyPatch() throws CoreException, IOException {
117
		patch("exp_addition.txt", "addition.txt", "patch_additionB2.txt");
118
	}
119
	
120
	public void testEmptyFilesPatch() throws CoreException, IOException {
121
		patch("empty1.txt", "empty2.txt", "patch_empty.txt");
122
	}
123
	
124
	public void testUnterminatedCreatePatch() throws CoreException, IOException {
125
		patch("addition.txt", "exp_addition2.txt", "patch_additionC2.txt");
126
	}
127
	
128
	public void testCreateExamplePatch()throws CoreException, IOException {
129
		patch("context.txt", "exp_context.txt", "patch_additionD2.txt");
130
	}
131
	
132
	public void testBothFilesWithoutEndingNewlinePatch()throws CoreException, IOException {
133
		patch("no_newline.txt", "exp_no_newline.txt", "patch_no_newline.txt");
134
	}
135
	
136
	private void patch(String fromFilePath, String toFilePath, String expectedPatch) throws CoreException, IOException{
137
		String fromFileContent = readFileToString(fromFilePath);
138
		String toFileContent = readFileToString(toFilePath);
139
		
140
		final Document fromDoc = new Document(fromFileContent);
141
		final Document toDoc = new Document(toFileContent);
142
143
		DocumentMerger merger = new DocumentMerger(new FormatterTestDocMerger(fromDoc, toDoc));
144
		merger.doDiff();
145
146
		UnifiedDiffFormatter formatter = new UnifiedDiffFormatter(merger,
147
                fromDoc, toDoc, fromFilePath, toFilePath, false);		
148
		File file = getFile(TESTPATCHPATH);
149
		
150
		formatter.generateDiff(file);
151
152
		String patchContent = readFileToString(TESTPATCHFILE);
153
		String expectedContent = readFileToString(expectedPatch);
154
		
155
		String[] patchContents = patchContent.split("\n");
156
		String[] expectedContents = expectedContent.split("\n");
157
		
158
		patchContent = getContentFromLines(patchContents);
159
		expectedContent = getContentFromLines(expectedContents);
160
		System.out.println(patchContent);
161
		System.out.println(expectedContent);
162
//		assertEquals(patchContent.length(), expectedContent.length());
163
		assertEquals(patchContent, expectedContent);
164
	}
165
	
166
	private File getFile(String path) throws IOException {
167
		File result = new File(path);
168
		if(!result.exists()) {
169
				result.createNewFile();
170
		}
171
		return result;
172
	}
173
	
174
	public String readFileToString(String path) throws IOException {
175
		BufferedReader reader = getReader(path);
176
		StringBuffer str = new StringBuffer();
177
		int c;
178
179
		while (((c = reader.read()) != -1)){
180
			str.append((char)c);
181
		}
182
		reader.close();
183
		return str.toString();
184
	}
185
186
	private BufferedReader getReader(String name) {
187
		InputStream resourceAsStream = asInputStream(name);
188
		InputStreamReader reader2= new InputStreamReader(resourceAsStream);
189
		return new BufferedReader(reader2);
190
	}
191
192
	private InputStream asInputStream(String name) {
193
		IPath path= new Path(PATCHDATA).append(name);
194
		try {
195
			URL url= new URL(getBundle().getEntry("/"), path.toString());
196
			return url.openStream();
197
		} catch (IOException e) {
198
			fail("Failed while reading " + name);
199
			return null; // never reached
200
		}
201
	}
202
	private Bundle getBundle() {
203
		return CompareTestPlugin.getDefault().getBundle();
204
	}
205
	
206
	private String getContentFromLines(String [] patchContents){
207
		String patchContent = new String();
208
		for (int i=0;i< patchContents.length;i++){
209
			String line =patchContents[i];
210
			if (line.startsWith(UnifiedDiffFormatter.NEW_FILE_PREFIX)||line.startsWith(UnifiedDiffFormatter.OLD_FILE_PREFIX)){
211
				String[] line_split = line.split("\t");
212
				patchContent += line_split[0];
213
			} else if (line.startsWith(UnifiedDiffFormatter.NEW_LINE_PREFIX)||line.startsWith(UnifiedDiffFormatter.OLD_LINE_PREFIX)||line.startsWith(UnifiedDiffFormatter.RANGE_INFORMATION_PREFIX)){
214
					patchContent +=line;
215
			} else if(!line.startsWith("Index: ") && !line.startsWith("==========")){
216
				patchContent +=line;
217
			}
218
		}
219
		return patchContent;
220
	}
221
}

Return to bug 71374