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

Return to bug 347557