Skip to content

Commit

Permalink
Added new ANT parameters failOnValidation...; #50
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed May 28, 2019
1 parent 8bfbb0f commit d661f2d
Show file tree
Hide file tree
Showing 12 changed files with 284 additions and 11 deletions.
Expand Up @@ -217,6 +217,24 @@ void addToMap (@Nonnull final Map <String, String> aMap)
*/
private boolean m_bFailOnError = true;

/**
* <code>true</code> if the build should fail if any validation "error"
* occurs. Defaults to <code>false</code>. Since v5.0.11.
*/
private boolean m_bFailOnValidationError = false;

/**
* <code>true</code> if the build should fail if any validation "warnings"
* occurs. Defaults to <code>false</code>. Since v5.0.11.
*/
private boolean m_bFailOnValidationWarn = false;

/**
* <code>true</code> if the build should fail if any validation "information"
* occurs. Defaults to <code>false</code>. Since v5.0.11.
*/
private boolean m_bFailOnValidationInfo = false;

/**
* For resolving entities such as DTDs. This is used both for the Schematron
* file as well as for the XML files to be validated.
Expand Down Expand Up @@ -353,6 +371,27 @@ public void setFailOnError (final boolean bFailOnError)
_debug (bFailOnError ? "Will fail on error" : "Will not fail on error");
}

public void setFailOnValidationError (final boolean bFail)
{
m_bFailOnValidationError = bFail;

_debug (bFail ? "Will fail on validation error" : "Will not fail on validation error");
}

public void setFailOnValidationWarn (final boolean bFail)
{
m_bFailOnValidationWarn = bFail;

_debug (bFail ? "Will fail on validation warning" : "Will not fail on validation warning");
}

public void setFailOnValidationInfo (final boolean bFail)
{
m_bFailOnValidationInfo = bFail;

_debug (bFail ? "Will fail on validation information" : "Will not fail on validation information");
}

/**
* Add the catalog to our internal catalog
*
Expand Down Expand Up @@ -508,10 +547,13 @@ private void _performValidation (@Nonnull final ISchematronResource aSch,
_debug ("Created SVRL:\n" + new SVRLMarshaller ().getAsString (aSOT));

final ICommonsList <AbstractSVRLMessage> aMessages = SVRLHelper.getAllFailedAssertionsAndSuccessfulReports (aSOT);
final int nErrorMessages = aMessages.getCount (x -> x.getFlag ().isError ());
final int nWarningMessages = aMessages.size () - nErrorMessages;
final int nErrorMessages = aMessages.getCount (x -> x.getFlag ().isGT (EErrorLevel.WARN));
final int nWarningMessages = aMessages.getCount (x -> x.getFlag ().isEQ (EErrorLevel.WARN));
final int nInfoMessages = aMessages.getCount (x -> x.getFlag ().isLT (EErrorLevel.WARN));
final String sErrors = nErrorMessages + " Schematron error" + (nErrorMessages == 1 ? "" : "s");
final String sWarnings = nWarningMessages + " Schematron warning" + (nWarningMessages == 1 ? "" : "s");
// No plural - haha
final String sInfos = nInfoMessages + " Schematron information";

final boolean bExpectationFulfilled;
if (bExpectSuccess)
Expand All @@ -526,16 +568,19 @@ private void _performValidation (@Nonnull final ISchematronResource aSch,
"' was validated against Schematron '" +
aSch.getResource ().getPath () +
"' and matches the rules" +
(nWarningMessages > 0 ? " - only " +
(nWarningMessages > 0 ? " (" +
sWarnings +
(nWarningMessages == 1 ? " is" : " are") +
" contained"
: ""));
" contained)"
: "") +
(nInfoMessages > 0 ? " (" + sInfos + (nInfoMessages == 1 ? " is" : " are") + " contained)"
: ""));
}
else
{
_error (sErrors +
(nWarningMessages > 0 ? " and " + sWarnings : "") +
(nInfoMessages > 0 ? " and " + sInfos : "") +
" for XML file '" +
aXMLFile.getPath () +
"'");
Expand All @@ -555,16 +600,22 @@ private void _performValidation (@Nonnull final ISchematronResource aSch,
"' - " +
sErrors +
(nWarningMessages > 0 ? " and " + sWarnings : "") +
(nErrorMessages == 1 && nWarningMessages == 0 ? " was" : " were") +
(nInfoMessages > 0 ? " and " + sInfos : "") +
(nErrorMessages == 1 && (nWarningMessages + nInfoMessages) == 0 ? " was" : " were") +
" found (as expected)");
}
else
{
String sMessage = "No Schematron errors for erroneous XML file '" + aXMLFile.getPath () + "'";
if (nWarningMessages > 0)
sMessage += " - only " + sWarnings + (nWarningMessages == 1 ? " is" : " are") + " contained";

_error (sMessage);
_error ("No Schematron errors for erroneous XML file '" +
aXMLFile.getPath () +
"'" +
(nWarningMessages > 0 ? " (" +
sWarnings +
(nWarningMessages == 1 ? " is" : " are") +
" contained)"
: "") +
(nInfoMessages > 0 ? " (" + sInfos + (nInfoMessages == 1 ? " is" : " are") + " contained)"
: ""));
}
}

Expand All @@ -585,6 +636,12 @@ private void _performValidation (@Nonnull final ISchematronResource aSch,

if (!bExpectationFulfilled)
_errorOrFail ("The expectations were not fullfilled, therefore the overall result is negative");
if (nErrorMessages > 0 && m_bFailOnValidationError)
throw new BuildException ("Validation errors are present.");
if (nWarningMessages > 0 && m_bFailOnValidationWarn)
throw new BuildException ("Validation warnings are present.");
if (nInfoMessages > 0 && m_bFailOnValidationInfo)
throw new BuildException ("Validation information are present.");
}
catch (final BuildException up)
{
Expand Down
@@ -0,0 +1,102 @@
/**
* Copyright (C) 2017-2019 Philip Helger (www.helger.com)
* philip[at]helger[dot]com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.helger.schematron.ant;

import static org.junit.Assert.fail;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildFileRule;
import org.junit.Rule;
import org.junit.Test;

public final class Issue50Test
{
@Rule
public final BuildFileRule m_aBuildRule = new BuildFileRule ();

@Test
public void testSuccess ()
{
m_aBuildRule.configureProject ("src/test/resources/issues/50/build.xml");
m_aBuildRule.getProject ().addBuildListener (new LoggingBuildListener ());
m_aBuildRule.getProject ().executeTarget ("schematron");
}

@Test
public void testFailureError ()
{
try
{
m_aBuildRule.configureProject ("src/test/resources/issues/50/build-fail-error.xml");
m_aBuildRule.getProject ().addBuildListener (new LoggingBuildListener ());
m_aBuildRule.getProject ().executeTarget ("schematron");
fail ();
}
catch (final BuildException ex)
{
// Expected
}
}

@Test
public void testFailureFatal ()
{
try
{
m_aBuildRule.configureProject ("src/test/resources/issues/50/build-fail-fatal.xml");
m_aBuildRule.getProject ().addBuildListener (new LoggingBuildListener ());
m_aBuildRule.getProject ().executeTarget ("schematron");
fail ();
}
catch (final BuildException ex)
{
// Expected
}
}

@Test
public void testFailureInfo ()
{
try
{
m_aBuildRule.configureProject ("src/test/resources/issues/50/build-fail-info.xml");
m_aBuildRule.getProject ().addBuildListener (new LoggingBuildListener ());
m_aBuildRule.getProject ().executeTarget ("schematron");
fail ();
}
catch (final BuildException ex)
{
// Expected
}
}

@Test
public void testFailureWarn ()
{
try
{
m_aBuildRule.configureProject ("src/test/resources/issues/50/build-fail-warn.xml");
m_aBuildRule.getProject ().addBuildListener (new LoggingBuildListener ());
m_aBuildRule.getProject ().executeTarget ("schematron");
fail ();
}
catch (final BuildException ex)
{
// Expected
}
}
}
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<taskdef name="schematron" classname="com.helger.schematron.ant.Schematron" />

<target name="schematron" description="Schematron rule tests">
<schematron schematronFile="rule-error.sch" expectSuccess="false" failOnError="false" failOnValidationError="true" failOnValidationWarn="false" failOnValidationInfo="false">
<fileset dir=".">
<include name="dog*.xml" />
</fileset>
</schematron>
</target>
</project>
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<taskdef name="schematron" classname="com.helger.schematron.ant.Schematron" />

<target name="schematron" description="Schematron rule tests">
<schematron schematronFile="rule-fatal.sch" expectSuccess="false" failOnError="false" failOnValidationError="true" failOnValidationWarn="false" failOnValidationInfo="false">
<fileset dir=".">
<include name="dog*.xml" />
</fileset>
</schematron>
</target>
</project>
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<taskdef name="schematron" classname="com.helger.schematron.ant.Schematron" />

<target name="schematron" description="Schematron rule tests">
<schematron schematronFile="rule-info.sch" expectSuccess="true" failOnError="false" failOnValidationError="false" failOnValidationWarn="false" failOnValidationInfo="true">
<fileset dir=".">
<include name="dog*.xml" />
</fileset>
</schematron>
</target>
</project>
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<taskdef name="schematron" classname="com.helger.schematron.ant.Schematron" />

<target name="schematron" description="Schematron rule tests">
<schematron schematronFile="rule-warn.sch" expectSuccess="true" failOnError="false" failOnValidationError="false" failOnValidationWarn="true" failOnValidationInfo="false">
<fileset dir=".">
<include name="dog*.xml" />
</fileset>
</schematron>
</target>
</project>
30 changes: 30 additions & 0 deletions ph-schematron-ant-task/src/test/resources/issues/50/build.xml
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<taskdef name="schematron" classname="com.helger.schematron.ant.Schematron" />

<target name="schematron" description="Schematron rule tests">
<schematron schematronFile="rule-error.sch" expectSuccess="false" failOnError="false" failOnValidationError="false" failOnValidationWarn="true" failOnValidationInfo="true">
<fileset dir=".">
<include name="dog*.xml" />
</fileset>
</schematron>

<schematron schematronFile="rule-fatal.sch" expectSuccess="false" failOnError="false" failOnValidationError="false" failOnValidationWarn="true" failOnValidationInfo="true">
<fileset dir=".">
<include name="dog*.xml" />
</fileset>
</schematron>

<schematron schematronFile="rule-info.sch" expectSuccess="true" failOnError="false" failOnValidationError="true" failOnValidationWarn="true" failOnValidationInfo="false">
<fileset dir=".">
<include name="dog*.xml" />
</fileset>
</schematron>

<schematron schematronFile="rule-warn.sch" expectSuccess="true" failOnError="false" failOnValidationError="true" failOnValidationWarn="false" failOnValidationInfo="true">
<fileset dir=".">
<include name="dog*.xml" />
</fileset>
</schematron>
</target>
</project>
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<dog xml:lang="de">
<fleas/>
</dog>
@@ -0,0 +1,8 @@
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
<title>Example of Multi-Lingual Schema</title>
<pattern>
<rule context="dog">
<assert test="bone" role="error">A dog should have a bone.</assert>
</rule>
</pattern>
</schema>
@@ -0,0 +1,8 @@
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
<title>Example of Multi-Lingual Schema</title>
<pattern>
<rule context="dog">
<assert test="bone" role="fatal">A dog should have a bone.</assert>
</rule>
</pattern>
</schema>
@@ -0,0 +1,8 @@
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
<title>Example of Multi-Lingual Schema</title>
<pattern>
<rule context="dog">
<assert test="bone" role="info">A dog should have a bone.</assert>
</rule>
</pattern>
</schema>
@@ -0,0 +1,8 @@
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
<title>Example of Multi-Lingual Schema</title>
<pattern>
<rule context="dog">
<assert test="bone" role="warn">A dog should have a bone.</assert>
</rule>
</pattern>
</schema>

0 comments on commit d661f2d

Please sign in to comment.