Standard Libraries

import Type
import Member

/** Classes to represent JUnit 3.8 test cases and test methods. */
class TypeJUnitTestCase extends RefType {
  TypeJUnitTestCase() {
    this.hasQualifiedName("junit.framework", "TestCase")
  }
}

class TypeJUnitTest extends RefType {
  TypeJUnitTest() {
    this.hasQualifiedName("junit.framework", "Test")
  }
}

class TypeJUnitTestSuite extends RefType {
  TypeJUnitTestSuite() {
    this.hasQualifiedName("junit.framework", "TestSuite")
  }
}

/** A JUnit 3.8 test class */
class JUnit38TestClass extends Class {
  JUnit38TestClass() {
    exists(TypeJUnitTestCase tc | this.hasSupertype+(tc))
  }
}

/** A JUnit 3.8 tearDown method */
class TearDownMethod extends Method {
   TearDownMethod() {
      this.hasName("tearDown") and
      this.hasNoParameters() and
      this.getType().hasName("void") and
      exists(Method m | m.getDeclaringType() instanceof TypeJUnitTestCase and 
                       this.overrides*(m))
   }
}

/** A test class */
class TestClass extends Class {
  TestClass() {
    this instanceof JUnit38TestClass
  }
}

class TestMethod extends Method {
  TestMethod() {
    // JUnit 3.8 test method
       this.getDeclaringType() instanceof JUnit38TestClass and 
       this.getName().matches("test%") and 
       this.getType().hasName("void")  and 
       this.hasNoParameters()
    // JUnit 4 test method
    or this.getAnAnnotation().getType().hasQualifiedName("org.junit", "Test")
    // TestNG test method
    or this.getAnAnnotation().getType().hasQualifiedName("org.testng.annotations", 
                                                         "Test")
  }
}