MavenGoal extends TransformationUtility<MavenGoal> { @Override protected TUExecutionResult execution(File transformedAppFolder, TransformationContext transformationContext) { File file = getAbsoluteFile(transformedAppFolder, transformationContext); TUExecutionResult result = null; InvocationResult invocationResult = null; try { for (MavenInvocationOutputHandler outputHandler : outputHandlers) { multipleOutputHandler.register(outputHandler); } if (file.exists() && file.isFile() && file.getName().equals("pom.xml")) { request.setPomFile(file); } else { request.setBaseDirectory(file); } request.setGoals(Arrays.asList(goals)); request.setOutputHandler(multipleOutputHandler); request.setBatchMode(true); if (properties != null && !properties.isEmpty()) { request.setProperties(properties); } invocationResult = invoker.execute(request); int exitCode = invocationResult.getExitCode(); Map<Class<? extends MavenInvocationOutputHandler>, Object> outputHandlersResult = multipleOutputHandler.getResult(); if (exitCode == 0) { result = TUExecutionResult.value(this, outputHandlersResult); } else { Exception e = invocationResult.getExecutionException(); if (e == null) { e = new TransformationUtilityException(String.format("Maven goals %s execution failed with exit code %d", Arrays.toString(goals), exitCode)); } if (warnOnError) { result = TUExecutionResult.warning(this, e, outputHandlersResult); } else { result = TUExecutionResult.error(this, e, outputHandlersResult); } } } catch (Exception e) { if (invocationResult != null) { Exception invocationException = invocationResult.getExecutionException(); if (invocationException != null) { result = TUExecutionResult.error(this, invocationException); } } if (result == null) { result = TUExecutionResult.error(this, e); } } return result; } MavenGoal();  MavenGoal(String... goals);  MavenGoal(String[] goals, MavenInvocationOutputHandler[] outputHandlers); MavenGoal setGoals(String... goals); MavenGoal setOutputHandlers(MavenInvocationOutputHandler... outputHandlers); MavenGoal setProperties(Properties properties); MavenGoal setWarnOnError(boolean warnOnError); String[] getGoals(); Properties getProperties(); MavenInvocationOutputHandler[] getOutputHandlers(); @Override String getDescription(); @Override MavenGoal clone();  }
@Test public void doesntSetIfFailureBehaviorIsUnset() { Mockito.reset(request); mavenGoal.execution(null, null); }
@Test public void isResultExceptionIfNonZeroExitCodeAndNoInvokerException() throws MavenInvocationException { InvocationResult r = Mockito.mock(InvocationResult.class); IllegalStateException e = new IllegalStateException("exception"); Mockito.when(multipleOutputHandler.getResult()).thenThrow(e); Mockito.when(r.getExitCode()).thenReturn(1); Mockito.when(r.getExecutionException()).thenReturn(null); Mockito.when(invoker.execute(request)).thenReturn(r); TUExecutionResult result = mavenGoal.execution(new File("/blah/pom.xml"), null); assertEquals(result.getException(), e); assertEquals(result.getValue(), null); }
@Test public void isSelfMadeExceptionIfNonZeroExitCodeAndHandlerGetResultIsValidAndNoInvokerException() throws MavenInvocationException { InvocationResult r = Mockito.mock(InvocationResult.class); Map<Class<? extends MavenInvocationOutputHandler>, Object> value = new HashMap<Class<? extends MavenInvocationOutputHandler>, Object>(); value.put(new GenericErrorsOutputHandler().getClass(), "Hello!"); Mockito.when(multipleOutputHandler.getResult()).thenReturn(value); Mockito.when(r.getExitCode()).thenReturn(1); Mockito.when(r.getExecutionException()).thenReturn(null); Mockito.when(invoker.execute(request)).thenReturn(r); TUExecutionResult result = mavenGoal.execution(new File("/blah/pom.xml"), null); assertEquals(result.getException().getMessage(), "Maven goals [a, b, c] execution failed with exit code 1"); assertEquals(result.getValue(), value); }
@Test public void isInvokerExceptionWithResultDataIfNonZeroExitCodeAndHandlerGetResultIsValid() throws MavenInvocationException { InvocationResult r = Mockito.mock(InvocationResult.class); Map<Class<? extends MavenInvocationOutputHandler>, Object> value = new HashMap<Class<? extends MavenInvocationOutputHandler>, Object>(); value.put(new GenericErrorsOutputHandler().getClass(), "Hello!"); CommandLineException invokerException = new CommandLineException("test error"); Mockito.when(multipleOutputHandler.getResult()).thenReturn(value); Mockito.when(r.getExitCode()).thenReturn(1); Mockito.when(r.getExecutionException()).thenReturn(invokerException); Mockito.when(invoker.execute(request)).thenReturn(r); TUExecutionResult result = mavenGoal.execution(new File("/blah/pom.xml"), null); assertEquals(result.getException(), invokerException); assertEquals(result.getValue(), value); }
@Test public void isValidResultIfZeroErrorCode() throws MavenInvocationException { InvocationResult r = Mockito.mock(InvocationResult.class); Map<Class<? extends MavenInvocationOutputHandler>, Object> value = new HashMap<Class<? extends MavenInvocationOutputHandler>, Object>(); value.put(new GenericErrorsOutputHandler().getClass(), "Hello!"); Mockito.when(multipleOutputHandler.getResult()).thenReturn(value); Mockito.when(r.getExitCode()).thenReturn(0); Mockito.when(r.getExecutionException()).thenReturn(null); Mockito.when(invoker.execute(request)).thenReturn(r); TUExecutionResult result = mavenGoal.execution(new File("/blah/pom.xml"), null); assertEquals(result.getException(), null); assertEquals(result.getValue(), value); }