View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd;
5   
6   import static org.junit.Assert.assertEquals;
7   import static org.junit.Assert.assertTrue;
8   
9   import java.io.File;
10  import java.util.List;
11  
12  import org.junit.Test;
13  
14  public class RuleSetReferenceIdTest {
15  
16      private static void assertRuleSetReferenceId(final boolean expectedExternal, final String expectedRuleSetFileName,
17  	    final boolean expectedAllRules, final String expectedRuleName, final String expectedToString,
18  	    final RuleSetReferenceId reference) {
19  	assertEquals("Wrong external", expectedExternal, reference.isExternal());
20  	assertEquals("Wrong RuleSet file name", expectedRuleSetFileName, reference.getRuleSetFileName());
21  	assertEquals("Wrong all Rule reference", expectedAllRules, reference.isAllRules());
22  	assertEquals("Wrong Rule name", expectedRuleName, reference.getRuleName());
23  	assertEquals("Wrong toString()", expectedToString, reference.toString());
24      }
25  
26      @Test(expected = IllegalArgumentException.class)
27      public void testCommaInSingleId() {
28  	new RuleSetReferenceId("bad,id");
29      }
30  
31      @Test(expected = IllegalArgumentException.class)
32      public void testInternalWithInternal() {
33  	new RuleSetReferenceId("SomeRule", new RuleSetReferenceId("SomeOtherRule"));
34      }
35  
36      @Test(expected = IllegalArgumentException.class)
37      public void testExternalWithExternal() {
38  	new RuleSetReferenceId("someruleset.xml/SomeRule", new RuleSetReferenceId("someruleset.xml/SomeOtherRule"));
39      }
40  
41      @Test(expected = IllegalArgumentException.class)
42      public void testExternalWithInternal() {
43  	new RuleSetReferenceId("someruleset.xml/SomeRule", new RuleSetReferenceId("SomeOtherRule"));
44      }
45  
46      @Test
47      public void testInteralWithExternal() {
48  	// This is okay
49  	new RuleSetReferenceId("SomeRule", new RuleSetReferenceId("someruleset.xml/SomeOtherRule"));
50      }
51  
52      @Test
53      public void testEmptyRuleSet() {
54  	// This is representative of how the Test framework creates RuleSetReferenceId from static RuleSet XMLs
55  	RuleSetReferenceId reference = new RuleSetReferenceId(null);
56  	assertRuleSetReferenceId(true, null, true, null, "anonymous all Rule", reference);
57      }
58  
59      @Test
60      public void testInternalWithExternalRuleSet() {
61  	// This is representative of how the RuleSetFactory temporarily pairs an internal reference
62  	// with an external reference.
63  	RuleSetReferenceId internalRuleSetReferenceId = new RuleSetReferenceId("MockRuleName");
64  	assertRuleSetReferenceId(false, null, false, "MockRuleName", "MockRuleName", internalRuleSetReferenceId);
65  	RuleSetReferenceId externalRuleSetReferenceId = new RuleSetReferenceId("rulesets/java/basic.xml");
66  	assertRuleSetReferenceId(true, "rulesets/java/basic.xml", true, null, "rulesets/java/basic.xml",
67  		externalRuleSetReferenceId);
68  
69  	RuleSetReferenceId pairRuleSetReferenceId = new RuleSetReferenceId("MockRuleName", externalRuleSetReferenceId);
70  	assertRuleSetReferenceId(true, "rulesets/java/basic.xml", false, "MockRuleName",
71  		"rulesets/java/basic.xml/MockRuleName", pairRuleSetReferenceId);
72      }
73  
74      @Test
75      public void testOneSimpleRuleSet() {
76  	List<RuleSetReferenceId> references = RuleSetReferenceId.parse("java-basic");
77  	assertEquals(1, references.size());
78  	assertRuleSetReferenceId(true, "rulesets/java/basic.xml", true, null, "rulesets/java/basic.xml", references
79  		.get(0));
80      }
81  
82      @Test
83      public void testMultipleSimpleRuleSet() {
84  	List<RuleSetReferenceId> references = RuleSetReferenceId.parse("java-unusedcode,java-basic");
85  	assertEquals(2, references.size());
86  	assertRuleSetReferenceId(true, "rulesets/java/unusedcode.xml", true, null, "rulesets/java/unusedcode.xml",
87  		references.get(0));
88  	assertRuleSetReferenceId(true, "rulesets/java/basic.xml", true, null, "rulesets/java/basic.xml", references
89  		.get(1));
90      }
91  
92      /**
93       * See https://sourceforge.net/p/pmd/bugs/1201/
94       */
95      @Test
96      public void testMultipleRulesWithSpaces() {
97          List<RuleSetReferenceId> references = RuleSetReferenceId.parse("ecmascript-basic, ecmascript-braces, ecmascript-unnecessary");
98          assertEquals(3, references.size());
99          assertRuleSetReferenceId(true, "rulesets/ecmascript/basic.xml", true, null, "rulesets/ecmascript/basic.xml", references.get(0));
100         assertRuleSetReferenceId(true, "rulesets/ecmascript/braces.xml", true, null, "rulesets/ecmascript/braces.xml", references.get(1));
101         assertRuleSetReferenceId(true, "rulesets/ecmascript/unnecessary.xml", true, null, "rulesets/ecmascript/unnecessary.xml", references.get(2));
102     }
103 
104     @Test
105     public void testOneReleaseRuleSet() {
106 	List<RuleSetReferenceId> references = RuleSetReferenceId.parse("50");
107 	assertEquals(1, references.size());
108 	assertRuleSetReferenceId(true, "rulesets/releases/50.xml", true, null, "rulesets/releases/50.xml", references
109 		.get(0));
110     }
111 
112     @Test
113     public void testOneFullRuleSet() {
114 	List<RuleSetReferenceId> references = RuleSetReferenceId.parse("rulesets/java/unusedcode.xml");
115 	assertEquals(1, references.size());
116 	assertRuleSetReferenceId(true, "rulesets/java/unusedcode.xml", true, null, "rulesets/java/unusedcode.xml",
117 		references.get(0));
118     }
119 
120     @Test
121     public void testOneFullRuleSetURL() {
122 	List<RuleSetReferenceId> references = RuleSetReferenceId.parse("file://somepath/rulesets/java/unusedcode.xml");
123 	assertEquals(1, references.size());
124 	assertRuleSetReferenceId(true, "file://somepath/rulesets/java/unusedcode.xml", true, null,
125 		"file://somepath/rulesets/java/unusedcode.xml", references.get(0));
126     }
127 
128     @Test
129     public void testMultipleFullRuleSet() {
130 	List<RuleSetReferenceId> references = RuleSetReferenceId
131 		.parse("rulesets/java/unusedcode.xml,rulesets/java/basic.xml");
132 	assertEquals(2, references.size());
133 	assertRuleSetReferenceId(true, "rulesets/java/unusedcode.xml", true, null, "rulesets/java/unusedcode.xml",
134 		references.get(0));
135 	assertRuleSetReferenceId(true, "rulesets/java/basic.xml", true, null, "rulesets/java/basic.xml", references
136 		.get(1));
137     }
138 
139     @Test
140     public void testMixRuleSet() {
141 	List<RuleSetReferenceId> references = RuleSetReferenceId.parse("rulesets/java/unusedcode.xml,xml-basic");
142 	assertEquals(2, references.size());
143 	assertRuleSetReferenceId(true, "rulesets/java/unusedcode.xml", true, null, "rulesets/java/unusedcode.xml",
144 		references.get(0));
145 	assertRuleSetReferenceId(true, "rulesets/xml/basic.xml", true, null, "rulesets/xml/basic.xml", references
146 		.get(1));
147     }
148 
149     @Test
150     public void testUnknownRuleSet() {
151 	List<RuleSetReferenceId> references = RuleSetReferenceId.parse("nonexistant.xml");
152 	assertEquals(1, references.size());
153 	assertRuleSetReferenceId(true, "nonexistant.xml", true, null, "nonexistant.xml", references.get(0));
154     }
155 
156     @Test
157     public void testUnknownAndSimpleRuleSet() {
158 	List<RuleSetReferenceId> references = RuleSetReferenceId.parse("jsp-basic,nonexistant.xml");
159 	assertEquals(2, references.size());
160 	assertRuleSetReferenceId(true, "rulesets/jsp/basic.xml", true, null, "rulesets/jsp/basic.xml", references
161 		.get(0));
162 	assertRuleSetReferenceId(true, "nonexistant.xml", true, null, "nonexistant.xml", references.get(1));
163     }
164 
165     @Test
166     public void testSimpleRuleSetAndRule() {
167 	List<RuleSetReferenceId> references = RuleSetReferenceId.parse("java-basic/EmptyCatchBlock");
168 	assertEquals(1, references.size());
169 	assertRuleSetReferenceId(true, "rulesets/java/basic.xml", false, "EmptyCatchBlock",
170 		"rulesets/java/basic.xml/EmptyCatchBlock", references.get(0));
171     }
172 
173     @Test
174     public void testFullRuleSetAndRule() {
175 	List<RuleSetReferenceId> references = RuleSetReferenceId.parse("rulesets/java/basic.xml/EmptyCatchBlock");
176 	assertEquals(1, references.size());
177 	assertRuleSetReferenceId(true, "rulesets/java/basic.xml", false, "EmptyCatchBlock",
178 		"rulesets/java/basic.xml/EmptyCatchBlock", references.get(0));
179     }
180 
181     @Test
182     public void testFullRuleSetURLAndRule() {
183 	List<RuleSetReferenceId> references = RuleSetReferenceId
184 		.parse("file://somepath/rulesets/java/unusedcode.xml/EmptyCatchBlock");
185 	assertEquals(1, references.size());
186 	assertRuleSetReferenceId(true, "file://somepath/rulesets/java/unusedcode.xml", false, "EmptyCatchBlock",
187 		"file://somepath/rulesets/java/unusedcode.xml/EmptyCatchBlock", references.get(0));
188     }
189 
190     @Test
191     public void testInternalRuleSetAndRule() {
192 	List<RuleSetReferenceId> references = RuleSetReferenceId.parse("EmptyCatchBlock");
193 	assertEquals(1, references.size());
194 	assertRuleSetReferenceId(false, null, false, "EmptyCatchBlock", "EmptyCatchBlock", references.get(0));
195     }
196 
197     @Test
198     public void testRelativePathRuleSet() {
199         List<RuleSetReferenceId> references = RuleSetReferenceId.parse("pmd/pmd-ruleset.xml");
200         assertEquals(1, references.size());
201         assertRuleSetReferenceId(true, "pmd/pmd-ruleset.xml", true, null, "pmd/pmd-ruleset.xml", references.get(0));
202     }
203 
204     @Test
205     public void testAbsolutePathRuleSet() {
206         List<RuleSetReferenceId> references = RuleSetReferenceId.parse("/home/foo/pmd/pmd-ruleset.xml");
207         assertEquals(1, references.size());
208         assertRuleSetReferenceId(true, "/home/foo/pmd/pmd-ruleset.xml", true, null,
209                 "/home/foo/pmd/pmd-ruleset.xml", references.get(0));
210     }
211 
212     @Test
213     public void testFooRules() throws Exception {
214         String fooRulesFile = new File("./src/test/resources/net/sourceforge/pmd/rulesets/foo-project/foo-rules").getCanonicalPath();
215         List<RuleSetReferenceId> references = RuleSetReferenceId.parse(fooRulesFile);
216         assertEquals(1, references.size());
217         assertRuleSetReferenceId(true, fooRulesFile, true, null, fooRulesFile, references.get(0));
218     }
219 
220     @Test
221     public void testNullRulesetString() throws Exception {
222         List<RuleSetReferenceId> references = RuleSetReferenceId.parse(null);
223         assertTrue(references.isEmpty());
224     }
225 
226     public static junit.framework.Test suite() {
227 	return new junit.framework.JUnit4TestAdapter(RuleSetReferenceIdTest.class);
228     }
229 }