Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JBIDE-19696 Rename of an exception class does not work properly #395

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -16,6 +16,7 @@

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.ltk.core.refactoring.participants.MoveParticipant;
import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
import org.eclipse.ltk.core.refactoring.participants.RenameParticipant;
Expand All @@ -28,15 +29,15 @@ public AbstractRefactorTest(String name){
super(name);
}

public static void checkRename(RenameProcessor processor, List<TestChangeStructure> changeList) throws CoreException{
public static void checkRename(RenameProcessor processor, List<TestChangeStructure> changeList) throws CoreException, BadLocationException{
RenameParticipantTestUtil.checkRenameProcessor(processor, changeList);
}

public static void checkMove(RefactoringProcessor processor, IResource oldObject, IResource destinationObject, MoveParticipant participant, List<TestChangeStructure> changeList) throws CoreException {
public static void checkMove(RefactoringProcessor processor, IResource oldObject, IResource destinationObject, MoveParticipant participant, List<TestChangeStructure> changeList) throws CoreException , BadLocationException{
RenameParticipantTestUtil.checkMoveParticipant(processor, oldObject, destinationObject, participant, changeList);
}

public static void checkRename(RefactoringProcessor processor, IResource oldObject, String newName, RenameParticipant participant, List<TestChangeStructure> changeList) throws CoreException {
public static void checkRename(RefactoringProcessor processor, IResource oldObject, String newName, RenameParticipant participant, List<TestChangeStructure> changeList) throws CoreException, BadLocationException {
RenameParticipantTestUtil.checkRenameParticipant(processor, oldObject, newName, participant, changeList);
}
}
Expand Up @@ -26,6 +26,11 @@
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.FindReplaceDocumentAdapter;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Region;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.CompositeChange;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
Expand All @@ -39,6 +44,11 @@
import org.eclipse.ltk.core.refactoring.participants.RenameParticipant;
import org.eclipse.ltk.core.refactoring.participants.RenameProcessor;
import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.text.edits.ReplaceEdit;
import org.eclipse.text.edits.TextEdit;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.texteditor.DocumentProviderRegistry;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.jboss.tools.common.EclipseUtil;
import org.jboss.tools.common.util.FileUtil;
import org.jboss.tools.test.util.JobUtils;
Expand All @@ -47,7 +57,7 @@
public class RenameParticipantTestUtil {
public static void checkRenameParticipant(IJavaElement element, RenameProcessor renameProcessor,
RefactoringParticipant participant, String newName, List<TestChangeStructure> changeList)
throws CoreException {
throws CoreException, BadLocationException {
processTestChanges(changeList);

JobUtils.waitForIdle();
Expand All @@ -65,7 +75,7 @@ public static void checkRenameParticipant(IJavaElement element, RenameProcessor
}

public static void checkRenameProcessor(RenameProcessor processor, List<TestChangeStructure> changeList)
throws CoreException {
throws CoreException, BadLocationException {
processTestChanges(changeList);

JobUtils.waitForIdle();
Expand Down Expand Up @@ -101,7 +111,7 @@ public static void checkRenameProcessor(RenameProcessor processor, List<TestChan

public static void checkMoveParticipant(RefactoringProcessor processor, IResource oldObject,
IResource destinationObject, MoveParticipant participant, List<TestChangeStructure> changeList)
throws CoreException {
throws CoreException, BadLocationException {
processTestChanges(changeList);

JobUtils.waitForIdle();
Expand Down Expand Up @@ -135,7 +145,7 @@ public static void checkMoveParticipant(RefactoringProcessor processor, IResourc

public static void checkRenameParticipant(RefactoringProcessor processor, IResource oldObject,
String newName, RenameParticipant participant, List<TestChangeStructure> changeList)
throws CoreException {
throws CoreException, BadLocationException {
processTestChanges(changeList);

JobUtils.waitForIdle(2000);
Expand Down Expand Up @@ -167,17 +177,46 @@ public static void checkRenameParticipant(RefactoringProcessor processor, IResou
checkChanges(rootChange, changeList);
}

private static void processTestChanges(List<TestChangeStructure> structureList) throws CoreException {
private static void processTestChanges(List<TestChangeStructure> structureList) throws CoreException, BadLocationException {
// int offset = 0;
// for (TestChangeStructure tStructure : structureList) {
// IFile file = tStructure.getProject().getFile(tStructure.getFileName());
// String fileContent = FileUtil.readStream(file);
// for (TestTextChange tChange : tStructure.getTextChanges()) {
// if (tChange.getOffset() == -1) {
// offset = fileContent.indexOf(tChange.getSearchText(), offset+1);
// tChange.setOffset(offset);
// }
// }
// }
int offset = 0;
for (TestChangeStructure tStructure : structureList) {
IFile file = tStructure.getProject().getFile(tStructure.getFileName());
String fileContent = FileUtil.readStream(file);
FileEditorInput editorInput = new FileEditorInput(file);

IDocumentProvider documentProvider = null;
documentProvider = DocumentProviderRegistry.getDefault().getDocumentProvider(editorInput);

Assert.assertNotNull("The document provider for the file \"" + file.getFullPath() + "\" is not loaded", documentProvider); //$NON-NLS-1$ //$NON-NLS-2$

documentProvider.connect(editorInput);
IDocument document = documentProvider.getDocument(editorInput);
FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(document);
IRegion region = new Region(0,0);
for (TestTextChange tChange : tStructure.getTextChanges()) {
if (tChange.getOffset() == -1) {
int offset = fileContent.indexOf(tChange.getSearchText());
tChange.setOffset(offset);
IRegion newRegion = adapter.find(region.getOffset()+region.getLength(), tChange.getSearchText(), true, true, false, false);
if(newRegion != null){
tChange.setOffset(newRegion.getOffset());
region = newRegion;
}else
Assert.fail("Can not find string - "+tChange.getSearchText()); //$NON-NLS-1$

}
}
documentProvider.disconnect(editorInput);
}

}

private static void checkBeforeRefactoring(List<TestChangeStructure> changeList) {
Expand Down Expand Up @@ -212,45 +251,44 @@ private static void checkChanges(CompositeChange rootChange, List<TestChangeStru
if (fileChange instanceof TextFileChange) {
edit = (MultiTextEdit) ((TextFileChange) fileChange).getEdit();
file = ((TextFileChange) fileChange).getFile();
//((JBDSFileChange)fileChange).setSaveMode(TextFileChange.FORCE_SAVE);
} else if (fileChange instanceof TextFileChange) {
edit = (MultiTextEdit) ((TextFileChange) fileChange).getEdit();
file = ((TextFileChange) fileChange).getFile();
} else{
Assert.fail("fileChange must be instance of TextFileChange but was - "+fileChange.getClass());
}

//System.out.println("File - "+fileChange.getFile().getFullPath()+" offset - "+edit.getOffset());

TestChangeStructure change = findChange(changeList, file);
if (change != null) {

Assert.assertNotNull("Change not found for file - "+file.getFullPath(), change);
Assert.assertEquals(change.size(), edit.getChildrenSize());
}
}

rootChange.perform(new NullProgressMonitor());
JobUtils.waitForIdle(2000);

// Test results
for (TestChangeStructure changeStructure : changeList) {
IFile file = changeStructure.getProject().getFile(changeStructure.getFileName());
String content = null;
content = readStream(file);
for (TestTextChange change : changeStructure.getTextChanges()) {
Assert.assertEquals("There is unexpected change in resource - " + file.getName(),
change.getText(),
content.substring(change.getOffset(), change.getOffset() + change.getLength()));
}
for(TextEdit te : edit.getChildren()){
checkEdit(change, (ReplaceEdit)te);
}
}
Assert.assertEquals("There is unexpected number of changes", changeList.size(), numberOfChanges);
}

public static TestChangeStructure findChange(List<TestChangeStructure> changeList, IFile file) {
for (TestChangeStructure tcs : changeList) {
if (tcs.getFileName().equals("/" + file.getFullPath().removeFirstSegments(1).toString()))
if (tcs.getFileName().endsWith(file.getFullPath().removeFirstSegments(1).toString()))
return tcs;
}
return null;
}

private static void checkEdit(TestChangeStructure change, ReplaceEdit edit) throws CoreException{
IFile file = change.getProject().getFile(change.getFileName());
String fileContent = FileUtil.readStream(file);
String newText = edit.getText();

String editText = fileContent.substring(edit.getOffset(),edit.getOffset()+edit.getLength());
for (TestTextChange ttc : change.getTextChanges()) {
if(ttc.getOffset() == edit.getOffset()){
Assert.assertEquals("Wrong length of TextEdit file - "+file.getFullPath()+" editText - "+editText+" newText - "+newText, ttc.getLength(), newText.length());
Assert.assertEquals("Wrong text of TextEdit file - "+file.getFullPath()+" editText - "+editText, ttc.getText(), newText);
return;
}
}
Assert.fail("Unexpected edit file - "+file.getFullPath()+" offset - "+edit.getOffset()+" length - "+edit.getLength()+" text - "+editText+" newText - "+newText);
}

public static IType getJavaType(IProject project, String className) {
IJavaProject javaProject = EclipseUtil.getJavaProject(project);
if (javaProject != null) {
Expand Down