use of org.eclipse.ltk.core.refactoring.Refactoring in project che by eclipse.
the class RenamePackageTest method testPackageRenameWithResource1.
@Test
public void testPackageRenameWithResource1() throws Exception {
IPackageFragment fragment = getRoot().createPackageFragment("org.test", true, null);
StringBuffer buf = new StringBuffer();
buf.append("package org.test;\n");
buf.append("public class MyClass {\n");
buf.append(" org.test.MyClass me;\n");
buf.append("}\n");
fragment.createCompilationUnit("MyClass.java", buf.toString(), true, null);
IFile file = ((IFolder) getRoot().getResource()).getFile("x.properties");
byte[] content = "This is about 'org.test' and more".getBytes();
file.create(new ByteArrayInputStream(content), true, null);
file.refreshLocal(IResource.DEPTH_ONE, null);
RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_PACKAGE);
descriptor.setJavaElement(fragment);
descriptor.setNewName("org.test2");
descriptor.setUpdateReferences(true);
descriptor.setUpdateQualifiedNames(true);
descriptor.setFileNamePatterns("*.properties");
Refactoring refactoring = createRefactoring(descriptor);
RefactoringStatus status = performRefactoring(refactoring);
if (status != null)
assertTrue(status.toString(), status.isOK());
RefactoringProcessor processor = ((RenameRefactoring) refactoring).getProcessor();
IResourceMapper rm = (IResourceMapper) processor.getAdapter(IResourceMapper.class);
IJavaElementMapper jm = (IJavaElementMapper) processor.getAdapter(IJavaElementMapper.class);
checkMappingUnchanged(jm, rm, new Object[] { getRoot().getJavaProject(), getRoot(), file });
IFile newFile = ((IContainer) getRoot().getResource()).getFile(new Path("x.properties"));
assertEquals("This is about 'org.test2' and more", getContents(newFile));
checkMappingChanged(jm, rm, new Object[][] { { fragment, getRoot().getPackageFragment("org.test2") } });
}
use of org.eclipse.ltk.core.refactoring.Refactoring in project che by eclipse.
the class RenamePackageTest method helper2.
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);
TestCase.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;
}
use of org.eclipse.ltk.core.refactoring.Refactoring in project che by eclipse.
the class RefactoringTest method createRefactoring.
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;
}
use of org.eclipse.ltk.core.refactoring.Refactoring in project che by eclipse.
the class RenameTypeTest method helperWithTextual.
private String[] helperWithTextual(String oldCuName, String oldName, String newName, String newCUName, boolean updateReferences, boolean updateTextualMatches) throws Exception {
ICompilationUnit cu = createCUfromTestFile(getPackageP(), oldCuName);
IType classA = getType(cu, oldName);
IJavaElement[] classAMembers = classA.getChildren();
IPackageFragment pack = (IPackageFragment) cu.getParent();
String[] renameHandles = null;
if (classA.getDeclaringType() == null && cu.getElementName().startsWith(classA.getElementName())) {
renameHandles = ParticipantTesting.createHandles(classA, cu, cu.getResource());
} else {
renameHandles = ParticipantTesting.createHandles(classA);
}
RenameJavaElementDescriptor descriptor = createRefactoringDescriptor(classA, newName);
descriptor.setUpdateReferences(updateReferences);
descriptor.setUpdateTextualOccurrences(updateTextualMatches);
Refactoring refactoring = createRefactoring(descriptor);
assertEquals("was supposed to pass", null, performRefactoring(refactoring));
ICompilationUnit newcu = pack.getCompilationUnit(newCUName + ".java");
TestCase.assertTrue("cu " + newcu.getElementName() + " does not exist", newcu.exists());
assertEqualLines("invalid renaming", getFileContents(getOutputTestFileName(newCUName)), newcu.getSource());
INameUpdating nameUpdating = ((INameUpdating) refactoring.getAdapter(INameUpdating.class));
IType newElement = (IType) nameUpdating.getNewElement();
TestCase.assertTrue("new element does not exist:\n" + newElement.toString(), newElement.exists());
checkMappers(refactoring, classA, newCUName + ".java", classAMembers);
return renameHandles;
}
use of org.eclipse.ltk.core.refactoring.Refactoring in project che by eclipse.
the class RenameVirtualMethodInClassTest method helper2_0.
private void helper2_0(String methodName, String newMethodName, String[] signatures, boolean shouldPass, boolean updateReferences, boolean createDelegate) throws Exception {
final ICompilationUnit cu = createCUfromTestFile(getPackageP(), "A");
final IType classA = getType(cu, "A");
final IMethod method = classA.getMethod(methodName, signatures);
final RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_METHOD);
descriptor.setJavaElement(method);
descriptor.setNewName(newMethodName);
descriptor.setUpdateReferences(updateReferences);
descriptor.setKeepOriginal(createDelegate);
descriptor.setDeprecateDelegate(createDelegate);
final RefactoringStatus status = new RefactoringStatus();
final Refactoring refactoring = descriptor.createRefactoring(status);
assertNotNull("Refactoring should not be null", refactoring);
assertTrue("status should be ok", status.isOK());
assertEquals("was supposed to pass", null, performRefactoring(refactoring));
if (!shouldPass) {
assertTrue("incorrect renaming because of java model", !getFileContents(getOutputTestFileName("A")).equals(cu.getSource()));
return;
}
String expectedRenaming = getFileContents(getOutputTestFileName("A"));
String actuaRenaming = cu.getSource();
assertEqualLines("incorrect renaming", expectedRenaming, actuaRenaming);
assertTrue("anythingToUndo", RefactoringCore.getUndoManager().anythingToUndo());
assertTrue("! anythingToRedo", !RefactoringCore.getUndoManager().anythingToRedo());
//assertEquals("1 to undo", 1, Refactoring.getUndoManager().getRefactoringLog().size());
RefactoringCore.getUndoManager().performUndo(null, new NullProgressMonitor());
assertEqualLines("invalid undo", getFileContents(getInputTestFileName("A")), cu.getSource());
assertTrue("! anythingToUndo", !RefactoringCore.getUndoManager().anythingToUndo());
assertTrue("anythingToRedo", RefactoringCore.getUndoManager().anythingToRedo());
//assertEquals("1 to redo", 1, Refactoring.getUndoManager().getRedoStack().size());
RefactoringCore.getUndoManager().performRedo(null, new NullProgressMonitor());
assertEqualLines("invalid redo", getFileContents(getOutputTestFileName("A")), cu.getSource());
}
Aggregations