|
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.TestCase; |
| 22 |
|
| 23 |
import org.eclipse.compare.CompareConfiguration; |
| 24 |
import org.eclipse.compare.CompareEditorInput; |
| 25 |
import org.eclipse.compare.ITypedElement; |
| 26 |
import org.eclipse.compare.contentmergeviewer.TextMergeViewer; |
| 27 |
import org.eclipse.compare.internal.MergeSourceViewer; |
| 28 |
import org.eclipse.compare.structuremergeviewer.Differencer; |
| 29 |
import org.eclipse.compare.structuremergeviewer.ICompareInput; |
| 30 |
import org.eclipse.compare.tests.ReflectionUtils; |
| 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.IProgressMonitor; |
| 37 |
import org.eclipse.core.runtime.ISafeRunnable; |
| 38 |
import org.eclipse.core.runtime.ListenerList; |
| 39 |
import org.eclipse.core.runtime.SafeRunner; |
| 40 |
import org.eclipse.jface.dialogs.Dialog; |
| 41 |
import org.eclipse.jface.util.IPropertyChangeListener; |
| 42 |
import org.eclipse.jface.util.PropertyChangeEvent; |
| 43 |
import org.eclipse.swt.custom.StyledText; |
| 44 |
import org.eclipse.swt.graphics.Image; |
| 45 |
import org.eclipse.swt.widgets.Composite; |
| 46 |
import org.eclipse.swt.widgets.Control; |
| 47 |
import org.eclipse.swt.widgets.Shell; |
| 48 |
import org.eclipse.team.internal.ui.mapping.AbstractCompareInput; |
| 49 |
import org.eclipse.team.internal.ui.mapping.CompareInputChangeNotifier; |
| 50 |
import org.eclipse.team.internal.ui.synchronize.LocalResourceTypedElement; |
| 51 |
import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput; |
| 52 |
import org.eclipse.ui.PlatformUI; |
| 53 |
|
| 54 |
public class SaveableCompareEditorInputTest extends TestCase { |
| 55 |
|
| 56 |
private class FileElement implements ITypedElement { |
| 57 |
|
| 58 |
private IFile file; |
| 59 |
|
| 60 |
public IFile getFile() { |
| 61 |
return file; |
| 62 |
} |
| 63 |
|
| 64 |
public FileElement(IFile file) { |
| 65 |
super(); |
| 66 |
this.file = file; |
| 67 |
} |
| 68 |
|
| 69 |
public String getName() { |
| 70 |
return file.getName(); |
| 71 |
} |
| 72 |
|
| 73 |
public Image getImage() { |
| 74 |
return null; |
| 75 |
} |
| 76 |
|
| 77 |
public String getType() { |
| 78 |
return TEXT_TYPE; |
| 79 |
} |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
public class TestDiffNode extends AbstractCompareInput { |
| 84 |
|
| 85 |
private CompareInputChangeNotifier notifier = new CompareInputChangeNotifier() { |
| 86 |
|
| 87 |
private IResource getResource(ITypedElement el) { |
| 88 |
if (el instanceof LocalResourceTypedElement) { |
| 89 |
return ((LocalResourceTypedElement) el).getResource(); |
| 90 |
} |
| 91 |
if (el instanceof FileElement) { |
| 92 |
return ((FileElement) el).getFile(); |
| 93 |
} |
| 94 |
return null; |
| 95 |
} |
| 96 |
|
| 97 |
protected IResource[] getResources(ICompareInput input) { |
| 98 |
|
| 99 |
List resources = new ArrayList(); |
| 100 |
if (getResource(getLeft()) != null) { |
| 101 |
resources.add(getResource(getLeft())); |
| 102 |
} |
| 103 |
if (getResource(getRight()) != null) { |
| 104 |
resources.add(getResource(getRight())); |
| 105 |
} |
| 106 |
|
| 107 |
return (IResource[]) resources.toArray(new IResource[2]); |
| 108 |
} |
| 109 |
}; |
| 110 |
|
| 111 |
public TestDiffNode(ITypedElement left, ITypedElement right) { |
| 112 |
super(Differencer.CHANGE, null, left, right); |
| 113 |
} |
| 114 |
|
| 115 |
public void fireChange() { |
| 116 |
super.fireChange(); |
| 117 |
} |
| 118 |
|
| 119 |
protected CompareInputChangeNotifier getChangeNotifier() { |
| 120 |
return notifier; |
| 121 |
} |
| 122 |
|
| 123 |
public boolean needsUpdate() { |
| 124 |
// The remote never changes |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
public void update() { |
| 129 |
fireChange(); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
private class TestSaveableEditorInputLocal extends |
| 134 |
SaveableCompareEditorInput { |
| 135 |
|
| 136 |
protected ITypedElement left; |
| 137 |
protected ITypedElement right; |
| 138 |
private ICompareInput input; |
| 139 |
|
| 140 |
public Object getCompareResult() { |
| 141 |
return input; |
| 142 |
} |
| 143 |
|
| 144 |
public TestSaveableEditorInputLocal(ITypedElement left, |
| 145 |
ITypedElement right, CompareConfiguration conf) { |
| 146 |
super(conf, PlatformUI.getWorkbench().getActiveWorkbenchWindow() |
| 147 |
.getActivePage()); |
| 148 |
this.left = left; |
| 149 |
this.right = right; |
| 150 |
} |
| 151 |
|
| 152 |
protected ICompareInput prepareCompareInput(IProgressMonitor monitor) |
| 153 |
throws InvocationTargetException, InterruptedException { |
| 154 |
input = createCompareInput(); |
| 155 |
getCompareConfiguration().setLeftEditable(true); |
| 156 |
getCompareConfiguration().setRightEditable(false); |
| 157 |
return null; |
| 158 |
} |
| 159 |
|
| 160 |
private ICompareInput createCompareInput() { |
| 161 |
return new TestDiffNode(left, right); |
| 162 |
} |
| 163 |
|
| 164 |
protected void fireInputChange() { |
| 165 |
((TestDiffNode) getCompareResult()).fireChange(); |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
public void saveChanges(IProgressMonitor monitor) throws CoreException { |
| 170 |
super.saveChanges(monitor); |
| 171 |
} |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
private TextMergeViewer viewer; |
| 176 |
|
| 177 |
public void testDirtyFlagOnStandardLeft() throws CoreException, |
| 178 |
InvocationTargetException, InterruptedException, |
| 179 |
IllegalArgumentException, SecurityException, |
| 180 |
IllegalAccessException, NoSuchFieldException, |
| 181 |
NoSuchMethodException, IOException { |
| 182 |
IProject project = ResourcesPlugin.getWorkspace().getRoot() |
| 183 |
.getProject("Project_" + System.currentTimeMillis()); |
| 184 |
project.create(null); |
| 185 |
project.open(null); |
| 186 |
|
| 187 |
String fileContents1 = "FileContents"; |
| 188 |
String appendFileContents = "_append"; |
| 189 |
String fileContents2 = "FileContents2"; |
| 190 |
|
| 191 |
IFile file1 = project.getFile("File1_" + System.currentTimeMillis() |
| 192 |
+ ".txt"); |
| 193 |
file1.create(new ByteArrayInputStream(fileContents1.getBytes()), true, |
| 194 |
null); |
| 195 |
|
| 196 |
IFile file2 = project.getFile("File2_" + System.currentTimeMillis() |
| 197 |
+ ".txt"); |
| 198 |
file2.create(new ByteArrayInputStream(fileContents2.getBytes()), true, |
| 199 |
null); |
| 200 |
|
| 201 |
LocalResourceTypedElement el1 = (LocalResourceTypedElement) SaveableCompareEditorInput |
| 202 |
.createFileElement(file1); |
| 203 |
ITypedElement el2 = new FileElement(file2); |
| 204 |
|
| 205 |
CompareConfiguration conf = new CompareConfiguration(); |
| 206 |
conf.setLeftEditable(true); |
| 207 |
TestSaveableEditorInputLocal compareEditorInput = new TestSaveableEditorInputLocal( |
| 208 |
el1, el2, conf); |
| 209 |
|
| 210 |
final TestSaveableEditorInputLocal[] editorInputs = { compareEditorInput }; |
| 211 |
compareEditorInput.prepareCompareInput(null); |
| 212 |
|
| 213 |
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow() |
| 214 |
.getShell(); |
| 215 |
|
| 216 |
Dialog dialog = new Dialog(shell) { |
| 217 |
protected Control createDialogArea(Composite parent) { |
| 218 |
Composite composite = (Composite) super |
| 219 |
.createDialogArea(parent); |
| 220 |
viewer = (TextMergeViewer) editorInputs[0].findContentViewer( |
| 221 |
null, editorInputs[0].input, composite); |
| 222 |
viewer.setInput(editorInputs[0].getCompareResult()); |
| 223 |
return composite; |
| 224 |
} |
| 225 |
}; |
| 226 |
dialog.setBlockOnOpen(false); |
| 227 |
dialog.open(); |
| 228 |
|
| 229 |
MergeSourceViewer left = (MergeSourceViewer) ReflectionUtils.getField( |
| 230 |
viewer, "fLeft"); |
| 231 |
|
| 232 |
StyledText leftText = left.getSourceViewer().getTextWidget(); |
| 233 |
|
| 234 |
// modify the left side of editor |
| 235 |
leftText.append(appendFileContents); |
| 236 |
|
| 237 |
assertTrue(compareEditorInput.isDirty()); |
| 238 |
|
| 239 |
// save editor |
| 240 |
viewer.flush(null); |
| 241 |
|
| 242 |
assertFalse(compareEditorInput.isDirty()); |
| 243 |
|
| 244 |
dialog.close(); |
| 245 |
|
| 246 |
// check weather file was saved |
| 247 |
|
| 248 |
BufferedReader reader = new BufferedReader(new InputStreamReader( |
| 249 |
file1.getContents())); |
| 250 |
String line = reader.readLine(); |
| 251 |
|
| 252 |
assertEquals(fileContents1 + appendFileContents, line); |
| 253 |
|
| 254 |
} |
| 255 |
|
| 256 |
public void testDirtyFlagOnCustomLeft() throws CoreException, |
| 257 |
InvocationTargetException, InterruptedException, |
| 258 |
IllegalArgumentException, SecurityException, |
| 259 |
IllegalAccessException, NoSuchFieldException, |
| 260 |
NoSuchMethodException, IOException { |
| 261 |
IProject project = ResourcesPlugin.getWorkspace().getRoot() |
| 262 |
.getProject("Project_" + System.currentTimeMillis()); |
| 263 |
project.create(null); |
| 264 |
project.open(null); |
| 265 |
|
| 266 |
String fileContents1 = "FileContents"; |
| 267 |
String appendFileContents = "_append"; |
| 268 |
String fileContents2 = "FileContents2"; |
| 269 |
|
| 270 |
IFile file1 = project.getFile("File1_" + System.currentTimeMillis() |
| 271 |
+ ".txt"); |
| 272 |
file1.create(new ByteArrayInputStream(fileContents1.getBytes()), true, |
| 273 |
null); |
| 274 |
|
| 275 |
IFile file2 = project.getFile("File2_" + System.currentTimeMillis() |
| 276 |
+ ".txt"); |
| 277 |
file2.create(new ByteArrayInputStream(fileContents2.getBytes()), true, |
| 278 |
null); |
| 279 |
|
| 280 |
ITypedElement el1 = new FileElement(file1); |
| 281 |
ITypedElement el2 = new FileElement(file2); |
| 282 |
|
| 283 |
CompareConfiguration conf = new CompareConfiguration(); |
| 284 |
conf.setLeftEditable(true); |
| 285 |
TestSaveableEditorInputLocal compareEditorInput = new TestSaveableEditorInputLocal( |
| 286 |
el1, el2, conf); |
| 287 |
|
| 288 |
final TestSaveableEditorInputLocal[] editorInputs = { compareEditorInput }; |
| 289 |
compareEditorInput.prepareCompareInput(null); |
| 290 |
|
| 291 |
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow() |
| 292 |
.getShell(); |
| 293 |
|
| 294 |
Dialog dialog = new Dialog(shell) { |
| 295 |
protected Control createDialogArea(Composite parent) { |
| 296 |
Composite composite = (Composite) super |
| 297 |
.createDialogArea(parent); |
| 298 |
viewer = (TextMergeViewer) editorInputs[0].findContentViewer( |
| 299 |
null, editorInputs[0].input, composite); |
| 300 |
viewer.setInput(editorInputs[0].getCompareResult()); |
| 301 |
return composite; |
| 302 |
} |
| 303 |
}; |
| 304 |
dialog.setBlockOnOpen(false); |
| 305 |
dialog.open(); |
| 306 |
|
| 307 |
MergeSourceViewer left = (MergeSourceViewer) ReflectionUtils.getField( |
| 308 |
viewer, "fLeft"); |
| 309 |
|
| 310 |
StyledText leftText = left.getSourceViewer().getTextWidget(); |
| 311 |
|
| 312 |
leftText.append(appendFileContents); |
| 313 |
|
| 314 |
assertTrue(compareEditorInput.isDirty()); |
| 315 |
|
| 316 |
viewer.flush(null); |
| 317 |
|
| 318 |
assertFalse(compareEditorInput.isDirty()); |
| 319 |
|
| 320 |
dialog.close(); |
| 321 |
|
| 322 |
/* |
| 323 |
* not checking if changes were saved because in this case saving is not |
| 324 |
* handled |
| 325 |
*/ |
| 326 |
|
| 327 |
} |
| 328 |
|
| 329 |
public void testBug347557() throws CoreException, IllegalArgumentException, |
| 330 |
NoSuchMethodException, IllegalAccessException, |
| 331 |
InvocationTargetException, InterruptedException, SecurityException, |
| 332 |
NoSuchFieldException { |
| 333 |
IProject project = ResourcesPlugin.getWorkspace().getRoot() |
| 334 |
.getProject("Project_" + System.currentTimeMillis()); |
| 335 |
project.create(null); |
| 336 |
project.open(null); |
| 337 |
|
| 338 |
String fileContents1 = "FileContents"; |
| 339 |
String fileContents2 = "FileContents2"; |
| 340 |
|
| 341 |
IFile file1 = project.getFile("File1_" + System.currentTimeMillis() |
| 342 |
+ ".txt"); |
| 343 |
file1.create(new ByteArrayInputStream(fileContents1.getBytes()), true, |
| 344 |
null); |
| 345 |
|
| 346 |
IFile file2 = project.getFile("File2_" + System.currentTimeMillis() |
| 347 |
+ ".txt"); |
| 348 |
file2.create(new ByteArrayInputStream(fileContents2.getBytes()), true, |
| 349 |
null); |
| 350 |
|
| 351 |
ITypedElement el1 = new FileElement(file1); |
| 352 |
ITypedElement el2 = new FileElement(file2); |
| 353 |
|
| 354 |
CompareConfiguration conf = new CompareConfiguration(); |
| 355 |
conf.setLeftEditable(true); |
| 356 |
TestSaveableEditorInputLocal compareEditorInput = new TestSaveableEditorInputLocal( |
| 357 |
el1, el2, conf); |
| 358 |
compareEditorInput.prepareCompareInput(null); |
| 359 |
|
| 360 |
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow() |
| 361 |
.getShell(); |
| 362 |
|
| 363 |
viewer = (TextMergeViewer) compareEditorInput.findContentViewer(null, |
| 364 |
compareEditorInput.input, shell); |
| 365 |
viewer.setInput(compareEditorInput.getCompareResult()); |
| 366 |
|
| 367 |
// get listeners list |
| 368 |
final ListenerList listenerList = (ListenerList) ReflectionUtils |
| 369 |
.getField(viewer, "fListenerList", true); |
| 370 |
|
| 371 |
final PropertyChangeEvent event = new PropertyChangeEvent(viewer, |
| 372 |
CompareEditorInput.DIRTY_STATE, null, Boolean.TRUE); |
| 373 |
|
| 374 |
// Notify all the listeners about dirty property change |
| 375 |
Object[] listeners = listenerList.getListeners(); |
| 376 |
for (int i = 0; i < listeners.length; i++) { |
| 377 |
final IPropertyChangeListener listener = (IPropertyChangeListener) listeners[i]; |
| 378 |
SafeRunner.run(new ISafeRunnable() { |
| 379 |
public void run() throws Exception { |
| 380 |
listener.propertyChange(event); |
| 381 |
} |
| 382 |
|
| 383 |
public void handleException(Throwable exception) { |
| 384 |
fail(exception.getMessage()); |
| 385 |
} |
| 386 |
}); |
| 387 |
} |
| 388 |
|
| 389 |
} |
| 390 |
} |