Skip to content

Commit

Permalink
Fixed JENKINS-10909
Browse files Browse the repository at this point in the history
  • Loading branch information
gboissinot committed Sep 9, 2011
1 parent ea2662f commit ea51f37
Show file tree
Hide file tree
Showing 10 changed files with 324 additions and 7 deletions.
@@ -0,0 +1,47 @@
package org.jenkinsci.plugins.xunit.types;

import com.thalesgroup.dtkit.junit.model.JUnitModel;
import com.thalesgroup.dtkit.metrics.model.InputMetricXSL;
import com.thalesgroup.dtkit.metrics.model.InputType;
import com.thalesgroup.dtkit.metrics.model.OutputMetric;

/**
* @author Gregory Boissinot
*/
public class CheckInputMetric extends InputMetricXSL {

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

@Override
public String getToolVersion() {
return "N/A";
}

@Override
public String getToolName() {
return "Check";
}

@Override
public boolean isDefault() {
return true;
}

@Override
public String getXslName() {
return "check-to-junit-4.xsl";
}

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

@Override
public OutputMetric getOutputFormatType() {
return JUnitModel.OUTPUT_JUNIT_4;
}
}
43 changes: 43 additions & 0 deletions src/main/java/org/jenkinsci/plugins/xunit/types/CheckType.java
@@ -0,0 +1,43 @@
package org.jenkinsci.plugins.xunit.types;

import com.thalesgroup.dtkit.metrics.hudson.api.descriptor.TestTypeDescriptor;
import com.thalesgroup.dtkit.metrics.hudson.api.type.TestType;
import com.thalesgroup.dtkit.metrics.model.InputMetric;
import com.thalesgroup.dtkit.metrics.model.InputMetricException;
import com.thalesgroup.dtkit.metrics.model.InputMetricFactory;
import com.thalesgroup.hudson.plugins.xunit.types.CustomInputMetric;
import hudson.Extension;
import org.kohsuke.stapler.DataBoundConstructor;

/**
* @author Gregory Boissinot
*/
public class CheckType extends TestType {

@DataBoundConstructor
public CheckType(String pattern, boolean failureIfNotNew, boolean deleteOutputFiles, boolean stopProcessingIfError) {
super(pattern, failureIfNotNew, deleteOutputFiles, stopProcessingIfError);
}

@Extension
public static class CheckInputMetricDescriptor extends TestTypeDescriptor<CheckType> {

public CheckInputMetricDescriptor() {
super(CheckType.class, null);
}

@Override
public String getId() {
return this.getClass().getName();
}

@Override
public InputMetric getInputMetric() {
try {
return InputMetricFactory.getInstance(CheckInputMetric.class);
} catch (InputMetricException e) {
throw new RuntimeException("Can't create the inputMetric object for the class " + CustomInputMetric.class);
}
}
}
}
@@ -0,0 +1,49 @@
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ck="http://check.sourceforge.net/ns"
exclude-result-prefixes="ck">
<xsl:output method="xml" indent="yes"/>

<xsl:variable name="checkCount" select="count(//ck:suite/ck:test)"/>
<xsl:variable name="checkCountFailure"
select="count(//ck:suite/ck:test[@result='failure'])"/>
<xsl:variable name="suitename" select="//ck:suite/ck:title"/>

<xsl:template match="/">
<testsuite>
<xsl:attribute name="errors">0</xsl:attribute>
<xsl:attribute name="tests">
<xsl:value-of select="$checkCount"/>
</xsl:attribute>
<xsl:attribute name="failures">
<xsl:value-of select="$checkCountFailure"/>
</xsl:attribute>
<xsl:attribute name="name">
<xsl:value-of select="$suitename"/>
</xsl:attribute>
<xsl:apply-templates/>
</testsuite>
</xsl:template>

<xsl:template match="//ck:suite/ck:test">
<testcase>
<xsl:attribute name="name">
<xsl:value-of select="./ck:id"/>
</xsl:attribute>
<xsl:attribute name="classname">
<xsl:value-of select="$suitename"/>
</xsl:attribute>
<xsl:attribute name="time">0</xsl:attribute>
<xsl:if test="@result = 'failure'">
<error type="error">
<xsl:attribute name="message">
<xsl:value-of select="./ck:message"/>
</xsl:attribute>
</error>
</xsl:if>
</testcase>
</xsl:template>

<!-- this swallows all unmatched text -->
<xsl:template match="text()|@*"/>
</xsl:stylesheet>
Expand Up @@ -4,21 +4,22 @@
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import com.thalesgroup.dtkit.metrics.model.InputMetric;
import com.thalesgroup.dtkit.util.converter.ConversionService;
import com.thalesgroup.dtkit.util.validator.ValidationError;
import com.thalesgroup.dtkit.util.validator.ValidationService;
import com.thalesgroup.hudson.plugins.xunit.types.CustomInputMetric;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.XMLUnit;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.*;

/**
* @author Gregory Boissinot
*/
public class CustomTest {
public abstract class AbstractTest {

private static Injector injector;

Expand Down Expand Up @@ -57,7 +58,36 @@ private String readXmlAsString(File input)
return xmlString;
}

private void convertAndValidate(String inputXMLPath, String inputXSLPath, String expectedResultPath) throws Exception {
protected void convertAndValidate(Class<? extends InputMetric> metricClass, String inputXMLPath, String expectedResultPath) throws Exception {
InputMetric inputMetric = injector.getInstance(metricClass);

File outputXMLFile = File.createTempFile("result", "xml");
File inputXMLFile = new File(this.getClass().getResource(inputXMLPath).toURI());

//The input file must be valid
boolean inputResult = inputMetric.validateInputFile(inputXMLFile);
for (ValidationError validatorError : inputMetric.getInputValidationErrors()) {
System.out.println("[ERROR] " + validatorError.toString());
}
Assert.assertTrue(inputResult);

inputMetric.convert(inputXMLFile, outputXMLFile);
XMLUnit.setIgnoreWhitespace(true);
Diff myDiff = new Diff(readXmlAsString(new File(this.getClass().getResource(expectedResultPath).toURI())), readXmlAsString(outputXMLFile));
Assert.assertTrue("XSL transformation did not work" + myDiff, myDiff.similar());

//The generated output file must be valid
boolean outputResult = inputMetric.validateOutputFile(outputXMLFile);
for (ValidationError validatorError : inputMetric.getOutputValidationErrors()) {
System.out.println(validatorError);
}
Assert.assertTrue(outputResult);

outputXMLFile.deleteOnExit();

}

protected void convertAndValidate(String inputXMLPath, String inputXSLPath, String expectedResultPath) throws Exception {

CustomInputMetric customInputMetric = injector.getInstance(CustomInputMetric.class);
customInputMetric.setCustomXSLFile(new File(this.getClass().getResource(inputXSLPath).toURI()));
Expand Down Expand Up @@ -86,9 +116,5 @@ private void convertAndValidate(String inputXMLPath, String inputXSLPath, String
outputXMLFile.deleteOnExit();
}

@Test
public void testTestcase1() throws Exception {
convertAndValidate("customTool/testcase1/input.xml", "customTool/testcase1/input.xsl", "customTool/testcase1/result.xml");
}

}
19 changes: 19 additions & 0 deletions src/test/java/org/jenkinsci/plugins/xunit/types/CheckTypeTest.java
@@ -0,0 +1,19 @@
package org.jenkinsci.plugins.xunit.types;

import org.junit.Test;

/**
* @author Gregory Boissinot
*/
public class CheckTypeTest extends AbstractTest {

@Test
public void testTestCase1() throws Exception {
convertAndValidate(CheckInputMetric.class, "check/testcase1/input.xml", "check/testcase1/result.xml");
}

@Test
public void testTestCase2() throws Exception {
convertAndValidate(CheckInputMetric.class, "check/testcase2/input.xml", "check/testcase2/result.xml");
}
}
@@ -0,0 +1,15 @@
package org.jenkinsci.plugins.xunit.types;

import org.junit.Test;

/**
* @author Gregory Boissinot
*/
public class CustomTypeTest extends AbstractTest {

@Test
public void testTestCase1() throws Exception {
convertAndValidate("customTool/testcase1/input.xml", "customTool/testcase1/input.xsl", "customTool/testcase1/result.xml");
}

}
@@ -0,0 +1,48 @@
<?xml version="1.0"?>
<testsuites xmlns="http://check.sourceforge.net/ns">
<datetime>2011-09-08 09:40:32</datetime>
<suite>
<title>test_check1.c</title>
<test result="success">
<path>.</path>
<fn>test_check1.c:23</fn>
<id>test_list_3</id>
<iteration>0</iteration>
<description>Core</description>
<message>Passed</message>
</test>
<test result="success">
<path>.</path>
<fn>test_check1.c:33</fn>
<id>test_single_entry</id>
<iteration>0</iteration>
<description>Core</description>
<message>Passed</message>
</test>
<test result="success">
<path>.</path>
<fn>test_check1.c:46</fn>
<id>test_allow_spaces</id>
<iteration>0</iteration>
<description>Core</description>
<message>Passed</message>
</test>
<test result="success">
<path>.</path>
<fn>test_check1.c:60</fn>
<id>test_only_first_entries</id>
<iteration>0</iteration>
<description>Core</description>
<message>Passed</message>
</test>
<test result="success">
<path>.</path>
<fn>test_check1.c:69</fn>
<id>test_empty</id>
<iteration>0</iteration>
<description>Core</description>
<message>Passed</message>
</test>
</suite>
<duration>0.399376</duration>
</testsuites>
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuite errors="0" tests="5" failures="0" name="test_check1.c">
<testcase name="test_list_3" classname="test_check1.c" time="0"/>
<testcase name="test_single_entry" classname="test_check1.c" time="0"/>
<testcase name="test_allow_spaces" classname="test_check1.c" time="0"/>
<testcase name="test_only_first_entries" classname="test_check1.c" time="0"/>
<testcase name="test_empty" classname="test_check1.c" time="0"/>
</testsuite>
@@ -0,0 +1,48 @@
<?xml version="1.0"?>
<testsuites xmlns="http://check.sourceforge.net/ns">
<datetime>2011-09-08 09:43:11</datetime>
<suite>
<title>test_check1.c</title>
<test result="failure">
<path>.</path>
<fn>test_check1.c:19</fn>
<id>test_list_3</id>
<iteration>0</iteration>
<description>Core</description>
<message>Should have found 3 hosts</message>
</test>
<test result="success">
<path>.</path>
<fn>test_check1.c:33</fn>
<id>test_single_entry</id>
<iteration>0</iteration>
<description>Core</description>
<message>Passed</message>
</test>
<test result="failure">
<path>.</path>
<fn>test_check1.c:42</fn>
<id>test_allow_spaces</id>
<iteration>0</iteration>
<description>Core</description>
<message>Should have found 3 hosts</message>
</test>
<test result="failure">
<path>.</path>
<fn>test_check1.c:58</fn>
<id>test_only_first_entries</id>
<iteration>0</iteration>
<description>Core</description>
<message>Assertion 'strcmp("bbb", mylist[1]) == 0' failed</message>
</test>
<test result="success">
<path>.</path>
<fn>test_check1.c:69</fn>
<id>test_empty</id>
<iteration>0</iteration>
<description>Core</description>
<message>Passed</message>
</test>
</suite>
<duration>0.759352</duration>
</testsuites>
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuite errors="0" tests="5" failures="3" name="test_check1.c">
<testcase name="test_list_3" classname="test_check1.c" time="0">
<error type="error" message="Should have found 3 hosts"/>
</testcase>
<testcase name="test_single_entry" classname="test_check1.c" time="0"/>
<testcase name="test_allow_spaces" classname="test_check1.c" time="0">
<error type="error" message="Should have found 3 hosts"/>
</testcase>
<testcase name="test_only_first_entries" classname="test_check1.c" time="0">
<error type="error" message="Assertion 'strcmp(&#34;bbb&#34;, mylist[1]) == 0' failed"/>
</testcase>
<testcase name="test_empty" classname="test_check1.c" time="0"/>
</testsuite>

0 comments on commit ea51f37

Please sign in to comment.