001: package net.sf.mockcreator.utils;
002:
003: import java.util.List;
004:
005: import net.sf.mockcreator.TestCase;
006:
007: public class ArgumentParserTest extends TestCase {
008:
009: String[] args;
010: private ArgumentParser ap;
011:
012: public ArgumentParserTest(String name) {
013: super (name);
014: }
015:
016: public void testDefaults() throws Exception {
017: args = null;
018: ap = new ArgumentParser(args);
019:
020: assertEquals(".", ap.getOutputPath());
021: assertEquals(true, ap.getSkipUptodate());
022: assertEquals("", ap.getTargetPackage());
023: assertEquals(0, ap.getClassNames().size());
024: assertEquals(0, ap.getSourceDirectories().size());
025: }
026:
027: public void testNoParams() throws Exception {
028: args = new String[] {};
029: ap = new ArgumentParser(args);
030:
031: assertEquals(".", ap.getOutputPath());
032: assertTrue(ap.getSkipUptodate());
033: assertEquals("", ap.getTargetPackage());
034: assertEquals(0, ap.getClassNames().size());
035: assertEquals(0, ap.getSourceDirectories().size());
036: }
037:
038: public void testOutputDir() throws Exception {
039: args = new String[] { "-o", "foobar" };
040: ap = new ArgumentParser(args);
041: assertEquals("foobar", ap.getOutputPath());
042: }
043:
044: public void testOutputDirMissed() throws Exception {
045: args = new String[] { "-o" };
046: try {
047: ap = new ArgumentParser(args);
048: fail();
049: } catch (IllegalArgumentException ex) {
050: // okay
051: }
052: }
053:
054: public void testOutputDirGlued() throws Exception {
055: args = new String[] { "-ofoobar" };
056: ap = new ArgumentParser(args);
057: assertEquals("foobar", ap.getOutputPath());
058: }
059:
060: public void testTargetPackage() throws Exception {
061: args = new String[] { "-p", "foobar" };
062: ap = new ArgumentParser(args);
063: assertEquals("foobar", ap.getTargetPackage());
064: }
065:
066: public void testTargetMissed() throws Exception {
067: args = new String[] { "-p" };
068: try {
069: ap = new ArgumentParser(args);
070: fail();
071: } catch (IllegalArgumentException ex) {
072: // okay
073: }
074: }
075:
076: public void testTargetPackageGlued() throws Exception {
077: args = new String[] { "-pfoobar" };
078: ap = new ArgumentParser(args);
079: assertEquals("foobar", ap.getTargetPackage());
080: }
081:
082: public void testUpdate() throws Exception {
083: args = new String[] { "-u" };
084: ap = new ArgumentParser(args);
085: assertFalse(ap.getSkipUptodate());
086: }
087:
088: public void testUpdateWrong() throws Exception {
089: args = new String[] { "-up", "pkg" };
090: try {
091: ap = new ArgumentParser(args);
092: fail("-u is an standalone option, no parameter allowed");
093: } catch (IllegalArgumentException ex) {
094: // okay
095: }
096: }
097:
098: public void testSourceDirectoryOne() throws Exception {
099: args = new String[] { "-d", "foo" };
100: ap = new ArgumentParser(args);
101: List dirs = ap.getSourceDirectories();
102: assertEquals(1, dirs.size());
103: assertEquals("foo", dirs.get(0));
104: }
105:
106: public void testSourceDirectoryMissed() throws Exception {
107: args = new String[] { "-d" };
108: try {
109: ap = new ArgumentParser(args);
110: fail();
111: } catch (IllegalArgumentException ex) {
112: // okay
113: }
114: }
115:
116: public void testSourceDirectoryOneGlued() throws Exception {
117: args = new String[] { "-dfoo" };
118: ap = new ArgumentParser(args);
119: List dirs = ap.getSourceDirectories();
120: assertEquals(1, dirs.size());
121: assertEquals("foo", dirs.get(0));
122: }
123:
124: public void testSourceDirectoryColon() throws Exception {
125: args = new String[] { "-d", "foo:bar:baz" };
126: ap = new ArgumentParser(args);
127: List dirs = ap.getSourceDirectories();
128: assertEquals(3, dirs.size());
129: assertEquals("foo", dirs.get(0));
130: assertEquals("bar", dirs.get(1));
131: assertEquals("baz", dirs.get(2));
132: }
133:
134: public void testSourceDirectoryColonGlued() throws Exception {
135: args = new String[] { "-dfoo:bar:baz" };
136: ap = new ArgumentParser(args);
137: List dirs = ap.getSourceDirectories();
138: assertEquals(3, dirs.size());
139: assertEquals("foo", dirs.get(0));
140: assertEquals("bar", dirs.get(1));
141: assertEquals("baz", dirs.get(2));
142: }
143:
144: public void testSourceDirectorySemiColon() throws Exception {
145: args = new String[] { "-d", "foo;bar;baz" };
146: ap = new ArgumentParser(args);
147: List dirs = ap.getSourceDirectories();
148: assertEquals(3, dirs.size());
149: assertEquals("foo", dirs.get(0));
150: assertEquals("bar", dirs.get(1));
151: assertEquals("baz", dirs.get(2));
152: }
153:
154: public void testSourceDirectorySemiColonGlued() throws Exception {
155: args = new String[] { "-dfoo;bar;baz" };
156: ap = new ArgumentParser(args);
157: List dirs = ap.getSourceDirectories();
158: assertEquals(3, dirs.size());
159: assertEquals("foo", dirs.get(0));
160: assertEquals("bar", dirs.get(1));
161: assertEquals("baz", dirs.get(2));
162: }
163:
164: public void testSourceDirectoryStrange() throws Exception {
165: args = new String[] { "-d", "::;;foo;;::bar;;::" };
166: ap = new ArgumentParser(args);
167: List dirs = ap.getSourceDirectories();
168: assertEquals(2, dirs.size());
169: assertEquals("foo", dirs.get(0));
170: assertEquals("bar", dirs.get(1));
171: }
172:
173: public void testSourceDirectoryStrangeGlued() throws Exception {
174: args = new String[] { "-d::;;foo;;::bar;;::" };
175: ap = new ArgumentParser(args);
176: List dirs = ap.getSourceDirectories();
177: assertEquals(2, dirs.size());
178: assertEquals("foo", dirs.get(0));
179: assertEquals("bar", dirs.get(1));
180: }
181:
182: public void testSourceDirectoryStrange2() throws Exception {
183: args = new String[] { "-d", ":" };
184: ap = new ArgumentParser(args);
185: List dirs = ap.getSourceDirectories();
186: assertEquals(0, dirs.size());
187: }
188:
189: public void testSourceDirectoryStrange2Glued() throws Exception {
190: args = new String[] { "-d:" };
191: ap = new ArgumentParser(args);
192: List dirs = ap.getSourceDirectories();
193: assertEquals(0, dirs.size());
194: }
195:
196: public void testInterfaceNames() throws Exception {
197: args = new String[] { "foo", "bar", "baz" };
198: ap = new ArgumentParser(args);
199: List ifaces = ap.getClassNames();
200: assertEquals(3, ifaces.size());
201: assertEquals("foo", ifaces.get(0));
202: assertEquals("bar", ifaces.get(1));
203: assertEquals("baz", ifaces.get(2));
204: }
205:
206: public void testCombination() throws Exception {
207: args = new String[] { "-u", "-p", "pkg", "-o", "dir", "-d",
208: "foo:bar", "iface1", "iface2" };
209: ap = new ArgumentParser(args);
210:
211: assertFalse(ap.getSkipUptodate());
212:
213: assertEquals("pkg", ap.getTargetPackage());
214:
215: assertEquals("dir", ap.getOutputPath());
216:
217: List dirs = ap.getSourceDirectories();
218: assertEquals(2, dirs.size());
219: assertEquals("foo", dirs.get(0));
220: assertEquals("bar", dirs.get(1));
221:
222: List ifaces = ap.getClassNames();
223: assertEquals(2, ifaces.size());
224: assertEquals("iface1", ifaces.get(0));
225: assertEquals("iface2", ifaces.get(1));
226: }
227:
228: public void testUnknownOption() throws Exception {
229: args = new String[] { "-u", "-z" };
230:
231: try {
232: ap = new ArgumentParser(args);
233: fail();
234: } catch (IllegalArgumentException ex) {
235: // okay
236: }
237: }
238:
239: public void testParameterInWrongPlace() throws Exception {
240: args = new String[] { "-u", "foobar", "-o", "dir" };
241:
242: try {
243: ap = new ArgumentParser(args);
244: fail();
245: } catch (IllegalArgumentException ex) {
246: // okay
247: }
248: }
249: }
|