PageRenderTime 28ms CodeModel.GetById 13ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/db-migrate/src/test/java/com/googlecode/hdbc/dbmigrate/commandline/CommandLineParserTest.java

http://hdbc.googlecode.com/
Java | 121 lines | 83 code | 18 blank | 20 comment | 0 complexity | c49b43e67f3dadf6d12ed5545858a939 MD5 | raw file
  1package com.googlecode.hdbc.dbmigrate.commandline;
  2
  3import static org.junit.Assert.*;
  4
  5import java.sql.SQLException;
  6
  7import org.junit.Before;
  8import org.junit.Test;
  9import org.junit.runner.RunWith;
 10import com.googlecode.hdbc.dbmigrate.io.IDatabaseProvider;
 11import com.googlecode.hdbc.dbmigrate.io.IFileProvider;
 12import org.jmock.Expectations;
 13import org.jmock.Mockery;
 14import org.jmock.integration.junit4.JMock;
 15import org.jmock.integration.junit4.JUnit4Mockery;
 16
 17@RunWith(JMock.class)
 18public class CommandLineParserTest {
 19	private CommandLineParser parser;
 20	private IDatabaseProvider db;
 21	private IFileProvider fs;
 22	private Mockery context;
 23	
 24	private static final String[] files = new String[] {
 25			"00020-do_a.sql",
 26			"00025-do_b.sql",
 27			"00024-do_c.sql",
 28			"00021-do_d.sql",
 29			"00023-do_e.sql",
 30			"00021-do_f.sql"
 31		}; 
 32	
 33	@Before
 34	public void setUp() {
 35		context = new JUnit4Mockery();
 36		db = context.mock(IDatabaseProvider.class);
 37		fs = context.mock(IFileProvider.class);
 38		parser = new CommandLineParser(db, fs);
 39	}
 40	
 41	/**
 42	 * args = []
 43	 */
 44	@Test
 45	public void testInteractiveModeStdCase() throws SQLException {
 46		RunMode actual = parser.parse(new String[] {});
 47		assertTrue(actual instanceof InteractiveMode);
 48	}
 49	
 50	/**
 51	 * contains "^-t\d*" args == False
 52	 */
 53	@Test 
 54	public void testInteractiveMode2() throws SQLException {
 55		RunMode actual = parser.parse(new String[] {"10-t", "-a"});
 56		assertTrue(actual instanceof InteractiveMode);
 57	}
 58	
 59	/**
 60	 * contains "^-t" args = True
 61	 * <p>
 62	 * In this case we want the database to be upgraded to the highest
 63	 * version available 
 64	 */
 65	@Test
 66	public void testAutoModeStdCase() throws SQLException {
 67		context.checking(new Expectations() {{
 68			oneOf(db).getCurrentVersion();
 69			will(returnValue(23));
 70			
 71			oneOf(fs).migrationFileList();
 72			will(returnValue(files));
 73		}});
 74
 75		RunMode actual = parser.parse(new String[] {"-t"});
 76		assertTrue(actual instanceof AutomatedMode);
 77		assertEquals(23, actual.getFromVersion());
 78		assertEquals(25, actual.getToVersion());
 79	}
 80	
 81	/**
 82	 * contains "^-t(\d*)" args == True && (\d*) <= version max
 83	 */
 84	@Test 
 85	public void testAutoMode2() throws SQLException {
 86		context.checking(new Expectations() {{
 87			oneOf(db).getCurrentVersion();
 88			will(returnValue(23));
 89			
 90			oneOf(fs).migrationFileList();
 91			will(returnValue(files));
 92		}});
 93
 94		RunMode actual = parser.parse(new String[] {"-t24"});
 95		assertTrue(actual instanceof AutomatedMode);
 96		assertEquals(23, actual.getFromVersion());
 97		assertEquals(24, actual.getToVersion());
 98	}
 99	
100	/**
101	 * contains "^-t(\d*) args == True && (\d*) > version max
102	 * <p>
103	 * In this case we want to default the to variable to version max
104	 */
105	@Test
106	public void testAutoMode3() throws SQLException {
107		context.checking(new Expectations() {{
108			oneOf(db).getCurrentVersion();
109			will(returnValue(23));
110			
111			oneOf(fs).migrationFileList();
112			will(returnValue(files));
113		}});
114		
115		RunMode actual = parser.parse(new String[] {"-t532"});
116		assertTrue(actual instanceof AutomatedMode);
117		assertEquals(23, actual.getFromVersion());
118		assertEquals(25, actual.getToVersion());
119	}
120	
121}