Author: dazarov
Date: 2012-01-05 12:18:24 -0500 (Thu, 05 Jan 2012)
New Revision: 37657
Added:
trunk/common/tests/org.jboss.tools.common.base.test/src/org/jboss/tools/common/base/test/AbstractRefactorTest.java
Removed:
trunk/tests/plugins/org.jboss.tools.tests/src/org/jboss/tools/tests/AbstractRefactorTest.java
Modified:
trunk/cdi/tests/org.jboss.tools.cdi.core.test/src/org/jboss/tools/cdi/core/test/tck/NamedBeanRefactoringTest.java
trunk/cdi/tests/org.jboss.tools.cdi.ui.test/src/org/jboss/tools/cdi/ui/test/marker/CDIMarkerResolutionTest.java
trunk/common/tests/org.jboss.tools.common.base.test/META-INF/MANIFEST.MF
trunk/jsf/tests/org.jboss.tools.jsf.test/src/org/jboss/tools/jsf/test/refactoring/ELVariableRefactoringTest.java
trunk/jsf/tests/org.jboss.tools.jsf.test/src/org/jboss/tools/jsf/test/refactoring/JSF2RefactoringTest.java
trunk/jsf/tests/org.jboss.tools.jsf.test/src/org/jboss/tools/jsf/test/refactoring/MessagePropertyRefactoringTest.java
trunk/seam/tests/org.jboss.tools.seam.core.test/META-INF/MANIFEST.MF
trunk/seam/tests/org.jboss.tools.seam.core.test/src/org/jboss/tools/seam/core/test/refactoring/SeamRefactoringTest.java
trunk/tests/plugins/org.jboss.tools.tests/META-INF/MANIFEST.MF
Log:
AbstractRefactorTest moved to org.jboss.tools.common.base.test in order to avoid
dependency from org.jboss.tools.test to org.jboss.tools.common
Modified:
trunk/cdi/tests/org.jboss.tools.cdi.core.test/src/org/jboss/tools/cdi/core/test/tck/NamedBeanRefactoringTest.java
===================================================================
---
trunk/cdi/tests/org.jboss.tools.cdi.core.test/src/org/jboss/tools/cdi/core/test/tck/NamedBeanRefactoringTest.java 2012-01-05
14:41:24 UTC (rev 37656)
+++
trunk/cdi/tests/org.jboss.tools.cdi.core.test/src/org/jboss/tools/cdi/core/test/tck/NamedBeanRefactoringTest.java 2012-01-05
17:18:24 UTC (rev 37657)
@@ -10,9 +10,9 @@
import org.jboss.tools.cdi.core.IBean;
import org.jboss.tools.cdi.core.ICDIProject;
import org.jboss.tools.cdi.internal.core.refactoring.RenameNamedBeanProcessor;
-import org.jboss.tools.tests.AbstractRefactorTest;
-import org.jboss.tools.tests.AbstractRefactorTest.TestChangeStructure;
-import org.jboss.tools.tests.AbstractRefactorTest.TestTextChange;
+import org.jboss.tools.common.base.test.AbstractRefactorTest;
+import org.jboss.tools.common.base.test.AbstractRefactorTest.TestChangeStructure;
+import org.jboss.tools.common.base.test.AbstractRefactorTest.TestTextChange;
public class NamedBeanRefactoringTest extends TCKTest {
private static final String FILE_NAME1 =
"JavaSource/org/jboss/jsr299/tck/tests/jbt/refactoring/Gamme.java";
Modified:
trunk/cdi/tests/org.jboss.tools.cdi.ui.test/src/org/jboss/tools/cdi/ui/test/marker/CDIMarkerResolutionTest.java
===================================================================
---
trunk/cdi/tests/org.jboss.tools.cdi.ui.test/src/org/jboss/tools/cdi/ui/test/marker/CDIMarkerResolutionTest.java 2012-01-05
14:41:24 UTC (rev 37656)
+++
trunk/cdi/tests/org.jboss.tools.cdi.ui.test/src/org/jboss/tools/cdi/ui/test/marker/CDIMarkerResolutionTest.java 2012-01-05
17:18:24 UTC (rev 37657)
@@ -45,12 +45,12 @@
import org.jboss.tools.cdi.ui.marker.MakeMethodPublicMarkerResolution;
import org.jboss.tools.cdi.ui.marker.TestableResolutionWithDialog;
import org.jboss.tools.cdi.ui.marker.TestableResolutionWithRefactoringProcessor;
+import org.jboss.tools.common.base.test.AbstractRefactorTest.TestChangeStructure;
import org.jboss.tools.common.base.test.validation.TestUtil;
import org.jboss.tools.common.refactoring.JBDSFileChange;
import org.jboss.tools.common.ui.marker.AddSuppressWarningsMarkerResolution;
import org.jboss.tools.common.ui.marker.ConfigureProblemSeverityMarkerResolution;
import org.jboss.tools.common.util.FileUtil;
-import org.jboss.tools.tests.AbstractRefactorTest.TestChangeStructure;
/**
* @author Daniel Azarov
Modified: trunk/common/tests/org.jboss.tools.common.base.test/META-INF/MANIFEST.MF
===================================================================
--- trunk/common/tests/org.jboss.tools.common.base.test/META-INF/MANIFEST.MF 2012-01-05
14:41:24 UTC (rev 37656)
+++ trunk/common/tests/org.jboss.tools.common.base.test/META-INF/MANIFEST.MF 2012-01-05
17:18:24 UTC (rev 37657)
@@ -29,7 +29,8 @@
org.eclipse.ui.ide;bundle-version="3.7.0",
org.jboss.tools.common.validation,
org.eclipse.wst.validation;bundle-version="1.2.300",
- org.eclipse.wst.common.frameworks;bundle-version="1.2.100"
+ org.eclipse.wst.common.frameworks;bundle-version="1.2.100",
+ org.eclipse.ltk.core.refactoring;bundle-version="3.5.201"
Bundle-Version: 3.3.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ClassPath: .
Copied:
trunk/common/tests/org.jboss.tools.common.base.test/src/org/jboss/tools/common/base/test/AbstractRefactorTest.java
(from rev 37656,
trunk/tests/plugins/org.jboss.tools.tests/src/org/jboss/tools/tests/AbstractRefactorTest.java)
===================================================================
---
trunk/common/tests/org.jboss.tools.common.base.test/src/org/jboss/tools/common/base/test/AbstractRefactorTest.java
(rev 0)
+++
trunk/common/tests/org.jboss.tools.common.base.test/src/org/jboss/tools/common/base/test/AbstractRefactorTest.java 2012-01-05
17:18:24 UTC (rev 37657)
@@ -0,0 +1,286 @@
+package org.jboss.tools.common.base.test;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.ltk.core.refactoring.Change;
+import org.eclipse.ltk.core.refactoring.CompositeChange;
+import org.eclipse.ltk.core.refactoring.RefactoringStatus;
+import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry;
+import org.eclipse.ltk.core.refactoring.TextFileChange;
+import org.eclipse.ltk.core.refactoring.participants.MoveArguments;
+import org.eclipse.ltk.core.refactoring.participants.MoveParticipant;
+import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
+import org.eclipse.ltk.core.refactoring.participants.RenameArguments;
+import org.eclipse.ltk.core.refactoring.participants.RenameParticipant;
+import org.eclipse.ltk.core.refactoring.participants.RenameProcessor;
+import org.eclipse.text.edits.MultiTextEdit;
+import org.jboss.tools.common.refactoring.JBDSFileChange;
+import org.jboss.tools.test.util.JobUtils;
+
+public class AbstractRefactorTest extends TestCase{
+
+ public AbstractRefactorTest(String name){
+ super(name);
+ }
+
+ public static void checkRename(RenameProcessor processor,
List<TestChangeStructure> changeList) throws CoreException{
+ JobUtils.waitForIdle(2000);
+
+ // Test before renaming
+ checkBeforeRefactoring(changeList);
+
+ // Rename
+ RefactoringStatus status = processor.checkInitialConditions(new
NullProgressMonitor());
+
+ RefactoringStatusEntry[] entries = status.getEntries();
+ for(RefactoringStatusEntry entry : entries){
+ System.out.println("Refactor status - "+entry.getMessage());
+ }
+
+ assertNull("Rename processor returns fatal error",
status.getEntryMatchingSeverity(RefactoringStatus.FATAL));
+
+ status = processor.checkFinalConditions(new NullProgressMonitor(), null);
+
+ entries = status.getEntries();
+ for(RefactoringStatusEntry entry : entries){
+ System.out.println("Refactor status - "+entry.getMessage());
+ }
+
+ assertNull("Rename processor returns fatal error",
status.getEntryMatchingSeverity(RefactoringStatus.FATAL));
+
+
+ CompositeChange rootChange = (CompositeChange)processor.createChange(new
NullProgressMonitor());
+
+ checkChanges(rootChange, changeList);
+ }
+
+ public static void checkMove(RefactoringProcessor processor, IResource oldObject,
IResource destinationObject, MoveParticipant participant, List<TestChangeStructure>
changeList) throws CoreException {
+ JobUtils.waitForIdle(2000);
+
+ // Test before moving
+ checkBeforeRefactoring(changeList);
+
+ // Move
+ MoveArguments arguments = new MoveArguments(destinationObject, true);
+ boolean initialized = participant.initialize(processor, oldObject, arguments);
+
+ assertTrue("Participant has not been initialized", initialized);
+
+ RefactoringStatus status = participant.checkConditions(new NullProgressMonitor(),
null);
+
+ RefactoringStatusEntry[] entries = status.getEntries();
+ for(RefactoringStatusEntry entry : entries){
+ System.out.println("Refactor status - "+entry.getMessage());
+ }
+
+ assertNull("Move processor returns fatal error",
status.getEntryMatchingSeverity(RefactoringStatus.FATAL));
+
+ CompositeChange rootChange = (CompositeChange)participant.createChange(new
NullProgressMonitor());
+
+ Change mainChange = processor.createChange(new NullProgressMonitor());
+ mainChange.perform(new NullProgressMonitor());
+
+ checkChanges(rootChange, changeList);
+ }
+
+ public static void checkRename(RefactoringProcessor processor, IResource oldObject,
String newName, RenameParticipant participant, List<TestChangeStructure> changeList)
throws CoreException {
+ JobUtils.waitForIdle(2000);
+
+ // Test before renaming
+ checkBeforeRefactoring(changeList);
+
+ // Rename
+ RenameArguments arguments = new RenameArguments(newName, true);
+ boolean initialized = participant.initialize(processor, oldObject, arguments);
+
+ assertTrue("Participant has not been initialized", initialized);
+
+ RefactoringStatus status = participant.checkConditions(new NullProgressMonitor(),
null);
+
+ RefactoringStatusEntry[] entries = status.getEntries();
+ for(RefactoringStatusEntry entry : entries){
+ System.out.println("Refactor status - "+entry.getMessage());
+ }
+
+ assertNull("Rename processor returns fatal error",
status.getEntryMatchingSeverity(RefactoringStatus.FATAL));
+
+ CompositeChange rootChange = (CompositeChange)participant.createChange(new
NullProgressMonitor());
+
+ Change mainChange = processor.createChange(new NullProgressMonitor());
+ mainChange.perform(new NullProgressMonitor());
+
+ checkChanges(rootChange, changeList);
+ }
+
+ public static void checkBeforeRefactoring(List<TestChangeStructure> changeList){
+ for(TestChangeStructure changeStructure : changeList){
+ IFile file = changeStructure.getProject().getFile(changeStructure.getFileName());
+ String content = null;
+ try {
+ content = readStream(file);
+ } catch (CoreException e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+
+ for(TestTextChange change : changeStructure.getTextChanges()){
+ assertNotSame(change.getText(), content.substring(change.getOffset(),
change.getOffset()+change.getLength()));
+ }
+ }
+ }
+
+ public static void checkChanges(CompositeChange rootChange,
List<TestChangeStructure> changeList) throws CoreException {
+ assertNotNull("Root change is null",rootChange);
+
+ int numberOfChanges = rootChange.getChildren().length;
+
+ for(int i = 0; i < rootChange.getChildren().length;i++){
+ Change fileChange = rootChange.getChildren()[i];
+
+ MultiTextEdit edit = null;
+ IFile file = null;
+ if(fileChange instanceof JBDSFileChange){
+ edit = (MultiTextEdit)((JBDSFileChange)fileChange).getEdit();
+ file = ((JBDSFileChange)fileChange).getFile();
+ //((JBDSFileChange)fileChange).setSaveMode(TextFileChange.FORCE_SAVE);
+ }else if(fileChange instanceof TextFileChange){
+ edit = (MultiTextEdit)((TextFileChange)fileChange).getEdit();
+ file = ((TextFileChange)fileChange).getFile();
+ }
+
+ //System.out.println("File - "+fileChange.getFile().getFullPath()+"
offset - "+edit.getOffset());
+
+ TestChangeStructure change = findChange(changeList, file);
+ if(change != null){
+ 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()){
+ assertEquals("There is unexpected change in resource -
"+file.getName(),change.getText(), content.substring(change.getOffset(),
change.getOffset()+change.getLength()));
+ }
+ }
+ 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()))
+ return tcs;
+ }
+ return null;
+ }
+
+
+ public static class TestChangeStructure{
+ private IProject project;
+ private String fileName;
+ ArrayList<TestTextChange> textChanges = new ArrayList<TestTextChange>();
+
+
+ public TestChangeStructure(IProject project, String fileName){
+ this.project = project;
+ this.fileName = fileName;
+ }
+
+ public IProject getProject(){
+ return project;
+ }
+
+ public String getFileName(){
+ return fileName;
+ }
+
+ public ArrayList<TestTextChange> getTextChanges(){
+ return textChanges;
+ }
+
+ public void addTextChange(TestTextChange change){
+ textChanges.add(change);
+ }
+
+ public int size(){
+ return textChanges.size();
+ }
+
+ }
+
+ public static class TestTextChange{
+ private int offset;
+ private int length;
+ private String text;
+
+ public TestTextChange(int offset, int length, String text){
+ this.offset = offset;
+ this.length = length;
+ this.text = text;
+ }
+
+ public int getOffset(){
+ return offset;
+ }
+
+ public int getLength(){
+ return length;
+ }
+
+ public String getText(){
+ return text;
+ }
+ }
+
+ public static String readStream(InputStream is) {
+ StringBuffer sb = new StringBuffer(""); //$NON-NLS-1$
+ try {
+ byte[] b = new byte[4096];
+ while(true) {
+ int l = is.read(b, 0, b.length);
+ if(l < 0) break;
+ sb.append(new String(b, 0, l));
+ }
+ is.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ return sb.toString();
+ }
+
+ public static String readStream(IFile file) throws CoreException {
+ String content = null;
+ InputStream in = null;
+ try {
+ in = file.getContents();
+ content = readStream(in);
+ } finally {
+ if(in!=null) {
+ try {
+ in.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ }
+ }
+ return content;
+ }
+
+}
Property changes on:
trunk/common/tests/org.jboss.tools.common.base.test/src/org/jboss/tools/common/base/test/AbstractRefactorTest.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Modified:
trunk/jsf/tests/org.jboss.tools.jsf.test/src/org/jboss/tools/jsf/test/refactoring/ELVariableRefactoringTest.java
===================================================================
---
trunk/jsf/tests/org.jboss.tools.jsf.test/src/org/jboss/tools/jsf/test/refactoring/ELVariableRefactoringTest.java 2012-01-05
14:41:24 UTC (rev 37656)
+++
trunk/jsf/tests/org.jboss.tools.jsf.test/src/org/jboss/tools/jsf/test/refactoring/ELVariableRefactoringTest.java 2012-01-05
17:18:24 UTC (rev 37657)
@@ -7,9 +7,9 @@
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
+import org.jboss.tools.common.base.test.AbstractRefactorTest;
import org.jboss.tools.jsf.el.refactoring.RenameELVariableProcessor;
import org.jboss.tools.test.util.ProjectImportTestSetup;
-import org.jboss.tools.tests.AbstractRefactorTest;
public class ELVariableRefactoringTest extends AbstractRefactorTest {
static String projectName = "JSFKickStartOldFormat";
Modified:
trunk/jsf/tests/org.jboss.tools.jsf.test/src/org/jboss/tools/jsf/test/refactoring/JSF2RefactoringTest.java
===================================================================
---
trunk/jsf/tests/org.jboss.tools.jsf.test/src/org/jboss/tools/jsf/test/refactoring/JSF2RefactoringTest.java 2012-01-05
14:41:24 UTC (rev 37656)
+++
trunk/jsf/tests/org.jboss.tools.jsf.test/src/org/jboss/tools/jsf/test/refactoring/JSF2RefactoringTest.java 2012-01-05
17:18:24 UTC (rev 37657)
@@ -10,9 +10,9 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.ltk.internal.core.refactoring.resource.MoveResourcesProcessor;
import org.eclipse.ltk.internal.core.refactoring.resource.RenameResourceProcessor;
+import org.jboss.tools.common.base.test.AbstractRefactorTest;
import org.jboss.tools.jsf.jsf2.refactoring.JSF2RenameParticipant;
import org.jboss.tools.jsf.jsf2.refactoring.JSf2MoveParticipant;
-import org.jboss.tools.tests.AbstractRefactorTest;
public class JSF2RefactoringTest extends AbstractRefactorTest {
static String PROJECT_NAME = "JSF2ComponentsValidator";
Modified:
trunk/jsf/tests/org.jboss.tools.jsf.test/src/org/jboss/tools/jsf/test/refactoring/MessagePropertyRefactoringTest.java
===================================================================
---
trunk/jsf/tests/org.jboss.tools.jsf.test/src/org/jboss/tools/jsf/test/refactoring/MessagePropertyRefactoringTest.java 2012-01-05
14:41:24 UTC (rev 37656)
+++
trunk/jsf/tests/org.jboss.tools.jsf.test/src/org/jboss/tools/jsf/test/refactoring/MessagePropertyRefactoringTest.java 2012-01-05
17:18:24 UTC (rev 37657)
@@ -7,12 +7,12 @@
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
+import org.jboss.tools.common.base.test.AbstractRefactorTest;
import org.jboss.tools.common.el.core.parser.LexicalToken;
import org.jboss.tools.common.util.FileUtil;
import org.jboss.tools.jsf.el.refactoring.RenameMessagePropertyProcessor;
import org.jboss.tools.jst.web.kb.el.MessagePropertyELSegmentImpl;
import org.jboss.tools.test.util.ProjectImportTestSetup;
-import org.jboss.tools.tests.AbstractRefactorTest;
public class MessagePropertyRefactoringTest extends AbstractRefactorTest{
static String projectName = "JSFKickStartOldFormat";
Modified: trunk/seam/tests/org.jboss.tools.seam.core.test/META-INF/MANIFEST.MF
===================================================================
--- trunk/seam/tests/org.jboss.tools.seam.core.test/META-INF/MANIFEST.MF 2012-01-05
14:41:24 UTC (rev 37656)
+++ trunk/seam/tests/org.jboss.tools.seam.core.test/META-INF/MANIFEST.MF 2012-01-05
17:18:24 UTC (rev 37657)
@@ -38,6 +38,7 @@
org.eclipse.jst.standard.schemas,
org.eclipse.wst.standard.schemas,
org.jboss.tools.seam.base.test,
- org.jboss.tools.common.validation
+ org.jboss.tools.common.validation,
+ org.jboss.tools.common.base.test;bundle-version="3.3.0"
Bundle-Localization: plugin
Bundle-RequiredExecutionEnvironment: J2SE-1.5
Modified:
trunk/seam/tests/org.jboss.tools.seam.core.test/src/org/jboss/tools/seam/core/test/refactoring/SeamRefactoringTest.java
===================================================================
---
trunk/seam/tests/org.jboss.tools.seam.core.test/src/org/jboss/tools/seam/core/test/refactoring/SeamRefactoringTest.java 2012-01-05
14:41:24 UTC (rev 37656)
+++
trunk/seam/tests/org.jboss.tools.seam.core.test/src/org/jboss/tools/seam/core/test/refactoring/SeamRefactoringTest.java 2012-01-05
17:18:24 UTC (rev 37657)
@@ -8,11 +8,11 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jst.j2ee.internal.common.classpath.J2EEComponentClasspathUpdater;
+import org.jboss.tools.common.base.test.AbstractRefactorTest;
import org.jboss.tools.seam.core.ISeamProject;
import org.jboss.tools.seam.core.SeamCorePlugin;
import org.jboss.tools.test.util.JobUtils;
import org.jboss.tools.test.util.ProjectImportTestSetup;
-import org.jboss.tools.tests.AbstractRefactorTest;
public class SeamRefactoringTest extends AbstractRefactorTest {
static String warProjectName = "Test1";
Modified: trunk/tests/plugins/org.jboss.tools.tests/META-INF/MANIFEST.MF
===================================================================
--- trunk/tests/plugins/org.jboss.tools.tests/META-INF/MANIFEST.MF 2012-01-05 14:41:24 UTC
(rev 37656)
+++ trunk/tests/plugins/org.jboss.tools.tests/META-INF/MANIFEST.MF 2012-01-05 17:18:24 UTC
(rev 37657)
@@ -12,8 +12,7 @@
org.apache.xerces;bundle-version="[2.9.0,3.0.0)",
org.eclipse.ltk.core.refactoring;bundle-version="3.5.0",
org.eclipse.text;bundle-version="3.5.0",
- org.eclipse.ui.ide.application;bundle-version="1.0.200",
- org.jboss.tools.common;bundle-version="3.3.0"
+ org.eclipse.ui.ide.application;bundle-version="1.0.200"
Bundle-ActivationPolicy: lazy
Export-Package: org.jboss.tools.test.resource,
org.jboss.tools.test.util,
Deleted:
trunk/tests/plugins/org.jboss.tools.tests/src/org/jboss/tools/tests/AbstractRefactorTest.java
===================================================================
---
trunk/tests/plugins/org.jboss.tools.tests/src/org/jboss/tools/tests/AbstractRefactorTest.java 2012-01-05
14:41:24 UTC (rev 37656)
+++
trunk/tests/plugins/org.jboss.tools.tests/src/org/jboss/tools/tests/AbstractRefactorTest.java 2012-01-05
17:18:24 UTC (rev 37657)
@@ -1,286 +0,0 @@
-package org.jboss.tools.tests;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.List;
-
-import junit.framework.TestCase;
-
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IProject;
-import org.eclipse.core.resources.IResource;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.NullProgressMonitor;
-import org.eclipse.ltk.core.refactoring.Change;
-import org.eclipse.ltk.core.refactoring.CompositeChange;
-import org.eclipse.ltk.core.refactoring.RefactoringStatus;
-import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry;
-import org.eclipse.ltk.core.refactoring.TextFileChange;
-import org.eclipse.ltk.core.refactoring.participants.MoveArguments;
-import org.eclipse.ltk.core.refactoring.participants.MoveParticipant;
-import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
-import org.eclipse.ltk.core.refactoring.participants.RenameArguments;
-import org.eclipse.ltk.core.refactoring.participants.RenameParticipant;
-import org.eclipse.ltk.core.refactoring.participants.RenameProcessor;
-import org.eclipse.text.edits.MultiTextEdit;
-import org.jboss.tools.common.refactoring.JBDSFileChange;
-import org.jboss.tools.test.util.JobUtils;
-
-public class AbstractRefactorTest extends TestCase{
-
- public AbstractRefactorTest(String name){
- super(name);
- }
-
- public static void checkRename(RenameProcessor processor,
List<TestChangeStructure> changeList) throws CoreException{
- JobUtils.waitForIdle(2000);
-
- // Test before renaming
- checkBeforeRefactoring(changeList);
-
- // Rename
- RefactoringStatus status = processor.checkInitialConditions(new
NullProgressMonitor());
-
- RefactoringStatusEntry[] entries = status.getEntries();
- for(RefactoringStatusEntry entry : entries){
- System.out.println("Refactor status - "+entry.getMessage());
- }
-
- assertNull("Rename processor returns fatal error",
status.getEntryMatchingSeverity(RefactoringStatus.FATAL));
-
- status = processor.checkFinalConditions(new NullProgressMonitor(), null);
-
- entries = status.getEntries();
- for(RefactoringStatusEntry entry : entries){
- System.out.println("Refactor status - "+entry.getMessage());
- }
-
- assertNull("Rename processor returns fatal error",
status.getEntryMatchingSeverity(RefactoringStatus.FATAL));
-
-
- CompositeChange rootChange = (CompositeChange)processor.createChange(new
NullProgressMonitor());
-
- checkChanges(rootChange, changeList);
- }
-
- public static void checkMove(RefactoringProcessor processor, IResource oldObject,
IResource destinationObject, MoveParticipant participant, List<TestChangeStructure>
changeList) throws CoreException {
- JobUtils.waitForIdle(2000);
-
- // Test before moving
- checkBeforeRefactoring(changeList);
-
- // Move
- MoveArguments arguments = new MoveArguments(destinationObject, true);
- boolean initialized = participant.initialize(processor, oldObject, arguments);
-
- assertTrue("Participant has not been initialized", initialized);
-
- RefactoringStatus status = participant.checkConditions(new NullProgressMonitor(),
null);
-
- RefactoringStatusEntry[] entries = status.getEntries();
- for(RefactoringStatusEntry entry : entries){
- System.out.println("Refactor status - "+entry.getMessage());
- }
-
- assertNull("Move processor returns fatal error",
status.getEntryMatchingSeverity(RefactoringStatus.FATAL));
-
- CompositeChange rootChange = (CompositeChange)participant.createChange(new
NullProgressMonitor());
-
- Change mainChange = processor.createChange(new NullProgressMonitor());
- mainChange.perform(new NullProgressMonitor());
-
- checkChanges(rootChange, changeList);
- }
-
- public static void checkRename(RefactoringProcessor processor, IResource oldObject,
String newName, RenameParticipant participant, List<TestChangeStructure> changeList)
throws CoreException {
- JobUtils.waitForIdle(2000);
-
- // Test before renaming
- checkBeforeRefactoring(changeList);
-
- // Rename
- RenameArguments arguments = new RenameArguments(newName, true);
- boolean initialized = participant.initialize(processor, oldObject, arguments);
-
- assertTrue("Participant has not been initialized", initialized);
-
- RefactoringStatus status = participant.checkConditions(new NullProgressMonitor(),
null);
-
- RefactoringStatusEntry[] entries = status.getEntries();
- for(RefactoringStatusEntry entry : entries){
- System.out.println("Refactor status - "+entry.getMessage());
- }
-
- assertNull("Rename processor returns fatal error",
status.getEntryMatchingSeverity(RefactoringStatus.FATAL));
-
- CompositeChange rootChange = (CompositeChange)participant.createChange(new
NullProgressMonitor());
-
- Change mainChange = processor.createChange(new NullProgressMonitor());
- mainChange.perform(new NullProgressMonitor());
-
- checkChanges(rootChange, changeList);
- }
-
- public static void checkBeforeRefactoring(List<TestChangeStructure> changeList){
- for(TestChangeStructure changeStructure : changeList){
- IFile file = changeStructure.getProject().getFile(changeStructure.getFileName());
- String content = null;
- try {
- content = readStream(file);
- } catch (CoreException e) {
- e.printStackTrace();
- fail(e.getMessage());
- }
-
- for(TestTextChange change : changeStructure.getTextChanges()){
- assertNotSame(change.getText(), content.substring(change.getOffset(),
change.getOffset()+change.getLength()));
- }
- }
- }
-
- public static void checkChanges(CompositeChange rootChange,
List<TestChangeStructure> changeList) throws CoreException {
- assertNotNull("Root change is null",rootChange);
-
- int numberOfChanges = rootChange.getChildren().length;
-
- for(int i = 0; i < rootChange.getChildren().length;i++){
- Change fileChange = rootChange.getChildren()[i];
-
- MultiTextEdit edit = null;
- IFile file = null;
- if(fileChange instanceof JBDSFileChange){
- edit = (MultiTextEdit)((JBDSFileChange)fileChange).getEdit();
- file = ((JBDSFileChange)fileChange).getFile();
- //((JBDSFileChange)fileChange).setSaveMode(TextFileChange.FORCE_SAVE);
- }else if(fileChange instanceof TextFileChange){
- edit = (MultiTextEdit)((TextFileChange)fileChange).getEdit();
- file = ((TextFileChange)fileChange).getFile();
- }
-
- //System.out.println("File - "+fileChange.getFile().getFullPath()+"
offset - "+edit.getOffset());
-
- TestChangeStructure change = findChange(changeList, file);
- if(change != null){
- 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()){
- assertEquals("There is unexpected change in resource -
"+file.getName(),change.getText(), content.substring(change.getOffset(),
change.getOffset()+change.getLength()));
- }
- }
- 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()))
- return tcs;
- }
- return null;
- }
-
-
- public static class TestChangeStructure{
- private IProject project;
- private String fileName;
- ArrayList<TestTextChange> textChanges = new ArrayList<TestTextChange>();
-
-
- public TestChangeStructure(IProject project, String fileName){
- this.project = project;
- this.fileName = fileName;
- }
-
- public IProject getProject(){
- return project;
- }
-
- public String getFileName(){
- return fileName;
- }
-
- public ArrayList<TestTextChange> getTextChanges(){
- return textChanges;
- }
-
- public void addTextChange(TestTextChange change){
- textChanges.add(change);
- }
-
- public int size(){
- return textChanges.size();
- }
-
- }
-
- public static class TestTextChange{
- private int offset;
- private int length;
- private String text;
-
- public TestTextChange(int offset, int length, String text){
- this.offset = offset;
- this.length = length;
- this.text = text;
- }
-
- public int getOffset(){
- return offset;
- }
-
- public int getLength(){
- return length;
- }
-
- public String getText(){
- return text;
- }
- }
-
- public static String readStream(InputStream is) {
- StringBuffer sb = new StringBuffer(""); //$NON-NLS-1$
- try {
- byte[] b = new byte[4096];
- while(true) {
- int l = is.read(b, 0, b.length);
- if(l < 0) break;
- sb.append(new String(b, 0, l));
- }
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- fail(e.getMessage());
- }
- return sb.toString();
- }
-
- public static String readStream(IFile file) throws CoreException {
- String content = null;
- InputStream in = null;
- try {
- in = file.getContents();
- content = readStream(in);
- } finally {
- if(in!=null) {
- try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- fail(e.getMessage());
- }
- }
- }
- return content;
- }
-
-}