KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > RuleTest


1 /*
2  * Copyright(C) 2002 Mika Riekkinen, Joni Suominen
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or(at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package alt.jiapi;
20
21 import junit.framework.TestCase;
22
23 /**
24  * A JUnit test case for Rule.
25  *
26  * @author Mika Riekkinen
27  * @author Joni Suominen
28  * @version $Revision: 1.2 $ $Date: 2004/03/29 10:10:24 $
29  */

30 public class RuleTest extends TestCase {
31     private Rule rule1;
32     private Rule rule2;
33     private Rule rule3;
34     private Rule rule4;
35     private Rule rule5;
36     private Rule rule6;
37     private Rule rule7;
38     private Rule rule8;
39     private Rule rule9;
40     private Rule rule10;
41     private Rule rule11;
42     private String JavaDoc className1;
43     private String JavaDoc className2;
44     private String JavaDoc className3;
45     private String JavaDoc className4;
46     private String JavaDoc className5;
47
48     public RuleTest(String JavaDoc name) {
49         super(name);
50     }
51
52     public String JavaDoc getName() {
53         return "RuleTest: " + super.getName();
54     }
55
56     protected void setUp() {
57         try {
58             rule1 = new Rule("com.example.Foo");
59             rule2 = new Rule("com.example.*.Foo");
60             rule3 = new Rule("com.*.Foo");
61
62             rule4 = new Rule("com.*.example.Foo");
63             rule5 = new Rule("com.*.f.b.*.Foo");
64             rule6 = new Rule("com.*.f.*.b.Foo");
65             rule7 = new Rule("com.example.Foo2");
66
67             rule8 = new Rule("a");
68             rule9 = new Rule("com.*");
69             rule10 = new Rule("*.Foo");
70             rule11 = new Rule("*.example.*.Foo");
71         } catch (Exception JavaDoc e) {
72             e.printStackTrace();
73         }
74
75         className1 = "com.example.Foo";
76         className2 = "com.example.foo.Foo";
77         className3 = "com.example.Bar";
78         className4 = "com.example.fsdf.dfgdfg.Foo";
79         className5 = "com.example.Foo$InnerClass";
80     }
81
82     public void testEquals() {
83         assertTrue(rule1.equals(rule1));
84         assertTrue(!rule1.equals(rule2));
85     }
86
87     /**
88      * Tests that compareTo is consistent with equals.
89      * @see java.lang.Compareable
90      */

91     public void testCompareTo() {
92         assertTrue(rule1.compareTo(rule1) == 0);
93
94         // NOTE! The precedence of rule1 and rule7 is same,
95
// but they don't equal, that's why the compareTo must != 0.
96
assertTrue(rule1.getPrecedence() == rule7.getPrecedence());
97         assertTrue(rule1.compareTo(rule7) != 0);
98     }
99
100     public void testPrecedence() {
101         assertTrue(rule1.isMoreSignificant(rule2));
102         assertTrue(rule2.isMoreSignificant(rule3));
103         assertTrue(!rule3.isMoreSignificant(rule4));
104         assertTrue(rule1.isMoreSignificant(rule3));
105         assertTrue(rule1.isMoreSignificant(rule4));
106         assertTrue(rule2.isMoreSignificant(rule4));
107         assertTrue(rule5.isMoreSignificant(rule6));
108     }
109
110     public void testMatch() {
111         assertTrue(rule1.match(className1));
112         assertTrue(!rule1.match(className2));
113         assertTrue(!rule1.match(className3));
114
115         assertTrue(!rule2.match(className1));
116         assertTrue(rule2.match(className2));
117         assertTrue(!rule2.match(className3));
118
119         assertTrue(rule3.match(className1));
120         assertTrue(rule3.match(className2));
121         assertTrue(!rule3.match(className3));
122
123         assertTrue(!rule4.match(className1));
124         assertTrue(!rule4.match(className2));
125         assertTrue(!rule4.match(className3));
126
127         assertTrue(!rule8.match("aaa"));
128         assertTrue(rule9.match(className1));
129         assertTrue(rule9.match(className5)); // Inner class test
130

131         assertTrue(rule10.match(className1));
132         assertTrue(rule10.match(className2));
133         assertTrue(!rule10.match(className3));
134
135         assertTrue(!rule11.match(className1));
136         assertTrue(rule11.match(className2));
137         assertTrue(!rule11.match(className3));
138         assertTrue(rule11.match(className4));
139     }
140 }
141
142
Popular Tags