Controller { public void processChangeScripts(Long lastChangeToApply) throws DbDeployException, IOException { if (lastChangeToApply != Long.MAX_VALUE) { info("Only applying changes up and including change script #" + lastChangeToApply); } List<ChangeScript> scripts = availableChangeScriptsProvider.getAvailableChangeScripts(); List<Long> applied = appliedChangesProvider.getAppliedChanges(); List<ChangeScript> toApply = identifyChangesToApply(lastChangeToApply, scripts, applied); logStatus(scripts, applied, toApply); changeScriptApplier.apply(Collections.unmodifiableList(toApply)); if (undoScriptApplier != null) { info("Generating undo scripts..."); Collections.reverse(toApply); undoScriptApplier.apply(Collections.unmodifiableList(toApply)); } } Controller(AvailableChangeScriptsProvider availableChangeScriptsProvider,
					  AppliedChangesProvider appliedChangesProvider,
					  ChangeScriptApplier changeScriptApplier, ChangeScriptApplier undoScriptApplier); void processChangeScripts(Long lastChangeToApply);  }
@Test public void shouldApplyChangeScriptsInOrder() throws Exception { when(appliedChangesProvider.getAppliedChanges()).thenReturn(Collections.<Long>emptyList()); controller.processChangeScripts(Long.MAX_VALUE); assertThat(applier.changeScripts.size(), is(3)); assertThat(applier.changeScripts.get(0), is(change1)); assertThat(applier.changeScripts.get(1), is(change2)); assertThat(applier.changeScripts.get(2), is(change3)); }
@Test public void shouldNotCrashWhenPassedANullUndoApplier() throws Exception { controller = new Controller(availableChangeScriptsProvider, appliedChangesProvider, applier, null); when(appliedChangesProvider.getAppliedChanges()).thenReturn(Collections.<Long>emptyList()); controller.processChangeScripts(Long.MAX_VALUE); }
@Test public void shouldApplyUndoScriptsInReverseOrder() throws Exception { when(appliedChangesProvider.getAppliedChanges()).thenReturn(Collections.<Long>emptyList()); controller.processChangeScripts(Long.MAX_VALUE); assertThat(undoApplier.changeScripts.size(), is(3)); assertThat(undoApplier.changeScripts.get(0), is(change3)); assertThat(undoApplier.changeScripts.get(1), is(change2)); assertThat(undoApplier.changeScripts.get(2), is(change1)); }
@Test public void shouldIgnoreChangesAlreadyAppliedToTheDatabase() throws Exception { when(appliedChangesProvider.getAppliedChanges()).thenReturn(Arrays.asList(1L)); controller.processChangeScripts(Long.MAX_VALUE); assertThat(applier.changeScripts.size(), is(2)); assertThat(applier.changeScripts.get(0), is(change2)); assertThat(applier.changeScripts.get(1), is(change3)); }
@Test public void shouldNotApplyChangesGreaterThanTheMaxChangeToApply() throws Exception { when(appliedChangesProvider.getAppliedChanges()).thenReturn(Collections.<Long>emptyList()); controller.processChangeScripts(2L); assertThat(applier.changeScripts.size(), is(2)); assertThat(applier.changeScripts.get(0), is(change1)); assertThat(applier.changeScripts.get(1), is(change2)); }