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

(-)a/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/synchronize/LocalResourceSaveableComparison.java (-4 / +4 lines)
Lines 248-257 public abstract class LocalResourceSaveableComparison extends SaveableComparison Link Here
248
	 */
248
	 */
249
	public String getName() {
249
	public String getName() {
250
		// Return the name of the file element as held in the compare input
250
		// Return the name of the file element as held in the compare input
251
		if (fileElement.equals(input.getLeft())) {
251
		if (input.getLeft().equals(fileElement)) {
252
			return input.getLeft().getName();
252
			return input.getLeft().getName();
253
		}
253
		}
254
		if (fileElement.equals(input.getRight())) {
254
		if (input.getRight().equals(fileElement)) {
255
			return input.getRight().getName();
255
			return input.getRight().getName();
256
		}
256
		}
257
		// Fallback call returning name of the main non-null element of the input
257
		// Fallback call returning name of the main non-null element of the input
Lines 289-302 public abstract class LocalResourceSaveableComparison extends SaveableComparison Link Here
289
289
290
			ContentMergeViewer cmv = (ContentMergeViewer) e.getSource();
290
			ContentMergeViewer cmv = (ContentMergeViewer) e.getSource();
291
291
292
			if (fileElement.equals(input.getLeft())) {
292
			if (input.getLeft().equals(fileElement)) {
293
				if (changed && cmv.internalIsLeftDirty())
293
				if (changed && cmv.internalIsLeftDirty())
294
					setDirty(changed);
294
					setDirty(changed);
295
				else if (!changed && !cmv.internalIsLeftDirty()) {
295
				else if (!changed && !cmv.internalIsLeftDirty()) {
296
					setDirty(changed);
296
					setDirty(changed);
297
				}
297
				}
298
			}
298
			}
299
			if (fileElement.equals(input.getRight())) {
299
			if (input.getRight().equals(fileElement)) {
300
				if (changed && cmv.internalIsRightDirty())
300
				if (changed && cmv.internalIsRightDirty())
301
					setDirty(changed);
301
					setDirty(changed);
302
				else if (!changed && !cmv.internalIsRightDirty()) {
302
				else if (!changed && !cmv.internalIsRightDirty()) {
(-)a/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/ReflectionUtils.java (-1 / +24 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2009 IBM Corporation and others.
2
 * Copyright (c) 2009, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 53-56 public class ReflectionUtils { Link Here
53
		return ret;
53
		return ret;
54
	}
54
	}
55
55
56
	public static Object getField(Object object, String name, boolean deep)
57
			throws IllegalArgumentException, IllegalAccessException,
58
			SecurityException, NoSuchFieldException {
59
		Class clazz = object.getClass();
60
		NoSuchFieldException ex = null;
61
		while (clazz != null) {
62
			try {
63
				Field field = clazz.getDeclaredField(name);
64
				field.setAccessible(true);
65
				return field.get(object);
66
			} catch (NoSuchFieldException e) {
67
				if (ex == null) {
68
					ex = e;
69
				}
70
				if (!deep)
71
					break;
72
				clazz = clazz.getSuperclass();
73
74
			}
75
		}
76
		throw ex;
77
	}
78
56
}
79
}
(-)a/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/core/AllTeamUITests.java (-1 / +3 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2007 IBM Corporation and others.
2
 * Copyright (c) 2007, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 15-20 import junit.framework.TestSuite; Link Here
15
15
16
import org.eclipse.core.tests.resources.ResourceTest;
16
import org.eclipse.core.tests.resources.ResourceTest;
17
import org.eclipse.team.tests.core.mapping.ScopeTests;
17
import org.eclipse.team.tests.core.mapping.ScopeTests;
18
import org.eclipse.team.tests.ui.SaveableCompareEditorInputTest;
18
19
19
public class AllTeamUITests extends ResourceTest {
20
public class AllTeamUITests extends ResourceTest {
20
21
Lines 29-34 public class AllTeamUITests extends ResourceTest { Link Here
29
	public static Test suite() {
30
	public static Test suite() {
30
		TestSuite suite = new TestSuite();
31
		TestSuite suite = new TestSuite();
31
		suite.addTest(ScopeTests.suite());
32
		suite.addTest(ScopeTests.suite());
33
		suite.addTest(SaveableCompareEditorInputTest.suite());
32
		return suite;
34
		return suite;
33
	}
35
	}
34
}
36
}
(-)a/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/ui/SaveableCompareEditorInputTest.java (+275 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 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.team.tests.ui;
12
13
import java.io.ByteArrayInputStream;
14
import java.io.IOException;
15
import java.lang.reflect.InvocationTargetException;
16
import java.util.ArrayList;
17
import java.util.List;
18
19
import junit.framework.Test;
20
21
import org.eclipse.compare.CompareConfiguration;
22
import org.eclipse.compare.ITypedElement;
23
import org.eclipse.compare.contentmergeviewer.TextMergeViewer;
24
import org.eclipse.compare.internal.MergeSourceViewer;
25
import org.eclipse.compare.structuremergeviewer.Differencer;
26
import org.eclipse.compare.structuremergeviewer.ICompareInput;
27
import org.eclipse.compare.tests.ReflectionUtils;
28
import org.eclipse.core.internal.runtime.RuntimeLog;
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.runtime.CoreException;
33
import org.eclipse.core.runtime.ILogListener;
34
import org.eclipse.core.runtime.IProgressMonitor;
35
import org.eclipse.core.runtime.IStatus;
36
import org.eclipse.swt.custom.StyledText;
37
import org.eclipse.swt.graphics.Image;
38
import org.eclipse.swt.widgets.Shell;
39
import org.eclipse.team.internal.ui.mapping.AbstractCompareInput;
40
import org.eclipse.team.internal.ui.mapping.CompareInputChangeNotifier;
41
import org.eclipse.team.internal.ui.synchronize.LocalResourceTypedElement;
42
import org.eclipse.team.tests.core.TeamTest;
43
import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput;
44
import org.eclipse.ui.PlatformUI;
45
46
public class SaveableCompareEditorInputTest extends TeamTest {
47
48
	public static Test suite() {
49
		return suite(SaveableCompareEditorInputTest.class);
50
	}
51
52
	private IFile file1;
53
	private IFile file2;
54
	private String appendFileContents = "_append";
55
	private String fileContents1 = "FileContents";
56
	private String fileContents2 = "FileContents2";
57
	private TestLogListener logListener = new TestLogListener();
58
59
	protected void setUp() throws Exception {
60
		super.setUp();
61
62
		IProject project = createProject("Project_", new String[] {
63
				"File1.txt", "File2.txt" });
64
65
		file1 = project.getFile("File1.txt");
66
		file2 = project.getFile("File2.txt");
67
		file1.setContents(new ByteArrayInputStream(fileContents1.getBytes()),
68
				true, true, null);
69
		file2.setContents(new ByteArrayInputStream(fileContents2.getBytes()),
70
				true, true, null);
71
72
		RuntimeLog.addLogListener(logListener);
73
	}
74
75
	protected void tearDown() throws Exception {
76
		// remove log listener
77
		RuntimeLog.removeLogListener(logListener);
78
		super.tearDown();
79
	}
80
81
	private class TestFileElement implements ITypedElement {
82
83
		private IFile file;
84
85
		public IFile getFile() {
86
			return file;
87
		}
88
89
		public TestFileElement(IFile file) {
90
			super();
91
			this.file = file;
92
		}
93
94
		public String getName() {
95
			return file.getName();
96
		}
97
98
		public Image getImage() {
99
			return null;
100
		}
101
102
		public String getType() {
103
			return TEXT_TYPE;
104
		}
105
	}
106
107
	private class TestLogListener implements ILogListener {
108
		public void logging(IStatus status, String plugin) {
109
			fail(status.getMessage());
110
		}
111
	}
112
113
	private class TestDiffNode extends AbstractCompareInput {
114
115
		private CompareInputChangeNotifier notifier = new CompareInputChangeNotifier() {
116
117
			private IResource getResource(ITypedElement el) {
118
				if (el instanceof LocalResourceTypedElement) {
119
					return ((LocalResourceTypedElement) el).getResource();
120
				}
121
				if (el instanceof TestFileElement) {
122
					return ((TestFileElement) el).getFile();
123
				}
124
				return null;
125
			}
126
127
			protected IResource[] getResources(ICompareInput input) {
128
129
				List resources = new ArrayList();
130
				if (getResource(getLeft()) != null) {
131
					resources.add(getResource(getLeft()));
132
				}
133
				if (getResource(getRight()) != null) {
134
					resources.add(getResource(getRight()));
135
				}
136
				return (IResource[]) resources.toArray(new IResource[2]);
137
			}
138
		};
139
140
		public TestDiffNode(ITypedElement left, ITypedElement right) {
141
			super(Differencer.CHANGE, null, left, right);
142
		}
143
144
		public void fireChange() {
145
			super.fireChange();
146
		}
147
148
		protected CompareInputChangeNotifier getChangeNotifier() {
149
			return notifier;
150
		}
151
152
		public boolean needsUpdate() {
153
			// The remote never changes
154
			return false;
155
		}
156
157
		public void update() {
158
			fireChange();
159
		}
160
	}
161
162
	private class TestSaveableEditorInput extends SaveableCompareEditorInput {
163
164
		protected ITypedElement left;
165
		protected ITypedElement right;
166
		private ICompareInput input;
167
168
		public Object getCompareResult() {
169
			return input;
170
		}
171
172
		public TestSaveableEditorInput(ITypedElement left, ITypedElement right,
173
				CompareConfiguration conf) {
174
			super(conf, PlatformUI.getWorkbench().getActiveWorkbenchWindow()
175
					.getActivePage());
176
			this.left = left;
177
			this.right = right;
178
		}
179
180
		protected ICompareInput prepareCompareInput(IProgressMonitor monitor)
181
				throws InvocationTargetException, InterruptedException {
182
			input = createCompareInput();
183
			getCompareConfiguration().setLeftEditable(true);
184
			getCompareConfiguration().setRightEditable(false);
185
			return null;
186
		}
187
188
		private ICompareInput createCompareInput() {
189
			return new TestDiffNode(left, right);
190
		}
191
192
		protected void fireInputChange() {
193
			((TestDiffNode) getCompareResult()).fireChange();
194
		}
195
	}
196
197
	private void verifyDirtyStateChanges(
198
			TestSaveableEditorInput compareEditorInput)
199
			throws IllegalArgumentException, SecurityException,
200
			IllegalAccessException, NoSuchFieldException {
201
		Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
202
				.getShell();
203
204
		TextMergeViewer viewer = (TextMergeViewer) compareEditorInput
205
				.findContentViewer(null, compareEditorInput.input, shell);
206
		viewer.setInput(compareEditorInput.getCompareResult());
207
208
		MergeSourceViewer left = (MergeSourceViewer) ReflectionUtils.getField(
209
				viewer, "fLeft");
210
211
		StyledText leftText = left.getSourceViewer().getTextWidget();
212
213
		// modify the left side of editor
214
		leftText.append(appendFileContents);
215
216
		assertTrue(compareEditorInput.isDirty());
217
218
		// save editor
219
		viewer.flush(null);
220
221
		assertFalse(compareEditorInput.isDirty());
222
	}
223
224
	public void testDirtyFlagOnLocalResourceTypedElement()
225
			throws CoreException, InvocationTargetException,
226
			InterruptedException, IllegalArgumentException, SecurityException,
227
			IllegalAccessException, NoSuchFieldException,
228
			NoSuchMethodException, IOException {
229
230
		// Create left element by SaveableCompareEditorInput to be properly
231
		// saved, see javadoc to SaveableCompareEditorInput
232
		LocalResourceTypedElement el1 = (LocalResourceTypedElement) SaveableCompareEditorInput
233
				.createFileElement(file1);
234
		ITypedElement el2 = new TestFileElement(file2);
235
236
		CompareConfiguration conf = new CompareConfiguration();
237
		conf.setLeftEditable(true);
238
		TestSaveableEditorInput compareEditorInput = new TestSaveableEditorInput(
239
				el1, el2, conf);
240
241
		compareEditorInput.prepareCompareInput(null);
242
243
		verifyDirtyStateChanges(compareEditorInput);
244
245
		// check whether file was saved
246
247
		assertTrue(compareContent(new ByteArrayInputStream(
248
				(fileContents1 + appendFileContents).getBytes()),
249
				file1.getContents()));
250
	}
251
252
	public void testDirtyFlagOnCustomTypedElement() throws CoreException,
253
			InvocationTargetException, InterruptedException,
254
			IllegalArgumentException, SecurityException,
255
			IllegalAccessException, NoSuchFieldException,
256
			NoSuchMethodException, IOException {
257
258
		ITypedElement el1 = new TestFileElement(file1);
259
		ITypedElement el2 = new TestFileElement(file2);
260
261
		CompareConfiguration conf = new CompareConfiguration();
262
		conf.setLeftEditable(true);
263
		TestSaveableEditorInput compareEditorInput = new TestSaveableEditorInput(
264
				el1, el2, conf);
265
266
		compareEditorInput.prepareCompareInput(null);
267
268
		verifyDirtyStateChanges(compareEditorInput);
269
270
		/*
271
		 * not checking if changes were saved because in this case saving is not
272
		 * handled, see javadoc to SaveableCompareEditorInput.
273
		 */
274
	}
275
}

Return to bug 347557