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.util.List;
    9
    10  import net.sourceforge.pmd.RuleSetReferenceId;
    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      @Test
    93      public void testOneReleaseRuleSet() {
    94  	List<RuleSetReferenceId> references = RuleSetReferenceId.parse("50");
    95  	assertEquals(1, references.size());
    96  	assertRuleSetReferenceId(true, "rulesets/releases/50.xml", true, null, "rulesets/releases/50.xml", references
    97  		.get(0));
    98      }
    99
    100     @Test
    101     public void testOneFullRuleSet() {
    102 	List<RuleSetReferenceId> references = RuleSetReferenceId.parse("rulesets/java/unusedcode.xml");
    103 	assertEquals(1, references.size());
    104 	assertRuleSetReferenceId(true, "rulesets/java/unusedcode.xml", true, null, "rulesets/java/unusedcode.xml",
    105 		references.get(0));
    106     }
    107
    108     @Test
    109     public void testOneFullRuleSetURL() {
    110 	List<RuleSetReferenceId> references = RuleSetReferenceId.parse("file://somepath/rulesets/java/unusedcode.xml");
    111 	assertEquals(1, references.size());
    112 	assertRuleSetReferenceId(true, "file://somepath/rulesets/java/unusedcode.xml", true, null,
    113 		"file://somepath/rulesets/java/unusedcode.xml", references.get(0));
    114     }
    115
    116     @Test
    117     public void testMultipleFullRuleSet() {
    118 	List<RuleSetReferenceId> references = RuleSetReferenceId
    119 		.parse("rulesets/java/unusedcode.xml,rulesets/java/basic.xml");
    120 	assertEquals(2, references.size());
    121 	assertRuleSetReferenceId(true, "rulesets/java/unusedcode.xml", true, null, "rulesets/java/unusedcode.xml",
    122 		references.get(0));
    123 	assertRuleSetReferenceId(true, "rulesets/java/basic.xml", true, null, "rulesets/java/basic.xml", references
    124 		.get(1));
    125     }
    126
    127     @Test
    128     public void testMixRuleSet() {
    129 	List<RuleSetReferenceId> references = RuleSetReferenceId.parse("rulesets/java/unusedcode.xml,xml-basic");
    130 	assertEquals(2, references.size());
    131 	assertRuleSetReferenceId(true, "rulesets/java/unusedcode.xml", true, null, "rulesets/java/unusedcode.xml",
    132 		references.get(0));
    133 	assertRuleSetReferenceId(true, "rulesets/xml/basic.xml", true, null, "rulesets/xml/basic.xml", references
    134 		.get(1));
    135     }
    136
    137     @Test
    138     public void testUnknownRuleSet() {
    139 	List<RuleSetReferenceId> references = RuleSetReferenceId.parse("nonexistant.xml");
    140 	assertEquals(1, references.size());
    141 	assertRuleSetReferenceId(true, "nonexistant.xml", true, null, "nonexistant.xml", references.get(0));
    142     }
    143
    144     @Test
    145     public void testUnknownAndSimpleRuleSet() {
    146 	List<RuleSetReferenceId> references = RuleSetReferenceId.parse("jsp-basic,nonexistant.xml");
    147 	assertEquals(2, references.size());
    148 	assertRuleSetReferenceId(true, "rulesets/jsp/basic.xml", true, null, "rulesets/jsp/basic.xml", references
    149 		.get(0));
    150 	assertRuleSetReferenceId(true, "nonexistant.xml", true, null, "nonexistant.xml", references.get(1));
    151     }
    152
    153     @Test
    154     public void testSimpleRuleSetAndRule() {
    155 	List<RuleSetReferenceId> references = RuleSetReferenceId.parse("java-basic/EmptyCatchBlock");
    156 	assertEquals(1, references.size());
    157 	assertRuleSetReferenceId(true, "rulesets/java/basic.xml", false, "EmptyCatchBlock",
    158 		"rulesets/java/basic.xml/EmptyCatchBlock", references.get(0));
    159     }
    160
    161     @Test
    162     public void testFullRuleSetAndRule() {
    163 	List<RuleSetReferenceId> references = RuleSetReferenceId.parse("rulesets/java/basic.xml/EmptyCatchBlock");
    164 	assertEquals(1, references.size());
    165 	assertRuleSetReferenceId(true, "rulesets/java/basic.xml", false, "EmptyCatchBlock",
    166 		"rulesets/java/basic.xml/EmptyCatchBlock", references.get(0));
    167     }
    168
    169     @Test
    170     public void testFullRuleSetURLAndRule() {
    171 	List<RuleSetReferenceId> references = RuleSetReferenceId
    172 		.parse("file://somepath/rulesets/java/unusedcode.xml/EmptyCatchBlock");
    173 	assertEquals(1, references.size());
    174 	assertRuleSetReferenceId(true, "file://somepath/rulesets/java/unusedcode.xml", false, "EmptyCatchBlock",
    175 		"file://somepath/rulesets/java/unusedcode.xml/EmptyCatchBlock", references.get(0));
    176     }
    177
    178     @Test
    179     public void testInternalRuleSetAndRule() {
    180 	List<RuleSetReferenceId> references = RuleSetReferenceId.parse("EmptyCatchBlock");
    181 	assertEquals(1, references.size());
    182 	assertRuleSetReferenceId(false, null, false, "EmptyCatchBlock", "EmptyCatchBlock", references.get(0));
    183     }
    184
    185     public static junit.framework.Test suite() {
    186 	return new junit.framework.JUnit4TestAdapter(RuleSetReferenceIdTest.class);
    187     }
    188 }