001: /**
002: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003: */package test.net.sourceforge.pmd.testframework;
004:
005: import java.util.ArrayList;
006: import java.util.List;
007:
008: import net.sourceforge.pmd.Rule;
009:
010: import org.junit.Test;
011: import org.junit.internal.runners.InitializationError;
012: import org.junit.internal.runners.JUnit4ClassRunner;
013: import org.junit.runner.Description;
014: import org.junit.runner.RunWith;
015: import org.junit.runner.notification.Failure;
016: import org.junit.runner.notification.RunNotifier;
017:
018: /**
019: * Standard methods for (simple) testcases.
020: */
021: @RunWith(SimpleAggregatorTst.CustomXmlTestClassMethodsRunner.class)
022: public abstract class SimpleAggregatorTst extends RuleTst {
023: /**
024: * Run a set of tests defined in an XML test-data file for a rule. The file
025: * should be ./xml/RuleName.xml relative to the test-class. The format is
026: * defined in test-data.xsd.
027: */
028: public void runTests(Rule rule) {
029: runTests(extractTestsFromXml(rule));
030: }
031:
032: /**
033: * Run a set of tests defined in a XML test-data file. The file should be
034: * ./xml/[testsFileName].xml relative to the test-class. The format is
035: * defined in test-data.xsd.
036: */
037: public void runTests(Rule rule, String testsFileName) {
038: runTests(extractTestsFromXml(rule, testsFileName));
039: }
040:
041: /**
042: * Run a set of tests of a certain sourceType.
043: */
044: public void runTests(TestDescriptor[] tests) {
045: for (int i = 0; i < tests.length; i++) {
046: runTest(tests[i]);
047: }
048: }
049:
050: private List<Rule> rules = new ArrayList<Rule>();
051:
052: /**
053: * Add new XML tests associated with the rule to the test suite. This should
054: * be called from the setup method.
055: */
056: protected void addRule(String ruleSet, String ruleName) {
057: rules.add(findRule(ruleSet, ruleName));
058: }
059:
060: /**
061: * Run a set of tests for all rules added in the setup method.
062: */
063: @Test
064: public void testAll() {
065: boolean regressionTest = TestDescriptor.inRegressionTestMode();
066: ArrayList<Failure> l = new ArrayList<Failure>();
067: for (Rule r : rules) {
068: TestDescriptor[] tests = extractTestsFromXml(r);
069: for (TestDescriptor test : tests) {
070: try {
071: if (!regressionTest || test.isRegressionTest()) {
072: runTest(test);
073: }
074: } catch (Throwable t) {
075: Failure f = CustomXmlTestClassMethodsRunner
076: .createFailure(r, t);
077: l.add(f);
078: }
079: }
080: }
081: for (Failure f : l) {
082: CustomXmlTestClassMethodsRunner.addFailure(f);
083: }
084: }
085:
086: public static class CustomXmlTestClassMethodsRunner extends
087: JUnit4ClassRunner {
088: public CustomXmlTestClassMethodsRunner(Class<?> klass)
089: throws InitializationError {
090: super (klass);
091: }
092:
093: public static Failure createFailure(Rule rule,
094: Throwable targetException) {
095: return new Failure(Description.createTestDescription(
096: SimpleAggregatorTst.class, "xml."
097: + rule.getRuleSetName() + '.'
098: + rule.getName()), targetException);
099: }
100:
101: public static void addFailure(Failure failure) {
102: synchronized (CustomXmlTestClassMethodsRunner.class) {
103: NOTIFIER.fireTestFailure(failure);
104: }
105: }
106:
107: @Override
108: public void run(RunNotifier n) {
109: synchronized (CustomXmlTestClassMethodsRunner.class) {
110: // synchronized so that access to NOTIFIER is safe: only
111: // one runner at a time is active
112: NOTIFIER = n;
113: super .run(n);
114: }
115: }
116:
117: private static RunNotifier NOTIFIER;
118: }
119:
120: }
|