001: package csdl.jblanket.methodset;
002:
003: import csdl.jblanket.modifier.MethodCollector;
004:
005: import java.io.File;
006: import java.io.FileOutputStream;
007: import java.io.FileInputStream;
008: import java.util.ArrayList;
009: import java.util.Date;
010: import java.util.List;
011:
012: import junit.framework.TestCase;
013: import junit.framework.TestSuite;
014: import junit.textui.TestRunner;
015:
016: /**
017: * Tests operation of the MethodSet ADT.
018: * <p>
019: * If using Ant to execute this test class a 'jblanket.testdir' system property needs to be set
020: * in the build.xml file. If running test class from command line, must provide -D argument with
021: * 'jblanket.testdir' to set the system property. This value is used in the testFileOperations
022: * method.
023: *
024: * @author Joy M. Agustin
025: * @version $Id: TestMethodSet.java,v 1.1 2004/11/07 00:32:40 timshadel Exp $
026: */
027: public class TestMethodSet extends TestCase {
028:
029: /** Directory holding data for testing */
030: private String testDir;
031: /** Name of output XML file for testing file operations */
032: private final String xmlFile = "testMethodSet.xml";
033: /** MethodSet object with both MethodInfo objects */
034: private MethodSet methodSet1;
035: /** MethodSet object with both MethodInfo objects */
036: private MethodSet methodSet2;
037: /** MethodSet object with methodInfo1 */
038: private MethodSet methodSet3;
039: /** MethodSet object with methodInfo2 */
040: private MethodSet methodSet4;
041: /** MethodSet object with no MethodInfo objects */
042: private MethodSet methodSet5;
043: /** MethodInfo object with 2 parameters */
044: private MethodInfo methodInfo1;
045: /** MethodInfo object with no parameters */
046: private MethodInfo methodInfo2;
047: /** Fully qualified class name for first class */
048: private final String class1 = "java.lang.String";
049: /** Fully qualified class name for second class */
050: private final String class2 = "java.lang.Boolean";
051: /** Fully qualified class name for third class */
052: private final String class3 = "foo.bar.Baz";
053: /** Fully qualified class name for fourth class */
054: private final String class4 = "foo.bar.Foo";
055: /** Name of first method */
056: private final String method1 = "getQux";
057: /** Name of second method */
058: private final String method2 = "setQux";
059:
060: /**
061: * Required for JUnit.
062: *
063: * @param name Test case name.
064: */
065: public TestMethodSet(String name) {
066: super (name);
067: }
068:
069: /**
070: * Sets up variables for testing.
071: */
072: public void setUp() {
073: // Create MethodInfo instances.
074: List params = new ArrayList();
075: params.add(class1);
076: params.add(class2);
077: methodInfo1 = new MethodInfo(class3, method1, params);
078:
079: methodInfo2 = new MethodInfo(class4, method2, new ArrayList());
080:
081: // Create MethodSets instances
082: methodSet1 = new MethodSet();
083: methodSet1.add(methodInfo1);
084: methodSet1.add(methodInfo2);
085:
086: methodSet2 = new MethodSet();
087: methodSet2.add(methodInfo1);
088: methodSet2.add(methodInfo2);
089:
090: methodSet3 = new MethodSet();
091: methodSet3.add(methodInfo1);
092:
093: methodSet4 = new MethodSet();
094: methodSet4.add(methodInfo2);
095:
096: methodSet5 = new MethodSet();
097: }
098:
099: /**
100: * Tests the basic functions add, remove, size, and equals methods from the MethodSet class.
101: *
102: * @throws Exception If problems occur.
103: */
104: public void testBasicFunctions() throws Exception {
105:
106: assertTrue("Checking that the methodset has 2 elements.",
107: methodSet1.size() == 2);
108:
109: // Try adding method1 again and making sure it's there.
110: assertTrue("Checking presence of method.", !methodSet1
111: .add(methodInfo1));
112:
113: // Try removing method1 and making sure it's gone.
114: methodSet1.remove(methodInfo1);
115: assertTrue("Checking absence of method.", methodSet1
116: .add(methodInfo1));
117:
118: // Try checking that methodSet1 is back to the way it was.
119: assertTrue("Checking two methodsets for equality.", methodSet1
120: .equals(methodSet2));
121: }
122:
123: /**
124: * Tests store and load methods from the MethodSet class.
125: *
126: * @throws Exception If problems occur.
127: */
128: public void testFileOperations() throws Exception {
129:
130: // If using Ant, this is set automatically in build.xml.
131: // If running from command line, must provide -D argument.
132: testDir = System.getProperty("jblanket.testdir");
133: assertNotNull("Checking for presence of jblanket.testdir.",
134: testDir);
135:
136: Date now = new Date();
137:
138: // Write out the methodset.
139: File testFile = new File(testDir, xmlFile);
140: FileOutputStream fostream = new FileOutputStream(testFile);
141: methodSet1.store(fostream, null, now);
142:
143: // Read in the method set to a new container.
144: FileInputStream inStream = new FileInputStream(testFile);
145: Date later = methodSet5.load(inStream);
146: assertTrue("Checking that loaded is same as stored.",
147: methodSet1.equals(methodSet5));
148:
149: // Unformatted date is in milliseconds, so must check formatted date (to nearest second).
150: assertEquals(
151: "Checking that formatted date stored is same as formatted date retrieved.",
152: MethodCollector.getDateFormat().format(now),
153: MethodCollector.getDateFormat().format(later));
154: }
155:
156: /**
157: * Tests the difference method from the MethodSet class.
158: *
159: * @throws Exception If problems occur.
160: */
161: public void testDifference() throws Exception {
162:
163: methodSet1.difference(methodSet5);
164: assertTrue("Checking difference with empty methodset.",
165: methodSet1.equals(methodSet2));
166:
167: methodSet1.difference(methodSet4);
168: assertTrue("Checking difference has 1 method.", methodSet1
169: .equals(methodSet3));
170:
171: methodSet1.difference(methodSet1);
172: assertTrue("Checking difference has no methods.", methodSet1
173: .equals(methodSet5));
174: }
175:
176: /**
177: * Tests the intersection method from the MethodSet class.
178: *
179: * @throws Exception If problems occur.
180: */
181: public void testIntersection() throws Exception {
182:
183: MethodSet methodSet6 = new MethodSet();
184: methodSet6.intersection(methodSet1);
185: assertTrue("Checking intersection of empty methodset.",
186: methodSet6.equals(methodSet5));
187:
188: methodSet6.intersection(methodSet3);
189: assertTrue("Checking intersection with empty and 1 method.",
190: methodSet6.equals(methodSet5));
191:
192: methodSet1.intersection(methodSet1);
193: assertTrue("Checking intersection wtih 2 methods each.",
194: methodSet1.equals(methodSet2));
195: }
196:
197: /**
198: * Tests the union method from the MethodSet class.
199: *
200: * @throws Exception If problems occur.
201: */
202: public void testUnion() throws Exception {
203:
204: MethodSet methodSet6 = new MethodSet();
205: methodSet6.union(methodSet5);
206: assertTrue("Checking union of empty methodset.", methodSet6
207: .equals(methodSet5));
208:
209: methodSet6.union(methodSet3);
210: assertTrue("Checking union of empty and 1 method.", methodSet6
211: .equals(methodSet3));
212:
213: methodSet6.union(methodSet3);
214: assertTrue("Checking union of 1 method each.", methodSet6
215: .equals(methodSet3));
216: }
217:
218: /**
219: * Provide stand-alone execution of this test case during initial development.
220: *
221: * @param args The command line arguments
222: */
223: public static void main(String[] args) {
224: System.out.println("JUnit testing MethodSet.");
225: //Runs all no-arg methods starting with "test".
226: TestRunner.run(new TestSuite(TestMethodSet.class));
227: }
228: }
|