Skip to content
Snippets Groups Projects
Commit 31d50169 authored by Goik Martin's avatar Goik Martin
Browse files

Individual test validation

parent 07aeac9a
No related branches found
No related tags found
No related merge requests found
......@@ -52,6 +52,13 @@
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-core</artifactId>
<version>2.6.2</version>
</dependency>
</dependencies>
<build>
......
......@@ -7,27 +7,25 @@ public class RunTests {
static public final MarkingListener markingListener = new MarkingListener();
@SafeVarargs
public static void exec(Class<? extends Object> ... classz) {
private static void execImpl(Class<? extends Object> ... classz) {
final JUnitCore jc = new JUnitCore();
jc.addListener(markingListener);
System.setOut(DummyPrintStream.out);
jc.run(classz);
System.setOut(DummyPrintStream.systemDotOut);
System.out.println(RunTests.getMarking());
}
@SafeVarargs
public static void exec(Class<? extends Object> ... classz) {
execImpl(classz);
System.out.println(getMarking());
}
@SafeVarargs
public static void exec(final String exerciseTitle, Class<? extends Object> ... classz) {
markingListener.clear();
final JUnitCore jc = new JUnitCore();
jc.addListener(markingListener);
System.setOut(DummyPrintStream.out);
jc.run(classz);
System.setOut(DummyPrintStream.systemDotOut);
System.out.println(RunTests.getMarking(exerciseTitle));
execImpl(classz);
System.out.println(getMarking(exerciseTitle));
System.out.println("-----------------------------------------------------\n");
}
......@@ -38,4 +36,4 @@ public class RunTests {
static public String getMarking(final String exerciseTitle) {
return markingListener.getPointSumms(exerciseTitle);
}
}
}
\ No newline at end of file
package de.hdm_stuttgart.mi.exam.unitmarking.xml;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.List;
import java.util.Vector;
import java.util.stream.Collectors;
public class WellformedCheckHandler extends DefaultHandler {
private List<SAXParseException> problems = new Vector<>();
@Override
public void warning(SAXParseException e) throws SAXException {
problems.add(e);
}
@Override
public void error(SAXParseException e) throws SAXException {
problems.add(e);
}
@Override
public void fatalError(SAXParseException e) throws SAXException {
problems.add(e);
}
public String formatErrorMessages() {
return problems.stream().map(s -> "line=" + s.getLineNumber() +
", column=" + s.getColumnNumber() + ":\n " + s.getMessage()).
collect(Collectors.joining("\n "));
}
public void clear() {
problems.clear();
}
}
\ No newline at end of file
package de.hdm_stuttgart.mi.exam.unitmarking.xml;
import org.junit.Assert;
import org.xml.sax.SAXException;
import org.xmlunit.XMLUnitException;
import org.xmlunit.builder.Input;
import org.xmlunit.validation.Languages;
import org.xmlunit.validation.ValidationResult;
import org.xmlunit.validation.Validator;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.IOException;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
public class XmlTestSupport {
static {
final Validator xmlSchemaValidatorTmp = Validator.forLanguage(Languages.W3C_XML_SCHEMA_NS_URI);
xmlSchemaValidatorTmp.setSchemaSource(new StreamSource("src/main/resources/XMLSchema.xsd"));
xmlSchemaValidator = xmlSchemaValidatorTmp;
}
static final Validator xmlSchemaValidator;
private SAXParser nonValidatingParser;
private WellformedCheckHandler wch;
private final String schemaFileName;
private final Validator validator;
public final String schemaInvalidErrorMessages;
public XmlTestSupport(final String schemaFileName) {
this.schemaFileName = schemaFileName;
Validator validatorTmp = null;
String schemaInvalidErrorMessagesTmp = null;
wch = new WellformedCheckHandler();
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setValidating(false);
spf.setNamespaceAware(true);
nonValidatingParser = spf.newSAXParser();
nonValidatingParser.parse(new File(schemaFileName), wch);
// Document is well-formed, so what about validity?
try {
final Source source = Input.fromFile(schemaFileName).build();
final ValidationResult r = xmlSchemaValidator.validateInstance(source);
if (r.isValid()) {
validatorTmp = Validator.forLanguage(Languages.W3C_XML_SCHEMA_NS_URI);
validatorTmp.setSchemaSource(new StreamSource(schemaFileName));
} else {
schemaInvalidErrorMessagesTmp = "Schema " + schemaFileName + " is invalid:"
+ formatErrorMessages(r);
}
} catch (final XMLUnitException e) {
final Throwable cause = e.getCause();
schemaInvalidErrorMessagesTmp =
null == cause?
e.getMessage():
" Not well-formed?\n " + cause.getMessage();
}
} catch (SAXException e) {
schemaInvalidErrorMessagesTmp = " Unable to parse " + schemaFileName + " " +
wch.formatErrorMessages() + "\n schema is possibly not well-formed\n " +
e.getMessage();
}catch (ParserConfigurationException | IOException e) {
schemaInvalidErrorMessagesTmp = " Unable to parse " + schemaFileName + " " +
e.getMessage() ;
}
validator = validatorTmp;
schemaInvalidErrorMessages = schemaInvalidErrorMessagesTmp;
}
public void assertValid(final String instanceFileName) {
parseWellFormed(instanceFileName);
final ValidationResult r = validator.validateInstance(Input.fromFile(instanceFileName).build());
if (! r.isValid()) {
Assert.fail(instanceFileName + " expected to be valid but:\n " + formatErrorMessages(r) + "\n");
}
}
private void parseWellFormed(final String instanceFileName) {
wch.clear();
try {
nonValidatingParser.parse(new File(instanceFileName), wch);
} catch (final SAXException e) {
Assert.fail(instanceFileName + " possibly not well-formed:\n " +
wch.formatErrorMessages());
}catch (final IOException e) {
Assert.fail(e.getMessage());
}
}
public void assertInvalid(final String instanceFileName) {
parseWellFormed(instanceFileName);
if (validator.validateInstance(Input.fromFile(instanceFileName).build()).isValid()) {
Assert.fail(instanceFileName + "should be invalid\n");
}
}
static public String formatErrorMessages(final ValidationResult r) {
return StreamSupport.
stream(r.getProblems().spliterator(), true).
map(p -> "\n line=" + p.getLine() + ", column=" + p.getColumn() + ":\n " + p.getMessage()).
collect(Collectors.joining("\n "));
}
}
\ No newline at end of file
This diff is collapsed.
<?xml version='1.0'?>
<xs:schema targetNamespace="http://www.w3.org/XML/1998/namespace"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns ="http://www.w3.org/1999/xhtml"
xml:lang="en">
<xs:annotation>
<xs:documentation>
<div>
<h1>About the XML namespace</h1>
<div class="bodytext">
<p>
This schema document describes the XML namespace, in a form
suitable for import by other schema documents.
</p>
<p>
See <a href="http://www.w3.org/XML/1998/namespace.html">
http://www.w3.org/XML/1998/namespace.html</a> and
<a href="http://www.w3.org/TR/REC-xml">
http://www.w3.org/TR/REC-xml</a> for information
about this namespace.
</p>
<p>
Note that local names in this namespace are intended to be
defined only by the World Wide Web Consortium or its subgroups.
The names currently defined in this namespace are listed below.
They should not be used with conflicting semantics by any Working
Group, specification, or document instance.
</p>
<p>
See further below in this document for more information about <a
href="#usage">how to refer to this schema document from your own
XSD schema documents</a> and about <a href="#nsversioning">the
namespace-versioning policy governing this schema document</a>.
</p>
</div>
</div>
</xs:documentation>
</xs:annotation>
<xs:attribute name="lang">
<xs:annotation>
<xs:documentation>
<div>
<h3>lang (as an attribute name)</h3>
<p>
denotes an attribute whose value
is a language code for the natural language of the content of
any element; its value is inherited. This name is reserved
by virtue of its definition in the XML specification.</p>
</div>
<div>
<h4>Notes</h4>
<p>
Attempting to install the relevant ISO 2- and 3-letter
codes as the enumerated possible values is probably never
going to be a realistic possibility.
</p>
<p>
See BCP 47 at <a href="http://www.rfc-editor.org/rfc/bcp/bcp47.txt">
http://www.rfc-editor.org/rfc/bcp/bcp47.txt</a>
and the IANA language subtag registry at
<a href="http://www.iana.org/assignments/language-subtag-registry">
http://www.iana.org/assignments/language-subtag-registry</a>
for further information.
</p>
<p>
The union allows for the 'un-declaration' of xml:lang with
the empty string.
</p>
</div>
</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:union memberTypes="xs:language">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value=""/>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="space">
<xs:annotation>
<xs:documentation>
<div>
<h3>space (as an attribute name)</h3>
<p>
denotes an attribute whose
value is a keyword indicating what whitespace processing
discipline is intended for the content of the element; its
value is inherited. This name is reserved by virtue of its
definition in the XML specification.</p>
</div>
</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:NCName">
<xs:enumeration value="default"/>
<xs:enumeration value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="base" type="xs:anyURI"> <xs:annotation>
<xs:documentation>
<div>
<h3>base (as an attribute name)</h3>
<p>
denotes an attribute whose value
provides a URI to be used as the base for interpreting any
relative URIs in the scope of the element on which it
appears; its value is inherited. This name is reserved
by virtue of its definition in the XML Base specification.</p>
<p>
See <a
href="http://www.w3.org/TR/xmlbase/">http://www.w3.org/TR/xmlbase/</a>
for information about this attribute.
</p>
</div>
</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="id" type="xs:ID">
<xs:annotation>
<xs:documentation>
<div>
<h3>id (as an attribute name)</h3>
<p>
denotes an attribute whose value
should be interpreted as if declared to be of type ID.
This name is reserved by virtue of its definition in the
xml:id specification.</p>
<p>
See <a
href="http://www.w3.org/TR/xml-id/">http://www.w3.org/TR/xml-id/</a>
for information about this attribute.
</p>
</div>
</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attributeGroup name="specialAttrs">
<xs:attribute ref="xml:base"/>
<xs:attribute ref="xml:lang"/>
<xs:attribute ref="xml:space"/>
<xs:attribute ref="xml:id"/>
</xs:attributeGroup>
<xs:annotation>
<xs:documentation>
<div>
<h3>Father (in any context at all)</h3>
<div class="bodytext">
<p>
denotes Jon Bosak, the chair of
the original XML Working Group. This name is reserved by
the following decision of the W3C XML Plenary and
XML Coordination groups:
</p>
<blockquote>
<p>
In appreciation for his vision, leadership and
dedication the W3C XML Plenary on this 10th day of
February, 2000, reserves for Jon Bosak in perpetuity
the XML name "xml:Father".
</p>
</blockquote>
</div>
</div>
</xs:documentation>
</xs:annotation>
<xs:annotation>
<xs:documentation>
<div xml:id="usage" id="usage">
<h2><a name="usage">About this schema document</a></h2>
<div class="bodytext">
<p>
This schema defines attributes and an attribute group suitable
for use by schemas wishing to allow <code>xml:base</code>,
<code>xml:lang</code>, <code>xml:space</code> or
<code>xml:id</code> attributes on elements they define.
</p>
<p>
To enable this, such a schema must import this schema for
the XML namespace, e.g. as follows:
</p>
<pre>
&lt;schema . . .>
. . .
&lt;import namespace="http://www.w3.org/XML/1998/namespace"
schemaLocation="http://www.w3.org/2001/xml.xsd"/>
</pre>
<p>
or
</p>
<pre>
&lt;import namespace="http://www.w3.org/XML/1998/namespace"
schemaLocation="http://www.w3.org/2009/01/xml.xsd"/>
</pre>
<p>
Subsequently, qualified reference to any of the attributes or the
group defined below will have the desired effect, e.g.
</p>
<pre>
&lt;type . . .>
. . .
&lt;attributeGroup ref="xml:specialAttrs"/>
</pre>
<p>
will define a type which will schema-validate an instance element
with any of those attributes.
</p>
</div>
</div>
</xs:documentation>
</xs:annotation>
<xs:annotation>
<xs:documentation>
<div id="nsversioning" xml:id="nsversioning">
<h2><a name="nsversioning">Versioning policy for this schema document</a></h2>
<div class="bodytext">
<p>
In keeping with the XML Schema WG's standard versioning
policy, this schema document will persist at
<a href="http://www.w3.org/2009/01/xml.xsd">
http://www.w3.org/2009/01/xml.xsd</a>.
</p>
<p>
At the date of issue it can also be found at
<a href="http://www.w3.org/2001/xml.xsd">
http://www.w3.org/2001/xml.xsd</a>.
</p>
<p>
The schema document at that URI may however change in the future,
in order to remain compatible with the latest version of XML
Schema itself, or with the XML namespace itself. In other words,
if the XML Schema or XML namespaces change, the version of this
document at <a href="http://www.w3.org/2001/xml.xsd">
http://www.w3.org/2001/xml.xsd
</a>
will change accordingly; the version at
<a href="http://www.w3.org/2009/01/xml.xsd">
http://www.w3.org/2009/01/xml.xsd
</a>
will not change.
</p>
<p>
Previous dated (and unchanging) versions of this schema
document are at:
</p>
<ul>
<li><a href="http://www.w3.org/2009/01/xml.xsd">
http://www.w3.org/2009/01/xml.xsd</a></li>
<li><a href="http://www.w3.org/2007/08/xml.xsd">
http://www.w3.org/2007/08/xml.xsd</a></li>
<li><a href="http://www.w3.org/2004/10/xml.xsd">
http://www.w3.org/2004/10/xml.xsd</a></li>
<li><a href="http://www.w3.org/2001/03/xml.xsd">
http://www.w3.org/2001/03/xml.xsd</a></li>
</ul>
</div>
</div>
</xs:documentation>
</xs:annotation>
</xs:schema>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment