KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > csdl > jblanket > modifier > TestMethodCollector


1 package csdl.jblanket.modifier;
2
3 import csdl.jblanket.methodset.MethodInfo;
4 import csdl.jblanket.methodset.MethodSet;
5
6 import java.io.File JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.Date JavaDoc;
9 import java.util.List JavaDoc;
10
11 import junit.framework.TestCase;
12
13 /**
14  * Tests the MethodCollector class.
15  *
16  * @author Joy M. Agustin
17  * @version $Id: TestMethodCollector.java,v 1.1 2004/11/07 00:32:30 timshadel Exp $
18  */

19 public class TestMethodCollector extends TestCase {
20
21   /** Directory separator */
22   private static final String JavaDoc SLASH = File.separator;
23
24   /**
25    * Constructor required by JUnit.
26    *
27    * @param name the name of this test class.
28    */

29   public TestMethodCollector(String JavaDoc name) {
30     super(name);
31   }
32
33   /**
34    * Tests the isTestClass method.
35    *
36    * @throws Exception if an error occurs.
37    */

38   public void testIsTestClass() throws Exception JavaDoc {
39
40     // Test class names
41
final String JavaDoc prefixFoo = "Prefixfoo";
42     final String JavaDoc testFoo = "Testfoo";
43     final String JavaDoc fooTest = "fooTest";
44     final String JavaDoc fooEnding = "fooEnding";
45     
46     // valid grammars that begin with 'Prefix'
47
final String JavaDoc testPrefix1 = "Prefix*.class";
48     final String JavaDoc testPrefix2 = "Prefix*.java";
49     final String JavaDoc testPrefix3 = "Prefix*.";
50     final String JavaDoc testPrefix4 = "Prefix*";
51
52     // valid grammars that begin with 'Test'
53
final String JavaDoc testGrammar1 = "Test*.class";
54     final String JavaDoc testGrammar2 = "Test*.java";
55     final String JavaDoc testGrammar3 = "Test*.";
56     final String JavaDoc testGrammar4 = "Test*";
57
58     // valid grammars that end with 'Test'
59
final String JavaDoc grammarTest1 = "*Test.class";
60     final String JavaDoc grammarTest2 = "*Test.java";
61     final String JavaDoc grammarTest3 = "*Test.";
62     final String JavaDoc grammarTest4 = "*Test";
63
64     // valid grammars that end with 'Ending'
65
final String JavaDoc grammarEnding1 = "*Ending.class";
66     final String JavaDoc grammarEnding2 = "*Ending.java";
67     final String JavaDoc grammarEnding3 = "*Ending.";
68     final String JavaDoc grammarEnding4 = "*Ending";
69
70     // begin tests
71
// test valid grammars that begin with 'Prefix'
72
assertTrue("Checking " + testPrefix1, MethodCollector.isTestClass(prefixFoo, testPrefix1));
73     assertTrue("Checking " + testPrefix2, MethodCollector.isTestClass(prefixFoo, testPrefix2));
74     assertTrue("Checking " + testPrefix3, MethodCollector.isTestClass(prefixFoo, testPrefix3));
75     assertTrue("Checking " + testPrefix4, MethodCollector.isTestClass(prefixFoo, testPrefix4));
76
77     // test valid grammars that begin with 'Test'
78
assertTrue("Checking " + testGrammar1, MethodCollector.isTestClass(testFoo, testGrammar1));
79     assertTrue("Checking " + testGrammar2, MethodCollector.isTestClass(testFoo, testGrammar2));
80     assertTrue("Checking " + testGrammar3, MethodCollector.isTestClass(testFoo, testGrammar3));
81     assertTrue("Checking " + testGrammar4, MethodCollector.isTestClass(testFoo, testGrammar4));
82
83     // test valid grammars that end with 'Test'
84
assertTrue("Checking " + grammarTest1, MethodCollector.isTestClass(fooTest, grammarTest1));
85     assertTrue("Checking " + grammarTest2, MethodCollector.isTestClass(fooTest, grammarTest2));
86     assertTrue("Checking " + grammarTest3, MethodCollector.isTestClass(fooTest, grammarTest3));
87     assertTrue("Checking " + grammarTest4, MethodCollector.isTestClass(fooTest, grammarTest4));
88
89     // test valid grammars that end with 'TestCase'
90
assertTrue("Checking " + grammarEnding1,
91             MethodCollector.isTestClass(fooEnding, grammarEnding1));
92     assertTrue("Checking " + grammarEnding2,
93             MethodCollector.isTestClass(fooEnding, grammarEnding2));
94     assertTrue("Checking " + grammarEnding3,
95             MethodCollector.isTestClass(fooEnding, grammarEnding3));
96     assertTrue("Checking " + grammarEnding4,
97             MethodCollector.isTestClass(fooEnding, grammarEnding4));
98
99     // test valid grammars with invalid class names
100
assertTrue("Checking invalid class name " + testFoo + " with " + grammarTest1,
101                !MethodCollector.isTestClass(testFoo, grammarTest1));
102     assertTrue("Checking invalid class name " + fooTest + " with " + testGrammar1,
103                !MethodCollector.isTestClass(fooTest, testGrammar1));
104   }
105
106   /**
107    * Tests the getJBlanketDir method.
108    *
109    * @throws Exception if error occurs.
110    */

111   public void testGetJBlanketDir() throws Exception JavaDoc {
112
113     String JavaDoc userDir = System.getProperty("user.home");
114     String JavaDoc defaultJBlanketDir = userDir + SLASH + "jblanket";
115     System.setProperty("jblanket.dir", "");
116     String JavaDoc jblanketDir = MethodCollector.getJBlanketDir();
117
118     // begin tests
119
assertEquals("Checking default JBlanket directory", defaultJBlanketDir, jblanketDir);
120     jblanketDir = System.getProperty("jblanket.dir");
121     assertEquals("Checking JBlanket directory value", defaultJBlanketDir, jblanketDir);
122   }
123
124   /**
125    * Tests the reconstructType method.
126    */

127   public void testReconstructType() {
128
129     // constant Pool type 'byte'
130
final String JavaDoc byteType = "B";
131     // constant Pool type 'char'
132
final String JavaDoc charType = "C";
133     // Constant Pool type 'double'
134
final String JavaDoc doubleType = "D";
135     // constant Pool type 'float'
136
final String JavaDoc floatType = "F";
137     // constant Pool type 'integer'
138
final String JavaDoc intType = "I";
139     // constant Pool type 'long'
140
final String JavaDoc longType = "J";
141     // constant Pool type 'short'
142
final String JavaDoc shortType = "S";
143     // constant Pool type 'boolean'
144
final String JavaDoc booleanType = "Z";
145     // constant Pool type 'java.util.List'
146
final String JavaDoc objectType = "Ljava/util/List;";
147     // constant Pool type 'double[][][]'
148
final String JavaDoc arrayType = "[[[D";
149     // constant Pool type 'csdl.jblanket.modify.MethodCollector[][]'
150
final String JavaDoc objectArrayType = "[[" + objectType;
151
152     // begin tests
153
assertEquals("checking 'byte' base type", "byte",
154                  MethodCollector.reconstructType(byteType));
155     assertEquals("checking 'char' base type", "char",
156                  MethodCollector.reconstructType(charType));
157     assertEquals("checking 'double' base type", "double",
158                  MethodCollector.reconstructType(doubleType));
159     assertEquals("checking 'float' base type", "float",
160                  MethodCollector.reconstructType(floatType));
161     assertEquals("checking 'int' base type", "int",
162                  MethodCollector.reconstructType(intType));
163     assertEquals("checking 'long' base type", "long",
164                  MethodCollector.reconstructType(longType));
165     assertEquals("checking 'short' base type", "short",
166                  MethodCollector.reconstructType(shortType));
167     assertEquals("checking 'boolean' base type", "boolean",
168                  MethodCollector.reconstructType(booleanType));
169     assertEquals("checking object type", "java.util.List",
170                  MethodCollector.reconstructType(objectType));
171     assertEquals("checking 3-D 'double' array type", "double[][][]",
172                  MethodCollector.reconstructType(arrayType));
173     assertEquals("checking 2-D object array type", "java.util.List[][]",
174                  MethodCollector.reconstructType(objectArrayType));
175   }
176
177   /**
178    * Tests the storeMethodData method.
179    *
180    * @throws Exception if error occurs.
181    */

182   public void testStoreMethodData() throws Exception JavaDoc {
183
184     // test MethodSet to store
185
MethodSet testSet = new MethodSet();
186
187     // directory holding data for testing
188
String JavaDoc testDir = System.getProperty("jblanket.testdir") + SLASH + "testmethodcollector";
189     // name of output XML file for output
190
final String JavaDoc xmlFile = "testMethodSet.xml";
191
192     // MethodInfo object with 2 parameters
193
MethodInfo methodInfo1;
194     // MethodInfo object with no parameters
195
MethodInfo methodInfo2;
196
197     // fully qualified class name for first class
198
final String JavaDoc class1 = "java.lang.String";
199     // fully qualified class name for second class
200
final String JavaDoc class2 = "java.lang.Boolean";
201     // fully qualified class name for third class
202
final String JavaDoc class3 = "foo.bar.Baz";
203     // fully qualified class name for fourth class
204
final String JavaDoc class4 = "foo.bar.Foo";
205
206     // name of first method
207
final String JavaDoc method1 = "getQux";
208     // name of second method
209
final String JavaDoc method2 = "setQux";
210
211     // creat MethodInfo instances.
212
List JavaDoc params = new ArrayList JavaDoc();
213     params.add(class1);
214     params.add(class2);
215     methodInfo1 = new MethodInfo(class3, method1, params);
216     methodInfo2 = new MethodInfo(class4, method2, new ArrayList JavaDoc());
217
218     // load test MethodSet
219
testSet.add(methodInfo1);
220     testSet.add(methodInfo2);
221
222     // create output directory
223
File JavaDoc outDir = new File JavaDoc(testDir);
224     if (!outDir.exists()) {
225       outDir.mkdirs();
226     }
227
228     // begin test
229
try {
230       MethodCollector.storeMethodData(testSet, testDir + SLASH + xmlFile, class3, new Date JavaDoc());
231     }
232     catch (Exception JavaDoc e) {
233       fail("Checking storeMethodData method");
234     }
235   }
236   
237   /**
238    * Tests the removePackagePrefix method.
239    */

240   public void testRemovePackagePrefix() {
241     // fully qualified class name for String class
242
final String JavaDoc string = "java.lang.String";
243     assertEquals("Checking removePackagePrefix method", "String",
244                  MethodCollector.removePackagePrefix(string));
245   }
246 }
247
248
Popular Tags