Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 347557 | Differences between
and this patch

Collapse All | Expand All

(-)META-INF/MANIFEST.MF (-1 / +2 lines)
Lines 14-20 Link Here
14
 org.eclipse.core.resources,
14
 org.eclipse.core.resources,
15
 org.eclipse.core.tests.resources,
15
 org.eclipse.core.tests.resources,
16
 org.eclipse.core.tests.harness,
16
 org.eclipse.core.tests.harness,
17
 org.eclipse.core.filesystem
17
 org.eclipse.core.filesystem,
18
 org.eclipse.team.ui
18
Bundle-Activator: org.eclipse.compare.tests.CompareTestPlugin
19
Bundle-Activator: org.eclipse.compare.tests.CompareTestPlugin
19
Bundle-ActivationPolicy: lazy
20
Bundle-ActivationPolicy: lazy
20
Bundle-Vendor: %providerName
21
Bundle-Vendor: %providerName
(-)src/org/eclipse/compare/tests/SaveableCompareEditorInputTest.java (+330 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011, 2011 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
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.compare.tests;
12
13
import java.io.BufferedReader;
14
import java.io.ByteArrayInputStream;
15
import java.io.IOException;
16
import java.io.InputStreamReader;
17
import java.lang.reflect.InvocationTargetException;
18
import java.util.ArrayList;
19
import java.util.List;
20
21
import junit.framework.TestCase;
22
23
import org.eclipse.compare.CompareConfiguration;
24
import org.eclipse.compare.ITypedElement;
25
import org.eclipse.compare.contentmergeviewer.TextMergeViewer;
26
import org.eclipse.compare.internal.MergeSourceViewer;
27
import org.eclipse.compare.structuremergeviewer.Differencer;
28
import org.eclipse.compare.structuremergeviewer.ICompareInput;
29
import org.eclipse.core.resources.IFile;
30
import org.eclipse.core.resources.IProject;
31
import org.eclipse.core.resources.IResource;
32
import org.eclipse.core.resources.ResourcesPlugin;
33
import org.eclipse.core.runtime.CoreException;
34
import org.eclipse.core.runtime.IProgressMonitor;
35
import org.eclipse.jface.dialogs.Dialog;
36
import org.eclipse.swt.custom.StyledText;
37
import org.eclipse.swt.graphics.Image;
38
import org.eclipse.swt.widgets.Composite;
39
import org.eclipse.swt.widgets.Control;
40
import org.eclipse.swt.widgets.Shell;
41
import org.eclipse.team.internal.ui.mapping.AbstractCompareInput;
42
import org.eclipse.team.internal.ui.mapping.CompareInputChangeNotifier;
43
import org.eclipse.team.internal.ui.synchronize.LocalResourceTypedElement;
44
import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput;
45
import org.eclipse.ui.IWorkbenchPage;
46
import org.eclipse.ui.PlatformUI;
47
48
public class SaveableCompareEditorInputTest extends TestCase {
49
50
	private class FileElement implements ITypedElement {
51
52
		private IFile file;
53
54
		public IFile getFile() {
55
			return file;
56
		}
57
58
		public FileElement(IFile file) {
59
			super();
60
			this.file = file;
61
		}
62
63
		public String getName() {
64
			return file.getName();
65
		}
66
67
		public Image getImage() {
68
			return null;
69
		}
70
71
		public String getType() {
72
			return TEXT_TYPE;
73
		}
74
75
	}
76
77
	public class TestDiffNode extends AbstractCompareInput {
78
79
		private CompareInputChangeNotifier notifier = new CompareInputChangeNotifier() {
80
81
			private IResource getResource(ITypedElement el) {
82
				if (el instanceof LocalResourceTypedElement) {
83
					return ((LocalResourceTypedElement) el).getResource();
84
				}
85
				if (el instanceof FileElement) {
86
					return ((FileElement) el).getFile();
87
				}
88
				return null;
89
			}
90
91
			protected IResource[] getResources(ICompareInput input) {
92
93
				List resources = new ArrayList();
94
				if (getResource(getLeft()) != null) {
95
					resources.add(getResource(getLeft()));
96
				}
97
				if (getResource(getRight()) != null) {
98
					resources.add(getResource(getRight()));
99
				}
100
101
				return (IResource[]) resources.toArray(new IResource[2]);
102
			}
103
		};
104
105
		public TestDiffNode(ITypedElement left, ITypedElement right) {
106
			super(Differencer.CHANGE, null, left, right);
107
		}
108
109
		public void fireChange() {
110
			super.fireChange();
111
		}
112
113
		protected CompareInputChangeNotifier getChangeNotifier() {
114
			return notifier;
115
		}
116
117
		public boolean needsUpdate() {
118
			// The remote never changes
119
			return false;
120
		}
121
122
		public void update() {
123
			fireChange();
124
		}
125
	}
126
127
	private class TestSaveableEditorInputLocal extends
128
			SaveableCompareEditorInput {
129
130
		protected ITypedElement left;
131
		protected ITypedElement right;
132
		private ICompareInput input;
133
134
		public Object getCompareResult() {
135
			return input;
136
		}
137
138
		public TestSaveableEditorInputLocal(ITypedElement left,
139
				ITypedElement right, CompareConfiguration conf) {
140
			super(conf, PlatformUI.getWorkbench().getActiveWorkbenchWindow()
141
					.getActivePage());
142
			this.left = left;
143
			this.right = right;
144
		}
145
146
		protected ICompareInput prepareCompareInput(IProgressMonitor monitor)
147
				throws InvocationTargetException, InterruptedException {
148
			input = createCompareInput();
149
			getCompareConfiguration().setLeftEditable(true);
150
			getCompareConfiguration().setRightEditable(false);
151
			return null;
152
		}
153
154
		private ICompareInput createCompareInput() {
155
			return new TestDiffNode(left, right);
156
		}
157
158
		protected void fireInputChange() {
159
			((TestDiffNode) getCompareResult()).fireChange();
160
161
		}
162
163
		public void saveChanges(IProgressMonitor monitor) throws CoreException {
164
			super.saveChanges(monitor);
165
		}
166
167
	}
168
169
	private TextMergeViewer viewer;
170
171
	public void testDirtyFlagOnStandardLeft() throws CoreException,
172
			InvocationTargetException, InterruptedException,
173
			IllegalArgumentException, SecurityException,
174
			IllegalAccessException, NoSuchFieldException,
175
			NoSuchMethodException, IOException {
176
		IProject project = ResourcesPlugin.getWorkspace().getRoot()
177
				.getProject("Project_" + System.currentTimeMillis());
178
		project.create(null);
179
		project.open(null);
180
181
		String fileContents1 = "FileContents";
182
		String appendFileContents = "_append";
183
		String fileContents2 = "FileContents2";
184
185
		IFile file1 = project.getFile("File1_" + System.currentTimeMillis()
186
				+ ".txt");
187
		file1.create(new ByteArrayInputStream(fileContents1.getBytes()), true,
188
				null);
189
190
		IFile file2 = project.getFile("File2_" + System.currentTimeMillis()
191
				+ ".txt");
192
		file2.create(new ByteArrayInputStream(fileContents2.getBytes()), true,
193
				null);
194
195
		LocalResourceTypedElement el1 = (LocalResourceTypedElement) SaveableCompareEditorInput
196
				.createFileElement(file1);
197
		ITypedElement el2 = new FileElement(file2);
198
199
		IWorkbenchPage page = PlatformUI.getWorkbench()
200
				.getActiveWorkbenchWindow().getActivePage();
201
202
		CompareConfiguration conf = new CompareConfiguration();
203
		conf.setLeftEditable(true);
204
		TestSaveableEditorInputLocal compareEditorInput = new TestSaveableEditorInputLocal(
205
				el1, el2, conf);
206
207
		final TestSaveableEditorInputLocal[] editorInputs = { compareEditorInput };
208
		compareEditorInput.prepareCompareInput(null);
209
210
		Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
211
				.getShell();
212
213
		Dialog dialog = new Dialog(shell) {
214
			protected Control createDialogArea(Composite parent) {
215
				Composite composite = (Composite) super
216
						.createDialogArea(parent);
217
				viewer = (TextMergeViewer) editorInputs[0].findContentViewer(
218
						null, editorInputs[0].input, composite);
219
				viewer.setInput(editorInputs[0].getCompareResult());
220
				return composite;
221
			}
222
		};
223
		dialog.setBlockOnOpen(false);
224
		dialog.open();
225
226
		MergeSourceViewer left = (MergeSourceViewer) ReflectionUtils.getField(
227
				viewer, "fLeft");
228
229
		StyledText leftText = left.getSourceViewer().getTextWidget();
230
231
		// modify the left side of editor
232
		leftText.append(appendFileContents);
233
234
		assertTrue(compareEditorInput.isDirty());
235
236
		// save editor
237
		viewer.flush(null);
238
239
		assertFalse(compareEditorInput.isDirty());
240
241
		dialog.close();
242
243
		// check weather file was saved
244
245
		BufferedReader reader = new BufferedReader(new InputStreamReader(
246
				file1.getContents()));
247
		String line = reader.readLine();
248
249
		assertEquals(fileContents1 + appendFileContents, line);
250
251
	}
252
253
254
	public void testDirtyFlagOnCustomLeft() throws CoreException,
255
			InvocationTargetException, InterruptedException,
256
			IllegalArgumentException, SecurityException,
257
			IllegalAccessException, NoSuchFieldException,
258
			NoSuchMethodException, IOException {
259
		IProject project = ResourcesPlugin.getWorkspace().getRoot()
260
				.getProject("Project_" + System.currentTimeMillis());
261
		project.create(null);
262
		project.open(null);
263
264
		String fileContents1 = "FileContents";
265
		String appendFileContents = "_append";
266
		String fileContents2 = "FileContents2";
267
268
		IFile file1 = project.getFile("File1_" + System.currentTimeMillis()
269
				+ ".txt");
270
		file1.create(new ByteArrayInputStream(fileContents1.getBytes()), true,
271
				null);
272
273
		IFile file2 = project.getFile("File2_" + System.currentTimeMillis()
274
				+ ".txt");
275
		file2.create(new ByteArrayInputStream(fileContents2.getBytes()), true,
276
				null);
277
278
		ITypedElement el1 = new FileElement(file1);
279
		ITypedElement el2 = new FileElement(file2);
280
281
		IWorkbenchPage page = PlatformUI.getWorkbench()
282
				.getActiveWorkbenchWindow().getActivePage();
283
284
		CompareConfiguration conf = new CompareConfiguration();
285
		conf.setLeftEditable(true);
286
		TestSaveableEditorInputLocal compareEditorInput = new TestSaveableEditorInputLocal(
287
				el1, el2, conf);
288
289
		final TestSaveableEditorInputLocal[] editorInputs = { compareEditorInput };
290
		compareEditorInput.prepareCompareInput(null);
291
292
		Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
293
				.getShell();
294
295
		Dialog dialog = new Dialog(shell) {
296
			protected Control createDialogArea(Composite parent) {
297
				Composite composite = (Composite) super
298
						.createDialogArea(parent);
299
				viewer = (TextMergeViewer) editorInputs[0].findContentViewer(
300
						null, editorInputs[0].input, composite);
301
				viewer.setInput(editorInputs[0].getCompareResult());
302
				return composite;
303
			}
304
		};
305
		dialog.setBlockOnOpen(false);
306
		dialog.open();
307
308
		MergeSourceViewer left = (MergeSourceViewer) ReflectionUtils.getField(
309
				viewer, "fLeft");
310
311
		StyledText leftText = left.getSourceViewer().getTextWidget();
312
313
		leftText.append(appendFileContents);
314
315
		assertTrue(compareEditorInput.isDirty());
316
317
		viewer.flush(null);
318
319
		assertFalse(compareEditorInput.isDirty());
320
321
		dialog.close();
322
323
		/*
324
		 * not checking if changes were saved because in this case saving is not
325
		 * handled
326
		 */
327
328
	}
329
330
}

Return to bug 347557