Skip to content

Commit

Permalink
Improve and revise tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dizzzz committed Jan 11, 2025
1 parent dc88631 commit 1cfc847
Showing 1 changed file with 123 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@


import org.exist.test.ExistXmldbEmbeddedServer;

import org.junit.*;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.XMLDBException;

import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.modules.CollectionManagementService;

import static org.exist.collections.CollectionConfiguration.DEFAULT_COLLECTION_CONFIG_FILE;
Expand All @@ -44,14 +45,103 @@ public class CollectionConfigurationValidationModeTest {
@ClassRule
public static final ExistXmldbEmbeddedServer existEmbeddedServer = new ExistXmldbEmbeddedServer(false, true, true);

private static final String valid = "<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" targetNamespace=\"http://jmvanel.free.fr/xsd/addressBook\" elementFormDefault=\"qualified\">" + "<xsd:attribute name=\"uselessAttribute\" type=\"xsd:string\"/>" + "<xsd:complexType name=\"record\">" + "<xsd:sequence>" + "<xsd:element name=\"cname\" type=\"xsd:string\"/>" + "<xsd:element name=\"email\" type=\"xsd:string\"/>" + "</xsd:sequence>" + "</xsd:complexType>" + "<xsd:element name=\"addressBook\">" + "<xsd:complexType>" + "<xsd:sequence>" + "<xsd:element name=\"owner\" type=\"record\"/>" + "<xsd:element name=\"person\" type=\"record\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>" + "</xsd:sequence>" + "</xsd:complexType>" + "</xsd:element>" + "</xsd:schema>";
private static final String invalid = "<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" targetNamespace=\"http://jmvanel.free.fr/xsd/addressBook\" elementFormDefault=\"qualified\">" + "<xsd:attribute name=\"uselessAttribute\" type=\"xsd:string\"/>" + "<xsd:complexType name=\"record\">" + "<xsd:sequence>" + "<xsd:elementa name=\"cname\" type=\"xsd:string\"/>" + "<xsd:elementb name=\"email\" type=\"xsd:string\"/>" + "</xsd:sequence>" + "</xsd:complexType>" + "<xsd:element name=\"addressBook\">" + "<xsd:complexType>" + "<xsd:sequence>" + "<xsd:element name=\"owner\" type=\"record\"/>" + "<xsd:element name=\"person\" type=\"record\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>" + "</xsd:sequence>" + "</xsd:complexType>" + "</xsd:element>" + "</xsd:schema>";
private static final String anonymous = "<schema elementFormDefault=\"qualified\">" + "<attribute name=\"uselessAttribute\" type=\"string\"/>" + "<complexType name=\"record\">" + "<sequence>" + "<elementa name=\"cname\" type=\"string\"/>" + "<elementb name=\"email\" type=\"string\"/>" + "</sequence>" + "</complexType>" + "<element name=\"addressBook\">" + "<complexType>" + "<sequence>" + "<element name=\"owner\" type=\"record\"/>" + "<element name=\"person\" type=\"record\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>" + "</sequence>" + "</complexType>" + "</element>" + "</schema>";
private static final String different = "<asd:schema xmlns:asd=\"https://www.w3.org/2001/XMLSchemaschema\" targetNamespace=\"http://jmvanel.free.fr/xsd/addressBookbook\" elementFormDefault=\"qualified\">" + "<asd:attribute name=\"uselessAttribute\" type=\"asd:string\"/>" + "<asd:complexType name=\"record\">" + "<asd:sequence>" + "<asd:element name=\"cname\" type=\"asd:string\"/>" + "<asd:element name=\"email\" type=\"asd:string\"/>" + "</asd:sequence>" + "</asd:complexType>" + "<asd:element name=\"addressBook\">" + "<asd:complexType>" + "<asd:sequence>" + "<asd:element name=\"owner\" type=\"record\"/>" + "<asd:element name=\"person\" type=\"record\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>" + "</asd:sequence>" + "</asd:complexType>" + "</asd:element>" + "</asd:schema>";
private static final String VALID_XSD =
"""
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://jmvanel.free.fr/xsd/addressBook" elementFormDefault="qualified">
<xsd:attribute name="uselessAttribute" type="xsd:string"/>
<xsd:complexType name="record">
<xsd:sequence>
<xsd:element name="cname" type="xsd:string"/>
<xsd:element name="email" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="addressBook">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="owner" type="record"/>
<xsd:element name="person" type="record" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
""";

private static final String INVALID_XSD = """
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://jmvanel.free.fr/xsd/addressBook" elementFormDefault="qualified">
<xsd:attribute name="uselessAttribute" type="xsd:string"/>
<xsd:complexType name="record">
<xsd:sequence>
<xsd:elementa name="cname" type="xsd:string"/>
<xsd:elementb name="email" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="addressBook">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="owner" type="record"/>
<xsd:element name="person" type="record" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
""";
private static final String ANONYMOUS_XSD = """
<schema elementFormDefault="qualified">
<attribute name="uselessAttribute" type="string"/>
<complexType name="record">
<sequence>
<elementa name="cname" type="string"/>
<elementb name="email" type="string"/>
</sequence>
</complexType>
<element name="addressBook">
<complexType>
<sequence>
<element name="owner" type="record"/>
<element name="person" type="record" minOccurs="0" maxOccurs="unbounded"/>
</sequence>
</complexType>
</element>
</schema>
""";

private static final String xconf_yes = "<collection xmlns=\"http://exist-db.org/collection-config/1.0\"><validation mode=\"yes\"/></collection>";
private static final String xconf_no = "<collection xmlns=\"http://exist-db.org/collection-config/1.0\"><validation mode=\"no\"/></collection>";
private static final String xconf_auto = "<collection xmlns=\"http://exist-db.org/collection-config/1.0\"><validation mode=\"auto\"/></collection>";
private static final String DIFFERENT_XSD = """
<xsd:schema xmlns:xsd="https://www.w3.org/2001/XMLSchemaschema"
targetNamespace="http://jmvanel.free.fr/xsd/addressBookbook" elementFormDefault="qualified">
<xsd:attribute name="uselessAttribute" type="xsd:string"/>
<xsd:complexType name="record">
<xsd:sequence>
<xsd:element name="cname" type="xsd:string"/>
<xsd:element name="email" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="addressBook">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="owner" type="record"/>
<xsd:element name="person" type="record" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
""";
private static final String XCONF_YES = """
<collection xmlns="http://exist-db.org/collection-config/1.0">
<validation mode="yes"/>
</collection>
""";
private static final String XCONF_NO = """
<collection xmlns="http://exist-db.org/collection-config/1.0">
<validation mode="no"/>
</collection>
""";
private static final String XCONF_AUTO = """
<collection xmlns="http://exist-db.org/collection-config/1.0">
<validation mode="auto"/>
</collection>
""";

@AfterClass
public static void tearDownClass() throws Exception {
Expand Down Expand Up @@ -87,94 +177,83 @@ private void storeDocument(final String collection, final String name, final Str
@Test
public void insertModeFalse() throws XMLDBException {
createCollection("/db/false");
storeCollectionXconf("/db/system/config/db/false", xconf_no);
storeCollectionXconf("/db/system/config/db/false", XCONF_NO);

// namespace provided, valid document; should pass
storeDocument("/db/false", "valid.xml", valid);
storeDocument("/db/false", "valid.xml", VALID_XSD);

// namespace provided, invalid document; should pass
storeDocument("/db/false", "invalid.xml", invalid);
storeDocument("/db/false", "invalid.xml", INVALID_XSD);

// no namespace provided, should pass
storeDocument("/db/false", "anonymous.xml", anonymous);
storeDocument("/db/false", "anonymous.xml", ANONYMOUS_XSD);

// non resolvable namespace provided, should pass
storeDocument("/db/false", "different.xml", different);
storeDocument("/db/false", "different.xml", DIFFERENT_XSD);
}

@Test
public void insertModeTrue() throws XMLDBException {
createCollection("/db/true");
storeCollectionXconf("/db/system/config/db/true", xconf_yes);
storeCollectionXconf("/db/system/config/db/true", XCONF_YES);

// namespace provided, valid document; should pass
storeDocument("/db/true", "valid.xml", valid);
storeDocument("/db/true", "valid.xml", VALID_XSD);

// namespace provided, invalid document; should fail
try {
storeDocument("/db/true", "invalid.xml", invalid);
storeDocument("/db/true", "invalid.xml", INVALID_XSD);
fail("should have failed");
} catch (XMLDBException ex) {
String msg = ex.getMessage();
if (!msg.contains("cvc-complex-type.2.4.a: Invalid content was found")) {
fail(msg);
}
assertTrue(msg.contains("cvc-complex-type.2.4.a: Invalid content was found"));
}

// no namespace provided; should fail
try {
storeDocument("/db/true", "anonymous.xml", anonymous);
storeDocument("/db/true", "anonymous.xml", ANONYMOUS_XSD);
fail("should have failed");
} catch (XMLDBException ex) {
String msg = ex.getMessage();
if (!msg.contains("cvc-elt.1.a: Cannot find the declaration of element 'schema'.")) {
fail(msg);
}
assertTrue(msg.contains("cvc-elt.1.a: Cannot find the declaration of element 'schema'."));
}

// non resolvable namespace provided, should fail
try {
storeDocument("/db/true", "different.xml", different);
storeDocument("/db/true", "different.xml", DIFFERENT_XSD);
fail("should have failed");
} catch (XMLDBException ex) {
String msg = ex.getMessage();
if (!msg.contains("cvc-elt.1.a: Cannot find the declaration of element 'asd:schema'")) {
fail(msg);
}
assertTrue(msg.contains("schema_reference.4: Failed to read schema document"));
}
}

@Test
public void insertModeAuto() throws XMLDBException {
createCollection("/db/auto");
storeCollectionXconf("/db/system/config/db/auto", xconf_auto);
storeCollectionXconf("/db/system/config/db/auto", XCONF_AUTO);

// namespace provided, valid document; should pass
storeDocument("/db/auto", "valid.xml", valid);
storeDocument("/db/auto", "valid.xml", VALID_XSD);

// namespace provided, invalid document, should fail
try {
storeDocument("/db/auto", "invalid.xml", invalid);
storeDocument("/db/auto", "invalid.xml", INVALID_XSD);
fail("should have failed");
} catch (XMLDBException ex) {
String msg = ex.getMessage();
if (!msg.contains("cvc-complex-type.2.4.a: Invalid content was found")) {
fail(msg);
}
assertTrue(msg.contains("cvc-complex-type.2.4.a: Invalid content was found"));
}

// no namespace reference, should pass
storeDocument("/db/auto", "anonymous.xml", ANONYMOUS_XSD);

// non resolvable namespace provided, should fail
try {
storeDocument("/db/auto", "anonymous.xml", anonymous);
storeDocument("/db/auto", "different.xml", DIFFERENT_XSD);
} catch (XMLDBException ex) {
String msg = ex.getMessage();
if (!msg.contains("Cannot find the declaration of element 'schema'.")) {
fail(msg);
}
assertTrue(msg.contains("schema_reference.4: Failed to read schema document"));
}

// non resolvable namespace provided, should fail
storeDocument("/db/auto", "different.xml", different);
fail("schema https://www.w3.org/2001/XMLSchemaschema is missing, document should not be insertable");
}
}

0 comments on commit 1cfc847

Please sign in to comment.