Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephan Herrmann2013-02-02 21:21:52 +0000
committerStephan Herrmann2013-02-02 21:25:55 +0000
commit4d9b750daf8a975bd03dc3148bc9e4af3268431b (patch)
tree89fdd2e914ec920e374975a609e3c10e1668cb0d
parent1a0ce3b4829b4fc25d98443f6eae48dab98a2131 (diff)
downloadorg.eclipse.objectteams-4d9b750daf8a975bd03dc3148bc9e4af3268431b.tar.gz
org.eclipse.objectteams-4d9b750daf8a975bd03dc3148bc9e4af3268431b.tar.xz
org.eclipse.objectteams-4d9b750daf8a975bd03dc3148bc9e4af3268431b.zip
Bug 382185 - [refactoring] renaming base package fails to rename base
import
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/rewrite/ImportRewrite.java10
-rw-r--r--plugins/org.eclipse.objectteams.otdt.refactoring/src/org/eclipse/objectteams/otdt/internal/refactoring/adaptor/RenameAdaptor.java125
-rw-r--r--testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/src/org/eclipse/objectteams/otdt/ui/tests/refactoring/RefactoringTest.java21
-rw-r--r--testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/src/org/eclipse/objectteams/otdt/ui/tests/refactoring/rename/AllTests.java8
-rw-r--r--testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/src/org/eclipse/objectteams/otdt/ui/tests/refactoring/rename/RenamePackageTests.java392
-rw-r--r--testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/testdata/RenamePackage/testRenameBaseImportedPackage1/in/bases/MyClass.java3
-rw-r--r--testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/testdata/RenamePackage/testRenameBaseImportedPackage1/in/teams/MyTeam.java7
-rw-r--r--testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/testdata/RenamePackage/testRenameBaseImportedPackage1/out/baseclasses/MyClass.java3
-rw-r--r--testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/testdata/RenamePackage/testRenameBaseImportedPackage1/out/teams/MyTeam.java7
9 files changed, 567 insertions, 9 deletions
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/rewrite/ImportRewrite.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/rewrite/ImportRewrite.java
index 29f4aa89a..d9d8240dd 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/rewrite/ImportRewrite.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/rewrite/ImportRewrite.java
@@ -1061,6 +1061,16 @@ public final class ImportRewrite {
return removeEntry(STATIC_PREFIX + qualifiedName);
}
+//{ObjectTeams:
+ /**
+ * Remove a base-import, similar to {@link #removeImport(String)} and {@link #removeStaticImport(String)}.
+ * @since 3.9
+ */
+ public boolean removeImportBase(String qualifiedName) {
+ return removeEntry(BASE_PREFIX + qualifiedName);
+ }
+// SH}
+
private static String getRawName(ITypeBinding normalizedBinding) {
return normalizedBinding.getTypeDeclaration().getName();
}
diff --git a/plugins/org.eclipse.objectteams.otdt.refactoring/src/org/eclipse/objectteams/otdt/internal/refactoring/adaptor/RenameAdaptor.java b/plugins/org.eclipse.objectteams.otdt.refactoring/src/org/eclipse/objectteams/otdt/internal/refactoring/adaptor/RenameAdaptor.java
index 0b15e3a92..0a19d0b9c 100644
--- a/plugins/org.eclipse.objectteams.otdt.refactoring/src/org/eclipse/objectteams/otdt/internal/refactoring/adaptor/RenameAdaptor.java
+++ b/plugins/org.eclipse.objectteams.otdt.refactoring/src/org/eclipse/objectteams/otdt/internal/refactoring/adaptor/RenameAdaptor.java
@@ -1,7 +1,7 @@
/**********************************************************************
* This file is part of "Object Teams Development Tooling"-Software
*
- * Copyright 2004, 2005, 2006 Fraunhofer Gesellschaft, Munich, Germany,
+ * Copyright 2004, 2013 Fraunhofer Gesellschaft, Munich, Germany,
* for its Fraunhofer Institute and Computer Architecture and Software
* Technology (FIRST), Berlin, Germany and Technical University Berlin,
* Germany.
@@ -10,7 +10,6 @@
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
- * $Id: RenameAdaptor.java 23473 2010-02-05 19:46:08Z stephan $
*
* Please visit http://www.objectteams.org for updates and contact.
*
@@ -20,13 +19,17 @@
**********************************************************************/
package org.eclipse.objectteams.otdt.internal.refactoring.adaptor;
+import java.util.ArrayList;
+import java.util.HashMap;
import java.util.Iterator;
+import java.util.List;
import java.util.Set;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IImportDeclaration;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IPackageFragment;
@@ -41,6 +44,10 @@ import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclaration;
+import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
+import org.eclipse.jdt.internal.compiler.lookup.ExtraCompilerModifiers;
+import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility;
+import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
import org.eclipse.jdt.internal.corext.refactoring.changes.TextChangeCompatibility;
import org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager;
import org.eclipse.jdt.internal.ui.JavaPlugin;
@@ -53,7 +60,9 @@ import org.eclipse.objectteams.otdt.internal.refactoring.adaptor.rename.RenameMe
import org.eclipse.objectteams.otdt.internal.refactoring.corext.OTRefactoringCoreMessages;
import org.eclipse.objectteams.otdt.internal.refactoring.corext.rename.BaseCallFinder;
import org.eclipse.objectteams.otdt.internal.refactoring.util.RefactoringUtil;
+import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.ReplaceEdit;
+import org.eclipse.text.edits.TextEdit;
import base org.eclipse.jdt.internal.corext.refactoring.rename.MethodChecks;
import base org.eclipse.jdt.internal.corext.refactoring.rename.RenameAnalyzeUtil;
@@ -61,6 +70,9 @@ import base org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProc
import base org.eclipse.jdt.internal.corext.refactoring.rename.RenameVirtualMethodProcessor;
import base org.eclipse.jdt.internal.corext.refactoring.rename.TempOccurrenceAnalyzer;
import base org.eclipse.jdt.internal.corext.refactoring.rename.RenameAnalyzeUtil.ProblemNodeFinder;
+import base org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProcessor.ImportsManager;
+import base org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProcessor.PackageRenamer;
+import base org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProcessor.ImportsManager.ImportChange;
/**
* @author stephan
@@ -308,9 +320,116 @@ public team class RenameAdaptor
return status;
}
return status;
- }
+ }
+ }
+
+ /** Add support for base imports: check if an import being updated is a base import and record those separately. */
+ protected class PackageRenamer playedBy PackageRenamer {
+
+ @SuppressWarnings("decapsulation")
+ ImportsManager getFImportsManager() -> get ImportsManager fImportsManager;
+
+ @SuppressWarnings("decapsulation")
+ updateImport <- replace updateImport;
+
+ @SuppressWarnings("basecall")
+ callin void updateImport(ICompilationUnit cu, IImportDeclaration importDeclaration, String updatedImport) throws JavaModelException {
+ if ((importDeclaration.getFlags() & ExtraCompilerModifiers.AccBase) != 0) {
+ final ImportsManager importsManager = getFImportsManager();
+ ImportChange<@importsManager> importChange= importsManager.getImportChange(cu);
+ importChange.removeBaseImport(importDeclaration.getElementName());
+ importChange.addBaseImport(updatedImport);
+
+ } else {
+ base.updateImport(cu, importDeclaration, updatedImport);
+ }
+ }
+ }
+
+ /** Add support for base imports: when performing the changes invoke choose the appropriate add- method in ImportRewrite. */
+ protected team class ImportsManager playedBy ImportsManager {
+
+ /** Extend existing record class by two more lists. */
+ @SuppressWarnings("decapsulation")
+ public class ImportChange playedBy ImportChange {
+ protected List<String> getStaticToRemove() -> get ArrayList<String> fStaticToRemove;
+ protected List<String> getToRemove() -> get ArrayList<String> fToRemove;
+ protected List<String[]> getStaticToAdd() -> get ArrayList<String[]> fStaticToAdd;
+ protected List<String> getToAdd() -> get ArrayList<String> fToAdd;
+
+ protected ArrayList<String> fBaseToRemove= new ArrayList<String>();
+ protected ArrayList<String> fBaseToAdd= new ArrayList<String>();
+
+ public void removeBaseImport(String elementName) {
+ fBaseToRemove.add(elementName);
+ }
+ public void addBaseImport(String updatedImport) {
+ fBaseToAdd.add(updatedImport);
+ }
+ }
+
+ @SuppressWarnings("decapsulation")
+ Iterator<ICompilationUnit> getCompilationUnits() -> get HashMap<ICompilationUnit, ImportChange> fImportChanges
+ with { result <- fImportChanges.keySet().iterator() }
+
+ protected ImportChange getImportChange(ICompilationUnit cu) -> ImportChange getImportChange(ICompilationUnit cu);
+
+ rewriteImports <- replace rewriteImports;
+
+ /* Copy from its base version. */
+ @SuppressWarnings("basecall")
+ callin void rewriteImports(TextChangeManager changeManager, IProgressMonitor pm) throws CoreException {
+//{ObjectTeams: separate iteration (over ICU) from fetching ImportChanges (needs lifting):
+ /* orig:
+ for (Iterator<Entry<ICompilationUnit, ImportChange>> iter= fImportChanges.entrySet().iterator(); iter.hasNext();) {
+ Entry<ICompilationUnit, ImportChange> entry= iter.next();
+ ICompilationUnit cu= entry.getKey();
+ ImportChange importChange= entry.getValue();
+ :giro */
+ for (Iterator<ICompilationUnit> iter= getCompilationUnits(); iter.hasNext();) {
+ ICompilationUnit cu= iter.next();
+ ImportChange importChange= getImportChange(cu);
+// SH}
+ ImportRewrite importRewrite= StubUtility.createImportRewrite(cu, true);
+ importRewrite.setFilterImplicitImports(false);
+ for (Iterator<String> iterator= importChange.getStaticToRemove().iterator(); iterator.hasNext();) {
+ importRewrite.removeStaticImport(iterator.next());
+ }
+ for (Iterator<String> iterator= importChange.getToRemove().iterator(); iterator.hasNext();) {
+ importRewrite.removeImport(iterator.next());
+ }
+//{ObectTeams: one more kind of imports to remove
+ for (Iterator<String> iterator= importChange.fBaseToRemove.iterator(); iterator.hasNext();) {
+ importRewrite.removeImportBase(iterator.next());
+ }
+// SH}
+ for (Iterator<String[]> iterator= importChange.getStaticToAdd().iterator(); iterator.hasNext();) {
+ String[] toAdd= iterator.next();
+ importRewrite.addStaticImport(toAdd[0], toAdd[1], true);
+ }
+ for (Iterator<String> iterator= importChange.getToAdd().iterator(); iterator.hasNext();) {
+ importRewrite.addImport(iterator.next());
+ }
+//{ObectTeams: one more kind of imports to add
+ for (Iterator<String> iterator= importChange.fBaseToAdd.iterator(); iterator.hasNext();) {
+ importRewrite.addImportBase(iterator.next());
+ }
+// SH}
+ if (importRewrite.hasRecordedChanges()) {
+ TextEdit importEdit= importRewrite.rewriteImports(pm);
+ String name= RefactoringCoreMessages.RenamePackageRefactoring_update_imports;
+ try {
+ TextChangeCompatibility.addTextEdit(changeManager.get(cu), name, importEdit);
+ } catch (MalformedTreeException e) {
+ JavaPlugin.logErrorMessage("MalformedTreeException while processing cu " + cu); //$NON-NLS-1$
+ throw e;
+ }
+ }
+ }
+ }
}
+
/**
* Find more occurrences, currently:
* - references to an argument with declared lifting via the fakedRoleVariable.
diff --git a/testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/src/org/eclipse/objectteams/otdt/ui/tests/refactoring/RefactoringTest.java b/testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/src/org/eclipse/objectteams/otdt/ui/tests/refactoring/RefactoringTest.java
index 803f37c4e..12e562eac 100644
--- a/testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/src/org/eclipse/objectteams/otdt/ui/tests/refactoring/RefactoringTest.java
+++ b/testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/src/org/eclipse/objectteams/otdt/ui/tests/refactoring/RefactoringTest.java
@@ -1,7 +1,7 @@
/**********************************************************************
* This file is part of "Object Teams Development Tooling"-Software
*
- * Copyright 2004, 2010 Fraunhofer Gesellschaft, Munich, Germany,
+ * Copyright 2004, 2013 Fraunhofer Gesellschaft, Munich, Germany,
* for its Fraunhofer Institute and Computer Architecture and Software
* Technology (FIRST), Berlin, Germany and Technical University Berlin,
* Germany.
@@ -10,7 +10,6 @@
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
- * $Id$
*
* Please visit http://www.eclipse.org/objectteams for updates and contact.
*
@@ -61,6 +60,7 @@ import org.eclipse.ltk.core.refactoring.IUndoManager;
import org.eclipse.ltk.core.refactoring.PerformChangeOperation;
import org.eclipse.ltk.core.refactoring.Refactoring;
import org.eclipse.ltk.core.refactoring.RefactoringCore;
+import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.objectteams.otdt.core.IOTJavaElement;
import org.eclipse.objectteams.otdt.internal.core.AbstractCalloutMapping;
@@ -197,6 +197,23 @@ public abstract class RefactoringTest extends TestCase
{
return _packageP;
}
+
+ protected final Refactoring createRefactoring(RefactoringDescriptor descriptor) throws CoreException {
+ RefactoringStatus status= new RefactoringStatus();
+ Refactoring refactoring= descriptor.createRefactoring(status);
+ assertNotNull("refactoring should not be null", refactoring);
+ assertTrue("status should be ok", status.isOK());
+ return refactoring;
+ }
+
+ protected final RefactoringStatus performRefactoring(RefactoringDescriptor descriptor) throws Exception {
+ return performRefactoring(descriptor, true);
+ }
+
+ protected final RefactoringStatus performRefactoring(RefactoringDescriptor descriptor, boolean providesUndo) throws Exception {
+ Refactoring refactoring= createRefactoring(descriptor);
+ return performRefactoring(refactoring, providesUndo);
+ }
protected final RefactoringStatus performRefactoring(Refactoring ref)
throws Exception
diff --git a/testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/src/org/eclipse/objectteams/otdt/ui/tests/refactoring/rename/AllTests.java b/testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/src/org/eclipse/objectteams/otdt/ui/tests/refactoring/rename/AllTests.java
index 74bc24974..72d07ab03 100644
--- a/testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/src/org/eclipse/objectteams/otdt/ui/tests/refactoring/rename/AllTests.java
+++ b/testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/src/org/eclipse/objectteams/otdt/ui/tests/refactoring/rename/AllTests.java
@@ -1,7 +1,7 @@
/**********************************************************************
* This file is part of "Object Teams Development Tooling"-Software
*
- * Copyright 2004, 2010 Fraunhofer Gesellschaft, Munich, Germany,
+ * Copyright 2004, 2013 Fraunhofer Gesellschaft, Munich, Germany,
* for its Fraunhofer Institute and Computer Architecture and Software
* Technology (FIRST), Berlin, Germany and Technical University Berlin,
* Germany.
@@ -10,7 +10,6 @@
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
- * $Id$
*
* Please visit http://www.eclipse.org/objectteams for updates and contact.
*
@@ -27,7 +26,7 @@ import junit.framework.TestSuite;
/**
* @author brcan
*
- * Runs all OT-specific refactoring tests and corresponding eclipse refactoring tests
+ * Runs all OT-specific refactoring tests
*/
public class AllTests
{
@@ -47,7 +46,8 @@ public class AllTests
suite.addTest(org.eclipse.objectteams.otdt.ui.tests.refactoring.rename.RenameStaticMethodTests.suite());
suite.addTest(org.eclipse.objectteams.otdt.ui.tests.refactoring.rename.RenameVirtualMethodInClassTests.suite());
suite.addTest(org.eclipse.objectteams.otdt.ui.tests.refactoring.rename.RenamePrivateFieldTests.suite());
- suite.addTest(org.eclipse.objectteams.otdt.ui.tests.refactoring.rename.RenameTypeTests.suite());
+ suite.addTest(org.eclipse.objectteams.otdt.ui.tests.refactoring.rename.RenameTypeTests.suite());
+ suite.addTest(org.eclipse.objectteams.otdt.ui.tests.refactoring.rename.RenamePackageTests.suite());
return suite;
}
diff --git a/testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/src/org/eclipse/objectteams/otdt/ui/tests/refactoring/rename/RenamePackageTests.java b/testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/src/org/eclipse/objectteams/otdt/ui/tests/refactoring/rename/RenamePackageTests.java
new file mode 100644
index 000000000..5d891e211
--- /dev/null
+++ b/testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/src/org/eclipse/objectteams/otdt/ui/tests/refactoring/rename/RenamePackageTests.java
@@ -0,0 +1,392 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2013 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ * Stephan Herrmann - Initial API and implementation
+ *******************************************************************************/
+package org.eclipse.objectteams.otdt.ui.tests.refactoring.rename;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.IPackageFragment;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.core.refactoring.IJavaRefactorings;
+import org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor;
+import org.eclipse.jdt.internal.core.refactoring.descriptors.RefactoringSignatureDescriptorFactory;
+import org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProcessor;
+import org.eclipse.jdt.ui.tests.refactoring.ParticipantTesting;
+import org.eclipse.jdt.ui.tests.refactoring.infra.DebugUtils;
+import org.eclipse.ltk.core.refactoring.Refactoring;
+import org.eclipse.ltk.core.refactoring.RefactoringStatus;
+import org.eclipse.ltk.core.refactoring.participants.MoveArguments;
+import org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring;
+import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
+import org.eclipse.ltk.core.refactoring.participants.RenameArguments;
+import org.eclipse.objectteams.otdt.ui.tests.refactoring.MySetup;
+import org.eclipse.objectteams.otdt.ui.tests.refactoring.RefactoringTest;
+
+/**
+ * Based on {@link org.eclipse.jdt.ui.tests.refactoring.RenamePackageTests} and adapted.
+ */
+@SuppressWarnings({"restriction", "rawtypes", "unchecked"})
+public class RenamePackageTests extends RefactoringTest {
+
+ private static final Class clazz= RenamePackageTests.class;
+ private static final String REFACTORING_PATH= "RenamePackage/";
+
+ private boolean fUpdateReferences;
+ private boolean fUpdateTextualMatches;
+ private String fQualifiedNamesFilePatterns;
+ private boolean fRenameSubpackages;
+
+ public RenamePackageTests(String name) {
+ super(name);
+ }
+
+ public static Test suite() {
+ return new MySetup(new TestSuite(clazz));
+ }
+
+ public static Test setUpTest(Test someTest) {
+ return new MySetup(someTest);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ fUpdateReferences= true;
+ fUpdateTextualMatches= false;
+ fQualifiedNamesFilePatterns= null;
+ fRenameSubpackages= false;
+ // fIsPreDeltaTest= true;
+ }
+
+ protected String getRefactoringPath() {
+ return REFACTORING_PATH;
+ }
+
+ // -------------
+ private RenameJavaElementDescriptor createRefactoringDescriptor(IPackageFragment pack, String newName) {
+ RenameJavaElementDescriptor descriptor= RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_PACKAGE);
+ descriptor.setJavaElement(pack);
+ descriptor.setNewName(newName);
+ descriptor.setUpdateReferences(true);
+ return descriptor;
+ }
+
+ /* non java-doc
+ * the 0th one is the one to rename
+ */
+ @SuppressWarnings("unused")
+ private void helper1(String packageNames[], String[][] packageFiles, String newPackageName) throws Exception{
+ IPackageFragment[] packages= new IPackageFragment[packageNames.length];
+ for (int i= 0; i < packageFiles.length; i++){
+ packages[i]= getRoot().createPackageFragment(packageNames[i], true, null);
+ for (int j= 0; j < packageFiles[i].length; j++){
+ createCUfromTestFile(packages[i], packageFiles[i][j], packageNames[i].replace('.', '/') + "/");
+ //DebugUtils.dump(cu.getElementName() + "\n" + cu.getSource());
+ }
+ }
+ IPackageFragment thisPackage= packages[0];
+ RefactoringStatus result= performRefactoring(createRefactoringDescriptor(thisPackage, newPackageName));
+ assertNotNull("precondition was supposed to fail", result);
+ if (_isVerbose)
+ DebugUtils.dump("" + result);
+ }
+
+ private RenamePackageProcessor helper2(String[] packageNames, String[][] packageFileNames, String newPackageName) throws Exception{
+ ParticipantTesting.reset();
+ IPackageFragment[] packages= new IPackageFragment[packageNames.length];
+ ICompilationUnit[][] cus= new ICompilationUnit[packageFileNames.length][packageFileNames[0].length];
+ for (int i= 0; i < packageNames.length; i++){
+ packages[i]= getRoot().createPackageFragment(packageNames[i], true, null);
+ for (int j= 0; j < packageFileNames[i].length; j++){
+ cus[i][j]= createCUfromTestFile(packages[i], packageFileNames[i][j], packageNames[i].replace('.', '/') + "/");
+ }
+ }
+ IPackageFragment thisPackage= packages[0];
+ boolean hasSubpackages= thisPackage.hasSubpackages();
+
+ IPath path= thisPackage.getParent().getPath();
+ path= path.append(newPackageName.replace('.', '/'));
+ IFolder target= ResourcesPlugin.getWorkspace().getRoot().getFolder(path);
+ boolean targetExists= target.exists();
+ boolean isRename= !targetExists && !thisPackage.hasSubpackages() && thisPackage.getResource().getParent().equals(target.getParent());
+
+ String[] createHandles= null;
+ String[] moveHandles= null;
+ String[] deleteHandles= null;
+ boolean doDelete= true;
+
+ String[] renameHandles= null;
+ if (isRename) {
+ renameHandles= ParticipantTesting.createHandles(thisPackage, thisPackage.getResource());
+ } else {
+ renameHandles= ParticipantTesting.createHandles(thisPackage);
+ IContainer loop= target;
+ List handles= new ArrayList();
+ while (loop != null && !loop.exists()) {
+ handles.add(ParticipantTesting.createHandles(loop)[0]);
+ loop= loop.getParent();
+ }
+ createHandles= (String[]) handles.toArray(new String[handles.size()]);
+ IFolder source= (IFolder)thisPackage.getResource();
+ deleteHandles= ParticipantTesting.createHandles(source);
+ IResource members[]= source.members();
+ List movedObjects= new ArrayList();
+ for (int i= 0; i < members.length; i++) {
+ if (members[i] instanceof IFolder) {
+ doDelete= false;
+ } else {
+ movedObjects.add(members[i]);
+ }
+ }
+ moveHandles= ParticipantTesting.createHandles(movedObjects.toArray());
+ }
+ RenameJavaElementDescriptor descriptor= createRefactoringDescriptor(thisPackage, newPackageName);
+ descriptor.setUpdateReferences(fUpdateReferences);
+ descriptor.setUpdateTextualOccurrences(fUpdateTextualMatches);
+ setFilePatterns(descriptor);
+ Refactoring refactoring= createRefactoring(descriptor);
+ RefactoringStatus result= performRefactoring(refactoring);
+ assertEquals("preconditions were supposed to pass", null, result);
+
+ if (isRename) {
+ ParticipantTesting.testRename(renameHandles,
+ new RenameArguments[] {
+ new RenameArguments(newPackageName, fUpdateReferences),
+ new RenameArguments(target.getName(), fUpdateReferences)
+ }
+ );
+ } else {
+ ParticipantTesting.testRename(renameHandles,
+ new RenameArguments[] {
+ new RenameArguments(newPackageName, fUpdateReferences)});
+
+ ParticipantTesting.testCreate(createHandles);
+
+ List args= new ArrayList();
+ for (int i= 0; i < packageFileNames[0].length; i++) {
+ args.add(new MoveArguments(target, fUpdateReferences));
+ }
+ ParticipantTesting.testMove(moveHandles, (MoveArguments[]) args.toArray(new MoveArguments[args.size()]));
+
+ if (doDelete) {
+ ParticipantTesting.testDelete(deleteHandles);
+ } else {
+ ParticipantTesting.testDelete(new String[0]);
+ }
+ }
+
+ //---
+
+ if (hasSubpackages) {
+ assertTrue("old package does not exist anymore", getRoot().getPackageFragment(packageNames[0]).exists());
+ } else {
+ assertTrue("package not renamed", ! getRoot().getPackageFragment(packageNames[0]).exists());
+ }
+ IPackageFragment newPackage= getRoot().getPackageFragment(newPackageName);
+ assertTrue("new package does not exist", newPackage.exists());
+
+ for (int i= 0; i < packageFileNames.length; i++){
+ String packageName= (i == 0)
+ ? newPackageName.replace('.', '/') + "/"
+ : packageNames[i].replace('.', '/') + "/";
+ for (int j= 0; j < packageFileNames[i].length; j++){
+ String s1= getFileContents(getOutputTestFileName(packageFileNames[i][j], packageName));
+ ICompilationUnit cu=
+ (i == 0)
+ ? newPackage.getCompilationUnit(packageFileNames[i][j] + ".java")
+ : cus[i][j];
+ //DebugUtils.dump("cu:" + cu.getElementName());
+ String s2= cu.getSource();
+
+ //DebugUtils.dump("expected:" + s1);
+ //DebugUtils.dump("was:" + s2);
+ assertEqualLines("invalid update in file " + cu.getElementName(), s1, s2);
+ }
+ }
+ RefactoringProcessor processor= ((ProcessorBasedRefactoring) refactoring).getProcessor();
+ return (RenamePackageProcessor) processor;
+ }
+
+ class PackageRename {
+ String[] fPackageNames;
+ final String[][] fPackageFileNames;
+ final String fNewPackageName;
+ final boolean fTestWithDummyFiles;
+
+ final IPackageFragment[] fPackages;
+ final ICompilationUnit[][] fCus;
+
+ public PackageRename (String[] packageNames, String[][] packageFileNames, String newPackageName) throws Exception {
+ this(packageNames, packageFileNames, newPackageName, false);
+ }
+
+ public PackageRename(String[] packageNames, String[][] packageFileNames, String newPackageName, boolean testWithDummyFiles) throws Exception {
+ fPackageNames= packageNames;
+ fPackageFileNames= packageFileNames;
+ fNewPackageName= newPackageName;
+ fTestWithDummyFiles= testWithDummyFiles;
+
+ fPackages= new IPackageFragment[packageNames.length];
+ fCus= new ICompilationUnit[packageFileNames.length][];
+ for (int i= 0; i < packageFileNames.length; i++){
+ fPackages[i]= getRoot().createPackageFragment(packageNames[i], true, null);
+ fCus[i]= new ICompilationUnit[packageFileNames[i].length];
+ for (int j= 0; j < packageFileNames[i].length; j++){
+ if (testWithDummyFiles) {
+ fCus[i][j]= createDummyCU(fPackages[i], packageFileNames[i][j]);
+ } else {
+ fCus[i][j]= createCUfromTestFile(fPackages[i], packageFileNames[i][j], packageNames[i].replace('.', '/') + "/");
+ }
+ }
+ }
+ }
+
+ private ICompilationUnit createDummyCU(IPackageFragment packageFragment, String typeName) throws JavaModelException {
+ String contents= getDummyContents(packageFragment.getElementName(), typeName);
+ return packageFragment.createCompilationUnit(typeName + ".java", contents, true, null);
+ }
+
+ private String getDummyContents(String packName, String typeName) {
+ StringBuffer contents= new StringBuffer();
+ if (packName.length() != 0)
+ contents.append("package ").append(packName).append(";\n");
+ contents.append("public class ").append(typeName).append(" { }\n");
+ return contents.toString();
+ }
+
+ public void createAndPerform(int expectedSeverity) throws CoreException, Exception {
+ IPackageFragment thisPackage= fPackages[0];
+ RenameJavaElementDescriptor descriptor= createRefactoringDescriptor(thisPackage, fNewPackageName);
+ descriptor.setUpdateReferences(fUpdateReferences);
+ descriptor.setUpdateTextualOccurrences(fUpdateTextualMatches);
+ setFilePatterns(descriptor);
+ descriptor.setUpdateHierarchy(fRenameSubpackages);
+ RefactoringStatus result= performRefactoring(descriptor);
+ if (expectedSeverity == RefactoringStatus.OK)
+ assertEquals("preconditions were supposed to pass", null, result);
+ else
+ assertEquals(expectedSeverity, result.getSeverity());
+ }
+
+ public void execute() throws Exception {
+ createAndPerform(RefactoringStatus.OK);
+
+ IPackageFragment oldPackage= getRoot().getPackageFragment(fPackageNames[0]);
+ assertTrue("package not renamed: " + fPackageNames[0], ! oldPackage.exists());
+ IPackageFragment newPackage= getRoot().getPackageFragment(fNewPackageName);
+ assertTrue("new package does not exist", newPackage.exists());
+
+ checkExpectedState();
+ }
+
+ public void checkExpectedState() throws IOException, JavaModelException {
+ for (int i= 0; i < fPackageFileNames.length; i++){
+ String packageName= getNewPackageName(fPackageNames[i]);
+ String packagePath= packageName.replace('.', '/') + "/";
+
+ for (int j= 0; j < fPackageFileNames[i].length; j++){
+ String expected;
+ if (fTestWithDummyFiles) {
+ expected= getDummyContents(packageName, fPackageFileNames[i][j]);
+ } else {
+ expected= getFileContents(getOutputTestFileName(fPackageFileNames[i][j], packagePath));
+ }
+ ICompilationUnit cu= getRoot().getPackageFragment(packageName).getCompilationUnit(fPackageFileNames[i][j] + ".java");
+ String actual= cu.getSource();
+ assertEqualLines("invalid update in file " + cu.getElementName(), expected, actual);
+ }
+ }
+ }
+
+ public String getNewPackageName(String oldPackageName) {
+ if (oldPackageName.equals(fPackageNames[0]))
+ return fNewPackageName;
+
+ if (fRenameSubpackages && oldPackageName.startsWith(fPackageNames[0] + "."))
+ return fNewPackageName + oldPackageName.substring(fPackageNames[0].length());
+
+ return oldPackageName;
+ }
+
+ public void checkOriginalState() throws Exception {
+ IJavaElement[] rootChildren= getRoot().getChildren();
+ ArrayList existingPacks= new ArrayList();
+ for (int i= 0; i < rootChildren.length; i++) {
+ existingPacks.add(rootChildren[i].getElementName());
+ }
+ assertEqualSets(Arrays.asList(fPackageNames), existingPacks);
+
+ for (int i= 0; i < fPackageFileNames.length; i++){
+ String packageName= fPackageNames[i];
+ String packagePath= packageName.replace('.', '/') + "/";
+ IPackageFragment pack= getRoot().getPackageFragment(packageName);
+
+ IJavaElement[] packChildren= pack.getChildren();
+ ArrayList existingCUs= new ArrayList();
+ for (int j= 0; j < packChildren.length; j++) {
+ String cuName= packChildren[j].getElementName();
+ existingCUs.add(cuName.substring(0, cuName.length() - 5));
+ }
+ assertEqualSets(Arrays.asList(fPackageFileNames[i]), existingCUs);
+
+ for (int j= 0; j < fPackageFileNames[i].length; j++){
+ String expected;
+ if (fTestWithDummyFiles) {
+ expected= getDummyContents(packageName, fPackageFileNames[i][j]);
+ } else {
+ expected= getFileContents(getInputTestFileName(fPackageFileNames[i][j], packagePath));
+ }
+ ICompilationUnit cu= pack.getCompilationUnit(fPackageFileNames[i][j] + ".java");
+ String actual= cu.getSource();
+ assertEqualLines("invalid undo in file " + cu.getElementName(), expected, actual);
+ }
+ }
+
+ }
+
+ private void assertEqualSets(Collection expected, Collection actual) {
+ HashSet expectedSet= new HashSet(expected);
+ expectedSet.removeAll(actual);
+ assertEquals("not all expected in actual", "[]", expectedSet.toString());
+
+ HashSet actualSet= new HashSet(actual);
+ actualSet.removeAll(expected);
+ assertEquals("not all actual in expected", "[]", actualSet.toString());
+ }
+ }
+
+ private void setFilePatterns(RenameJavaElementDescriptor descriptor) {
+ descriptor.setUpdateQualifiedNames(fQualifiedNamesFilePatterns != null);
+ if (fQualifiedNamesFilePatterns != null)
+ descriptor.setFileNamePatterns(fQualifiedNamesFilePatterns);
+ }
+
+ // ---------- tests -------------
+
+ public void testRenameBaseImportedPackage1() throws Exception {
+ helper2(new String[]{"bases", "teams"}, new String[][]{{"MyClass"}, {"MyTeam"}}, "baseclasses");
+ }
+}
diff --git a/testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/testdata/RenamePackage/testRenameBaseImportedPackage1/in/bases/MyClass.java b/testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/testdata/RenamePackage/testRenameBaseImportedPackage1/in/bases/MyClass.java
new file mode 100644
index 000000000..0be4b2dc1
--- /dev/null
+++ b/testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/testdata/RenamePackage/testRenameBaseImportedPackage1/in/bases/MyClass.java
@@ -0,0 +1,3 @@
+package bases;
+
+public class MyClass {}
diff --git a/testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/testdata/RenamePackage/testRenameBaseImportedPackage1/in/teams/MyTeam.java b/testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/testdata/RenamePackage/testRenameBaseImportedPackage1/in/teams/MyTeam.java
new file mode 100644
index 000000000..3a4d4b8ac
--- /dev/null
+++ b/testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/testdata/RenamePackage/testRenameBaseImportedPackage1/in/teams/MyTeam.java
@@ -0,0 +1,7 @@
+package teams;
+
+import base bases.MyClass;
+
+public team class MyTeam {
+ protected class R playedBy MyClass {}
+} \ No newline at end of file
diff --git a/testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/testdata/RenamePackage/testRenameBaseImportedPackage1/out/baseclasses/MyClass.java b/testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/testdata/RenamePackage/testRenameBaseImportedPackage1/out/baseclasses/MyClass.java
new file mode 100644
index 000000000..729668f06
--- /dev/null
+++ b/testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/testdata/RenamePackage/testRenameBaseImportedPackage1/out/baseclasses/MyClass.java
@@ -0,0 +1,3 @@
+package baseclasses;
+
+public class MyClass {}
diff --git a/testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/testdata/RenamePackage/testRenameBaseImportedPackage1/out/teams/MyTeam.java b/testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/testdata/RenamePackage/testRenameBaseImportedPackage1/out/teams/MyTeam.java
new file mode 100644
index 000000000..5761d5271
--- /dev/null
+++ b/testplugins/org.eclipse.objectteams.otdt.ui.tests.refactoring/testdata/RenamePackage/testRenameBaseImportedPackage1/out/teams/MyTeam.java
@@ -0,0 +1,7 @@
+package teams;
+
+import base baseclasses.MyClass;
+
+public team class MyTeam {
+ protected class R playedBy MyClass {}
+} \ No newline at end of file

Back to the top