View | Details | Raw Unified | Return to bug 117282
Collapse All | Expand All

(-)src/org/eclipse/jdt/core/tests/model/CopyMoveResourcesTests.java (-1 / +38 lines)
Lines 196-203 Link Here
196
	
196
	
197
	this.createJavaProject("P", new String[] {"src", "src2"}, "bin");
197
	this.createJavaProject("P", new String[] {"src", "src2"}, "bin");
198
}
198
}
199
static {
200
//	TESTS_NAMES = new String[] { "testCopyWorkingCopyDestination"};
201
}
199
public static Test suite() {
202
public static Test suite() {
200
	return new Suite(CopyMoveResourcesTests.class);
203
	return buildTestSuite(CopyMoveResourcesTests.class);
201
}
204
}
202
/**
205
/**
203
 * Cleanup after the previous test.
206
 * Cleanup after the previous test.
Lines 557-562 Link Here
557
		if (copy != null) copy.discardWorkingCopy();
560
		if (copy != null) copy.discardWorkingCopy();
558
	}
561
	}
559
}
562
}
563
/*
564
 * Ensures that a CU can be copied over an existing primary working copy in a different package.
565
 * (regression test for bug 117282 Package declaration inserted on wrong CU while copying class if names collide and editor opened)
566
 */
567
public void testCopyWorkingCopyDestination() throws CoreException {
568
	ICompilationUnit copy = null;
569
	try {
570
		createFolder("/P/src/p1");
571
		createFile(
572
			"/P/src/p1/X.java",
573
			"package p1;\n" +
574
			"public class X {\n" +
575
			"  void foo() {}\n" +
576
			"}"
577
		);
578
		ICompilationUnit cuSource = getCompilationUnit("/P/src/p1/X.java");
579
	
580
		createFolder("/P/src/p2");
581
		IPackageFragment pkgDest = getPackage("/P/src/p2");
582
		createFile(
583
			"/P/src/p2/X.java",
584
			"\n" +
585
			"package p1;\n" +
586
			"public class X {\n" +
587
			"}"
588
		);
589
		copy = getCompilationUnit("/P/src/p2/X.java");
590
		copy.becomeWorkingCopy(null, null);
591
	
592
		copyPositive(cuSource, pkgDest, null, null, true/*force*/);
593
	} finally {
594
		if (copy != null) copy.discardWorkingCopy();
595
	}
596
}
560
/**
597
/**
561
 * Ensures that a WorkingCopy can be copied to a different package, replacing an existing WorkingCopy.
598
 * Ensures that a WorkingCopy can be copied to a different package, replacing an existing WorkingCopy.
562
 */
599
 */
(-)model/org/eclipse/jdt/internal/core/CopyResourceElementsOperation.java (-17 / +21 lines)
Lines 247-273 Link Here
247
		org.eclipse.jdt.internal.core.CompilationUnit destCU = new org.eclipse.jdt.internal.core.CompilationUnit(dest, destName, DefaultWorkingCopyOwner.PRIMARY);
247
		org.eclipse.jdt.internal.core.CompilationUnit destCU = new org.eclipse.jdt.internal.core.CompilationUnit(dest, destName, DefaultWorkingCopyOwner.PRIMARY);
248
		if (!destFile.equals(sourceResource)) {
248
		if (!destFile.equals(sourceResource)) {
249
			try {
249
			try {
250
				if (destFile.exists()) {
250
				if (!destCU.isWorkingCopy()) {
251
					if (this.force) {
251
					if (destFile.exists()) {
252
						// we can remove it
252
						if (this.force) {
253
						deleteResource(destFile, IResource.KEEP_HISTORY);
253
							// we can remove it
254
						destCU.close(); // ensure the in-memory buffer for the dest CU is closed
254
							deleteResource(destFile, IResource.KEEP_HISTORY);
255
							destCU.close(); // ensure the in-memory buffer for the dest CU is closed
256
						} else {
257
							// abort
258
							throw new JavaModelException(new JavaModelStatus(
259
								IJavaModelStatusConstants.NAME_COLLISION, 
260
								Messages.bind(Messages.status_nameCollision, destFile.getFullPath().toString()))); 
261
						}
262
					}
263
					int flags = this.force ? IResource.FORCE : IResource.NONE;
264
					if (this.isMove()) {
265
						flags |= IResource.KEEP_HISTORY;
266
						sourceResource.move(destFile.getFullPath(), flags, getSubProgressMonitor(1));
255
					} else {
267
					} else {
256
						// abort
268
						if (rewrite != null) flags |= IResource.KEEP_HISTORY;
257
						throw new JavaModelException(new JavaModelStatus(
269
						sourceResource.copy(destFile.getFullPath(), flags, getSubProgressMonitor(1));
258
							IJavaModelStatusConstants.NAME_COLLISION, 
259
							Messages.bind(Messages.status_nameCollision, destFile.getFullPath().toString()))); 
260
					}
270
					}
261
				}
271
					this.setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE); 
262
				int flags = this.force ? IResource.FORCE : IResource.NONE;
263
				if (this.isMove()) {
264
					flags |= IResource.KEEP_HISTORY;
265
					sourceResource.move(destFile.getFullPath(), flags, getSubProgressMonitor(1));
266
				} else {
272
				} else {
267
					if (rewrite != null) flags |= IResource.KEEP_HISTORY;
273
					destCU.getBuffer().setContents(source.getBuffer().getContents());
268
					sourceResource.copy(destFile.getFullPath(), flags, getSubProgressMonitor(1));
269
				}
274
				}
270
				this.setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE); 
271
			} catch (JavaModelException e) {
275
			} catch (JavaModelException e) {
272
				throw e;
276
				throw e;
273
			} catch (CoreException e) {
277
			} catch (CoreException e) {

Return to bug 117282