001: package csdl.jblanket.modifier;
002:
003: import csdl.jblanket.methodset.MethodInfo;
004: import csdl.jblanket.methodset.MethodSet;
005:
006: import java.io.File;
007: import java.util.ArrayList;
008: import java.util.Date;
009: import java.util.List;
010:
011: import junit.framework.TestCase;
012:
013: /**
014: * Tests the MethodCollector class.
015: *
016: * @author Joy M. Agustin
017: * @version $Id: TestMethodCollector.java,v 1.1 2004/11/07 00:32:30 timshadel Exp $
018: */
019: public class TestMethodCollector extends TestCase {
020:
021: /** Directory separator */
022: private static final String SLASH = File.separator;
023:
024: /**
025: * Constructor required by JUnit.
026: *
027: * @param name the name of this test class.
028: */
029: public TestMethodCollector(String name) {
030: super (name);
031: }
032:
033: /**
034: * Tests the isTestClass method.
035: *
036: * @throws Exception if an error occurs.
037: */
038: public void testIsTestClass() throws Exception {
039:
040: // Test class names
041: final String prefixFoo = "Prefixfoo";
042: final String testFoo = "Testfoo";
043: final String fooTest = "fooTest";
044: final String fooEnding = "fooEnding";
045:
046: // valid grammars that begin with 'Prefix'
047: final String testPrefix1 = "Prefix*.class";
048: final String testPrefix2 = "Prefix*.java";
049: final String testPrefix3 = "Prefix*.";
050: final String testPrefix4 = "Prefix*";
051:
052: // valid grammars that begin with 'Test'
053: final String testGrammar1 = "Test*.class";
054: final String testGrammar2 = "Test*.java";
055: final String testGrammar3 = "Test*.";
056: final String testGrammar4 = "Test*";
057:
058: // valid grammars that end with 'Test'
059: final String grammarTest1 = "*Test.class";
060: final String grammarTest2 = "*Test.java";
061: final String grammarTest3 = "*Test.";
062: final String grammarTest4 = "*Test";
063:
064: // valid grammars that end with 'Ending'
065: final String grammarEnding1 = "*Ending.class";
066: final String grammarEnding2 = "*Ending.java";
067: final String grammarEnding3 = "*Ending.";
068: final String grammarEnding4 = "*Ending";
069:
070: // begin tests
071: // test valid grammars that begin with 'Prefix'
072: assertTrue("Checking " + testPrefix1, MethodCollector
073: .isTestClass(prefixFoo, testPrefix1));
074: assertTrue("Checking " + testPrefix2, MethodCollector
075: .isTestClass(prefixFoo, testPrefix2));
076: assertTrue("Checking " + testPrefix3, MethodCollector
077: .isTestClass(prefixFoo, testPrefix3));
078: assertTrue("Checking " + testPrefix4, MethodCollector
079: .isTestClass(prefixFoo, testPrefix4));
080:
081: // test valid grammars that begin with 'Test'
082: assertTrue("Checking " + testGrammar1, MethodCollector
083: .isTestClass(testFoo, testGrammar1));
084: assertTrue("Checking " + testGrammar2, MethodCollector
085: .isTestClass(testFoo, testGrammar2));
086: assertTrue("Checking " + testGrammar3, MethodCollector
087: .isTestClass(testFoo, testGrammar3));
088: assertTrue("Checking " + testGrammar4, MethodCollector
089: .isTestClass(testFoo, testGrammar4));
090:
091: // test valid grammars that end with 'Test'
092: assertTrue("Checking " + grammarTest1, MethodCollector
093: .isTestClass(fooTest, grammarTest1));
094: assertTrue("Checking " + grammarTest2, MethodCollector
095: .isTestClass(fooTest, grammarTest2));
096: assertTrue("Checking " + grammarTest3, MethodCollector
097: .isTestClass(fooTest, grammarTest3));
098: assertTrue("Checking " + grammarTest4, MethodCollector
099: .isTestClass(fooTest, grammarTest4));
100:
101: // test valid grammars that end with 'TestCase'
102: assertTrue("Checking " + grammarEnding1, MethodCollector
103: .isTestClass(fooEnding, grammarEnding1));
104: assertTrue("Checking " + grammarEnding2, MethodCollector
105: .isTestClass(fooEnding, grammarEnding2));
106: assertTrue("Checking " + grammarEnding3, MethodCollector
107: .isTestClass(fooEnding, grammarEnding3));
108: assertTrue("Checking " + grammarEnding4, MethodCollector
109: .isTestClass(fooEnding, grammarEnding4));
110:
111: // test valid grammars with invalid class names
112: assertTrue("Checking invalid class name " + testFoo + " with "
113: + grammarTest1, !MethodCollector.isTestClass(testFoo,
114: grammarTest1));
115: assertTrue("Checking invalid class name " + fooTest + " with "
116: + testGrammar1, !MethodCollector.isTestClass(fooTest,
117: testGrammar1));
118: }
119:
120: /**
121: * Tests the getJBlanketDir method.
122: *
123: * @throws Exception if error occurs.
124: */
125: public void testGetJBlanketDir() throws Exception {
126:
127: String userDir = System.getProperty("user.home");
128: String defaultJBlanketDir = userDir + SLASH + "jblanket";
129: System.setProperty("jblanket.dir", "");
130: String jblanketDir = MethodCollector.getJBlanketDir();
131:
132: // begin tests
133: assertEquals("Checking default JBlanket directory",
134: defaultJBlanketDir, jblanketDir);
135: jblanketDir = System.getProperty("jblanket.dir");
136: assertEquals("Checking JBlanket directory value",
137: defaultJBlanketDir, jblanketDir);
138: }
139:
140: /**
141: * Tests the reconstructType method.
142: */
143: public void testReconstructType() {
144:
145: // constant Pool type 'byte'
146: final String byteType = "B";
147: // constant Pool type 'char'
148: final String charType = "C";
149: // Constant Pool type 'double'
150: final String doubleType = "D";
151: // constant Pool type 'float'
152: final String floatType = "F";
153: // constant Pool type 'integer'
154: final String intType = "I";
155: // constant Pool type 'long'
156: final String longType = "J";
157: // constant Pool type 'short'
158: final String shortType = "S";
159: // constant Pool type 'boolean'
160: final String booleanType = "Z";
161: // constant Pool type 'java.util.List'
162: final String objectType = "Ljava/util/List;";
163: // constant Pool type 'double[][][]'
164: final String arrayType = "[[[D";
165: // constant Pool type 'csdl.jblanket.modify.MethodCollector[][]'
166: final String objectArrayType = "[[" + objectType;
167:
168: // begin tests
169: assertEquals("checking 'byte' base type", "byte",
170: MethodCollector.reconstructType(byteType));
171: assertEquals("checking 'char' base type", "char",
172: MethodCollector.reconstructType(charType));
173: assertEquals("checking 'double' base type", "double",
174: MethodCollector.reconstructType(doubleType));
175: assertEquals("checking 'float' base type", "float",
176: MethodCollector.reconstructType(floatType));
177: assertEquals("checking 'int' base type", "int", MethodCollector
178: .reconstructType(intType));
179: assertEquals("checking 'long' base type", "long",
180: MethodCollector.reconstructType(longType));
181: assertEquals("checking 'short' base type", "short",
182: MethodCollector.reconstructType(shortType));
183: assertEquals("checking 'boolean' base type", "boolean",
184: MethodCollector.reconstructType(booleanType));
185: assertEquals("checking object type", "java.util.List",
186: MethodCollector.reconstructType(objectType));
187: assertEquals("checking 3-D 'double' array type",
188: "double[][][]", MethodCollector
189: .reconstructType(arrayType));
190: assertEquals("checking 2-D object array type",
191: "java.util.List[][]", MethodCollector
192: .reconstructType(objectArrayType));
193: }
194:
195: /**
196: * Tests the storeMethodData method.
197: *
198: * @throws Exception if error occurs.
199: */
200: public void testStoreMethodData() throws Exception {
201:
202: // test MethodSet to store
203: MethodSet testSet = new MethodSet();
204:
205: // directory holding data for testing
206: String testDir = System.getProperty("jblanket.testdir") + SLASH
207: + "testmethodcollector";
208: // name of output XML file for output
209: final String xmlFile = "testMethodSet.xml";
210:
211: // MethodInfo object with 2 parameters
212: MethodInfo methodInfo1;
213: // MethodInfo object with no parameters
214: MethodInfo methodInfo2;
215:
216: // fully qualified class name for first class
217: final String class1 = "java.lang.String";
218: // fully qualified class name for second class
219: final String class2 = "java.lang.Boolean";
220: // fully qualified class name for third class
221: final String class3 = "foo.bar.Baz";
222: // fully qualified class name for fourth class
223: final String class4 = "foo.bar.Foo";
224:
225: // name of first method
226: final String method1 = "getQux";
227: // name of second method
228: final String method2 = "setQux";
229:
230: // creat MethodInfo instances.
231: List params = new ArrayList();
232: params.add(class1);
233: params.add(class2);
234: methodInfo1 = new MethodInfo(class3, method1, params);
235: methodInfo2 = new MethodInfo(class4, method2, new ArrayList());
236:
237: // load test MethodSet
238: testSet.add(methodInfo1);
239: testSet.add(methodInfo2);
240:
241: // create output directory
242: File outDir = new File(testDir);
243: if (!outDir.exists()) {
244: outDir.mkdirs();
245: }
246:
247: // begin test
248: try {
249: MethodCollector.storeMethodData(testSet, testDir + SLASH
250: + xmlFile, class3, new Date());
251: } catch (Exception e) {
252: fail("Checking storeMethodData method");
253: }
254: }
255:
256: /**
257: * Tests the removePackagePrefix method.
258: */
259: public void testRemovePackagePrefix() {
260: // fully qualified class name for String class
261: final String string = "java.lang.String";
262: assertEquals("Checking removePackagePrefix method", "String",
263: MethodCollector.removePackagePrefix(string));
264: }
265: }
|