Skip to content
Permalink
Browse files

[JENKINS-43529] Don’t create an invalid build xml logs file when the …

…Maven Event Spy is disabled
  • Loading branch information
Cyrille Le Clerc
Cyrille Le Clerc committed Apr 12, 2017
1 parent eaf719e commit d8c8b96e44a1b816847c9258d7f34830d9c3345b
@@ -70,7 +70,7 @@

public final static String DISABLE_MAVEN_EVENT_SPY_ENVIRONMENT_VARIABLE_NAME = "JENKINS_MAVEN_AGENT_DISABLED";

private final MavenEventReporter reporter;
private MavenEventReporter reporter;

/*
* visible for testing
@@ -86,10 +86,28 @@
private List<MavenEventHandler> handlers = new ArrayList();

public JenkinsMavenEventSpy() throws IOException {
this(new FileMavenEventReporter());
this.disabled = isEventSpyDisabled();
if (disabled) {
System.out.println("[jenkins-maven-event-spy] INFO Jenkins Maven Event Spy is disabled");
}
}

public JenkinsMavenEventSpy(MavenEventReporter reporter) throws IOException {
this();
this.reporter = reporter;
}

@Override
public void init(EventSpy.Context context) throws Exception {
if (disabled) {
this.reporter = new DevNullMavenEventReporter();
return;
}

if (reporter == null) {
this.reporter = new FileMavenEventReporter();
}
// Initialize handlers
handlers.add(new ProjectSucceededExecutionHandler(reporter));
handlers.add(new ProjectFailedExecutionHandler(reporter));
handlers.add(new ProjectStartedExecutionHandler(reporter));
@@ -104,20 +122,7 @@ public JenkinsMavenEventSpy(MavenEventReporter reporter) throws IOException {

handlers.add(new CatchAllExecutionHandler(reporter));

if (isEventSpyDisabled()) {
this.disabled = true;
this.reporter = new DevNullMavenEventReporter();
} else {
this.disabled = false;
this.reporter = reporter;
}
}

@Override
public void init(EventSpy.Context context) throws Exception {
if (disabled)
return;

// Print context
Xpp3Dom element = new Xpp3Dom("context");
for (Map.Entry<String, Object> entry : context.getData().entrySet()) {
Xpp3Dom entryElt = new Xpp3Dom(entry.getKey());
@@ -150,7 +155,7 @@ public void onEvent(Object event) throws Exception {

} catch (Throwable t) {
blackList.add(event.getClass());
System.err.println("Exception processing " + event);
System.err.println("[jenkins-maven-event-spy] WARNING Exception processing " + event);
reporter.print(getClass().getName() + ": Exception processing " + event);
t.printStackTrace();
}
@@ -166,7 +171,9 @@ public void close() {
reporter.close();
}


/**
* Visible for testing
*/
protected boolean isEventSpyDisabled(){
return "true".equalsIgnoreCase(System.getProperty(DISABLE_MAVEN_EVENT_SPY_PROPERTY_NAME)) ||
"true".equalsIgnoreCase(System.getenv(DISABLE_MAVEN_EVENT_SPY_ENVIRONMENT_VARIABLE_NAME));
@@ -81,7 +81,7 @@ public FileMavenEventReporter() throws IOException {
xmlWriter.startElement("mavenExecution");
xmlWriter.addAttribute("_time", new Timestamp(System.currentTimeMillis()).toString());

System.out.println("[jenkins-maven-event-spy] generate " + outFile.getAbsolutePath() + " ...");
System.out.println("[jenkins-maven-event-spy] INFO generate " + outFile.getAbsolutePath() + " ...");
}

@Override
@@ -102,6 +102,6 @@ public synchronized void close() {
xmlWriter.endElement();

out.close();
System.out.println("[jenkins-maven-event-spy] generated " + outFile.getAbsolutePath());
System.out.println("[jenkins-maven-event-spy] INFO generated " + outFile.getAbsolutePath());
}
}
@@ -53,6 +53,14 @@ protected boolean isEventSpyDisabled() {
return true;
}
};
Assert.assertThat(spy.getReporter(), CoreMatchers.nullValue());
spy.init(new EventSpy.Context() {
@Override
public Map<String, Object> getData() {
return new HashMap<String, Object>();
}
});

Assert.assertThat(spy.getReporter(), CoreMatchers.instanceOf(DevNullMavenEventReporter.class));
Assert.assertThat(spy.disabled, CoreMatchers.is(true));
}

0 comments on commit d8c8b96

Please sign in to comment.
You can’t perform that action at this time.