Skip to content

Commit

Permalink
Fixed JENKINS-11552
Browse files Browse the repository at this point in the history
  • Loading branch information
gboissinot committed Oct 30, 2011
1 parent b8e723a commit 05f1a21
Show file tree
Hide file tree
Showing 8 changed files with 491 additions and 412 deletions.
717 changes: 362 additions & 355 deletions src/main/java/com/thalesgroup/hudson/plugins/xunit/XUnitPublisher.java

Large diffs are not rendered by default.

Expand Up @@ -24,14 +24,14 @@
package com.thalesgroup.hudson.plugins.xunit.service;

import com.google.inject.Inject;
import com.thalesgroup.dtkit.metrics.hudson.api.type.TestType;
import com.thalesgroup.dtkit.metrics.model.InputMetric;
import com.thalesgroup.dtkit.util.converter.ConversionException;
import com.thalesgroup.hudson.plugins.xunit.exception.XUnitException;
import com.thalesgroup.hudson.plugins.xunit.types.CustomInputMetric;
import com.thalesgroup.hudson.plugins.xunit.types.CustomType;
import hudson.FilePath;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;


Expand All @@ -45,25 +45,26 @@ void load(XUnitLog xUnitLog) {
this.xUnitLog = xUnitLog;
}

/**
* Prepares the conversion by adding specific behavior for the CustomType
*
* @param xUnitToolInfo the xUnit info wrapper object
* @param workspace the current workspace
* @throws com.thalesgroup.hudson.plugins.xunit.exception.XUnitException
* an XUnitException is thrown if there is a preparation error.
*/
private void prepareConversion(XUnitToolInfo xUnitToolInfo, File workspace) throws XUnitException {
TestType testType = xUnitToolInfo.getTestType();
if (testType.getClass() == CustomType.class) {
String xsl = ((CustomType) testType).getCustomXSL();
File xslFile = new File(workspace, xsl);
if (!xslFile.exists()) {
throw new XUnitException("The input xsl '" + xsl + "' relative to the workspace '" + workspace + "'doesn't exist.");
}
xUnitToolInfo.setCusXSLFile(xslFile);
}
}
// /**
// * Prepares the conversion by adding specific behavior for the CustomType
// *
// * @param xUnitToolInfo the xUnit info wrapper object
// * @param workspace the current workspace
// * @throws com.thalesgroup.hudson.plugins.xunit.exception.XUnitException
// * an XUnitException is thrown if there is a preparation error.
// */
// private void prepareConversion(XUnitToolInfo xUnitToolInfo, File workspace) throws XUnitException {
// TestType testType = xUnitToolInfo.getTestType();
// InputMetric inputMetric = xUnitToolInfo.getInputMetric();
// if (testType.getClass() == CustomInputMetric.class) {
// String xsl = ((CustomType) testType).getCustomXSL();
// File xslFile = new File(workspace, xsl);
// if (!xslFile.exists()) {
// throw new XUnitException("The input xsl '" + xsl + "' relative to the workspace '" + workspace + "'doesn't exist.");
// }
// xUnitToolInfo.setCusXSLFile(xslFile);
// }
// }


/**
Expand All @@ -80,12 +81,9 @@ private void prepareConversion(XUnitToolInfo xUnitToolInfo, File workspace) thro
@SuppressWarnings("ResultOfMethodCallIgnored")
public File convert(XUnitToolInfo xUnitToolInfo, File inputFile, File workspace, File junitOutputDirectory) throws XUnitException {

//Prepare the conversion when there is a custom type
prepareConversion(xUnitToolInfo, workspace);

TestType testType = xUnitToolInfo.getTestType();
//prepareConversion(xUnitToolInfo, workspace);

InputMetric inputMetric = testType.getInputMetric();
InputMetric inputMetric = xUnitToolInfo.getInputMetric();

final String JUNIT_FILE_POSTFIX = ".xml";
final String JUNIT_FILE_PREFIX = "TEST-";
Expand All @@ -98,16 +96,28 @@ public File convert(XUnitToolInfo xUnitToolInfo, File inputFile, File workspace,
infoSystemLogger("Converting '" + inputFile + "' .");
try {

//Set the XSL for custom type
if (testType.getClass() == CustomType.class) {
((CustomInputMetric) inputMetric).setCustomXSLFile(xUnitToolInfo.getCusXSLFile());

if (inputMetric instanceof CustomInputMetric) {
CustomInputMetric customInputMetric = (CustomInputMetric) inputMetric;
FilePath xslFilePath = xUnitToolInfo.getCusXSLFile();
if (!xslFilePath.exists()) {
throw new XUnitException("The input xsl '" + xslFilePath.getName() + "' relative to the workspace '" + workspace + "'doesn't exist.");
}
customInputMetric.setCustomXSLFile(new File(xUnitToolInfo.getCusXSLFile().getRemote()));
}

inputMetric.convert(inputFile, junitTargetFile);

} catch (ConversionException ce) {
throw new XUnitException("Conversion error " + ce.getMessage(), ce);
} catch (InterruptedException ie) {
throw new XUnitException("Conversion error " + ie.getMessage(), ie);
} catch (IOException ie) {
throw new XUnitException("Conversion error " + ie.getMessage(), ie);
}

return junitTargetFile;
}


}
Expand Up @@ -24,7 +24,6 @@
package com.thalesgroup.hudson.plugins.xunit.service;

import com.google.inject.Inject;
import com.thalesgroup.dtkit.metrics.hudson.api.type.TestType;
import hudson.Util;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.types.FileSet;
Expand Down Expand Up @@ -66,7 +65,7 @@ public boolean isEmptyPattern(String pattern) {
*/
public List<String> findReports(XUnitToolInfo xUnitToolInfo, File parentPath, String pattern) {

String toolName = xUnitToolInfo.getTestType().getDescriptor().getDisplayName();
String toolName = xUnitToolInfo.getInputMetric().getLabel();

FileSet fs = Util.createFileSet(parentPath, pattern);
DirectoryScanner ds = fs.getDirectoryScanner();
Expand Down Expand Up @@ -99,9 +98,7 @@ public List<String> findReports(XUnitToolInfo xUnitToolInfo, File parentPath, St
*/
public boolean checkIfFindsFilesNewFiles(XUnitToolInfo xUnitToolInfo, List<String> files, File workspace) {

TestType testTool = xUnitToolInfo.getTestType();

if (testTool.isFaildedIfNotNew()) {
if (xUnitToolInfo.isFailIfNotNew()) {
ArrayList<File> oldResults = new ArrayList<File>();
for (String value : files) {
File reportFile = new File(workspace, value);
Expand Down Expand Up @@ -154,7 +151,6 @@ public File getCurrentReport(File root, String name) {
* @return true if the xUnit must stop at the first error
*/
public boolean isStopProcessingIfError(XUnitToolInfo xUnitToolInfo) {
TestType testTool = xUnitToolInfo.getTestType();
return testTool.isStopProcessingIfError();
return xUnitToolInfo.isStopProcessingIfError();
}
}
Expand Up @@ -23,38 +23,48 @@

package com.thalesgroup.hudson.plugins.xunit.service;

import com.thalesgroup.dtkit.metrics.hudson.api.type.TestType;
import com.thalesgroup.dtkit.metrics.model.InputMetric;
import hudson.FilePath;

import java.io.File;
import java.io.Serializable;


public class XUnitToolInfo implements Serializable {

private File cusXSLFile;

private final TestType testType;
private final InputMetric inputMetric;

private final String expandedPattern;

private final boolean failIfNotNew;

private final boolean deleteOutputFiles;

private boolean stopProcessingIfError;

private final long buildTime;

public XUnitToolInfo(TestType testType, String expandedPattern, long buildTime) {
this.testType = testType;
private FilePath cusXSLFile;

public XUnitToolInfo(InputMetric inputMetric, String expandedPattern, Boolean failIfNotNew, Boolean deleteOutputFiles, Boolean stopProcessingIfError, long buildTime, FilePath cusXSLFile) {
this.inputMetric = inputMetric;
this.expandedPattern = expandedPattern;
this.failIfNotNew = failIfNotNew;
this.deleteOutputFiles = deleteOutputFiles;
this.stopProcessingIfError = stopProcessingIfError;
this.buildTime = buildTime;
}

public void setCusXSLFile(File cusXSLFile) {
this.cusXSLFile = cusXSLFile;
}

public File getCusXSLFile() {
// public void setCusXSLFile(File cusXSLFile) {
// this.cusXSLFile = cusXSLFile;
// }

public FilePath getCusXSLFile() {
return cusXSLFile;
}

public TestType getTestType() {
return testType;
public InputMetric getInputMetric() {
return inputMetric;
}

public String getExpandedPattern() {
Expand All @@ -65,7 +75,15 @@ public long getBuildTime() {
return buildTime;
}

public String getToolName() {
return testType.getDescriptor().getDisplayName();
public boolean isFailIfNotNew() {
return failIfNotNew;
}

public boolean isDeleteOutputFiles() {
return deleteOutputFiles;
}

public boolean isStopProcessingIfError() {
return stopProcessingIfError;
}
}
Expand Up @@ -79,7 +79,7 @@ public Boolean invoke(File ws, VirtualChannel channel) throws IOException {
warningSystemLogger(msg);
}

String metricName = xUnitToolInfo.getToolName();
String metricName = xUnitToolInfo.getInputMetric().getToolName();

//Gets all input files matching the user pattern
List<String> resultFiles = xUnitReportProcessingService.findReports(xUnitToolInfo, ws, xUnitToolInfo.getExpandedPattern());
Expand Down
Expand Up @@ -64,7 +64,7 @@ public boolean checkFileIsNotEmpty(File inputFile) {
*/
public boolean validateInputFile(XUnitToolInfo xUnitToolInfo, File inputFile) throws XUnitException {

InputMetric inputMetric = xUnitToolInfo.getTestType().getInputMetric();
InputMetric inputMetric = xUnitToolInfo.getInputMetric();

//Validates the input file (not empty)
try {
Expand Down Expand Up @@ -95,7 +95,7 @@ public boolean validateInputFile(XUnitToolInfo xUnitToolInfo, File inputFile) th
* @throws XUnitException an XUnitException when there are validation exceptions
*/
public boolean validateOutputFile(XUnitToolInfo xUnitToolInfo, File inputFile, File junitTargetFile) throws XUnitException {
InputMetric inputMetric = xUnitToolInfo.getTestType().getInputMetric();
InputMetric inputMetric = xUnitToolInfo.getInputMetric();

try {
//Validates the output
Expand Down
Expand Up @@ -88,7 +88,6 @@ public String[] getInputXsdNameList() {
public OutputMetric getOutputFormatType() {
return JUnitModel.OUTPUT_JUNIT_1_0;
}

}

public static class MyTestTypeDescriptor extends TestTypeDescriptor<MyTestType> {
Expand Down Expand Up @@ -144,7 +143,7 @@ public void findReportsOneFile() throws IOException {
f1.createNewFile();

XUnitToolInfo xUnitToolInfoMock = mock(XUnitToolInfo.class);
when(xUnitToolInfoMock.getTestType()).thenReturn(new MyTestType("", true, true));
when(xUnitToolInfoMock.getInputMetric()).thenReturn(new MyInputMetric());

List<String> xUnitFiles = xUnitReportProcessingService.findReports(xUnitToolInfoMock, dir, "*.txt");
Assert.assertFalse(xUnitFiles.isEmpty());
Expand Down
Expand Up @@ -26,6 +26,11 @@
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Stage;
import com.thalesgroup.dtkit.junit.model.JUnitModel;
import com.thalesgroup.dtkit.metrics.model.InputMetricType;
import com.thalesgroup.dtkit.metrics.model.InputMetricXSL;
import com.thalesgroup.dtkit.metrics.model.InputType;
import com.thalesgroup.dtkit.metrics.model.OutputMetric;
import com.thalesgroup.hudson.plugins.xunit.service.*;
import hudson.model.BuildListener;
import hudson.remoting.VirtualChannel;
Expand Down Expand Up @@ -62,6 +67,10 @@ public class XUnitTransformerTest {
@SuppressWarnings("unused")
private XUnitValidationService xUnitValidationServiceMock;

@Mock
@SuppressWarnings("unused")
private XUnitToolInfo xUnitToolInfoMock;

@Rule
public TempWorkspace tempWorkspace = new TempWorkspace();

Expand All @@ -73,19 +82,59 @@ public void beforeTest() throws IOException {
MockitoAnnotations.initMocks(this);

when(buildListenerMock.getLogger()).thenReturn(new PrintStream(new ByteArrayOutputStream()));
when(xUnitToolInfoMock.getInputMetric()).thenReturn(new MyInputMetric());

xUnitTransformer = Guice.createInjector(Stage.DEVELOPMENT, new AbstractModule() {
@Override
protected void configure() {
bind(BuildListener.class).toInstance(buildListenerMock);
bind(XUnitToolInfo.class).toInstance(mock(XUnitToolInfo.class));
bind(XUnitToolInfo.class).toInstance(xUnitToolInfoMock);
bind(XUnitConversionService.class).toInstance(xUnitConversionServiceMock);
bind(XUnitValidationService.class).toInstance(xUnitValidationServiceMock);
bind(XUnitReportProcessingService.class).toInstance(xUnitReportProcessingServiceMock);
}
}).getInstance(XUnitTransformer.class);

}

public static class MyInputMetric extends InputMetricXSL {
@Override
public String getToolName() {
return "testTool";
}

@Override
public String getToolVersion() {
return "testVersion";
}

@Override
public InputMetricType getInputMetricType() {
return InputMetricType.XSL;
}

@Override
public InputType getToolType() {
return InputType.TEST;
}

@Override
public String getXslName() {
return null;
}

@Override
public String[] getInputXsdNameList() {
return null;
}

@Override
public OutputMetric getOutputFormatType() {
return JUnitModel.OUTPUT_JUNIT_1_0;
}
}


@Test
public void emptyResultFiles() throws Exception {

Expand Down

0 comments on commit 05f1a21

Please sign in to comment.