Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-46609] Suffix the maven report file by ".tmp" as long as the…
… build has not finished
  • Loading branch information
Cyrille Le Clerc committed Sep 2, 2017
1 parent 810fe9a commit 54e8d0f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
Expand Up @@ -48,12 +48,19 @@
*/
@ThreadSafe
public class FileMavenEventReporter implements MavenEventReporter {
/**
* report file gets initially created with a "maven-spy-*.log.tmp" file extension and gets renamed "maven-spy-*.log"
* at the end of the execution
*/
@GuardedBy("this")
File outFile;
@GuardedBy("this")
PrintWriter out;
@GuardedBy("this")
XMLWriter xmlWriter;

/**
* used to support multiple calls of {@link #close()} }
*/
boolean isOpen;

public FileMavenEventReporter() throws IOException {
Expand All @@ -76,7 +83,8 @@ public FileMavenEventReporter() throws IOException {
}

String now = new SimpleDateFormat("yyyyMMdd-HHmmss-S").format(new Date());
outFile = File.createTempFile("maven-spy-" + now, ".log", reportsFolder);
outFile = File.createTempFile("maven-spy-" + now, ".log.tmp", reportsFolder);

out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
xmlWriter = new PrettyPrintXMLWriter(out);
xmlWriter.startElement("mavenExecution");
Expand Down Expand Up @@ -111,6 +119,16 @@ public synchronized void close() {

isOpen = false;

String filePath = outFile.getAbsolutePath();
filePath = filePath.substring(0, filePath.length() - ".tmp".length());
File finalFile = new File(filePath);

boolean result = outFile.renameTo(finalFile);
if (result == false) {
System.out.println("[jenkins-maven-event-spy] WARNING failure to rename " + outFile + " into " + finalFile);
} else {
outFile = finalFile;
}
try {
System.out.println("[jenkins-maven-event-spy] INFO generated " + outFile.getCanonicalPath());
} catch (IOException e) {
Expand All @@ -119,7 +137,10 @@ public synchronized void close() {
}
}

public File getOutFile() {
/**
* Visible for test
*/
protected synchronized File getFinalFile() {
return outFile;
}
}
Expand Up @@ -25,7 +25,6 @@
package org.jenkinsci.plugins.pipeline.maven.eventspy;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
Expand All @@ -44,15 +43,11 @@
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.FileUtils;
import org.eclipse.sisu.bean.IgnoreSetters;
import org.hamcrest.CoreMatchers;
import org.jenkinsci.plugins.pipeline.maven.eventspy.reporter.FileMavenEventReporter;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.internal.runners.statements.Fail;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class JenkinsMavenEventSpyMTTest {

Expand Down Expand Up @@ -156,7 +151,7 @@ public void run() {

for (JenkinsMavenEventSpy spy : spyList) {
spy.close();
File outFile = ((FileMavenEventReporter) spy.getReporter()).getOutFile();
File outFile = ((FileMavenEventReporter) spy.getReporter()).getFinalFile();
System.out.println("Generated file: " + outFile);
String actual = FileUtils.fileRead(outFile);
Assert.assertThat(actual, CoreMatchers.containsString("MavenExecutionRequest"));
Expand Down Expand Up @@ -224,7 +219,7 @@ public void run() {
}

spy.close();
File outFile = ((FileMavenEventReporter) spy.getReporter()).getOutFile();
File outFile = ((FileMavenEventReporter) spy.getReporter()).getFinalFile();
System.out.println("Generated file: " + outFile);
String actual = FileUtils.fileRead(outFile);
Assert.assertThat(actual, CoreMatchers.containsString("MavenExecutionRequest"));
Expand Down

0 comments on commit 54e8d0f

Please sign in to comment.