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