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 151668 | Differences between
and this patch

Collapse All | Expand All

(-)a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/ccp/CopyTest.java (-2 / +96 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2012 IBM Corporation and others.
2
 * Copyright (c) 2000, 2014 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 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Yves Joan <yves.joan@oracle.com> - [reorg] Copy action should NOT add 'copy of' prefix - https://bugs.eclipse.org/bugs/show_bug.cgi?id=151668
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.ui.tests.refactoring.ccp;
12
package org.eclipse.jdt.ui.tests.refactoring.ccp;
12
13
Lines 173-184 public class CopyTest extends RefactoringTest { Link Here
173
		private static final String NEW_FILE_NAME= "UnusedName.gif";
174
		private static final String NEW_FILE_NAME= "UnusedName.gif";
174
		private static final String NEW_FOLDER_NAME= "UnusedName";
175
		private static final String NEW_FOLDER_NAME= "UnusedName";
175
		private static final String NEW_CU_NAME= "UnusedName";
176
		private static final String NEW_CU_NAME= "UnusedName";
177
		private String fCuInitialSuggestedName= "unset";
178
		private String fResourceInitialSuggestedName= "unset";
176
179
177
		public INewNameQuery createNewCompilationUnitNameQuery(ICompilationUnit cu, String s) {
180
		public INewNameQuery createNewCompilationUnitNameQuery(ICompilationUnit cu, String s) {
181
			setCuInitialSuggestedName(s);
178
			return createStaticQuery(NEW_CU_NAME);
182
			return createStaticQuery(NEW_CU_NAME);
179
		}
183
		}
180
184
181
		public INewNameQuery createNewResourceNameQuery(IResource res, String s) {
185
		public INewNameQuery createNewResourceNameQuery(IResource res, String s) {
186
			setResourceInitialSuggestedName(s);
182
			if (res instanceof IFile)
187
			if (res instanceof IFile)
183
				return createStaticQuery(NEW_FILE_NAME);
188
				return createStaticQuery(NEW_FILE_NAME);
184
			else
189
			else
Lines 204-209 public class CopyTest extends RefactoringTest { Link Here
204
		public INewNameQuery createNewPackageFragmentRootNameQuery(IPackageFragmentRoot root, String initialSuggestedName) {
209
		public INewNameQuery createNewPackageFragmentRootNameQuery(IPackageFragmentRoot root, String initialSuggestedName) {
205
			return createStaticQuery(NEW_PACKAGE_FRAGMENT_ROOT_NAME);
210
			return createStaticQuery(NEW_PACKAGE_FRAGMENT_ROOT_NAME);
206
		}
211
		}
212
213
		public String getCuInitialSuggestedName() {
214
			return fCuInitialSuggestedName;
215
		}
216
217
		private void setCuInitialSuggestedName(String cuInitialSuggestedName) {
218
			this.fCuInitialSuggestedName= cuInitialSuggestedName;
219
		}
220
221
		public String getResourceInitialSuggestedName() {
222
			return fResourceInitialSuggestedName;
223
		}
224
225
		public void setResourceInitialSuggestedName(String resourceInitialSuggestedName) {
226
			fResourceInitialSuggestedName= resourceInitialSuggestedName;
227
		}
207
	}
228
	}
208
229
209
	private static class MockCancelNameQueries implements INewNameQueries{
230
	private static class MockCancelNameQueries implements INewNameQueries{
Lines 1481-1486 public class CopyTest extends RefactoringTest { Link Here
1481
		ParticipantTesting.testCopy(handles, new CopyArguments[] {
1502
		ParticipantTesting.testCopy(handles, new CopyArguments[] {
1482
				new CopyArguments(destination, log)
1503
				new CopyArguments(destination, log)
1483
		});
1504
		});
1505
		assertEquals("a2.txt", ((MockNewNameQueries)queries).getResourceInitialSuggestedName());
1484
	}
1506
	}
1485
1507
1486
	public void testCopy_File_to_Same_Folder_Cancel() throws Exception {
1508
	public void testCopy_File_to_Same_Folder_Cancel() throws Exception {
Lines 1541-1546 public class CopyTest extends RefactoringTest { Link Here
1541
		assertTrue("new file does not exist after copying", newFile.exists());
1563
		assertTrue("new file does not exist after copying", newFile.exists());
1542
	}
1564
	}
1543
1565
1566
	public void testCopy_File_to_Itself_2() throws Exception {
1567
		IFolder parentFolder= (IFolder)getPackageP().getResource();
1568
		String fileName= "A.java";
1569
		IFile file= parentFolder.getFile(fileName);
1570
		file.create(getStream("123"), true, null);
1571
		String conflictingFileName= "A2.java";
1572
		IFile conflictingFile= parentFolder.getFile(conflictingFileName);
1573
		conflictingFile.create(getStream("456"), true, null);
1574
1575
		INewNameQueries queries= new MockNewNameQueries();
1576
1577
		IJavaElement[] javaElements= {};
1578
		IResource[] resources= { file };
1579
		JavaCopyProcessor ref= verifyEnabled(resources, javaElements, queries, createReorgQueries());
1580
1581
		Object destination= file;
1582
		verifyValidDestination(ref, destination);
1583
1584
		assertTrue("source file does not exist before copying", file.exists());
1585
1586
		RefactoringStatus status= performRefactoring(ref, false);
1587
		assertEquals(null, status);
1588
1589
		assertTrue("source file does not exist after copying", file.exists());
1590
1591
		IFile newFile= parentFolder.getFile(MockNewNameQueries.NEW_FILE_NAME);
1592
		assertTrue("new file does not exist after copying", newFile.exists());
1593
		assertEquals("A3.java", ((MockNewNameQueries)queries).getResourceInitialSuggestedName());
1594
	}
1595
1544
	public void testCopy_File_to_AnotherFile() throws Exception {
1596
	public void testCopy_File_to_AnotherFile() throws Exception {
1545
		IFolder parentFolder= (IFolder) getPackageP().getResource();
1597
		IFolder parentFolder= (IFolder) getPackageP().getResource();
1546
		String fileName= "a.txt";
1598
		String fileName= "a.txt";
Lines 1919-1924 public class CopyTest extends RefactoringTest { Link Here
1919
1971
1920
		ICompilationUnit newCu= getPackageP().getCompilationUnit(MockNewNameQueries.NEW_CU_NAME + ".java");
1972
		ICompilationUnit newCu= getPackageP().getCompilationUnit(MockNewNameQueries.NEW_CU_NAME + ".java");
1921
		assertTrue("new file does not exist after copying", newCu.exists());
1973
		assertTrue("new file does not exist after copying", newCu.exists());
1974
		assertEquals("A2", ((MockNewNameQueries)queries).getCuInitialSuggestedName());
1922
	}
1975
	}
1923
1976
1924
	public void testCopy_Cu_to_OtherPackage() throws Exception {
1977
	public void testCopy_Cu_to_OtherPackage() throws Exception {
Lines 2322-2328 public class CopyTest extends RefactoringTest { Link Here
2322
		folder.create(true, true, null);
2375
		folder.create(true, true, null);
2323
		IJavaElement[] javaElements= {};
2376
		IJavaElement[] javaElements= {};
2324
		IResource[] resources= {folder};
2377
		IResource[] resources= {folder};
2325
		JavaCopyProcessor ref= verifyEnabled(resources, javaElements, new MockNewNameQueries(), createReorgQueries());
2378
		INewNameQueries queries= new MockNewNameQueries();
2379
2380
		JavaCopyProcessor ref= verifyEnabled(resources, javaElements, queries, createReorgQueries());
2381
		String[] handles= ParticipantTesting.createHandles(folder);
2382
2383
		Object destination= superFolder;
2384
		verifyValidDestination(ref, destination);
2385
2386
		assertTrue("source does not exist before copying", folder.exists());
2387
2388
		RefactoringStatus status= performRefactoring(ref, false);
2389
		assertEquals(null, status);
2390
2391
		assertTrue("source does not exist after copying", folder.exists());
2392
		IFolder newFolder= superFolder.getFolder(MockNewNameQueries.NEW_FOLDER_NAME);
2393
		assertTrue("copied folder does not exist after copying", newFolder.exists());
2394
		ReorgExecutionLog log= new ReorgExecutionLog();
2395
		log.markAsProcessed(folder);
2396
		log.setNewName(folder, MockNewNameQueries.NEW_FOLDER_NAME);
2397
		ParticipantTesting.testCopy(handles, new CopyArguments[] {
2398
				new CopyArguments(destination, log)
2399
		});
2400
		assertEquals(folderName + "2", ((MockNewNameQueries)queries).getResourceInitialSuggestedName());
2401
2402
	}
2403
2404
	public void testCopy_folder_to_same_container_2() throws Exception {
2405
		ParticipantTesting.reset();
2406
		IProject superFolder= RefactoringTestSetup.getProject().getProject();
2407
		String folderName= "folder.name.with.segments";
2408
		IFolder folder= superFolder.getFolder(folderName);
2409
		folder.create(true, true, null);
2410
		IJavaElement[] javaElements= {};
2411
		IResource[] resources= { folder };
2412
		INewNameQueries queries= new MockNewNameQueries();
2413
2414
		String secondFolderName= "folder.name.with.segments2";
2415
		IFolder secondFolder= superFolder.getFolder(secondFolderName);
2416
		secondFolder.create(true, true, null);
2417
2418
		JavaCopyProcessor ref= verifyEnabled(resources, javaElements, queries, createReorgQueries());
2326
		String[] handles= ParticipantTesting.createHandles(folder);
2419
		String[] handles= ParticipantTesting.createHandles(folder);
2327
2420
2328
		Object destination= superFolder;
2421
		Object destination= superFolder;
Lines 2342-2347 public class CopyTest extends RefactoringTest { Link Here
2342
		ParticipantTesting.testCopy(handles, new CopyArguments[] {
2435
		ParticipantTesting.testCopy(handles, new CopyArguments[] {
2343
				new CopyArguments(destination, log)
2436
				new CopyArguments(destination, log)
2344
		});
2437
		});
2438
		assertEquals(folderName + "3", ((MockNewNameQueries)queries).getResourceInitialSuggestedName());
2345
2439
2346
	}
2440
	}
2347
2441
(-)a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/RefactoringCoreMessages.java (-8 / +1 lines)
Lines 9-14 Link Here
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Extract method and continue https://bugs.eclipse.org/bugs/show_bug.cgi?id=48056
10
 *     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Extract method and continue https://bugs.eclipse.org/bugs/show_bug.cgi?id=48056
11
 *     Samrat Dhillon <samrat.dhillon@gmail.com> - [introduce factory] Introduce Factory on an abstract class adds a statement to create an instance of that class - https://bugs.eclipse.org/bugs/show_bug.cgi?id=395016
11
 *     Samrat Dhillon <samrat.dhillon@gmail.com> - [introduce factory] Introduce Factory on an abstract class adds a statement to create an instance of that class - https://bugs.eclipse.org/bugs/show_bug.cgi?id=395016
12
 *     Yves Joan <yves.joan@oracle.com> - [reorg] Copy action should NOT add 'copy of' prefix - https://bugs.eclipse.org/bugs/show_bug.cgi?id=151668
12
 *******************************************************************************/
13
 *******************************************************************************/
13
package org.eclipse.jdt.internal.corext.refactoring;
14
package org.eclipse.jdt.internal.corext.refactoring;
14
15
Lines 276-293 public final class RefactoringCoreMessages extends NLS { Link Here
276
277
277
	public static String CopyPackageFragmentRootChange_copy;
278
	public static String CopyPackageFragmentRootChange_copy;
278
279
279
	public static String CopyRefactoring_cu_copyOf1;
280
281
	public static String CopyRefactoring_cu_copyOfMore;
282
283
	public static String CopyRefactoring_package_copyOf1;
280
	public static String CopyRefactoring_package_copyOf1;
284
281
285
	public static String CopyRefactoring_package_copyOfMore;
282
	public static String CopyRefactoring_package_copyOfMore;
286
283
287
	public static String CopyRefactoring_resource_copyOf1;
288
289
	public static String CopyRefactoring_resource_copyOfMore;
290
291
	public static String CopyRefactoring_update_ref;
284
	public static String CopyRefactoring_update_ref;
292
285
293
	public static String CopyResourceString_copy;
286
	public static String CopyResourceString_copy;
(-)a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/refactoring.properties (-4 / +1 lines)
Lines 10-15 Link Here
10
#     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Extract method and continue https://bugs.eclipse.org/bugs/show_bug.cgi?id=48056
10
#     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Extract method and continue https://bugs.eclipse.org/bugs/show_bug.cgi?id=48056
11
#     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Name ambiguous return value in error message - https://bugs.eclipse.org/bugs/show_bug.cgi?id=50607
11
#     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Name ambiguous return value in error message - https://bugs.eclipse.org/bugs/show_bug.cgi?id=50607
12
#	  Samrat Dhillon <samrat.dhillon@gmail.com> - [introduce factory] Introduce Factory on an abstract class adds a statement to create an instance of that class - https://bugs.eclipse.org/bugs/show_bug.cgi?id=395016
12
#	  Samrat Dhillon <samrat.dhillon@gmail.com> - [introduce factory] Introduce Factory on an abstract class adds a statement to create an instance of that class - https://bugs.eclipse.org/bugs/show_bug.cgi?id=395016
13
#     Yves Joan <yves.joan@oracle.com> - [reorg] Copy action should NOT add 'copy of' prefix - https://bugs.eclipse.org/bugs/show_bug.cgi?id=151668
13
###############################################################################
14
###############################################################################
14
# NLS properties for the Refactoring Core
15
# NLS properties for the Refactoring Core
15
16
Lines 579-588 MoveCuUpdateCreator_update_imports=Update imports Link Here
579
MoveCuUpdateCreator_searching=Searching for references to types in ''{0}''
580
MoveCuUpdateCreator_searching=Searching for references to types in ''{0}''
580
MoveCuUpdateCreator_update_references=Update references
581
MoveCuUpdateCreator_update_references=Update references
581
582
582
CopyRefactoring_cu_copyOf1=CopyOf{0}
583
CopyRefactoring_cu_copyOfMore=Copy_{0}_of_{1}
584
CopyRefactoring_resource_copyOf1=Copy of {0}
585
CopyRefactoring_resource_copyOfMore=Copy ({0}) of {1}
586
CopyRefactoring_package_copyOf1={0}.copy
583
CopyRefactoring_package_copyOf1={0}.copy
587
CopyRefactoring_package_copyOfMore={1}.copy{0}
584
CopyRefactoring_package_copyOfMore={1}.copy{0}
588
585
(-)a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/reorg/ReorgPolicyFactory.java (-15 / +36 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2013 IBM Corporation and others.
2
 * Copyright (c) 2000, 2014 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 8-13 Link Here
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Carsten Pfeiffer <carsten.pfeiffer@gebit.de> - [ccp] ReorgPolicies' canEnable() methods return true too often - https://bugs.eclipse.org/bugs/show_bug.cgi?id=303698
10
 *     Carsten Pfeiffer <carsten.pfeiffer@gebit.de> - [ccp] ReorgPolicies' canEnable() methods return true too often - https://bugs.eclipse.org/bugs/show_bug.cgi?id=303698
11
 *     Yves Joan <yves.joan@oracle.com> - [reorg] Copy action should NOT add 'copy of' prefix - https://bugs.eclipse.org/bugs/show_bug.cgi?id=151668
11
 *******************************************************************************/
12
 *******************************************************************************/
12
package org.eclipse.jdt.internal.corext.refactoring.reorg;
13
package org.eclipse.jdt.internal.corext.refactoring.reorg;
13
14
Lines 22-27 import java.util.List; Link Here
22
import java.util.Map;
23
import java.util.Map;
23
import java.util.Set;
24
import java.util.Set;
24
import java.util.StringTokenizer;
25
import java.util.StringTokenizer;
26
import java.util.regex.Matcher;
27
import java.util.regex.Pattern;
25
28
26
import org.eclipse.core.runtime.Assert;
29
import org.eclipse.core.runtime.Assert;
27
import org.eclipse.core.runtime.CoreException;
30
import org.eclipse.core.runtime.CoreException;
Lines 2557-2579 public final class ReorgPolicyFactory { Link Here
2557
2560
2558
		private final Set<String> fAutoGeneratedNewNames= new HashSet<String>(2);
2561
		private final Set<String> fAutoGeneratedNewNames= new HashSet<String>(2);
2559
2562
2563
		private String computeNewName(String str, int resourceType) {
2564
        	final int lastIndexOfDot= str.lastIndexOf('.');
2565
            String fileExtension= ""; //$NON-NLS-1$
2566
            String fileNameNoExtension= str;
2567
			if (lastIndexOfDot != -1 && resourceType == IResource.FILE) {
2568
            	fileExtension= str.substring(lastIndexOfDot);
2569
            	fileNameNoExtension= str.substring(0, lastIndexOfDot);
2570
            }
2571
            final Pattern p= Pattern.compile("[0-9]+$"); //$NON-NLS-1$
2572
            final Matcher m= p.matcher(fileNameNoExtension);
2573
            if (m.find()) {
2574
                // String ends with a number: increment it by 1
2575
                final Integer newNumber= new Integer(Integer.parseInt(m.group()) + 1);
2576
                final String numberStr= m.replaceFirst(newNumber.toString());
2577
                return numberStr + fileExtension;
2578
            } else {
2579
                return fileNameNoExtension + "2" + fileExtension; //$NON-NLS-1$
2580
            }
2581
        }
2582
2560
		public String createNewName(ICompilationUnit cu, IPackageFragment destination) {
2583
		public String createNewName(ICompilationUnit cu, IPackageFragment destination) {
2561
			if (isNewNameOk(destination, cu.getElementName()))
2584
			if (isNewNameOk(destination, cu.getElementName()))
2562
				return null;
2585
				return null;
2563
			if (!ReorgUtils.isParentInWorkspaceOrOnDisk(cu, destination))
2586
			if (!ReorgUtils.isParentInWorkspaceOrOnDisk(cu, destination))
2564
				return null;
2587
				return null;
2565
			int i= 1;
2588
			
2589
			int resourceType= cu.getResource().getType();
2590
			String newName= computeNewName(cu.getElementName(), resourceType);
2591
			
2566
			while (true) {
2592
			while (true) {
2567
				String newName;
2568
				if (i == 1)
2569
					newName= Messages.format(RefactoringCoreMessages.CopyRefactoring_cu_copyOf1, cu.getElementName()); // Don't use BasicElementLabels! No RTL!
2570
				else
2571
					newName= Messages.format(RefactoringCoreMessages.CopyRefactoring_cu_copyOfMore, new String[] { String.valueOf(i), cu.getElementName()}); // Don't use BasicElementLabels! No RTL!
2572
				if (isNewNameOk(destination, newName) && !fAutoGeneratedNewNames.contains(newName)) {
2593
				if (isNewNameOk(destination, newName) && !fAutoGeneratedNewNames.contains(newName)) {
2573
					fAutoGeneratedNewNames.add(newName);
2594
					fAutoGeneratedNewNames.add(newName);
2574
					return JavaCore.removeJavaLikeExtension(newName);
2595
					return JavaCore.removeJavaLikeExtension(newName);
2596
				} else {
2597
					newName= computeNewName(newName, resourceType);
2575
				}
2598
				}
2576
				i++;
2577
			}
2599
			}
2578
		}
2600
		}
2579
2601
Lines 2602-2619 public final class ReorgPolicyFactory { Link Here
2602
				return null;
2624
				return null;
2603
			if (!ReorgUtils.isParentInWorkspaceOrOnDisk(res, destination))
2625
			if (!ReorgUtils.isParentInWorkspaceOrOnDisk(res, destination))
2604
				return null;
2626
				return null;
2605
			int i= 1;
2627
			
2628
			int resourceType= res.getType();
2629
			String newName= computeNewName(res.getName(), resourceType);
2630
			
2606
			while (true) {
2631
			while (true) {
2607
				String newName;
2608
				if (i == 1)
2609
					newName= Messages.format(RefactoringCoreMessages.CopyRefactoring_resource_copyOf1, res.getName()); // Don't use BasicElementLabels! No RTL!
2610
				else
2611
					newName= Messages.format(RefactoringCoreMessages.CopyRefactoring_resource_copyOfMore, new String[] { String.valueOf(i), res.getName()}); // Don't use BasicElementLabels! No RTL!
2612
				if (isNewNameOk(destination, newName) && !fAutoGeneratedNewNames.contains(newName)) {
2632
				if (isNewNameOk(destination, newName) && !fAutoGeneratedNewNames.contains(newName)) {
2613
					fAutoGeneratedNewNames.add(newName);
2633
					fAutoGeneratedNewNames.add(newName);
2614
					return newName;
2634
					return newName;
2635
				} else {
2636
					newName= computeNewName(newName, resourceType);
2615
				}
2637
				}
2616
				i++;
2617
			}
2638
			}
2618
		}
2639
		}
2619
	}
2640
	}

Return to bug 151668