Skip to content

Commit

Permalink
Added simple Tests for constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
davidburkhart committed Mar 3, 2012
1 parent 531a619 commit 81904dd
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package test;

public class SuperClassWithExplicitlyUsedConstructor {

public static void main(String args) {
new Subclass();
}

public SuperClassWithExplicitlyUsedConstructor() {
}

class Subclass extends SuperClassWithExplicitlyUsedConstructor {
Subclass() {
super();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package test;

public class SuperClassWithUnusedConstructor {

public SuperClassWithUnusedConstructor() {
}
}
2 changes: 1 addition & 1 deletion unused.methods/test/unused/methods/AllPdeTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ FindUnusedMethodsInJavaProjectPdeTest.class })
@SuiteClasses({ FindUnusedMethodsInJavaProjectPdeTest.class, FindUnusedContructorsPdeTest.class })
// TODO test Markers
public class AllPdeTests {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package unused.methods;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.matchers.JUnitMatchers.hasItem;
import static unused.methods.MethodWithName.methodWithName;

import java.io.IOException;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.JavaModelException;
import org.junit.Test;

public class FindUnusedContructorsPdeTest extends PdeTestCaseWithTestProject {

@Test
public void findUnusedConstructorsInProject() throws CoreException, IOException {
List<IMethod> result = calculateUnusedMethods();
assertThat(result, not(hasItem(methodWithName("SuperClassWithExplicitlyUsedConstructor"))));
assertThat(result, hasItem(methodWithName("SuperClassWithUnusedConstructor")));
assertThat(result.size(), is(1));
}

private List<IMethod> calculateUnusedMethods() throws JavaModelException {
FindUnusedMethodsInJavaProject finder = new FindUnusedMethodsInJavaProject(project.asJavaProject());
finder.run(new NullProgressMonitor());
return finder.getUnusedMethods();
}

@Override
protected String[] getTestFiles() {
return new String[] { "/test/SuperClassWithExplicitlyUsedConstructor.java",
"/test/SuperClassWithUnusedConstructor.java" };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ private List<IMethod> calculateUnusedMethods() throws JavaModelException {
finder.run(new NullProgressMonitor());
return finder.getUnusedMethods();
}

@Override
protected String[] getTestFiles() {
return new String[] { "/test/Movie.java", "/test/Actor.java" };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Do not use classes from jdt in example code as no jre or classpath is set up
* in test project!!!
*/
public class PdeTestCaseWithTestProject {
public abstract class PdeTestCaseWithTestProject {

@Rule
public ProjectInWorkspace project = new ProjectInWorkspace().withJavaNature();
Expand All @@ -19,8 +19,12 @@ public class PdeTestCaseWithTestProject {
public void createTestJavaProject() throws CoreException, IOException {
project.createFolder("src");
project.createFolder("src/test");
project.createFile("src/test/Movie.java", new ResourceAsString("/test/Movie.java_").read());
project.createFile("src/test/Actor.java", new ResourceAsString("/test/Actor.java_").read());
for (String file : getTestFiles()) {
project.createFile("src" + file, new ResourceAsString(file + "_").read());
}
project.asJavaProject();
}

protected abstract String[] getTestFiles();

}

0 comments on commit 81904dd

Please sign in to comment.