Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeffrey Overbey2011-05-01 08:01:20 +0000
committerJeffrey Overbey2011-05-01 08:01:20 +0000
commitc532dfc2c3f8f245f86c9dc9ba2976a3d085a1f9 (patch)
treee2764a2e66c079eca60132bfc441791aab2d8789
parent867addf949411c49ef901f32d05c978e829d737a (diff)
downloadorg.eclipse.photran-c532dfc2c3f8f245f86c9dc9ba2976a3d085a1f9.tar.gz
org.eclipse.photran-c532dfc2c3f8f245f86c9dc9ba2976a3d085a1f9.tar.xz
org.eclipse.photran-c532dfc2c3f8f245f86c9dc9ba2976a3d085a1f9.zip
Added RefactoringTestCase, RefactoringTestSuite, WorkspaceTestCase
-rw-r--r--org.eclipse.rephraserengine.testing.junit3/META-INF/MANIFEST.MF1
-rw-r--r--org.eclipse.rephraserengine.testing.junit3/src/org/eclipse/rephraserengine/testing/junit3/RefactoringTestCase.java303
-rw-r--r--org.eclipse.rephraserengine.testing.junit3/src/org/eclipse/rephraserengine/testing/junit3/RefactoringTestSuite.java216
-rw-r--r--org.eclipse.rephraserengine.testing.junit3/src/org/eclipse/rephraserengine/testing/junit3/WorkspaceTestCase.java212
4 files changed, 732 insertions, 0 deletions
diff --git a/org.eclipse.rephraserengine.testing.junit3/META-INF/MANIFEST.MF b/org.eclipse.rephraserengine.testing.junit3/META-INF/MANIFEST.MF
index 232afee1..68a86952 100644
--- a/org.eclipse.rephraserengine.testing.junit3/META-INF/MANIFEST.MF
+++ b/org.eclipse.rephraserengine.testing.junit3/META-INF/MANIFEST.MF
@@ -7,6 +7,7 @@ Bundle-Vendor: Eclipse.org - Photran
Bundle-RequiredExecutionEnvironment: J2SE-1.5
Require-Bundle: org.junit,
org.eclipse.rephraserengine.core,
+ org.eclipse.rephraserengine.core.refactoring,
org.eclipse.rephraserengine.core.vpg,
org.eclipse.core.resources,
org.eclipse.core.runtime,
diff --git a/org.eclipse.rephraserengine.testing.junit3/src/org/eclipse/rephraserengine/testing/junit3/RefactoringTestCase.java b/org.eclipse.rephraserengine.testing.junit3/src/org/eclipse/rephraserengine/testing/junit3/RefactoringTestCase.java
new file mode 100644
index 00000000..28992740
--- /dev/null
+++ b/org.eclipse.rephraserengine.testing.junit3/src/org/eclipse/rephraserengine/testing/junit3/RefactoringTestCase.java
@@ -0,0 +1,303 @@
+/*******************************************************************************
+ * Copyright (c) 2011 University of Illinois at Urbana-Champaign 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:
+ * UIUC - Initial API and implementation
+ *******************************************************************************/
+package org.eclipse.rephraserengine.testing.junit3;
+
+import java.io.File;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.Map;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.OperationCanceledException;
+import org.eclipse.jface.text.TextSelection;
+import org.eclipse.ltk.core.refactoring.Change;
+import org.eclipse.ltk.core.refactoring.Refactoring;
+import org.eclipse.ltk.core.refactoring.RefactoringStatus;
+import org.eclipse.rephraserengine.core.refactorings.IEditorRefactoring;
+import org.eclipse.rephraserengine.core.refactorings.IResourceRefactoring;
+import org.eclipse.rephraserengine.core.util.StringUtil;
+import org.eclipse.rephraserengine.core.vpg.eclipse.EclipseVPG;
+
+/**
+ * Test case for a refactoring (used in a {@link GeneralTestSuiteFromMarkers}).
+ * <p>
+ * Most clients will want to subclass {@link RefactoringTestSuite} instead of manually constructing
+ * a suite of {@link RefactoringTestCase}s.
+ * <p>
+ * Assuming the marker prefix is &quot;!&lt;&lt;&lt;&lt;&lt;&quot; and the marker suffix is
+ * &quot;\n&quot;, markers are expected to have one of the following forms:
+ * <ol>
+ * <li> <tt>!&lt;&lt;&lt;&lt;&lt; line, col, ..., pass</tt>
+ * <li> <tt>!&lt;&lt;&lt;&lt;&lt; fromLine, fromCol, toLine, toCol, ..., pass</tt>
+ * </ol>
+ * That is, the first two fields in each marker are expected to be a line and column number; the
+ * text selection passed to the refactoring will be the offset of that line and column. The third
+ * fourth fields may also be a line and column number; then, the selection passed to the refactoring
+ * will extend from the first line/column to the second line/column.
+ * <p>
+ * The line and column numbers may be followed by an arbitrary number of fields that contain data
+ * specific to the refactoring being invoked. Many refactorings don't require any additional data;
+ * the Extract Local Variable test suite uses one field for the new variable declaration; the Add
+ * ONLY to USE Statement test suite uses these fields to list the module entities to add; etc.
+ * <p>
+ * The final field must be either &quot;pass&quot;, &quot;fail-initial&quot;, or
+ * &quot;fail-final&quot;, indicating whether the refactoring should succeed, fail its initial
+ * precondition check, or fail its final precondition check.
+ * <p>
+ * If the refactoring is expected to succeed, the program may be compiled and run before and after
+ * the refactoring in order to ensure that the refactoring actually preserved behavior. See the
+ * documentation for {@link RefactoringTestCase} for more information.
+ *
+ * @author Jeff Overbey
+ *
+ * @see RefactoringTestSuite
+ * @see GeneralTestSuiteFromMarkers
+ *
+ * @since 3.0
+ */
+public abstract class RefactoringTestCase<R extends Refactoring> extends WorkspaceTestCase
+{
+ /** Text of the last marker field when a refactoring is pass all precondition checks */
+ protected static final String PASS = "pass"; //$NON-NLS-1$
+ /** Text of the last marker field when a refactoring is expected to fail initial precondition check */
+ protected static final String FAIL_INITIAL = "fail-initial"; //$NON-NLS-1$
+ /** Text of the last marker field when a refactoring is expected to fail final precondition check */
+ protected static final String FAIL_FINAL = "fail-final"; //$NON-NLS-1$
+
+ private final File jioFileContainingMarker;
+ private final String markerText;
+ private final FilenameFilter filenameFilter;
+ private String description;
+ private Map<String, IFile> files;
+
+ public RefactoringTestCase(File fileContainingMarker,
+ int markerOffset,
+ String markerText,
+ FilenameFilter filenameFilter) throws Exception {
+ this(fileContainingMarker, markerOffset, markerText, null, filenameFilter);
+ }
+
+ public RefactoringTestCase(File fileContainingMarker,
+ int markerOffset,
+ String markerText,
+ EclipseVPG<?,?,?> vpg,
+ FilenameFilter filenameFilter) throws Exception {
+ super("test", vpg); //$NON-NLS-1$
+ this.jioFileContainingMarker = fileContainingMarker;
+ this.markerText = markerText;
+ this.filenameFilter = filenameFilter;
+ this.description = fileContainingMarker.getName();
+ }
+
+ public void test() throws Exception
+ {
+ this.files = importFiles();
+
+ IFile fileContainingMarker = files.get(jioFileContainingMarker.getName());
+ assertNotNull(fileContainingMarker);
+
+ R refactoring = createRefactoring();
+
+ LinkedList<String> markerFields = MarkerUtil.parseMarker(markerText);
+ TextSelection selection = MarkerUtil.determineSelection(markerFields, createDocument(fileContainingMarker));
+
+ appendFilenameToDescription(markerFields);
+
+ RefactoringStatus status =
+ checkInitialConditions(refactoring,
+ initializeRefactoring(refactoring, fileContainingMarker, selection, markerFields));
+
+ if (!status.hasFatalError())
+ {
+ String before = shouldCompile(fileContainingMarker)
+ ? compileAndRunProgram(files)
+ : ""; //$NON-NLS-1$
+
+ status = checkFinalConditions(refactoring,
+ configureRefactoring(refactoring, fileContainingMarker, selection, markerFields));
+
+ if (!status.hasFatalError())
+ {
+ performChange(refactoring);
+ refreshProject();
+
+ if (!status.hasError() && shouldCompile(fileContainingMarker))
+ assertEquals(before, compileAndRunProgram(files));
+ compareAgainstResultFile();
+ }
+ }
+
+ deinitializeRefactoring(refactoring, fileContainingMarker, selection, markerFields);
+ }
+
+ protected Map<String, IFile> importFiles() throws Exception
+ {
+ Map<String, IFile> result = importAllFiles(jioFileContainingMarker.getParentFile(), filenameFilter);
+ refreshProject();
+ return result;
+ }
+
+ protected void refreshProject() throws CoreException
+ {
+ NullProgressMonitor pm = new NullProgressMonitor();
+ project.refreshLocal(IResource.DEPTH_INFINITE, pm);
+ if (vpg != null) vpg.ensureVPGIsUpToDate(pm);
+ }
+
+ /**
+ * This method is invoked to instantiate the refactoring class.
+ */
+ protected abstract R createRefactoring() throws Exception;
+
+ private void appendFilenameToDescription(LinkedList<String> markerStrings)
+ {
+ description += " (" + jioFileContainingMarker.getName() + " " + markerStrings + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ }
+
+ /**
+ * This method is invoked after the refactoring is created ({@link #createRefactoring()}) but
+ * before {@link Refactoring#checkInitialConditions(org.eclipse.core.runtime.IProgressMonitor)}
+ * is invoked. Override if necessary.
+ *
+ * @return true iff the refactoring is expected to pass initial precondition checking
+ */
+ protected boolean initializeRefactoring(R refactoring, IFile file, TextSelection selection, LinkedList<String> markerFields)
+ {
+ if (refactoring instanceof IEditorRefactoring)
+ ((IEditorRefactoring)refactoring).initialize(file, selection);
+ else if (refactoring instanceof IResourceRefactoring)
+ ((IResourceRefactoring)refactoring).initialize(Collections.singletonList(file));
+ else
+ throw new IllegalStateException("Must override #initializeRefactoring"); //$NON-NLS-1$
+
+ if (lastMarkerField(markerFields).equals(PASS) || lastMarkerField(markerFields).equals(FAIL_FINAL))
+ return true;
+ else if (lastMarkerField(markerFields).equals(FAIL_INITIAL))
+ return false;
+ else
+ throw new IllegalStateException("Last marker field should be pass, fail-initial, or fail-final"); //$NON-NLS-1$
+ }
+
+ protected String lastMarkerField(LinkedList<String> markerFields)
+ {
+ return markerFields.getLast();
+ }
+
+ /**
+ * This method is invoked after initial preconditions have been checked
+ * ({@link Refactoring#checkInitialConditions(org.eclipse.core.runtime.IProgressMonitor)})
+ * but before final preconditions are checked
+ * ({@link Refactoring#checkFinalConditions(org.eclipse.core.runtime.IProgressMonitor)}).
+ * Override if necessary to supply user input.
+ *
+ * @return true iff the refactoring is expected to pass final precondition checking
+ */
+ protected boolean configureRefactoring(R refactoring, IFile file, TextSelection selection, LinkedList<String> markerFields)
+ {
+ if (lastMarkerField(markerFields).equals(PASS))
+ return true;
+ else if (lastMarkerField(markerFields).equals(FAIL_FINAL))
+ return false;
+ else
+ throw new IllegalStateException();
+ }
+
+ private RefactoringStatus checkInitialConditions(R refactoring, boolean shouldSucceed) throws OperationCanceledException, CoreException
+ {
+ RefactoringStatus status = refactoring.checkInitialConditions(new NullProgressMonitor());
+ if (shouldSucceed)
+ assertTrue(description + " failed initial precondition check: " + status.toString(), !status.hasError()); //$NON-NLS-1$
+ else
+ assertTrue(description + " should have failed initial precondition check but did not: " + status.toString(), status.hasError()); //$NON-NLS-1$
+ return status;
+ }
+
+ private RefactoringStatus checkFinalConditions(R refactoring, boolean shouldSucceed) throws OperationCanceledException, CoreException
+ {
+ RefactoringStatus status;
+ status = refactoring.checkFinalConditions(new NullProgressMonitor());
+ if (shouldSucceed)
+ assertTrue(description + " failed final precondition check: " + status.toString(), !status.hasError()); //$NON-NLS-1$
+ else
+ assertTrue(description + " should have failed final precondition check but did not: " + status.toString(), status.hasError()); //$NON-NLS-1$
+ return status;
+ }
+
+ private void performChange(R refactoring) throws CoreException
+ {
+ IProgressMonitor pm = new NullProgressMonitor();
+ Change change = refactoring.createChange(pm);
+ assertNotNull(description + " returned null Change object", change); //$NON-NLS-1$
+ assertTrue(description + " returned invalid Change object", change.isValid(pm).isOK()); //$NON-NLS-1$
+ change.perform(pm);
+ }
+
+ private void compareAgainstResultFile() throws IOException, URISyntaxException, CoreException
+ {
+ for (String filename : files.keySet())
+ {
+ if (resultFileFor(filename).exists())
+ {
+ String expected = StringUtil.read(resultFileFor(filename)).replaceAll("\\r", ""); //$NON-NLS-1$ //$NON-NLS-2$
+ String actual = readWorkspaceFile(filename).replaceAll("\\r", ""); //$NON-NLS-1$ //$NON-NLS-2$
+ assertEquals(expected, actual);
+ }
+ }
+ }
+
+ private File resultFileFor(String filename)
+ {
+ return new File(jioFileContainingMarker.getParent()
+ + File.separator
+ + filename
+ + ".result"); //$NON-NLS-1$
+ }
+
+ /**
+ * @return true iff the program should be compiled and run using {@link #compileAndRunProgram(Map)}
+ */
+ protected boolean shouldCompile(IFile fileContainingMarker)
+ {
+ return false;
+ }
+
+ /**
+ * Compiles and runs the test program.
+ * <p>
+ * This method is invoked iff {@link #shouldCompile(IFile)} returns <code>true</code>.
+ *
+ * @return the output of running the program
+ *
+ * @throws Exception if compilation or execution fails
+ */
+ protected String compileAndRunProgram(Map<String, IFile> files) throws Exception
+ {
+ throw new UnsupportedOperationException("Must override #compileAndRunProgram if #shouldCompile can return true"); //$NON-NLS-1$
+ }
+
+ /**
+ * This method is invoked after the refactoring has been performed (after
+ * {@link Refactoring#createChange(IProgressMonitor)} and
+ * {@link Change#perform(IProgressMonitor)}. Override if necessary.
+ *
+ * @throws Exception
+ */
+ protected void deinitializeRefactoring(R refactoring, IFile file, TextSelection selection, LinkedList<String> markerFields) throws Exception
+ {
+ }
+}
diff --git a/org.eclipse.rephraserengine.testing.junit3/src/org/eclipse/rephraserengine/testing/junit3/RefactoringTestSuite.java b/org.eclipse.rephraserengine.testing.junit3/src/org/eclipse/rephraserengine/testing/junit3/RefactoringTestSuite.java
new file mode 100644
index 00000000..76054fd2
--- /dev/null
+++ b/org.eclipse.rephraserengine.testing.junit3/src/org/eclipse/rephraserengine/testing/junit3/RefactoringTestSuite.java
@@ -0,0 +1,216 @@
+/*******************************************************************************
+ * Copyright (c) 2011 University of Illinois at Urbana-Champaign 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:
+ * UIUC - Initial API and implementation
+ *******************************************************************************/
+package org.eclipse.rephraserengine.testing.junit3;
+
+import java.io.File;
+import java.io.FilenameFilter;
+import java.util.LinkedList;
+import java.util.Map;
+
+import junit.framework.Test;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jface.text.TextSelection;
+import org.eclipse.ltk.core.refactoring.Change;
+import org.eclipse.ltk.core.refactoring.Refactoring;
+import org.eclipse.rephraserengine.core.vpg.eclipse.EclipseVPG;
+import org.eclipse.rephraserengine.testing.junit3.GeneralTestSuiteFromMarkers;
+
+/**
+ * Test suite for a refactoring.
+ * <p>
+ * The test suite is constructed by importing files from a directory in the source tree, searching
+ * its files for <i>markers,</i> and adding one test case to the suite for each marker.
+ * <p>
+ * The prefix and suffix of the marker are passed as the <code>marker</code> and
+ * <code>markerEnd</code> arguments to the constructor. Assuming <code>marker</code> is
+ * &quot;!&lt;&lt;&lt;&lt;&lt;&quot; and <code>markerEnd</code> is &quot;\n&quot;, markers are
+ * expected to have one of the following forms:
+ * <ol>
+ * <li> <tt>!&lt;&lt;&lt;&lt;&lt; line, col, ..., pass</tt>
+ * <li> <tt>!&lt;&lt;&lt;&lt;&lt; fromLine, fromCol, toLine, toCol, ..., pass</tt>
+ * </ol>
+ * That is, the first two fields in each marker are expected to be a line and column number; the
+ * text selection passed to the refactoring will be the offset of that line and column. The third
+ * fourth fields may also be a line and column number; then, the selection passed to the refactoring
+ * will extend from the first line/column to the second line/column.
+ * <p>
+ * The line and column numbers may be followed by an arbitrary number of fields that contain data
+ * specific to the refactoring being invoked. Many refactorings don't require any additional data;
+ * the Extract Local Variable test suite uses one field for the new variable declaration; the Add
+ * ONLY to USE Statement test suite uses these fields to list the module entities to add; etc.
+ * <p>
+ * The final field must be either &quot;pass&quot;, &quot;fail-initial&quot;, or
+ * &quot;fail-final&quot;, indicating whether the refactoring should succeed, fail its initial
+ * precondition check, or fail its final precondition check.
+ * <p>
+ * If the refactoring is expected to succeed, the program may be compiled and run before and after
+ * the refactoring in order to ensure that the refactoring actually preserved behavior. See the
+ * documentation for {@link RefactoringTestCase} for more information.
+ *
+ * @author Jeff Overbey
+ *
+ * @see RefactoringTestCase
+ * @see GeneralTestSuiteFromMarkers
+ *
+ * @since 3.0
+ */
+public class RefactoringTestSuite<R extends Refactoring> extends GeneralTestSuiteFromMarkers {
+
+ private Class<R> refactoringClass;
+ private EclipseVPG<?, ?, ?> vpg;
+ private FilenameFilter filenameFilter;
+
+ public RefactoringTestSuite(Class<R> refactoringClass,
+ String marker,
+ String markerEnd,
+ File fileOrDirectory,
+ FilenameFilter filenameFilter) throws Exception {
+ this(refactoringClass, marker, markerEnd, fileOrDirectory, filenameFilter, null);
+ }
+
+ public RefactoringTestSuite(Class<R> refactoringClass,
+ String marker,
+ String markerEnd,
+ File fileOrDirectory,
+ FilenameFilter filenameFilter,
+ EclipseVPG<?,?,?> vpg) throws Exception {
+ super(refactoringClass.getName(),
+ marker,
+ markerEnd,
+ fileOrDirectory,
+ filenameFilter,
+ // Initialization data:
+ refactoringClass,
+ filenameFilter,
+ vpg);
+ }
+
+ // Callback method which is invoked before adding tests to this test suite.
+ @SuppressWarnings("unchecked")
+ @Override
+ protected void initialize(Object... initializationData)
+ {
+ this.refactoringClass = (Class<R>)initializationData[0];
+ this.filenameFilter = (FilenameFilter)initializationData[1];
+ this.vpg = (EclipseVPG<?,?,?>)initializationData[2];
+ }
+
+ @Override
+ protected Test createTestFor(File fileContainingMarker, int markerOffset, String markerText)
+ throws Exception {
+ return new IndividualRefactoringTestCase(
+ fileContainingMarker,
+ markerOffset,
+ markerText,
+ refactoringClass,
+ vpg,
+ filenameFilter);
+ }
+
+ public class IndividualRefactoringTestCase extends RefactoringTestCase<R> {
+ private final Class<R> refactoringClass;
+
+ public IndividualRefactoringTestCase(File fileContainingMarker, int markerOffset, String markerText, Class<R> refactoringClass, EclipseVPG<?,?,?> vpg, FilenameFilter filenameFilter) throws Exception {
+ super(fileContainingMarker, markerOffset, markerText, vpg, filenameFilter);
+ this.refactoringClass = refactoringClass;
+ }
+
+ @Override
+ protected R createRefactoring() throws Exception {
+ return refactoringClass.newInstance();
+ }
+
+ @Override
+ protected boolean initializeRefactoring(R refactoring, IFile file, TextSelection selection, LinkedList<String> markerFields) {
+ RefactoringTestSuite.this.initializeRefactoring(refactoring, file, selection, markerFields);
+ return super.initializeRefactoring(refactoring, file, selection, markerFields);
+ }
+
+ @Override
+ protected boolean configureRefactoring(R refactoring, IFile file, TextSelection selection, LinkedList<String> markerFields) {
+ RefactoringTestSuite.this.configureRefactoring(refactoring, file, selection, markerFields);
+ return super.configureRefactoring(refactoring, file, selection, markerFields);
+ }
+
+ @Override
+ protected boolean shouldCompile(IFile fileContainingMarker)
+ {
+ return RefactoringTestSuite.this.shouldCompile(fileContainingMarker);
+ }
+
+ @Override
+ protected String compileAndRunProgram(Map<String, IFile> files) throws Exception
+ {
+ return RefactoringTestSuite.this.compileAndRunProgram(files);
+ }
+
+ @Override
+ protected void deinitializeRefactoring(R refactoring, IFile file, TextSelection selection, LinkedList<String> markerFields) throws Exception {
+ RefactoringTestSuite.this.deinitializeRefactoring(refactoring, file, selection, markerFields);
+ super.deinitializeRefactoring(refactoring, file, selection, markerFields);
+ }
+ }
+
+ /**
+ * This method is invoked after the refactoring is created ({@link #createRefactoring()}) but
+ * before {@link Refactoring#checkInitialConditions(org.eclipse.core.runtime.IProgressMonitor)}
+ * is invoked. Override if necessary.
+ */
+ protected void initializeRefactoring(R refactoring, IFile file, TextSelection selection, LinkedList<String> markerFields) {
+ ;
+ }
+
+ /**
+ * This method is invoked after initial preconditions have been checked
+ * ({@link Refactoring#checkInitialConditions(org.eclipse.core.runtime.IProgressMonitor)})
+ * but before final preconditions are checked
+ * ({@link Refactoring#checkFinalConditions(org.eclipse.core.runtime.IProgressMonitor)}).
+ * Override if necessary to supply user input.
+ */
+ protected void configureRefactoring(R refactoring, IFile file, TextSelection selection, LinkedList<String> markerFields) {
+ ;
+ }
+
+ /**
+ * @return true iff the program should be compiled and run using {@link #compileAndRunProgram(Map)}
+ */
+ protected boolean shouldCompile(IFile fileContainingMarker)
+ {
+ return false;
+ }
+
+ /**
+ * Compiles and runs the test program.
+ * <p>
+ * This method is invoked iff {@link #shouldCompile(IFile)} returns <code>true</code>.
+ *
+ * @return the output of running the program
+ *
+ * @throws Exception if compilation or execution fails
+ */
+ protected String compileAndRunProgram(Map<String, IFile> files) throws Exception
+ {
+ throw new UnsupportedOperationException("Must override #compileAndRunProgram if #shouldCompile can return true"); //$NON-NLS-1$
+ }
+
+ /**
+ * This method is invoked after the refactoring has been performed (after
+ * {@link Refactoring#createChange(IProgressMonitor)} and
+ * {@link Change#perform(IProgressMonitor)}. Override if necessary.
+ *
+ * @throws Exception
+ */
+ protected void deinitializeRefactoring(R refactoring, IFile file, TextSelection selection, LinkedList<String> markerFields) throws Exception {
+ ;
+ }
+}
diff --git a/org.eclipse.rephraserengine.testing.junit3/src/org/eclipse/rephraserengine/testing/junit3/WorkspaceTestCase.java b/org.eclipse.rephraserengine.testing.junit3/src/org/eclipse/rephraserengine/testing/junit3/WorkspaceTestCase.java
new file mode 100644
index 00000000..debe9033
--- /dev/null
+++ b/org.eclipse.rephraserengine.testing.junit3/src/org/eclipse/rephraserengine/testing/junit3/WorkspaceTestCase.java
@@ -0,0 +1,212 @@
+/*******************************************************************************
+ * Copyright (c) 2004, 2008 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
+ * Markus Schorn (Wind River Systems)
+ *******************************************************************************/
+
+/*
+ * Created on Oct 4, 2004
+ */
+package org.eclipse.rephraserengine.testing.junit3;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.Map;
+import java.util.TreeMap;
+
+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.resources.IWorkspace;
+import org.eclipse.core.resources.IWorkspaceRunnable;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Plugin;
+import org.eclipse.jface.text.Document;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.rephraserengine.core.util.StringUtil;
+import org.eclipse.rephraserengine.core.vpg.eclipse.EclipseVPG;
+
+/**
+ * Base class for a test case that imports files into the runtime workspace and then operates on the
+ * runtime workspace.
+ * <p>
+ * This class is based on org.eclipse.cdt.core.tests.BaseTestFramework.
+ *
+ * @author aniefer
+ * @author Jeff Overbey
+ *
+ * @since 3.0
+ */
+public abstract class WorkspaceTestCase extends TestCase
+{
+ /** Used to give each project a new name */
+ protected static int n = 0;
+
+ protected IWorkspace workspace;
+ protected IProject project;
+ protected final EclipseVPG<?,?,?> vpg;
+
+ public WorkspaceTestCase()
+ {
+ super();
+ this.vpg = null;
+ }
+
+ public WorkspaceTestCase(String name)
+ {
+ super(name);
+ this.vpg = null;
+ }
+
+ public WorkspaceTestCase(String name, EclipseVPG<?,?,?> vpg)
+ {
+ super(name);
+ this.vpg = vpg;
+ }
+
+ @Override
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+
+ if (project != null) return;
+
+ if (vpg != null)
+ {
+ vpg.releaseAllASTs();
+ vpg.clearDatabase();
+ }
+
+ workspace = ResourcesPlugin.getWorkspace();
+ project = createProject("TestProject" + (++n)); //$NON-NLS-1$
+ if (project == null)
+ fail("Unable to create project"); //$NON-NLS-1$
+ }
+
+ protected static IProject createProject(final String projectName) throws CoreException
+ {
+ class CreateProject implements IWorkspaceRunnable
+ {
+ IProject project = null;
+
+ public void run(IProgressMonitor monitor) throws CoreException {
+ IWorkspace workspace = ResourcesPlugin.getWorkspace();
+ project = workspace.getRoot().getProject(projectName);
+ if (!project.exists())
+ project.create(monitor);
+ else
+ project.refreshLocal(IResource.DEPTH_INFINITE, null);
+ if (!project.isOpen())
+ project.open(monitor);
+ }
+ }
+
+ CreateProject runnable = new CreateProject();
+ runnable.run(new NullProgressMonitor());
+ return runnable.project;
+ }
+
+ @Override
+ protected void tearDown() throws Exception
+ {
+ if (project == null || !project.exists()) return;
+
+ try
+ {
+ project.delete(true, true, new NullProgressMonitor());
+ }
+ catch (Throwable e)
+ {
+ project.close(new NullProgressMonitor());
+ }
+ finally
+ {
+ if (vpg != null)
+ {
+ // To speed things up a bit and conserve memory...
+ vpg.releaseAllASTs();
+ vpg.clearDatabase();
+ }
+
+ project = null;
+ }
+ }
+
+ protected IFile importFile(String fileName, String contents) throws Exception
+ {
+ IFile file = project.getProject().getFile(fileName);
+ InputStream stream = new ByteArrayInputStream(contents.getBytes());
+
+ if (file.exists())
+ file.setContents(stream, false, false, new NullProgressMonitor());
+ else
+ file.create(stream, false, new NullProgressMonitor());
+
+ return file;
+ }
+
+ protected IFile importFile(String fileName, File fileToCopyIntoWorkspace) throws Exception
+ {
+ return importFile(fileName, StringUtil.read(fileToCopyIntoWorkspace));
+ }
+
+ protected IFile importFile(File fileToCopyIntoWorkspace) throws Exception
+ {
+ return importFile(fileToCopyIntoWorkspace.getName(), fileToCopyIntoWorkspace);
+ }
+
+ protected IFile importFile(Plugin activator, String path) throws Exception
+ {
+ int lastSeparator = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'));
+ String filename = path.substring(lastSeparator+1);
+
+ //project.getProject().getFile(filename).delete(true, new NullProgressMonitor());
+ IFile result = importFile(filename, readTestFile(activator, path));
+ //project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
+ return result;
+ }
+
+ protected String readTestFile(Plugin activator, String path) throws IOException, URISyntaxException
+ {
+ URL resource = activator.getBundle().getResource(path);
+ assertNotNull(resource);
+ return StringUtil.read(resource.openStream());
+ }
+
+ protected Map<String, IFile> importAllFiles(File directory, FilenameFilter filenameFilter) throws Exception
+ {
+ Map<String, IFile> filesImported = new TreeMap<String, IFile>();
+ for (File file : directory.listFiles(filenameFilter))
+ {
+ IFile thisFile = importFile(file);
+ filesImported.put(thisFile.getName(), thisFile);
+ }
+ return filesImported;
+ }
+
+ protected String readWorkspaceFile(String filename) throws IOException, CoreException
+ {
+ return StringUtil.read(project.getFile(filename).getContents(true));
+ }
+
+ protected IDocument createDocument(IFile file) throws IOException, CoreException
+ {
+ return new Document(readWorkspaceFile(file.getName()));
+ }
+}

Back to the top