MojoSupport extends AbstractMojo { public void execute() throws MojoExecutionException, MojoFailureException { try { try { log.trace("Prepare"); prepare(); log.trace("Run"); run(); } finally { log.trace("Cleanup"); cleanup(); } } catch (Exception e) { log.trace("Failure", e); Throwables.propagateIfPossible(e, MojoExecutionException.class, MojoFailureException.class); throw Throwables.propagate(e); } } protected  MojoSupport(); void execute();  }
@Test public void execute_MojoExecutionException_propagates() throws Exception { MojoSupport underTest = new MojoSupportTester() { @Override protected void run() throws Exception { throw new MojoExecutionException("fail"); } }; try { underTest.execute(); fail(); } catch (Exception e) { assertThat(e, instanceOf(MojoExecutionException.class)); } }
@Test public void execute_MojoFailureException_propagates() throws Exception { MojoSupport underTest = new MojoSupportTester() { @Override protected void run() throws Exception { throw new MojoFailureException("fail"); } }; try { underTest.execute(); fail(); } catch (Exception e) { assertThat(e, instanceOf(MojoFailureException.class)); } }
@Test public void execute_prepare_run_cleanup() throws Exception { MojoSupport underTest = new MojoSupportTester(); underTest.execute(); assertThat(prepareCount.get(), is(1)); assertThat(runCount.get(), is(1)); assertThat(cleanupCount.get(), is(1)); }
@Test public void execute_prepareExceptionCallsCleanup() throws Exception { MojoSupport underTest = new MojoSupportTester() { @Override protected void prepare() throws Exception { throw new Exception("fail"); } }; try { underTest.execute(); fail(); } catch (Exception e) { } assertThat(runCount.get(), is(0)); assertThat(cleanupCount.get(), is(1)); }
@Test public void execute_runExceptionCallsCleanup() throws Exception { MojoSupport underTest = new MojoSupportTester() { @Override protected void run() throws Exception { throw new Exception("fail"); } }; try { underTest.execute(); fail(); } catch (Exception e) { } assertThat(prepareCount.get(), is(1)); assertThat(cleanupCount.get(), is(1)); }