|
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 |
} |