001: package csdl.jblanket.modifier;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005: import java.io.File;
006: import java.text.ParseException;
007:
008: import junit.framework.TestCase;
009:
010: /**
011: * Tests the Modifier class.
012: *
013: * @author Joy M. Agustin
014: * @version $Id: TestModifier.java,v 1.2 2005/02/19 05:55:19 timshadel Exp $
015: */
016: public class TestModifier extends TestCase {
017:
018: /** Modifier instance used for testing */
019: private Modifier modifier;
020:
021: /** Directory all testing output files should be stored in */
022: private final String testDir = System
023: .getProperty("jblanket.testdir");
024: /** Relative output directory for this test case */
025: private final String myTestDir = "testmodifier";
026: /** Directory separator */
027: private final String slash = File.separator;
028: /** Grammar for testing */
029: private final String testGrammar = "Test*.class";
030:
031: /** Describes if one-line methods should be excluded from the coverage measurement */
032: private final boolean excludeOneLineMethods = true;
033: /** Describes if constructors should be excluded from the coverage measurement */
034: private final boolean excludeConstructors = true;
035:
036: /** Name of total file for testing */
037: private final String totalFile = "mytotalMethods.xml";
038: /** Name of excluded file for testing */
039: private final String excludedFile = "myExcludedMethods.xml";
040: /** Name of one line file for testing */
041: private final String oneLineFile = "myOneLineMethods.xml";
042: /** Name of untestable file for testing */
043: private final String untestableFile = "myUntestableFile.xml";
044:
045: /** Package prefix for testing */
046: private ArrayList packagePrefixes = new ArrayList();
047:
048: /**
049: * Required by Junit.
050: *
051: * @param name Test case name.
052: */
053: public TestModifier(String name) {
054: super (name);
055: }
056:
057: /**
058: * Sets up instance variables for testing.
059: *
060: * @throws Exception if error while creating Modifier instance.
061: */
062: public void setUp() throws Exception {
063:
064: // add values to packagePrefix
065: this .packagePrefixes = new ArrayList();
066: this .packagePrefixes.add("csdl.jblanket.test.");
067:
068: // initialize modifier instance variable
069: this .modifier = new Modifier(false, this .testGrammar,
070: this .excludeOneLineMethods, this .excludeConstructors,
071: false, this .packagePrefixes);
072: }
073:
074: /**
075: * Tests the isValidTestGrammar method.
076: *
077: * @throws Exception if errors occur while deleting directories.
078: */
079: public void testIsValidTestGrammar() throws Exception {
080:
081: // Test class names
082: final String testFoo = "Testfoo";
083: final String fooTest = "fooTest";
084:
085: // valid grammars that begin with 'Prefix'
086: final String testPrefix1 = "Prefix*.class";
087: final String testPrefix2 = "Prefix*.java";
088: final String testPrefix3 = "Prefix*.";
089: final String testPrefix4 = "Prefix*";
090:
091: // valid grammars that begin with 'Test'
092: final String testGrammar1 = "Test*.class";
093: final String testGrammar2 = "Test*.java";
094: final String testGrammar3 = "Test*.";
095: final String testGrammar4 = "Test*";
096:
097: // valid grammars that end with 'Test'
098: final String grammarTest1 = "*Test.class";
099: final String grammarTest2 = "*Test.java";
100: final String grammarTest3 = "*Test.";
101: final String grammarTest4 = "*Test";
102:
103: // valid grammars that end with 'Ending'
104: final String grammarEnding1 = "*Ending.class";
105: final String grammarEnding2 = "*Ending.java";
106: final String grammarEnding3 = "*Ending.";
107: final String grammarEnding4 = "*Ending";
108:
109: // invalid grammars that do not begin with 'Test'
110: final String invalidTestGrammar1 = "FooTest%.class";
111: final String invalidTestGrammar2 = "FooTest%.java";
112: final String invalidTestGrammar3 = "FooTest%.";
113: final String invalidTestGrammar4 = "FooTest%";
114: final String invalidTestGrammar5 = "FooTest*.j";
115: final String invalidTestGrammar6 = "Foo.Test*.j";
116: final String invalidTestGrammar7 = "test%";
117:
118: // invalid grammars that do not end with 'Test'
119: final String invalidGrammarTest1 = "%TestFoo.class";
120: final String invalidGrammarTest2 = "%TestFoo.java";
121: final String invalidGrammarTest3 = "%TestFoo.";
122: final String invalidGrammarTest4 = "%TestFoo";
123: final String invalidGrammarTest5 = "*TestFoo.j";
124: final String invalidGrammarTest6 = "*Test.Foo.j";
125: final String invalidGrammarTest7 = "%test";
126:
127: // begin tests
128: // test valid grammars that begin with 'Prefix'
129: assertTrue("Checking " + testPrefix1, modifier
130: .isValidTestGrammar(testPrefix1));
131: assertTrue("Checking " + testPrefix2, modifier
132: .isValidTestGrammar(testPrefix2));
133: assertTrue("Checking " + testPrefix3, modifier
134: .isValidTestGrammar(testPrefix3));
135: assertTrue("Checking " + testPrefix4, modifier
136: .isValidTestGrammar(testPrefix4));
137:
138: // test valid grammars that begin with 'Test'
139: assertTrue("Checking " + testGrammar1, modifier
140: .isValidTestGrammar(testGrammar1));
141: assertTrue("Checking " + testGrammar2, modifier
142: .isValidTestGrammar(testGrammar2));
143: assertTrue("Checking " + testGrammar3, modifier
144: .isValidTestGrammar(testGrammar3));
145: assertTrue("Checking " + testGrammar4, modifier
146: .isValidTestGrammar(testGrammar4));
147:
148: // test valid grammars that end with 'Test'
149: assertTrue("Checking " + grammarTest1, modifier
150: .isValidTestGrammar(grammarTest1));
151: assertTrue("Checking " + grammarTest2, modifier
152: .isValidTestGrammar(grammarTest2));
153: assertTrue("Checking " + grammarTest3, modifier
154: .isValidTestGrammar(grammarTest3));
155: assertTrue("Checking " + grammarTest4, modifier
156: .isValidTestGrammar(grammarTest4));
157:
158: // test valid grammars that end with 'TestCase'
159: assertTrue("Checking " + grammarEnding1, modifier
160: .isValidTestGrammar(grammarEnding1));
161: assertTrue("Checking " + grammarEnding2, modifier
162: .isValidTestGrammar(grammarEnding2));
163: assertTrue("Checking " + grammarEnding3, modifier
164: .isValidTestGrammar(grammarEnding3));
165: assertTrue("Checking " + grammarEnding4, modifier
166: .isValidTestGrammar(grammarEnding4));
167:
168: // test invalid grammars that do not begin with 'Test'
169: try {
170: assertTrue("Checking invalid grammar "
171: + invalidTestGrammar1, modifier
172: .isValidTestGrammar(invalidTestGrammar1));
173: fail("Passed invalid grammar " + invalidTestGrammar1);
174: } catch (ParseException e) {
175: // Do nothing, test passed
176: }
177:
178: try {
179: modifier.isValidTestGrammar(invalidTestGrammar2);
180: fail("Passed invalid grammar " + invalidTestGrammar2);
181: } catch (ParseException e) {
182: // Do nothing, test passed
183: }
184:
185: try {
186: modifier.isValidTestGrammar(invalidTestGrammar3);
187: fail("Passed invalid grammar " + invalidTestGrammar3);
188: } catch (ParseException e) {
189: // Do nothing, test passed
190: }
191:
192: try {
193: modifier.isValidTestGrammar(invalidTestGrammar4);
194: fail("Passed invalid grammar " + invalidTestGrammar4);
195: } catch (ParseException e) {
196: // Do nothing, test passed
197: }
198:
199: try {
200: modifier.isValidTestGrammar(invalidTestGrammar5);
201: fail("Passed invalid grammar " + invalidTestGrammar5);
202: } catch (ParseException e) {
203: // Do nothing, test passed
204: }
205:
206: try {
207: modifier.isValidTestGrammar(invalidTestGrammar6);
208: fail("Passed invalid grammar " + invalidTestGrammar6);
209: } catch (ParseException e) {
210: // Do nothing, test passed
211: }
212:
213: try {
214: modifier.isValidTestGrammar(invalidTestGrammar7);
215: fail("Passed invalid grammar " + invalidTestGrammar7);
216: } catch (ParseException e) {
217: // Do nothing, test passed
218: }
219:
220: // test invalid grammars that do not end with 'Test'
221: try {
222: modifier.isValidTestGrammar(invalidGrammarTest1);
223: fail("Passed invalid grammar " + invalidGrammarTest1);
224: } catch (ParseException e) {
225: // Do nothing, test passed
226: }
227:
228: try {
229: modifier.isValidTestGrammar(invalidGrammarTest2);
230: fail("Passed invalid grammar " + invalidGrammarTest2);
231: } catch (ParseException e) {
232: // Do nothing, test passed
233: }
234:
235: try {
236: modifier.isValidTestGrammar(invalidGrammarTest3);
237: fail("Passed invalid grammar " + invalidGrammarTest3);
238: } catch (ParseException e) {
239: // Do nothing, test passed
240: }
241:
242: try {
243: modifier.isValidTestGrammar(invalidGrammarTest4);
244: fail("Passed invalid grammar " + invalidGrammarTest4);
245: } catch (ParseException e) {
246: // Do nothing, test passed
247: }
248:
249: try {
250: modifier.isValidTestGrammar(invalidGrammarTest5);
251: fail("Passed invalid grammar " + invalidGrammarTest5);
252: } catch (ParseException e) {
253: // Do nothing, test passed
254: }
255:
256: try {
257: modifier.isValidTestGrammar(invalidGrammarTest6);
258: fail("Passed invalid grammar " + invalidGrammarTest6);
259: } catch (ParseException e) {
260: // Do nothing, test passed
261: }
262:
263: try {
264: modifier.isValidTestGrammar(invalidGrammarTest7);
265: fail("Passed invalid grammar " + invalidGrammarTest7);
266: } catch (ParseException e) {
267: // Do nothing, test passed
268: }
269: }
270:
271: /**
272: * Tests the deleteDirectory method.
273: *
274: * @throws Exception if errors occur while deleting directories.
275: */
276: public void testDeleteDirectory() throws Exception {
277:
278: // fake directory to create
279: final String fakeDir = "first" + slash + "second" + slash
280: + "third";
281:
282: // set directory and subdirectories
283: File dir = new File(testDir + slash + myTestDir);
284: File subdir = new File(dir, "first");
285: File subdirs = new File(dir, fakeDir);
286:
287: // create directory
288: subdirs.mkdirs();
289:
290: // delete myTestDir and its subdirectories
291: this .modifier.deleteDirectory(subdir);
292:
293: // check if any subdirectories exist
294: String[] existingSubdirs = dir.list();
295: assertTrue("checking existence of subdirectories",
296: existingSubdirs.length == 0);
297: }
298:
299: /**
300: * Tests the toArrayList method.
301: */
302: public void testToArrayList() {
303:
304: String rawList = "a;b c;d:e;f;";
305: List list = Modifier.stringToArrayList(rawList);
306: assertEquals("checking size of array list", 4, list.size());
307:
308: // checking individual items in the list
309: assertEquals("checking array list", "a", list.get(0));
310: assertEquals("checking array list", "b c", list.get(1));
311: assertEquals("checking array list", "d:e", list.get(2));
312: assertEquals("checking array list", "f", list.get(3));
313: }
314: }
|