Skip to content

Commit

Permalink
Refactoring to try to get Stax2 validation tests more reusable
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Apr 2, 2018
1 parent 4a1549c commit bae0330
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 167 deletions.
4 changes: 4 additions & 0 deletions src/test/java/stax2/vstream/BaseStax2ValidationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
public abstract class BaseStax2ValidationTest
extends BaseStax2Test
{
// 02-Apr-2018, tatu: not the cleanest thing ever but has to do for now; needed
// by implementations other than Woodstox, that do NOT support non-ns mode.
protected final static boolean HAS_NON_NS_MODE = true;

protected XMLValidationSchemaFactory newW3CSchemaValidatorFactory() {
return new W3CSchemaFactory();
}
Expand Down
315 changes: 148 additions & 167 deletions src/test/java/stax2/vwstream/TestAttributeValidation.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,103 +26,104 @@ public class TestAttributeValidation
final String IMPLIED_NS_DTD_STR = "<!ELEMENT root EMPTY>\n"
+"<!ATTLIST root "+NS_PREFIX+":attr CDATA #REQUIRED>\n";

public void testValidFixedAttr()
throws XMLStreamException
public void testValidFixedAttr() throws XMLStreamException
{
for (int i = 0; i < 3; ++i) {
boolean nsAware = (i >= 1);
boolean repairing = (i == 2);
StringWriter strw = new StringWriter();
if (HAS_NON_NS_MODE) { // only test non-ns mode if supported
_testValidFixedAttr(true, false);
}
_testValidFixedAttr(true, false);
_testValidFixedAttr(true, true);
}

// Ok either without being added:
XMLStreamWriter2 sw = getDTDValidatingWriter(strw, FIXED_DTD_STR, nsAware, repairing);
sw.writeStartElement("root");
sw.writeEndElement();
sw.writeEndDocument();
sw.close();
private void _testValidFixedAttr(boolean nsAware, boolean repairing) throws XMLStreamException
{
StringWriter strw = new StringWriter();

// Ok either without being added:
XMLStreamWriter2 sw = getDTDValidatingWriter(strw, FIXED_DTD_STR, nsAware, repairing);
sw.writeStartElement("root");
sw.writeEndElement();
sw.writeEndDocument();
sw.close();

sw = getDTDValidatingWriter(strw, FIXED_DTD_STR, nsAware, repairing);
sw.writeEmptyElement("root");
sw.writeEndDocument();
sw.close();

// or by using the exact same value
sw = getDTDValidatingWriter(strw, FIXED_DTD_STR, nsAware, repairing);
sw.writeStartElement("root");
sw.writeAttribute("fixAttr", "fixedValue");
sw.writeEndElement();
sw.writeEndDocument();
sw.close();
}

sw = getDTDValidatingWriter(strw, FIXED_DTD_STR, nsAware, repairing);
sw.writeEmptyElement("root");
sw.writeEndDocument();
sw.close();
public void testInvalidFixedAttr() throws XMLStreamException
{
if (HAS_NON_NS_MODE) { // only test non-ns mode if supported
_testInvalidFixedAttr(true, false);
}
_testInvalidFixedAttr(true, false);
_testInvalidFixedAttr(true, true);
}

// or by using the exact same value
sw = getDTDValidatingWriter(strw, FIXED_DTD_STR, nsAware, repairing);
sw.writeStartElement("root");
sw.writeAttribute("fixAttr", "fixedValue");
sw.writeEndElement();
sw.writeEndDocument();
sw.close();
private void _testInvalidFixedAttr(boolean nsAware, boolean repairing)
throws XMLStreamException
{
String modeDesc = String.format("[ns-aware? %s, repairing? %s]", nsAware, repairing);

// Invalid case, trying to add some other value:

// non-empty but not same
StringWriter strw = new StringWriter();
XMLStreamWriter2 sw = getDTDValidatingWriter(strw, FIXED_DTD_STR, nsAware, repairing);
sw.writeStartElement("root");
try {
sw.writeAttribute("fixAttr", "otherValue");
fail(modeDesc+" Expected a validation exception when trying to add a #FIXED attribute with 'wrong' value");
} catch (XMLValidationException vex) {
// expected...
}
// Should not close, since stream is invalid now...

// empty is not the same as leaving it out:
strw = new StringWriter();
sw = getDTDValidatingWriter(strw, FIXED_DTD_STR, nsAware, repairing);
sw.writeStartElement("root");
try {
sw.writeAttribute("fixAttr", "");
fail(modeDesc+" Expected a validation exception when trying to add a #FIXED attribute with an empty value");
} catch (XMLValidationException vex) {
// expected...
}

// And finally, same for empty elem in case impl. is different
strw = new StringWriter();
sw = getDTDValidatingWriter(strw, FIXED_DTD_STR, nsAware, repairing);
sw.writeEmptyElement("root");
try {
sw.writeAttribute("fixAttr", "foobar");
fail(modeDesc+" Expected a validation exception when trying to add a #FIXED attribute with an empty value");
} catch (XMLValidationException vex) {
// expected...
}
}

public void testInvalidFixedAttr()
throws XMLStreamException
public void testValidRequiredAttr() throws XMLStreamException
{
for (int i = 0; i < 3; ++i) {
boolean nsAware, repairing;
String modeDesc;

switch (i) {
case 0:
modeDesc = "[non-namespace-aware]";
nsAware = repairing = false;
break;
case 1:
modeDesc = "[namespace-aware, non-repairing]";
nsAware = true;
repairing = false;
break;
default:
modeDesc = "[namespace-aware, repairing]";
nsAware = repairing = true;
break;
}

// Invalid case, trying to add some other value:

// non-empty but not same
StringWriter strw = new StringWriter();
XMLStreamWriter2 sw = getDTDValidatingWriter(strw, FIXED_DTD_STR, nsAware, repairing);
sw.writeStartElement("root");
try {
sw.writeAttribute("fixAttr", "otherValue");
fail(modeDesc+" Expected a validation exception when trying to add a #FIXED attribute with 'wrong' value");
} catch (XMLValidationException vex) {
// expected...
}
// Should not close, since stream is invalid now...

// empty is not the same as leaving it out:
strw = new StringWriter();
sw = getDTDValidatingWriter(strw, FIXED_DTD_STR, nsAware, repairing);
sw.writeStartElement("root");
try {
sw.writeAttribute("fixAttr", "");
fail(modeDesc+" Expected a validation exception when trying to add a #FIXED attribute with an empty value");
} catch (XMLValidationException vex) {
// expected...
}

// And finally, same for empty elem in case impl. is different
strw = new StringWriter();
sw = getDTDValidatingWriter(strw, FIXED_DTD_STR, nsAware, repairing);
sw.writeEmptyElement("root");
try {
sw.writeAttribute("fixAttr", "foobar");
fail(modeDesc+" Expected a validation exception when trying to add a #FIXED attribute with an empty value");
} catch (XMLValidationException vex) {
// expected...
}
if (HAS_NON_NS_MODE) { // only test non-ns mode if supported
_testValidRequiredAttr(true, false);
}
_testValidRequiredAttr(true, false);
_testValidRequiredAttr(true, true);
}

public void testValidRequiredAttr()
private void _testValidRequiredAttr(boolean nsAware, boolean repairing)
throws XMLStreamException
{
for (int i = 0; i < 3; ++i) {
boolean nsAware = (i >= 1);
boolean repairing = (i == 2);
StringWriter strw = new StringWriter();

// Ok if value is added:
Expand Down Expand Up @@ -150,106 +151,86 @@ public void testValidRequiredAttr()
}
}

public void testInvalidRequiredAttr()
public void testInvalidRequiredAttr() throws XMLStreamException
{
if (HAS_NON_NS_MODE) { // only test non-ns mode if supported
_testInvalidRequiredAttr(true, false);
}
_testInvalidRequiredAttr(true, false);
_testInvalidRequiredAttr(true, true);
}

public void _testInvalidRequiredAttr(boolean nsAware, boolean repairing)
throws XMLStreamException
{
for (int i = 0; i < 3; ++i) {
boolean nsAware, repairing;
String modeDesc;

switch (i) {
case 0:
modeDesc = "[non-namespace-aware]";
nsAware = repairing = false;
break;
case 1:
modeDesc = "[namespace-aware, non-repairing]";
nsAware = true;
repairing = false;
break;
default:
modeDesc = "[namespace-aware, repairing]";
nsAware = repairing = true;
break;
}

// Invalid case: leaving the required attr out:
StringWriter strw = new StringWriter();
XMLStreamWriter2 sw = getDTDValidatingWriter(strw, REQUIRED_DTD_STR, nsAware, repairing);
sw.writeStartElement("root");
try {
sw.writeEndElement();
fail(modeDesc+" Expected a validation exception when omitting a #REQUIRED attribute");
} catch (XMLValidationException vex) {
// expected...
}
// Should not close, since stream is invalid now...
String modeDesc = String.format("[ns-aware? %s, repairing? %s]", nsAware, repairing);

// Invalid case: leaving the required attr out:
StringWriter strw = new StringWriter();
XMLStreamWriter2 sw = getDTDValidatingWriter(strw, REQUIRED_DTD_STR, nsAware, repairing);
sw.writeStartElement("root");
try {
sw.writeEndElement();
fail(modeDesc+" Expected a validation exception when omitting a #REQUIRED attribute");
} catch (XMLValidationException vex) {
// expected...
}
// Should not close, since stream is invalid now...
}

/**
* Test to ensure that the namespace-prefix mapping works (to the degree
* it can... wrt dtd-non-ns-awareness) with attributes.
*/
public void testValidNsAttr()
throws XMLStreamException
public void testValidNsAttr() throws XMLStreamException
{
for (int i = 0; i < 2; ++i) {
boolean repairing = (i > 0);
StringWriter strw = new StringWriter();
_testValidNsAttr(false);
_testValidNsAttr(true);
}

/* Ok, as long as we use the right ns prefix... better also
* output namespace declaration, in non-repairing mode.
*/
XMLStreamWriter2 sw = getDTDValidatingWriter(strw, IMPLIED_NS_DTD_STR, true, repairing);
sw.writeStartElement("root");
if (!repairing) {
sw.writeNamespace(NS_PREFIX, NS_URI);
}
// prefix, uri, localname (for attrs!)
sw.writeAttribute(NS_PREFIX, NS_URI, "attr", "value");
sw.writeEndElement();
sw.writeEndDocument();
sw.close();
private void _testValidNsAttr(boolean repairing) throws XMLStreamException
{
StringWriter strw = new StringWriter();

// Ok, as long as we use the right ns prefix... better also
// output namespace declaration, in non-repairing mode.
XMLStreamWriter2 sw = getDTDValidatingWriter(strw, IMPLIED_NS_DTD_STR, true, repairing);
sw.writeStartElement("root");
if (!repairing) {
sw.writeNamespace(NS_PREFIX, NS_URI);
}
// prefix, uri, localname (for attrs!)
sw.writeAttribute(NS_PREFIX, NS_URI, "attr", "value");
sw.writeEndElement();
sw.writeEndDocument();
sw.close();
}

public void testInvalidNsAttr()
throws XMLStreamException
public void testInvalidNsAttr() throws XMLStreamException
{
for (int i = 0; i < 2; ++i) {
boolean repairing;
String modeDesc;

switch (i) {
case 0:
modeDesc = "[namespace-aware, non-repairing]";
repairing = false;
break;
default:
modeDesc = "[namespace-aware, repairing]";
repairing = true;
break;
}

// Invalid case, trying to use "wrong" prefix:
_testInvalidNsAttr(false);
_testInvalidNsAttr(true);
}

StringWriter strw = new StringWriter();
XMLStreamWriter2 sw = getDTDValidatingWriter(strw, IMPLIED_NS_DTD_STR, true, repairing);
sw.writeStartElement("root");
if (!repairing) {
sw.writeNamespace(NS_PREFIX, NS_URI);
}
// prefix, uri, localname (for attrs!)
try {
sw.writeAttribute(NS_PREFIX2, NS_URI, "attr", "value");
fail(modeDesc+" Expected a validation exception when trying to add an attribute with wrong ns prefix");
} catch (XMLValidationException vex) {
// expected...
}
// Should not close, since stream is invalid now...
private void _testInvalidNsAttr(boolean repairing) throws XMLStreamException
{
String modeDesc = "[namespace-aware, repairing? "+repairing+"]";

// Invalid case, trying to use "wrong" prefix:

StringWriter strw = new StringWriter();
XMLStreamWriter2 sw = getDTDValidatingWriter(strw, IMPLIED_NS_DTD_STR, true, repairing);
sw.writeStartElement("root");
if (!repairing) {
sw.writeNamespace(NS_PREFIX, NS_URI);
}
// prefix, uri, localname (for attrs!)
try {
sw.writeAttribute(NS_PREFIX2, NS_URI, "attr", "value");
fail(modeDesc+" Expected a validation exception when trying to add an attribute with wrong ns prefix");
} catch (XMLValidationException vex) {
// expected...
}
// Should not close, since stream is invalid now...
}

}

0 comments on commit bae0330

Please sign in to comment.