Skip to content

Commit

Permalink
Can override notifier implementation with system property
Browse files Browse the repository at this point in the history
Use -DnotifyWith=growl for example to use growl as notifier implementation
whatever configuration set in maven-notifier.properties.
  • Loading branch information
jcgay committed Jun 24, 2014
1 parent 7df6bc9 commit 9096f44
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

public abstract class AbstractCustomEventSpy implements Notifier {

static final String OVERRIDE_NOTIFIER_IMPLEMENTATION = "notifyWith";
protected Logger logger;
protected Configuration configuration;
protected Stopwatch stopwatch = new Stopwatch();
Expand All @@ -39,6 +40,11 @@ public void onFailWithoutProject(List<Throwable> exceptions) {

@Override
public boolean shouldNotify() {
String notifierOverride = System.getProperty(OVERRIDE_NOTIFIER_IMPLEMENTATION);
if (notifierOverride != null && !notifierOverride.equals("")) {
return getClass().getName().contains(notifierOverride);
}

if (getClass().getName().contains(configuration.getImplementation())) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,41 @@
import com.google.common.base.Stopwatch;
import org.apache.maven.eventspy.EventSpy;
import org.apache.maven.execution.DefaultMavenExecutionResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

public class AbstractCustomEventSpyTest {

private AbstractCustomEventSpy eventSpy;
private Stopwatch stopwatch;
private Configuration configuration;

@BeforeMethod
public void setUp() throws Exception {
eventSpy = new AbstractCustomEventSpy() {};
stopwatch = new Stopwatch(new KnownElapsedTimeTicker(TimeUnit.SECONDS.toNanos(2L)));
eventSpy.setStopwatch(stopwatch);

configuration = new Configuration();
ConfigurationParser parser = mock(ConfigurationParser.class);
when(parser.get()).thenReturn(configuration);
eventSpy.setConfiguration(parser);
}

@AfterMethod
public void tearDown() throws Exception {
System.setProperty(AbstractCustomEventSpy.OVERRIDE_NOTIFIER_IMPLEMENTATION, "");
}

@Test
Expand Down Expand Up @@ -51,4 +66,22 @@ public Map<String, Object> getData() {

assertEquals(stopwatch.elapsedTime(TimeUnit.SECONDS), 2L);
}

@Test
public void should_override_notifier_implementation_with_system_property() throws Exception {

System.setProperty(AbstractCustomEventSpy.OVERRIDE_NOTIFIER_IMPLEMENTATION, "Custom");
configuration.setImplementation("growl");

assertTrue(eventSpy.shouldNotify());
}

@Test
public void should_not_notify_when_system_property_is_set_and_does_not_match_implementation() throws Exception {

System.setProperty(AbstractCustomEventSpy.OVERRIDE_NOTIFIER_IMPLEMENTATION, "growl");
configuration.setImplementation("Custom");

assertFalse(eventSpy.shouldNotify());
}
}

0 comments on commit 9096f44

Please sign in to comment.