Skip to content

Commit

Permalink
Enhanced XSD liquibase#10
Browse files Browse the repository at this point in the history
  • Loading branch information
sim51 authored and fbiville committed Apr 26, 2015
1 parent a27f982 commit 2234820
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

Expand Down Expand Up @@ -85,9 +86,13 @@ public Collection<Changeset> parse(ClassLoader classLoader, String masterChangel

// We validate the XML with the schema
try {
this.validateChangelogBySchema(this.getChangeLogInputStream(classLoader, masterChangelog));
Collection<String> errors = this.validateChangelogBySchema(this.getChangeLogInputStream(classLoader, masterChangelog));
log.debug("XML file {} as {} error(s)", new Object[]{ masterChangelog, errors.size() });
if(errors.size() > 0) {
throw new IllegalArgumentException(format("Schema validation failed on changelog <%s> : <%s>", masterChangelog, Arrays.toString(errors.toArray())));
}
} catch (IOException | SAXException e) {
throw new IllegalArgumentException(format("Schema validation failed on changelog <%s>.", masterChangelog), e);
throw new IllegalArgumentException(format("Schema validation exception on changelog <%s>.", masterChangelog), e);
}

// We convert the XML file to Changelog
Expand Down Expand Up @@ -131,8 +136,6 @@ private Collection<String> validateChangelogBySchema(InputStream is) throws IOEx
// validate the XML
validator.validate(new StreamSource(is));

log.debug("XML file as {} error(s)", new Object[]{ customErrorHandler.errors.size() });

// return errors if there are some ...
return customErrorHandler.errors;
}
Expand All @@ -147,18 +150,17 @@ private class CustomErrorHandler implements ErrorHandler {
@Override
public void warning(SAXParseException exception) throws SAXException {
log.warn("XSD validation warning : {}", new Object[]{exception.getMessage()});
errors.add(exception.getMessage());
}

@Override
public void fatalError(SAXParseException exception) throws SAXException {
log.error("XSD validation fatal : {}", new Object[]{exception.getMessage()});
public void error(SAXParseException exception) throws SAXException {
log.warn("XSD validation error : {}", new Object[]{exception.getMessage()});
errors.add(exception.getMessage());
}

@Override
public void error(SAXParseException exception) throws SAXException {
log.warn("XSD validation error : {}", new Object[]{exception.getMessage()});
public void fatalError(SAXParseException exception) throws SAXException {
log.error("XSD validation fatal : {}", new Object[]{exception.getMessage()});
errors.add(exception.getMessage());
}
}
Expand Down
35 changes: 31 additions & 4 deletions liquigraph-core/src/main/resources/schema/changelog.xsd
Original file line number Diff line number Diff line change
@@ -1,19 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">


<!-- ==================================================================== -->
<!-- Define query type -->
<!-- ==================================================================== -->
<xs:simpleType name="QueryType">
<xs:restriction base="xs:string" />
</xs:simpleType>


<!-- ==================================================================== -->
<!-- Define precondition child type -->
<!-- ==================================================================== -->
<xs:complexType name="PreconditionChildType">
<xs:sequence>
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element name="and" type="PreconditionChildType" minOccurs="0" />
<xs:element name="or" type="PreconditionChildType" minOccurs="0" />
<xs:element name="query" type="QueryType" minOccurs="0" />
</xs:choice>
</xs:sequence>
</xs:complexType>


<!-- ==================================================================== -->
<!-- Define precondition type -->
<!-- ==================================================================== -->
<xs:complexType name="PreconditionType">
<xs:sequence>
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element name="and" type="PreconditionChildType" minOccurs="0" />
<xs:element name="or" type="PreconditionChildType" minOccurs="0" />
<xs:element name="query" type="QueryType" minOccurs="0" />
</xs:choice>
</xs:sequence>

<!-- List of all attributes -->
<xs:attribute name="if-not-met" type="xs:string" use="required" />
</xs:complexType>


<!-- ==================================================================== -->
<!-- Define changeset type -->
<!-- ==================================================================== -->
Expand All @@ -23,16 +50,16 @@
<xs:sequence>
<xs:choice minOccurs="1" maxOccurs="2">
<xs:element name="precondition" type="PreconditionType" minOccurs="0" maxOccurs="1" />
<xs:element name="query" type="QueryType" />
<xs:element name="query" type="QueryType" minOccurs="1" maxOccurs="1" />
</xs:choice>
</xs:sequence>

<!-- List of all attributes -->
<xs:attribute name="id" type="xs:string" use="required" />
<xs:attribute name="author" type="xs:string" use="required" />
<xs:attribute name="contexts" type="xs:string" use="optional" />
<xs:attribute name="run-always" type="xs:boolean" use="optional"/>
<xs:attribute name="run-on-change" type="xs:boolean" use="optional"/>
<xs:attribute name="run-always" type="xs:boolean" use="optional" />
<xs:attribute name="run-on-change" type="xs:boolean" use="optional" />

</xs:complexType>

Expand All @@ -43,7 +70,7 @@
<xs:element name="changelog">
<xs:complexType>
<xs:sequence>
<xs:element name="changeset" type="ChangesetType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="changeset" type="ChangesetType" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.liquigraph.core.model.*;
import org.xml.sax.SAXParseException;

import java.util.Collection;

Expand Down Expand Up @@ -119,13 +120,24 @@ private Precondition precondition(PreconditionErrorPolicy errorPolicy, String qu
}

@Test
public void parses_changelog_mal_formed() {
public void parses_schema_validation_changelog_mal_formed() {
try {
parser.parse(classLoader, "changelog/changelog-schema-mal-formed");
fail("Parser should failed with a mal formed XML");
parser.parse(classLoader, "changelog/schema/changelog-malformed.xml");
fail("Parser should failed with an error");
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("Schema validation failed on changelog <changelog/changelog-schema-mal-formed>.");
assertThat(e).hasMessage("Schema validation exception on changelog <changelog/schema/changelog-malformed.xml>.");
assertThat(e).hasCauseInstanceOf(SAXParseException.class);
}
}

@Test
public void parses_schema_validation_changelog_without_author() {
try {
parser.parse(classLoader, "changelog/schema/changelog-without-author.xml");
fail("Parser should failed with a mal formed XML");
} catch (IllegalArgumentException e) {
assertThat(e).hasMessageContaining("Schema validation failed on changelog <changelog/schema/changelog-without-author.xml>");
assertThat(e).hasMessageContaining("author");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<changelog>
<changeset id="first-changelog" author="fbiville">
<query><![CDATA[MATCH n RETURN n]]></query>
</changeset>
<changeset id="second-changelog">
<query><![CDATA[MATCH m RETURN m]]></query>
</changeset>
</changelog>

0 comments on commit 2234820

Please sign in to comment.