Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 903faa9dc19669f0cec4d79f5309b52bf078a242 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*******************************************************************************
 * Copyright (c) 2013, 2017 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.compare.tests;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import junit.framework.TestCase;

import org.eclipse.compare.ICompareFilter;
import org.eclipse.compare.ISharedDocumentAdapter;
import org.eclipse.compare.structuremergeviewer.DocumentRangeNode;
import org.eclipse.compare.structuremergeviewer.IStructureComparator;
import org.eclipse.compare.structuremergeviewer.StructureCreator;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Region;
import org.junit.Assert;

public class StructureCreatorTest extends TestCase {

	public StructureCreatorTest() {
		super();
	}

	public StructureCreatorTest(String name) {
		super(name);
	}

	public void testIgnoreWhitespace() {
		IDocument[] docs = new IDocument[15];
		docs[0] = new Document();
		docs[1] = new Document();
		docs[2] = new Document();
		docs[3] = new Document();
		docs[4] = new Document();
		docs[5] = new Document();
		docs[6] = new Document();
		docs[7] = new Document();
		docs[8] = new Document();
		docs[9] = new Document();
		docs[10] = new Document();
		docs[11] = new Document();
		docs[12] = new Document();
		docs[13] = new Document();
		docs[14] = new Document();

		docs[0].set("ABCDEF"); //$NON-NLS-1$
		docs[1].set("ABC DEF"); //$NON-NLS-1$
		docs[2].set("ABC\r\nDEF"); //$NON-NLS-1$
		docs[3].set("ABC\r\nDEF  "); //$NON-NLS-1$
		docs[4].set("\r\nABC\r\nDEF"); //$NON-NLS-1$
		docs[5].set("   \r\nABC\r\nDEF"); //$NON-NLS-1$
		docs[6].set("\r\nABC\r\nDEF\r\n"); //$NON-NLS-1$
		docs[7].set("\r\nA BC\r\nDE F\r\n"); //$NON-NLS-1$
		docs[8].set("ABC\r\n\r\nDEF"); //$NON-NLS-1$
		docs[9].set("ABC\nDEF"); //$NON-NLS-1$
		docs[10].set("ABC\nDEF   "); //$NON-NLS-1$
		docs[11].set("ABC\nDEF\n"); //$NON-NLS-1$
		docs[12].set("\nABCDEF"); //$NON-NLS-1$
		docs[13].set(" \nAB CD EF"); //$NON-NLS-1$
		docs[14].set("\nABC\nDEF\n"); //$NON-NLS-1$

		ICompareFilter[][] filters = new ICompareFilter[3][];
		filters[0] = null;
		filters[1] = new ICompareFilter[] { new ICompareFilter() {

			public void setInput(Object input, Object ancestor, Object left,
					Object right) {
				// EMPTY
			}

			public IRegion[] getFilteredRegions(HashMap lineComparison) {
				String line = lineComparison.get(ICompareFilter.THIS_LINE)
						.toString();
				int index = line.indexOf("A");
				if (index > -1)
					return new IRegion[] { new Region(index, 1) };
				return null;
			}

			public boolean isEnabledInitially() {
				return false;
			}

			public boolean canCacheFilteredRegions() {
				return true; // cache-able
			}
		} };

		filters[2] = new ICompareFilter[] { new ICompareFilter() {

			public void setInput(Object input, Object ancestor, Object left,
					Object right) {
				// EMPTY
			}

			public IRegion[] getFilteredRegions(HashMap lineComparison) {
				String line = lineComparison.get(ICompareFilter.THIS_LINE)
						.toString();
				int index = line.indexOf("E");
				if (index > -1)
					return new IRegion[] { new Region(index, 1) };
				return null;
			}

			public boolean isEnabledInitially() {
				return false;
			}

			public boolean canCacheFilteredRegions() {
				return false; // not cache-able
			}
		} };

		StructureCreator creator = new StructureCreator() {
			private Pattern whitespace = Pattern.compile("\\s+");
			private Matcher matcher = null;

			public String getName() {
				return "NAME";
			}

			public String getContents(Object node, boolean ignoreWhitespace) {
				DocumentRangeNode drn = (DocumentRangeNode) node;
				String retval = null;
				try {
					retval = drn.getDocument().get(drn.getRange().getOffset(),
							drn.getRange().getLength());
					if (ignoreWhitespace) {
						if (matcher == null)
							matcher = whitespace.matcher(retval);
						else
							matcher.reset(retval);
						retval = matcher.replaceAll("");
					}
				} catch (BadLocationException ble) {
					assertNull(ble);
				}
				return retval;
			}

			protected IStructureComparator createStructureComparator(
					Object element, IDocument document,
					ISharedDocumentAdapter sharedDocumentAdapter,
					IProgressMonitor monitor) throws CoreException {
				return new DocumentRangeNode(1, "ID", document, 0,
						document.getLength());
			}

		};
		DocumentRangeNode l, r;
		for (int i = 0; i < docs.length; i++)
			for (int j = i + 1; j < docs.length; j++)
				for (int k = 0; k < filters.length; k++) {

					l = new DocumentRangeNode(1, "ID", docs[i], 0,
							docs[i].getLength());
					r = new DocumentRangeNode(1, "ID", docs[j], 0,
							docs[j].getLength());
					creator.contentsEquals(l, 'L', r, 'R', true, filters[k]);
					Assert.assertFalse(creator.contentsEquals(l, 'L', r, 'R',
							false, filters[k]));
					Assert.assertTrue(creator.contentsEquals(l, 'L', r, 'R',
							true, filters[k]));
				}
	}
}

Back to the top