001: package liquibase.commandline;
002:
003: import liquibase.exception.CommandLineParsingException;
004: import liquibase.util.StringUtils;
005: import static org.junit.Assert.*;
006: import org.junit.Test;
007:
008: import java.io.*;
009: import java.net.URL;
010: import java.net.URLClassLoader;
011: import java.util.Arrays;
012: import java.util.Properties;
013:
014: /**
015: * Tests for {@link Main}
016: */
017: public class MainTest {
018:
019: @Test
020: public void migrateWithAllParameters() throws Exception {
021: String[] args = new String[] { "--driver=DRIVER",
022: "--username=USERNAME", "--password=PASSWORD",
023: "--url=URL", "--changeLogFile=FILE",
024: "--classpath=CLASSPATH;CLASSPATH2",
025: "--contexts=CONTEXT1,CONTEXT2",
026: "--promptForNonLocalDatabase=true", "update", };
027:
028: Main cli = new Main();
029: cli.parseOptions(args);
030:
031: assertEquals("DRIVER", cli.driver);
032: assertEquals("USERNAME", cli.username);
033: assertEquals("PASSWORD", cli.password);
034: assertEquals("URL", cli.url);
035: assertEquals("FILE", cli.changeLogFile);
036: assertEquals("CLASSPATH;CLASSPATH2", cli.classpath);
037: assertEquals("CONTEXT1,CONTEXT2", cli.contexts);
038: assertEquals(Boolean.TRUE, cli.promptForNonLocalDatabase);
039: assertEquals("update", cli.command);
040: }
041:
042: @Test
043: public void falseBooleanParameters() throws Exception {
044: String[] args = new String[] {
045: "--promptForNonLocalDatabase=false", "update", };
046:
047: Main cli = new Main();
048: cli.parseOptions(args);
049:
050: assertEquals(Boolean.FALSE, cli.promptForNonLocalDatabase);
051: assertEquals("update", cli.command);
052:
053: }
054:
055: @Test
056: public void convertMigrateToUpdate() throws Exception {
057: String[] args = new String[] {
058: "--promptForNonLocalDatabase=false", "migrate", };
059:
060: Main cli = new Main();
061: cli.parseOptions(args);
062:
063: assertEquals("update", cli.command);
064:
065: }
066:
067: @Test
068: public void trueBooleanParameters() throws Exception {
069: String[] args = new String[] {
070: "--promptForNonLocalDatabase=true", "update", };
071:
072: Main cli = new Main();
073: cli.parseOptions(args);
074:
075: assertEquals(Boolean.TRUE, cli.promptForNonLocalDatabase);
076: assertEquals("update", cli.command);
077:
078: }
079:
080: @Test(expected=CommandLineParsingException.class)
081: public void parameterWithoutDash() throws Exception {
082: String[] args = new String[] {
083: "promptForNonLocalDatabase=true", "update", };
084:
085: Main cli = new Main();
086: cli.parseOptions(args);
087: }
088:
089: @Test(expected=CommandLineParsingException.class)
090: public void unknownParameter() throws Exception {
091: String[] args = new String[] {
092: "--promptForNonLocalDatabase=true", "--badParam=here",
093: "migrate", };
094:
095: Main cli = new Main();
096: cli.parseOptions(args);
097: }
098:
099: @Test(expected=CommandLineParsingException.class)
100: public void configureNonExistantClassloaderLocation()
101: throws Exception {
102: Main cli = new Main();
103: cli.classpath = "badClasspathLocation";
104: cli.configureClassLoader();
105: }
106:
107: @Test
108: public void windowsConfigureClassLoaderLocation() throws Exception {
109: Main cli = new Main();
110:
111: if (cli.isWindows()) {
112: System.setProperty("os.name", "Windows XP");
113: cli.classpath = "c:\\;c:\\windows\\";
114: cli.applyDefaults();
115: cli.configureClassLoader();
116:
117: URL[] classloaderURLs = ((URLClassLoader) cli.classLoader)
118: .getURLs();
119: assertEquals(2, classloaderURLs.length);
120: assertEquals("file:/c:/", classloaderURLs[0]
121: .toExternalForm());
122: assertEquals("file:/c:/windows/", classloaderURLs[1]
123: .toExternalForm());
124: }
125: }
126:
127: @Test
128: public void unixConfigureClassLoaderLocation() throws Exception {
129: Main cli = new Main();
130:
131: if (!cli.isWindows()) {
132: System.setProperty("os.name", "Linux");
133: cli.classpath = "/tmp:/";
134: cli.applyDefaults();
135:
136: cli.configureClassLoader();
137:
138: URL[] classloaderURLs = ((URLClassLoader) cli.classLoader)
139: .getURLs();
140: assertEquals(2, classloaderURLs.length);
141: assertEquals("file:/tmp/", classloaderURLs[0]
142: .toExternalForm());
143: assertEquals("file:/", classloaderURLs[1].toExternalForm());
144: }
145: }
146:
147: @Test
148: public void propertiesFileWithNoOtherArgs() throws Exception {
149: Main cli = new Main();
150:
151: Properties props = new Properties();
152: props.setProperty("driver", "DRIVER");
153: props.setProperty("username", "USERNAME");
154: props.setProperty("password", "PASSWD");
155: props.setProperty("url", "URL");
156: props.setProperty("changeLogFile", "FILE");
157: props.setProperty("classpath", "CLASSPAHT");
158: props.setProperty("contexts", "CONTEXTS");
159: props.setProperty("promptForNonLocalDatabase", "TRUE");
160:
161: ByteArrayOutputStream propFile = new ByteArrayOutputStream();
162: props.store(propFile, "");
163:
164: cli.parsePropertiesFile(new ByteArrayInputStream(propFile
165: .toByteArray()));
166:
167: assertEquals("DRIVER", cli.driver);
168: assertEquals("USERNAME", cli.username);
169: assertEquals("PASSWD", cli.password);
170: assertEquals("URL", cli.url);
171: assertEquals("FILE", cli.changeLogFile);
172: assertEquals("CLASSPAHT", cli.classpath);
173: assertEquals("CONTEXTS", cli.contexts);
174: assertEquals(Boolean.TRUE, cli.promptForNonLocalDatabase);
175:
176: }
177:
178: @Test
179: public void propertiesFileWithOtherArgs() throws Exception {
180: Main cli = new Main();
181: cli.username = "PASSED USERNAME";
182: cli.password = "PASSED PASSWD";
183:
184: Properties props = new Properties();
185: props.setProperty("driver", "DRIVER");
186: props.setProperty("username", "USERNAME");
187: props.setProperty("password", "PASSWD");
188: props.setProperty("url", "URL");
189: props.setProperty("changeLogFile", "FILE");
190: props.setProperty("classpath", "CLASSPAHT");
191: props.setProperty("contexts", "CONTEXTS");
192: props.setProperty("promptForNonLocalDatabase", "TRUE");
193:
194: ByteArrayOutputStream propFile = new ByteArrayOutputStream();
195: props.store(propFile, "");
196:
197: cli.parsePropertiesFile(new ByteArrayInputStream(propFile
198: .toByteArray()));
199:
200: assertEquals("DRIVER", cli.driver);
201: assertEquals("PASSED USERNAME", cli.username);
202: assertEquals("PASSED PASSWD", cli.password);
203: assertEquals("URL", cli.url);
204: assertEquals("FILE", cli.changeLogFile);
205: assertEquals("CLASSPAHT", cli.classpath);
206: assertEquals("CONTEXTS", cli.contexts);
207: assertEquals(Boolean.TRUE, cli.promptForNonLocalDatabase);
208:
209: }
210:
211: @Test
212: public void applyDefaults() {
213: Main cli = new Main();
214:
215: cli.promptForNonLocalDatabase = Boolean.TRUE;
216: cli.applyDefaults();
217: assertEquals(Boolean.TRUE, cli.promptForNonLocalDatabase);
218:
219: cli.promptForNonLocalDatabase = Boolean.FALSE;
220: cli.applyDefaults();
221: assertEquals(Boolean.FALSE, cli.promptForNonLocalDatabase);
222:
223: cli.promptForNonLocalDatabase = null;
224: cli.applyDefaults();
225: assertEquals(Boolean.FALSE, cli.promptForNonLocalDatabase);
226:
227: }
228:
229: @Test(expected=CommandLineParsingException.class)
230: public void propertiesFileWithBadArgs() throws Exception {
231: Main cli = new Main();
232:
233: Properties props = new Properties();
234: props.setProperty("driver", "DRIVER");
235: props.setProperty("username", "USERNAME");
236: props.setProperty("badArg", "ARG");
237:
238: ByteArrayOutputStream propFile = new ByteArrayOutputStream();
239: props.store(propFile, "");
240:
241: cli.parsePropertiesFile(new ByteArrayInputStream(propFile
242: .toByteArray()));
243: }
244:
245: @Test
246: public void checkSetup() {
247: Main cli = new Main();
248: assertTrue(cli.checkSetup().size() > 0);
249:
250: cli.driver = "driver";
251: cli.username = "username";
252: cli.password = "pwd";
253: cli.url = "url";
254: cli.changeLogFile = "file";
255: cli.classpath = "classpath";
256:
257: assertTrue(cli.checkSetup().size() > 0);
258:
259: cli.command = "BadCommand";
260: assertTrue(cli.checkSetup().size() > 0);
261:
262: cli.command = "migrate";
263: assertEquals(0, cli.checkSetup().size());
264: }
265:
266: @Test
267: public void printHelp() throws Exception {
268: ByteArrayOutputStream stream = new ByteArrayOutputStream();
269: Main cli = new Main();
270: cli.printHelp(new PrintStream(stream));
271:
272: BufferedReader reader = new BufferedReader(new StringReader(
273: new String(stream.toByteArray())));
274: String line;
275: while ((line = reader.readLine()) != null) {
276: //noinspection MagicNumber
277: if (line.length() > 80) {
278: fail("'" + line + "' is longer than 80 chars");
279: }
280: }
281: }
282:
283: @Test
284: public void tag() throws Exception {
285: String[] args = new String[] { "--driver=DRIVER",
286: "--username=USERNAME", "--password=PASSWORD",
287: "--url=URL", "--changeLogFile=FILE",
288: "--classpath=CLASSPATH;CLASSPATH2",
289: "--contexts=CONTEXT1,CONTEXT2", "tag", "TagHere" };
290:
291: Main cli = new Main();
292: cli.parseOptions(args);
293:
294: assertEquals("DRIVER", cli.driver);
295: assertEquals("USERNAME", cli.username);
296: assertEquals("PASSWORD", cli.password);
297: assertEquals("URL", cli.url);
298: assertEquals("FILE", cli.changeLogFile);
299: assertEquals("CLASSPATH;CLASSPATH2", cli.classpath);
300: assertEquals("CONTEXT1,CONTEXT2", cli.contexts);
301: assertEquals("tag", cli.command);
302: assertEquals("TagHere", cli.commandParams.iterator().next());
303: }
304:
305: @Test
306: public void migrateWithEqualsInParams() throws Exception {
307: String url = "dbc:sqlserver://127.0.0.1;DatabaseName=dev_nn;user=ffdatabase;password=p!88worD";
308: String[] args = new String[] { "--url=" + url, "migrate", };
309:
310: Main cli = new Main();
311: cli.parseOptions(args);
312:
313: assertEquals(url, cli.url);
314: }
315:
316: @Test
317: public void fixArgs() {
318: Main liquibase = new Main();
319: String[] fixedArgs = liquibase.fixupArgs(new String[] {
320: "--defaultsFile", "liquibase.properties", "migrate" });
321: assertEquals("--defaultsFile=liquibase.properties migrate",
322: StringUtils.join(Arrays.asList(fixedArgs), " "));
323:
324: fixedArgs = liquibase.fixupArgs(new String[] {
325: "--defaultsFile=liquibase.properties", "migrate" });
326: assertEquals("--defaultsFile=liquibase.properties migrate",
327: StringUtils.join(Arrays.asList(fixedArgs), " "));
328:
329: fixedArgs = liquibase.fixupArgs(new String[] {
330: "--driver=DRIVER", "--username=USERNAME",
331: "--password=PASSWORD", "--url=URL",
332: "--changeLogFile=FILE",
333: "--classpath=CLASSPATH;CLASSPATH2",
334: "--contexts=CONTEXT1,CONTEXT2",
335: "--promptForNonLocalDatabase=true", "migrate" });
336: assertEquals(
337: "--driver=DRIVER --username=USERNAME --password=PASSWORD --url=URL --changeLogFile=FILE --classpath=CLASSPATH;CLASSPATH2 --contexts=CONTEXT1,CONTEXT2 --promptForNonLocalDatabase=true migrate",
338: StringUtils.join(Arrays.asList(fixedArgs), " "));
339: }
340:
341: @Test
342: public void testVersionArg() throws IOException,
343: CommandLineParsingException {
344: Main.main(new String[] { "--version" });
345:
346: }
347: }
|