]> git.uio.no Git - ifi-stolz-refaktor.git/commitdiff
Setting up test for SearchBasedExtractAndMoveMethodChanger.
authorErlend Kristiansen <erlenkr@ifi.uio.no>
Mon, 16 Dec 2013 13:15:23 +0000 (14:15 +0100)
committerErlend Kristiansen <erlenkr@ifi.uio.no>
Mon, 16 Dec 2013 13:15:23 +0000 (14:15 +0100)
software/no.uio.ifi.refaktor.tests/src/no/uio/ifi/refaktor/tests/SearchBasedExtractAndMoveMethodChangerTest.java [new file with mode: 0644]
software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/changers/SearchBasedExtractAndMoveMethodChanger.java [new file with mode: 0644]

diff --git a/software/no.uio.ifi.refaktor.tests/src/no/uio/ifi/refaktor/tests/SearchBasedExtractAndMoveMethodChangerTest.java b/software/no.uio.ifi.refaktor.tests/src/no/uio/ifi/refaktor/tests/SearchBasedExtractAndMoveMethodChangerTest.java
new file mode 100644 (file)
index 0000000..7f47f80
--- /dev/null
@@ -0,0 +1,92 @@
+package no.uio.ifi.refaktor.tests;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.fail;
+
+import java.lang.reflect.InvocationTargetException;
+
+import no.uio.ifi.refaktor.changers.RefaktorChanger;
+import no.uio.ifi.refaktor.changers.RefaktorChangerException;
+import no.uio.ifi.refaktor.changers.SearchBasedExtractAndMoveMethodChanger;
+import no.uio.ifi.refaktor.changers.UndoResources;
+
+import org.eclipse.core.resources.IMarker;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.IncrementalProjectBuilder;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.jdt.core.IJavaModelMarker;
+import org.eclipse.ltk.core.refactoring.RefactoringCore;
+import org.eclipse.ui.actions.WorkspaceModifyOperation;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class SearchBasedExtractAndMoveMethodChangerTest {
+
+       private NullProgressMonitor monitor;
+       private IProject project;
+
+       @Before
+       public void setUp() throws Exception {
+               monitor = new NullProgressMonitor();
+               project = ExampleCodeManager.INSTANCE.loadExampleCode(monitor);
+       }
+
+       @After
+       public void tearDown() throws Exception {
+       }
+
+       @Test
+       public void test() throws CoreException, InvocationTargetException, InterruptedException {
+               WorkspaceModifyOperation op = new WorkspaceModifyOperation(){
+
+                       @Override
+                       protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
+                               buildProject();
+
+                               RefaktorChanger changer = new SearchBasedExtractAndMoveMethodChanger();
+
+                               try {
+                                       changer.checkPreconditions();
+                               } catch (RefaktorChangerException e) {
+                                       fail(e.getMessage());
+                               }
+
+                               executeChangerAndCleanBuild(monitor, changer);
+                               assertFalse("The project should not have errors after build", hasErrors(project));
+                       }
+               };
+               op.run(monitor);
+       }
+
+       private void buildProject() {
+               try {
+                       project.build(IncrementalProjectBuilder.FULL_BUILD, null);
+               } catch (CoreException e) {
+                       fail("Project would not build before test. Fix the test code!");
+               }
+       }
+
+       private void executeChangerAndCleanBuild(IProgressMonitor monitor, RefaktorChanger changer) throws CoreException {
+               changer.executeChange(monitor, new UndoResources(RefactoringCore.getUndoManager(), changer.getClass().getSimpleName()));
+
+               try {
+                       project.build(IncrementalProjectBuilder.CLEAN_BUILD, null);
+                       project.build(IncrementalProjectBuilder.FULL_BUILD, null);
+               } catch (CoreException e) {
+                       fail("Exception occurred when project was built after extract and move refactoring.");
+               }
+       }
+
+       private boolean hasErrors(IProject project) throws CoreException {
+               IMarker[] markers = project.findMarkers(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, true, IResource.DEPTH_INFINITE);
+               for (IMarker marker: markers) {
+                       if (((Integer) marker.getAttribute(IMarker.SEVERITY)).intValue() == IMarker.SEVERITY_ERROR)
+                               return true;
+               }
+               return false;
+       }
+}
diff --git a/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/changers/SearchBasedExtractAndMoveMethodChanger.java b/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/changers/SearchBasedExtractAndMoveMethodChanger.java
new file mode 100644 (file)
index 0000000..fd00367
--- /dev/null
@@ -0,0 +1,20 @@
+package no.uio.ifi.refaktor.changers;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+
+public class SearchBasedExtractAndMoveMethodChanger implements RefaktorChanger {
+
+       @Override
+       public void checkPreconditions() throws RefaktorChangerException {
+               // TODO Auto-generated method stub
+
+       }
+
+       @Override
+       public void executeChange(IProgressMonitor monitor, UndoResources undoResources) throws CoreException {
+               // TODO Auto-generated method stub
+
+       }
+
+}