| Method from org.dom4j.io.DTDTest Detail: |
protected void assertSameDTDSubset(String txt,
List expected,
List actual) {
/*
* Nothing expected?
*/
if (expected == null) {
if (actual == null) {
return; // Nothing found.
} else {
fail("Not expecting " + txt + " DTD subset.");
}
} else {
/*
* Something expected.
*/
if (actual == null) {
fail("Expecting " + txt + " DTD subset.");
}
/*
* Correct #of declarations found?
*/
assertEquals(txt + " DTD subset has correct #of declarations"
+ ": expected=[" + expected.toString() + "]" + ", actual=["
+ actual.toString() + "]", expected.size(), actual.size());
/*
* Check order, type, and values of each declaration. Serialization
* tests are done separately.
*/
Iterator itr1 = expected.iterator();
Iterator itr2 = actual.iterator();
while (itr1.hasNext()) {
Object obj1 = itr1.next();
Object obj2 = itr2.next();
assertEquals(txt + " DTD subset: Same type of declaration",
obj1.getClass().getName(), obj2.getClass().getName());
if (obj1 instanceof AttributeDecl) {
assertSameDecl((AttributeDecl) obj1, (AttributeDecl) obj2);
} else if (obj1 instanceof ElementDecl) {
assertSameDecl((ElementDecl) obj1, (ElementDecl) obj2);
} else if (obj1 instanceof InternalEntityDecl) {
assertSameDecl((InternalEntityDecl) obj1,
(InternalEntityDecl) obj2);
} else if (obj1 instanceof ExternalEntityDecl) {
assertSameDecl((ExternalEntityDecl) obj1,
(ExternalEntityDecl) obj2);
} else {
throw new AssertionError("Unexpected declaration type: "
+ obj1.getClass());
}
}
}
}
Test helper method compares an expected set of DTD declarations with an
actual set of DTD declarations. This method should be invoked seperately
for the internal DTD subset and the external DTD subset. The declarations
must occur in their logical ordering. See Lexical Handler for conformance
criteria. |
public void assertSameDecl(AttributeDecl expected,
AttributeDecl actual) {
assertEquals("attributeName is correct", expected.getAttributeName(),
actual.getAttributeName());
assertEquals("elementName is correct", expected.getElementName(),
actual.getElementName());
assertEquals("type is correct", expected.getType(), actual.getType());
assertEquals("value is not correct", expected.getValue(), actual
.getValue());
assertEquals("valueDefault is correct", expected.getValueDefault(),
actual.getValueDefault());
assertEquals("toString() is correct", expected.toString(), actual
.toString());
}
Test helper method compares an expected and an actual AttributeDecl . |
protected void assertSameDecl(ElementDecl expected,
ElementDecl actual) {
assertEquals("name is correct", expected.getName(), actual.getName());
assertEquals("model is not correct", expected.getModel(), actual
.getModel());
assertEquals("toString() is correct", expected.toString(), actual
.toString());
}
Test helper method compares an expected and an actual ElementDecl . |
protected void assertSameDecl(InternalEntityDecl expected,
InternalEntityDecl actual) {
assertEquals("name is correct", expected.getName(), actual.getName());
assertEquals("value is not correct", expected.getValue(), actual
.getValue());
assertEquals("toString() is correct", expected.toString(), actual
.toString());
}
|
protected void assertSameDecl(ExternalEntityDecl expected,
ExternalEntityDecl actual) {
assertEquals("name is correct", expected.getName(), actual.getName());
assertEquals("publicID is correct", expected.getPublicID(), actual
.getPublicID());
assertEquals("systemID is correct", expected.getSystemID(), actual
.getSystemID());
assertEquals("toString() is correct", expected.toString(), actual
.toString());
}
|
protected void assertSameDocumentType(DocumentType expected,
DocumentType actual) {
/*
* Nothing expected?
*/
if (expected == null) {
if (actual == null) {
return; // Nothing found.
} else {
fail("Not expecting DOCTYPE.");
}
} else {
/*
* Something expected.
*/
if (actual == null) {
fail("Expecting DOCTYPE");
}
log("Expected DocumentType:\n" + expected.toString());
log("Actual DocumentType:\n" + actual.toString());
// Check the internal DTD subset.
assertSameDTDSubset("Internal", expected.getInternalDeclarations(),
actual.getInternalDeclarations());
// Check the external DTD subset.
assertSameDTDSubset("External", expected.getExternalDeclarations(),
actual.getExternalDeclarations());
}
}
Test helper method compares the expected and actual DocumentType
objects, including their internal and external DTD subsets.
|
protected List getExternalDeclarations() {
List decls = new ArrayList();
decls.add(new ElementDecl("another-greeting", "(#PCDATA)"));
return decls;
}
Test helper method returns a List of DTD declarations that
represents the expected external DTD subset (for the tests that use an
external DTD subset). |
protected List getInternalDeclarations() {
List decls = new ArrayList();
decls.add(new ElementDecl("greeting", "(#PCDATA)"));
decls.add(new AttributeDecl("greeting", "foo", "ID", "#IMPLIED", null));
decls.add(new InternalEntityDecl("%boolean", "( true | false )"));
return decls;
}
Test helper method returns a List of DTD declarations that
represents the expected internal DTD subset (for the tests that use an
internal DTD subset).
Note: The declarations returned by this method MUST agree those actually
declared in #XML_INTERNAL_FILE and #XML_MIXED .
|
public static void main(String[] args) {
TestRunner.run(DTDTest.class);
}
|
protected Document readDocument(String resourceName,
boolean includeInternal,
boolean includeExternal) throws Exception {
SAXReader reader = new SAXReader();
reader.setIncludeInternalDTDDeclarations(includeInternal);
reader.setIncludeExternalDTDDeclarations(includeExternal);
reader.setEntityResolver(new MyEntityResolver(DTD_FILE,
DTD_PUBLICID, DTD_SYSTEM_ID));
return getDocument(resourceName, reader);
}
Helper method reads a local resource and parses it as an XML document.
The internal and external DTD subsets are optionally retained by the
parser and exposed via the DocumentType object on the returned
Document . The parser is configured with an EntityResolver that knows how to find the local resource identified by
#DTD_FILE whose SYSTEM identifier is given by #DTD_SYSTEM_ID . |
public void testExternalDTDSubset() {
/*
* Setup the expected DocumentType.
*/
DocumentType expected = new DefaultDocumentType("another-greeting",
null, DTD_SYSTEM_ID);
expected.setExternalDeclarations(getExternalDeclarations());
/*
* Parse the test XML document and compare the expected and actual
* DOCTYPEs.
*/
try {
assertSameDocumentType(expected, readDocument(
XML_EXTERNAL_FILE, false, true).getDocType());
} catch (AssertionFailedError ex) {
throw ex;
} catch (Throwable t) {
fail("Not expecting: " + t);
}
}
Test verifies correct identification of the external DTD subset and
correct non-presence of the internal DTD subset. |
public void testInternalDTDSubset() throws Exception {
/*
* Setup the expected DocumentType.
*
* @todo dom4j should expose a DefaultDocumentType constructor that
* accepts only the elementName property. This is used when only an
* internal DTD subset is being provided via the < !DOCTYPE foo [...] >
* syntax, in which case there is neither a SYSTEM nor PUBLIC
* identifier.
*/
DocumentType expected = new DefaultDocumentType();
expected.setElementName("greeting");
expected.setInternalDeclarations(getInternalDeclarations());
/*
* Parse the test XML document and compare the expected and actual
* DOCTYPEs.
*/
try {
assertSameDocumentType(expected, readDocument(
XML_INTERNAL_FILE, true, false).getDocType());
} catch (AssertionFailedError ex) {
throw ex;
} catch (Throwable t) {
fail("Not expecting: " + t);
}
}
Test verifies correct identification of the internal DTD subset and
correct non-presence of the external DTD subset. |
public void testMixedDTDSubset() {
/*
* Setup the expected DocumentType.
*/
DocumentType expected = new DefaultDocumentType("another-greeting",
null, DTD_SYSTEM_ID);
expected.setInternalDeclarations(getInternalDeclarations());
expected.setExternalDeclarations(getExternalDeclarations());
/*
* Parse the test XML document and compare the expected and actual
* DOCTYPEs.
*/
try {
assertSameDocumentType(expected, readDocument(XML_MIXED,
true, true).getDocType());
} catch (AssertionFailedError ex) {
throw ex;
} catch (Throwable t) {
fail("Not expecting: " + t);
}
}
Test verifies correct identification of the internal and external DTD
subsets. |