|
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.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.Test; |
| 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.compare.tests.ReflectionUtils; |
| 30 |
import org.eclipse.core.internal.runtime.RuntimeLog; |
| 31 |
import org.eclipse.core.resources.IFile; |
| 32 |
import org.eclipse.core.resources.IProject; |
| 33 |
import org.eclipse.core.resources.IResource; |
| 34 |
import org.eclipse.core.resources.ResourcesPlugin; |
| 35 |
import org.eclipse.core.runtime.CoreException; |
| 36 |
import org.eclipse.core.runtime.ILogListener; |
| 37 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 38 |
import org.eclipse.core.runtime.IStatus; |
| 39 |
import org.eclipse.swt.custom.StyledText; |
| 40 |
import org.eclipse.swt.graphics.Image; |
| 41 |
import org.eclipse.swt.widgets.Shell; |
| 42 |
import org.eclipse.team.internal.ui.mapping.AbstractCompareInput; |
| 43 |
import org.eclipse.team.internal.ui.mapping.CompareInputChangeNotifier; |
| 44 |
import org.eclipse.team.internal.ui.synchronize.LocalResourceTypedElement; |
| 45 |
import org.eclipse.team.tests.core.TeamTest; |
| 46 |
import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput; |
| 47 |
import org.eclipse.ui.PlatformUI; |
| 48 |
|
| 49 |
public class SaveableCompareEditorInputTest extends TeamTest { |
| 50 |
|
| 51 |
public static Test suite() { |
| 52 |
return suite(SaveableCompareEditorInputTest.class); |
| 53 |
} |
| 54 |
|
| 55 |
private IFile file1; |
| 56 |
private IFile file2; |
| 57 |
private String appendFileContents = "_append"; |
| 58 |
private String fileContents1 = "FileContents"; |
| 59 |
private String fileContents2 = "FileContents2"; |
| 60 |
private TestLogListener logListener = new TestLogListener(); |
| 61 |
|
| 62 |
protected void setUp() throws Exception { |
| 63 |
super.setUp(); |
| 64 |
// prepare project with two files |
| 65 |
IProject project = ResourcesPlugin.getWorkspace().getRoot() |
| 66 |
.getProject("Project_" + System.currentTimeMillis()); |
| 67 |
project.create(null); |
| 68 |
project.open(null); |
| 69 |
|
| 70 |
file1 = project.getFile("File1_" + System.currentTimeMillis() + ".txt"); |
| 71 |
file1.create(new ByteArrayInputStream(fileContents1.getBytes()), true, |
| 72 |
null); |
| 73 |
|
| 74 |
file2 = project.getFile("File2_" + System.currentTimeMillis() + ".txt"); |
| 75 |
file2.create(new ByteArrayInputStream(fileContents2.getBytes()), true, |
| 76 |
null); |
| 77 |
|
| 78 |
RuntimeLog.addLogListener(logListener); |
| 79 |
} |
| 80 |
|
| 81 |
protected void tearDown() throws Exception { |
| 82 |
// remove log listener |
| 83 |
RuntimeLog.removeLogListener(logListener); |
| 84 |
super.tearDown(); |
| 85 |
} |
| 86 |
|
| 87 |
private class TestTestFileElement implements ITypedElement { |
| 88 |
|
| 89 |
private IFile file; |
| 90 |
|
| 91 |
public IFile getFile() { |
| 92 |
return file; |
| 93 |
} |
| 94 |
|
| 95 |
public TestTestFileElement(IFile file) { |
| 96 |
super(); |
| 97 |
this.file = file; |
| 98 |
} |
| 99 |
|
| 100 |
public String getName() { |
| 101 |
return file.getName(); |
| 102 |
} |
| 103 |
|
| 104 |
public Image getImage() { |
| 105 |
return null; |
| 106 |
} |
| 107 |
|
| 108 |
public String getType() { |
| 109 |
return TEXT_TYPE; |
| 110 |
} |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
private class TestLogListener implements ILogListener { |
| 115 |
|
| 116 |
public void logging(IStatus status, String plugin) { |
| 117 |
fail(status.getMessage()); |
| 118 |
} |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
private class TestDiffNode extends AbstractCompareInput { |
| 123 |
|
| 124 |
private CompareInputChangeNotifier notifier = new CompareInputChangeNotifier() { |
| 125 |
|
| 126 |
private IResource getResource(ITypedElement el) { |
| 127 |
if (el instanceof LocalResourceTypedElement) { |
| 128 |
return ((LocalResourceTypedElement) el).getResource(); |
| 129 |
} |
| 130 |
if (el instanceof TestTestFileElement) { |
| 131 |
return ((TestTestFileElement) el).getFile(); |
| 132 |
} |
| 133 |
return null; |
| 134 |
} |
| 135 |
|
| 136 |
protected IResource[] getResources(ICompareInput input) { |
| 137 |
|
| 138 |
List resources = new ArrayList(); |
| 139 |
if (getResource(getLeft()) != null) { |
| 140 |
resources.add(getResource(getLeft())); |
| 141 |
} |
| 142 |
if (getResource(getRight()) != null) { |
| 143 |
resources.add(getResource(getRight())); |
| 144 |
} |
| 145 |
|
| 146 |
return (IResource[]) resources.toArray(new IResource[2]); |
| 147 |
} |
| 148 |
}; |
| 149 |
|
| 150 |
public TestDiffNode(ITypedElement left, ITypedElement right) { |
| 151 |
super(Differencer.CHANGE, null, left, right); |
| 152 |
} |
| 153 |
|
| 154 |
public void fireChange() { |
| 155 |
super.fireChange(); |
| 156 |
} |
| 157 |
|
| 158 |
protected CompareInputChangeNotifier getChangeNotifier() { |
| 159 |
return notifier; |
| 160 |
} |
| 161 |
|
| 162 |
public boolean needsUpdate() { |
| 163 |
// The remote never changes |
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
public void update() { |
| 168 |
fireChange(); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
private class TestSaveableEditorInput extends SaveableCompareEditorInput { |
| 173 |
|
| 174 |
protected ITypedElement left; |
| 175 |
protected ITypedElement right; |
| 176 |
private ICompareInput input; |
| 177 |
|
| 178 |
public Object getCompareResult() { |
| 179 |
return input; |
| 180 |
} |
| 181 |
|
| 182 |
public TestSaveableEditorInput(ITypedElement left, ITypedElement right, |
| 183 |
CompareConfiguration conf) { |
| 184 |
super(conf, PlatformUI.getWorkbench().getActiveWorkbenchWindow() |
| 185 |
.getActivePage()); |
| 186 |
this.left = left; |
| 187 |
this.right = right; |
| 188 |
} |
| 189 |
|
| 190 |
protected ICompareInput prepareCompareInput(IProgressMonitor monitor) |
| 191 |
throws InvocationTargetException, InterruptedException { |
| 192 |
input = createCompareInput(); |
| 193 |
getCompareConfiguration().setLeftEditable(true); |
| 194 |
getCompareConfiguration().setRightEditable(false); |
| 195 |
return null; |
| 196 |
} |
| 197 |
|
| 198 |
private ICompareInput createCompareInput() { |
| 199 |
return new TestDiffNode(left, right); |
| 200 |
} |
| 201 |
|
| 202 |
protected void fireInputChange() { |
| 203 |
((TestDiffNode) getCompareResult()).fireChange(); |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
public void saveChanges(IProgressMonitor monitor) throws CoreException { |
| 208 |
super.saveChanges(monitor); |
| 209 |
} |
| 210 |
|
| 211 |
} |
| 212 |
|
| 213 |
private void verifyDirtyStateChanges( |
| 214 |
TestSaveableEditorInput compareEditorInput) |
| 215 |
throws IllegalArgumentException, SecurityException, |
| 216 |
IllegalAccessException, NoSuchFieldException { |
| 217 |
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow() |
| 218 |
.getShell(); |
| 219 |
|
| 220 |
TextMergeViewer viewer = (TextMergeViewer) compareEditorInput |
| 221 |
.findContentViewer(null, compareEditorInput.input, shell); |
| 222 |
viewer.setInput(compareEditorInput.getCompareResult()); |
| 223 |
|
| 224 |
MergeSourceViewer left = (MergeSourceViewer) ReflectionUtils.getField( |
| 225 |
viewer, "fLeft"); |
| 226 |
|
| 227 |
StyledText leftText = left.getSourceViewer().getTextWidget(); |
| 228 |
|
| 229 |
// modify the left side of editor |
| 230 |
leftText.append(appendFileContents); |
| 231 |
|
| 232 |
assertTrue(compareEditorInput.isDirty()); |
| 233 |
|
| 234 |
// save editor |
| 235 |
viewer.flush(null); |
| 236 |
|
| 237 |
assertFalse(compareEditorInput.isDirty()); |
| 238 |
|
| 239 |
} |
| 240 |
|
| 241 |
public void testDirtyFlagOnInputManagedFileElement() throws CoreException, |
| 242 |
InvocationTargetException, InterruptedException, |
| 243 |
IllegalArgumentException, SecurityException, |
| 244 |
IllegalAccessException, NoSuchFieldException, |
| 245 |
NoSuchMethodException, IOException { |
| 246 |
|
| 247 |
// Create left element by SaveableCompareEditorInput to be properly |
| 248 |
// saved, see javadoc to SaveableCompareEditorInput |
| 249 |
LocalResourceTypedElement el1 = (LocalResourceTypedElement) SaveableCompareEditorInput |
| 250 |
.createFileElement(file1); |
| 251 |
ITypedElement el2 = new TestTestFileElement(file2); |
| 252 |
|
| 253 |
CompareConfiguration conf = new CompareConfiguration(); |
| 254 |
conf.setLeftEditable(true); |
| 255 |
TestSaveableEditorInput compareEditorInput = new TestSaveableEditorInput( |
| 256 |
el1, el2, conf); |
| 257 |
|
| 258 |
compareEditorInput.prepareCompareInput(null); |
| 259 |
|
| 260 |
verifyDirtyStateChanges(compareEditorInput); |
| 261 |
|
| 262 |
// check weather file was saved |
| 263 |
|
| 264 |
BufferedReader reader = new BufferedReader(new InputStreamReader( |
| 265 |
file1.getContents())); |
| 266 |
String line = reader.readLine(); |
| 267 |
|
| 268 |
assertEquals(fileContents1 + appendFileContents, line); |
| 269 |
|
| 270 |
reader.close(); |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
public void testDirtyFlagOnCustomManagedFileElement() throws CoreException, |
| 275 |
InvocationTargetException, InterruptedException, |
| 276 |
IllegalArgumentException, SecurityException, |
| 277 |
IllegalAccessException, NoSuchFieldException, |
| 278 |
NoSuchMethodException, IOException { |
| 279 |
|
| 280 |
ITypedElement el1 = new TestTestFileElement(file1); |
| 281 |
ITypedElement el2 = new TestTestFileElement(file2); |
| 282 |
|
| 283 |
CompareConfiguration conf = new CompareConfiguration(); |
| 284 |
conf.setLeftEditable(true); |
| 285 |
TestSaveableEditorInput compareEditorInput = new TestSaveableEditorInput( |
| 286 |
el1, el2, conf); |
| 287 |
|
| 288 |
compareEditorInput.prepareCompareInput(null); |
| 289 |
|
| 290 |
verifyDirtyStateChanges(compareEditorInput); |
| 291 |
|
| 292 |
/* |
| 293 |
* not checking if changes were saved because in this case saving is not |
| 294 |
* handled, see javadoc to SaveableCompareEditorInput. |
| 295 |
*/ |
| 296 |
|
| 297 |
} |
| 298 |
} |