KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > csdl > jblanket > methodset > TestMethodSet


1 package csdl.jblanket.methodset;
2
3 import csdl.jblanket.modifier.MethodCollector;
4
5 import java.io.File JavaDoc;
6 import java.io.FileOutputStream JavaDoc;
7 import java.io.FileInputStream JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Date JavaDoc;
10 import java.util.List JavaDoc;
11
12 import junit.framework.TestCase;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15
16 /**
17  * Tests operation of the MethodSet ADT.
18  * <p>
19  * If using Ant to execute this test class a 'jblanket.testdir' system property needs to be set
20  * in the build.xml file. If running test class from command line, must provide -D argument with
21  * 'jblanket.testdir' to set the system property. This value is used in the testFileOperations
22  * method.
23  *
24  * @author Joy M. Agustin
25  * @version $Id: TestMethodSet.java,v 1.1 2004/11/07 00:32:40 timshadel Exp $
26  */

27 public class TestMethodSet extends TestCase {
28
29   /** Directory holding data for testing */
30   private String JavaDoc testDir;
31   /** Name of output XML file for testing file operations */
32   private final String JavaDoc xmlFile = "testMethodSet.xml";
33   /** MethodSet object with both MethodInfo objects */
34   private MethodSet methodSet1;
35   /** MethodSet object with both MethodInfo objects */
36   private MethodSet methodSet2;
37   /** MethodSet object with methodInfo1 */
38   private MethodSet methodSet3;
39   /** MethodSet object with methodInfo2 */
40   private MethodSet methodSet4;
41   /** MethodSet object with no MethodInfo objects */
42   private MethodSet methodSet5;
43   /** MethodInfo object with 2 parameters */
44   private MethodInfo methodInfo1;
45   /** MethodInfo object with no parameters */
46   private MethodInfo methodInfo2;
47   /** Fully qualified class name for first class */
48   private final String JavaDoc class1 = "java.lang.String";
49   /** Fully qualified class name for second class */
50   private final String JavaDoc class2 = "java.lang.Boolean";
51   /** Fully qualified class name for third class */
52   private final String JavaDoc class3 = "foo.bar.Baz";
53   /** Fully qualified class name for fourth class */
54   private final String JavaDoc class4 = "foo.bar.Foo";
55   /** Name of first method */
56   private final String JavaDoc method1 = "getQux";
57   /** Name of second method */
58   private final String JavaDoc method2 = "setQux";
59
60   /**
61    * Required for JUnit.
62    *
63    * @param name Test case name.
64    */

65   public TestMethodSet(String JavaDoc name) {
66     super(name);
67   }
68
69   /**
70    * Sets up variables for testing.
71    */

72   public void setUp() {
73     // Create MethodInfo instances.
74
List JavaDoc params = new ArrayList JavaDoc();
75     params.add(class1);
76     params.add(class2);
77     methodInfo1 = new MethodInfo(class3, method1, params);
78
79     methodInfo2 = new MethodInfo(class4, method2, new ArrayList JavaDoc());
80
81     // Create MethodSets instances
82
methodSet1 = new MethodSet();
83     methodSet1.add(methodInfo1);
84     methodSet1.add(methodInfo2);
85
86     methodSet2 = new MethodSet();
87     methodSet2.add(methodInfo1);
88     methodSet2.add(methodInfo2);
89
90     methodSet3 = new MethodSet();
91     methodSet3.add(methodInfo1);
92
93     methodSet4 = new MethodSet();
94     methodSet4.add(methodInfo2);
95
96
97     methodSet5 = new MethodSet();
98   }
99
100   /**
101    * Tests the basic functions add, remove, size, and equals methods from the MethodSet class.
102    *
103    * @throws Exception If problems occur.
104    */

105   public void testBasicFunctions() throws Exception JavaDoc {
106
107     assertTrue("Checking that the methodset has 2 elements.", methodSet1.size() == 2);
108
109     // Try adding method1 again and making sure it's there.
110
assertTrue("Checking presence of method.", !methodSet1.add(methodInfo1));
111
112     // Try removing method1 and making sure it's gone.
113
methodSet1.remove(methodInfo1);
114     assertTrue("Checking absence of method.", methodSet1.add(methodInfo1));
115
116     // Try checking that methodSet1 is back to the way it was.
117
assertTrue("Checking two methodsets for equality.", methodSet1.equals(methodSet2));
118   }
119
120   /**
121    * Tests store and load methods from the MethodSet class.
122    *
123    * @throws Exception If problems occur.
124    */

125   public void testFileOperations() throws Exception JavaDoc {
126
127     // If using Ant, this is set automatically in build.xml.
128
// If running from command line, must provide -D argument.
129
testDir = System.getProperty("jblanket.testdir");
130     assertNotNull("Checking for presence of jblanket.testdir.", testDir);
131
132     Date JavaDoc now = new Date JavaDoc();
133
134     // Write out the methodset.
135
File JavaDoc testFile = new File JavaDoc(testDir, xmlFile);
136     FileOutputStream JavaDoc fostream = new FileOutputStream JavaDoc(testFile);
137     methodSet1.store(fostream, null, now);
138
139     // Read in the method set to a new container.
140
FileInputStream JavaDoc inStream = new FileInputStream JavaDoc(testFile);
141     Date JavaDoc later = methodSet5.load(inStream);
142     assertTrue("Checking that loaded is same as stored.", methodSet1.equals(methodSet5));
143
144     // Unformatted date is in milliseconds, so must check formatted date (to nearest second).
145
assertEquals("Checking that formatted date stored is same as formatted date retrieved.",
146                  MethodCollector.getDateFormat().format(now),
147                  MethodCollector.getDateFormat().format(later));
148   }
149
150   /**
151    * Tests the difference method from the MethodSet class.
152    *
153    * @throws Exception If problems occur.
154    */

155   public void testDifference() throws Exception JavaDoc {
156
157     methodSet1.difference(methodSet5);
158     assertTrue("Checking difference with empty methodset.", methodSet1.equals(methodSet2));
159
160     methodSet1.difference(methodSet4);
161     assertTrue("Checking difference has 1 method.", methodSet1.equals(methodSet3));
162
163     methodSet1.difference(methodSet1);
164     assertTrue("Checking difference has no methods.", methodSet1.equals(methodSet5));
165   }
166
167   /**
168    * Tests the intersection method from the MethodSet class.
169    *
170    * @throws Exception If problems occur.
171    */

172   public void testIntersection() throws Exception JavaDoc {
173
174     MethodSet methodSet6 = new MethodSet();
175     methodSet6.intersection(methodSet1);
176     assertTrue("Checking intersection of empty methodset.", methodSet6.equals(methodSet5));
177
178     methodSet6.intersection(methodSet3);
179     assertTrue("Checking intersection with empty and 1 method.", methodSet6.equals(methodSet5));
180
181     methodSet1.intersection(methodSet1);
182     assertTrue("Checking intersection wtih 2 methods each.", methodSet1.equals(methodSet2));
183   }
184
185   /**
186    * Tests the union method from the MethodSet class.
187    *
188    * @throws Exception If problems occur.
189    */

190   public void testUnion() throws Exception JavaDoc {
191
192     MethodSet methodSet6 = new MethodSet();
193     methodSet6.union(methodSet5);
194     assertTrue("Checking union of empty methodset.", methodSet6.equals(methodSet5));
195
196     methodSet6.union(methodSet3);
197     assertTrue("Checking union of empty and 1 method.", methodSet6.equals(methodSet3));
198
199     methodSet6.union(methodSet3);
200     assertTrue("Checking union of 1 method each.", methodSet6.equals(methodSet3));
201   }
202
203   /**
204    * Provide stand-alone execution of this test case during initial development.
205    *
206    * @param args The command line arguments
207    */

208   public static void main(String JavaDoc[] args) {
209     System.out.println("JUnit testing MethodSet.");
210     //Runs all no-arg methods starting with "test".
211
TestRunner.run(new TestSuite(TestMethodSet.class));
212   }
213 }
214
Popular Tags