FailClosureTarget implements ClosureTarget { public Object call(final @Nullable Object[] args) throws Exception { if (args == null || args.length == 0) { throw new MojoExecutionException(FAILED); } else if (args.length == 1) { if (args[0] instanceof Throwable) { Throwable cause = (Throwable) args[0]; throw new MojoExecutionException(cause.getMessage(), cause); } else { throw new MojoExecutionException(String.valueOf(args[0])); } } else if (args.length == 2) { if (args[1] instanceof Throwable) { throw new MojoExecutionException(String.valueOf(args[0]), (Throwable) args[1]); } else { throw new Error("Invalid arguments to fail(Object, Throwable), second argument must be a Throwable"); } } else { throw new Error("Too many arguments; expected one of: fail(), fail(Object) or fail(Object, Throwable)"); } }  Object call(final @Nullable Object[] args);  }
@Test public void fail_null() throws Exception { try { underTest.call(null); } catch (MojoExecutionException e) { assertThat(e.getMessage(), is(FailClosureTarget.FAILED)); } }
@Test public void fail_noArgs() throws Exception { try { underTest.call(new Object[0]); } catch (MojoExecutionException e) { assertThat(e.getMessage(), is(FailClosureTarget.FAILED)); } }
@Test public void fail_object() throws Exception { try { underTest.call(new Object[]{"foo"}); } catch (MojoExecutionException e) { assertThat(e.getMessage(), is("foo")); assertThat(e.getCause(), nullValue()); } }
@Test public void fail_throwable() throws Exception { @SuppressWarnings("ThrowableInstanceNeverThrown") Throwable cause = new Throwable("foo"); try { underTest.call(new Object[]{cause}); } catch (MojoExecutionException e) { assertThat(e.getMessage(), is("foo")); assertThat(e.getCause(), is(cause)); } }
@Test public void fail_objectAndThrowable() throws Exception { @SuppressWarnings("ThrowableInstanceNeverThrown") Throwable cause = new Throwable("bar"); try { underTest.call(new Object[]{"foo", cause}); } catch (MojoExecutionException e) { assertThat(e.getMessage(), is("foo")); assertThat(e.getCause(), is(cause)); } }
@Test(expected = Error.class) public void fail_invalidArgs() throws Exception { underTest.call(new Object[]{1, 2}); }
@Test(expected = Error.class) public void fail_tooManyArgs() throws Exception { underTest.call(new Object[]{1, 2, 3}); }