/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

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