<?xml version="1.0"?>

<ruleset name="JUnit Rules" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="../etc/ruleset_xml_schema.xml">
  <description>
These rules deal with different problems that can occur with JUnit tests.
  </description>

    <rule name="JUnitStaticSuite"
          message="You have a suite() method that is not both public and static, so JUnit won't call it to get your TestSuite.  Is that what you wanted to do?"
          class="net.sourceforge.pmd.rules.XPathRule"
          externalInfoUrl="http://pmd.sourceforge.net/rules/junit.html#JUnitStaticSuite">
      <description>
The suite() method in a JUnit test needs to be both public and static.
      </description>
        <properties>
        <property name="xpath">
            <value>
                <![CDATA[
//MethodDeclaration[not(@Static='true') or not(@Public='true')]
[MethodDeclarator/@Image='suite']
[MethodDeclarator/FormalParameters/@ParameterCount=0]
                ]]>
            </value>
        </property>
        </properties>
        <priority>3</priority>

      <example>
  <![CDATA[
import junit.framework.*;
public class Foo extends TestCase {
 public void suite() {} // oops, should be static
 private static void suite() {} // oops, should be public
}
  ]]>
      </example>
      </rule>

  <rule name="JUnitSpelling"
        message="You may have misspelled a JUnit framework method (setUp or tearDown)"
        class="net.sourceforge.pmd.rules.XPathRule"
          externalInfoUrl="http://pmd.sourceforge.net/rules/junit.html#JUnitSpelling">
    <description>
Some JUnit framework methods are easy to misspell.
    </description>
      <properties>
      <property name="xpath">
          <value>
              <![CDATA[
//MethodDeclarator[(not(@Image = 'setUp')
 and translate(@Image, 'SETuP', 'setUp') = 'setUp')
 or (not(@Image = 'tearDown')
 and translate(@Image, 'TEARdOWN', 'tearDown') = 'tearDown')]
 [FormalParameters[count(*) = 0]]
              ]]>
          </value>
      </property>
      </properties>
      <priority>3</priority>
    <example>
<![CDATA[
import junit.framework.*;
public class Foo extends TestCase {
 public void setup() {} // oops, should be setUp
 public void TearDown() {} // oops, should be tearDown
}
]]>
    </example>
    </rule>

    <rule name="JUnitAssertionsShouldIncludeMessage"
          message="JUnit assertions should include a message"
          class="net.sourceforge.pmd.rules.junit.JUnitAssertionsShouldIncludeMessage"
          externalInfoUrl="http://pmd.sourceforge.net/rules/junit.html#JUnitAssertionsShouldIncludeMessage">
      <description>
JUnit assertions should include a message - i.e., use the three argument version of
assertEquals(), not the two argument version.
      </description>
        <priority>3</priority>
      <example>
  <![CDATA[
public class Foo extends TestCase {
 public void testSomething() {
  assertEquals("foo", "bar");
  // Use the form:
  // assertEquals("Foo does not equals bar", "foo", "bar");
  // instead
 }
}
  ]]>
      </example>
    </rule>

    <rule name="JUnitTestsShouldIncludeAssert"
          message="JUnit tests should include assert() or fail()"
          class="net.sourceforge.pmd.rules.junit.JUnitTestsShouldContainAsserts"
          externalInfoUrl="http://pmd.sourceforge.net/rules/junit.html#JUnitTestsShouldIncludeAssert">
        <description>
JUnit tests should include at least one assertion.  This makes the tests more robust, and
 using assert with messages provide the developer a clearer idea of what the test does.
        </description>
          <priority>3</priority>
        <example>
    <![CDATA[
public class Foo extends TestCase {
  public void testSomething() {
      Bar b = findBar();
  // This is better than having a NullPointerException
  // assertNotNull("bar not found", b);
  b.work();
  }
}
    ]]>
        </example>
      </rule>

    <rule name="TestClassWithoutTestCases"
          message="This class name ends with 'Test' but contains no test cases"
          class="net.sourceforge.pmd.rules.junit.TestClassWithoutTestCases"
          externalInfoUrl="http://pmd.sourceforge.net/rules/junit.html#TestClassWithoutTestCases">
      <description>
Test classes end with the suffix Test. Having a non-test class with that name is
not a good practice, since most people will assume it is a test case. Test
classes have test methods named testXXX.
      </description>
        <priority>3</priority>
      <example>
<![CDATA[
//Consider changing the name of the class if it is not a test
//Consider adding test methods if it is a test
public class CarTest {
   public static void main(String[] args) {
    // do something
   }
   // code
}
]]>
      </example>
    </rule>

    <rule name="UnnecessaryBooleanAssertion"
          message="assertTrue(true) or similar statements are unnecessary"
          class="net.sourceforge.pmd.rules.XPathRule"
          externalInfoUrl="http://pmd.sourceforge.net/rules/junit.html#UnnecessaryBooleanAssertion">
      <description>
A JUnit test assertion with a boolean literal is unnecessary since it always will eval to the same thing.
Consider using flow control (in case of assertTrue(false) or similar) or simply removing
statements like assertTrue(true) and assertFalse(false).  If you just want a test to halt, use the fail method.
      </description>
      <properties>
          <property name="xpath">
              <value>
    <![CDATA[
//StatementExpression
[
.//Name[@Image='assertTrue' or  @Image='assertFalse']
and
PrimaryExpression/PrimarySuffix/Arguments/ArgumentList
 /Expression/PrimaryExpression/PrimaryPrefix
  /Literal/BooleanLiteral
or
(
.//Name[@Image='assertTrue' or  @Image='assertFalse']
and
PrimaryExpression/PrimarySuffix/Arguments/ArgumentList
 /Expression/UnaryExpressionNotPlusMinus[@Image='!']
/PrimaryExpression/PrimaryPrefix[Literal/BooleanLiteral or Name[count(../../*)=1]])
]
]]>
              </value>
          </property>
      </properties>
        <priority>3</priority>

      <example>
<![CDATA[
public class SimpleTest extends TestCase {
 public void testX() {
  // Why on earth would you write this?
  assertTrue(true);
 }
}
]]>
          </example>
    </rule>

    <rule name="UseAssertEqualsInsteadOfAssertTrue"
          message="Use assertEquals(x, y) instead of assertTrue(x.equals(y))"
          class="net.sourceforge.pmd.rules.XPathRule"
          externalInfoUrl="http://pmd.sourceforge.net/rules/junit.html#UseAssertEqualsInsteadOfAssertTrue">
      <description>
This rule detects JUnit assertions in object equality. These assertions
should be made by more specific methods, like assertEquals.
      </description>
    <properties>
        <property name="xpath">
            <value>
                <![CDATA[
//PrimaryExpression[
    PrimaryPrefix/Name[@Image = 'assertTrue']
][
    PrimarySuffix/Arguments/ArgumentList/Expression/PrimaryExpression/PrimaryPrefix/Name
    [ends-with(@Image, '.equals')]
]
 ]]>
            </value>
        </property>
    </properties>
        <priority>3</priority>
      <example>
<![CDATA[
public class FooTest extends TestCase {
 void testCode() {
  Object a, b;
  assertTrue(a.equals(b)); // bad usage
  assertEquals(?a should equals b?, a, b); // good usage
 }
}
]]>
      </example>
    </rule>

   <rule name="UseAssertSameInsteadOfAssertTrue"
          message="Use assertSame(x, y) instead of assertTrue(x==y), or assertNotSame(x,y) vs assertFalse(x==y)"
          class="net.sourceforge.pmd.rules.XPathRule"
          externalInfoUrl="http://pmd.sourceforge.net/rules/junit.html#UseAssertSameInsteadOfAssertTrue">
      <description>
This rule detects JUnit assertions in object references equality. These assertions
should be made by more specific methods, like assertSame, assertNotSame.
      </description>
    <properties>
        <property name="xpath">
            <value>
                <![CDATA[
//PrimaryExpression [
    PrimaryPrefix/Name[@Image = 'assertTrue' or @Image = 'assertFalse']
]
[PrimarySuffix/Arguments/ArgumentList/Expression/EqualityExpression]
 ]]>
            </value>
        </property>
    </properties>
        <priority>3</priority>
      <example>
<![CDATA[
public class FooTest extends TestCase {
 void testCode() {
  Object a, b;
  assertTrue(a==b); // bad usage
  assertSame(a, b);  // good usage
 }
}
]]>
      </example>
    </rule>

    <rule name="UseAssertNullInsteadOfAssertTrue"
           message="Use assertNull(x) instead of assertTrue(x==null), or assertNotNull(x) vs assertFalse(x==null)"
           class="net.sourceforge.pmd.rules.XPathRule"
           externalInfoUrl="http://pmd.sourceforge.net/rules/junit.html#UseAssertNullInsteadOfAssertTrue">
       <description>
 This rule detects JUnit assertions in object references equality. These assertions
 should be made by more specific methods, like assertNull, assertNotNull.
       </description>
     <properties>
         <property name="xpath">
             <value>
                 <![CDATA[
//PrimaryExpression[
 PrimaryPrefix/Name[@Image = 'assertTrue' or @Image = 'assertFalse']
][
 PrimarySuffix/Arguments/ArgumentList[
  Expression/EqualityExpression/PrimaryExpression/PrimaryPrefix/Literal/NullLiteral
 ]
]
  ]]>
             </value>
         </property>
     </properties>
         <priority>3</priority>
       <example>
 <![CDATA[
 public class FooTest extends TestCase {
  void testCode() {
   Object a = doSomething();
   assertTrue(a==null); // bad usage
   assertNull(a);  // good usage
   assertTrue(a != null); // bad usage
   assertNotNull(a);  // good usage
  }
 }
 ]]>
       </example>
     </rule>

</ruleset>



                                             