View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.testframework;
5   
6   import java.util.ArrayList;
7   import java.util.List;
8   import java.util.Properties;
9   
10  import net.sourceforge.pmd.Rule;
11  import net.sourceforge.pmd.lang.LanguageVersion;
12  
13  import org.junit.Ignore;
14  
15  /**
16   * Stores the information required to run a complete test.
17   */
18  @Ignore("this is not a unit test")
19  public class TestDescriptor {
20      private Rule rule;
21      private Properties properties;
22      private String description;
23      private int numberOfProblemsExpected;
24      private List<String> expectedMessages = new ArrayList<>();
25      private List<Integer> expectedLineNumbers = new ArrayList<>();
26      private String code;
27      private LanguageVersion languageVersion;
28      private boolean reinitializeRule = true;   //default, avoids unintentional mixing of state between test cases
29      private boolean isRegressionTest = true;
30      private boolean useAuxClasspath = true;
31      private int numberInDocument = -1;
32  
33      // Empty descriptor added to please mvn surefire plugin
34      public TestDescriptor() {
35      	
36      }
37      
38      public TestDescriptor(String code, String description, int numberOfProblemsExpected, Rule rule) {
39          this(code, description, numberOfProblemsExpected, rule, rule.getLanguage().getDefaultVersion());
40      }
41      
42      public TestDescriptor(String code, String description, int numberOfProblemsExpected, Rule rule, LanguageVersion languageVersion) {
43          this.rule = rule;
44          this.code = code;
45          this.description = description;
46          this.numberOfProblemsExpected = numberOfProblemsExpected;
47          this.languageVersion = languageVersion;
48      }
49  
50      public int getNumberInDocument() {
51          return numberInDocument;
52      }
53  
54      public void setNumberInDocument(int numberInDocument) {
55          this.numberInDocument = numberInDocument;
56      }
57  
58      public void setExpectedMessages(List<String> messages) {
59          expectedMessages.clear();
60          expectedMessages.addAll(messages);
61      }
62  
63      public List<String> getExpectedMessages() {
64          return expectedMessages;
65      }
66  
67      public void setExpectedLineNumbers(List<Integer> expectedLineNumbers) {
68          this.expectedLineNumbers.clear();
69          this.expectedLineNumbers.addAll(expectedLineNumbers);
70      }
71  
72      public List<Integer> getExpectedLineNumbers() {
73          return expectedLineNumbers;
74      }
75  
76      public void setProperties(Properties properties) {
77          this.properties = properties;
78      }
79      
80      public Properties getProperties() {
81          return properties;
82      }
83      
84      public String getCode() {
85          return code;
86      }
87  
88      public LanguageVersion getLanguageVersion() {
89          return languageVersion;
90      }
91  
92      public String getDescription() {
93          return description;
94      }
95  
96      public int getNumberOfProblemsExpected() {
97          return numberOfProblemsExpected;
98      }
99  
100     public Rule getRule() {
101         return rule;
102     }
103 
104     public boolean getReinitializeRule() {
105         return reinitializeRule;
106     }
107 
108     public void setReinitializeRule(boolean reinitializeRule) {
109         this.reinitializeRule = reinitializeRule;
110     }
111 
112     /**
113      * Checks whether we are testing for regression problems only.
114      * Return value is based on the system property "pmd.regress".
115      * 
116      * @return <code>false</code> if system property "pmd.regress" is set to <code>false</code>, <code>true</code> otherwise
117      */
118     public static boolean inRegressionTestMode() {
119 	boolean inRegressionMode = true; // default
120 	try {
121 	    //get the "pmd.regress" System property
122 	    String property = System.getProperty("pmd.regress");
123 	    if (property != null) {
124 		inRegressionMode = Boolean.parseBoolean(property);
125 	    }
126 	} catch (IllegalArgumentException e) {
127 	}
128 	
129         return inRegressionMode;
130     }
131 
132     public boolean isRegressionTest() {
133         return isRegressionTest;
134     }
135 
136     public void setRegressionTest(boolean isRegressionTest) {
137         this.isRegressionTest = isRegressionTest;
138     }
139 
140     public void setUseAuxClasspath(boolean useAuxClasspath) {
141         this.useAuxClasspath = useAuxClasspath;
142     }
143 
144     public boolean isUseAuxClasspath() {
145         return useAuxClasspath;
146     }
147 }