--- /dev/null
+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;
+ }
+}