Removed some dead code and updated exception unit tests to lastest spec

This commit is contained in:
Willem Cazander 2025-01-29 17:20:43 +01:00
parent 856073cc5b
commit 275c4aa161
3 changed files with 76 additions and 128 deletions

View file

@ -261,10 +261,6 @@ public final class SAX3XMLConstants {
return true; return true;
} }
//static public boolean isCharRef(String c) {
// &#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';'
//}
static private boolean escapeXMLValueAppend(int codePoint, StringBuilder result) { static private boolean escapeXMLValueAppend(int codePoint, StringBuilder result) {
if (codePoint == '<') { if (codePoint == '<') {
result.append("&lt;"); result.append("&lt;");
@ -300,17 +296,7 @@ public final class SAX3XMLConstants {
if (escapeXMLValueAppend(c,result)) { if (escapeXMLValueAppend(c,result)) {
continue; continue;
} }
if (/*isNameChar(c)*/true==false) {// TODO: add correct result.appendCodePoint(c);
// 4.1 Character and Entity References;
// If the character reference begins with " &#x ", the digits and letters up to the terminating ; provide a hexadecimal representation of the character's code point in ISO/IEC 10646.
// If it begins just with " &# ", the digits up to the terminating ; provide a decimal representation of the character's code point.
result.append("&#x");
result.append(Integer.toHexString(c));
result.append(";");
continue;
} else {
result.appendCodePoint(c);
}
} }
return result.toString(); return result.toString();
} }
@ -320,8 +306,6 @@ public final class SAX3XMLConstants {
PrimitiveIterator.OfInt iterator = value.codePoints().iterator(); PrimitiveIterator.OfInt iterator = value.codePoints().iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
int c = iterator.nextInt(); int c = iterator.nextInt();
//for (int i=0;i<length;i++) {
// char c = value.charAt(i);
if (c == '&') { if (c == '&') {
StringBuilder entity = new StringBuilder(16); StringBuilder entity = new StringBuilder(16);
entity.appendCodePoint(c); entity.appendCodePoint(c);

View file

@ -83,35 +83,33 @@ public class SAX3WriterXmlAttributeTest {
@Test @Test
public void testAttributeWhiteSpace() throws Exception { public void testAttributeWhiteSpace() throws Exception {
StringWriter outputWriter = new StringWriter(); StringWriter outputWriter = new StringWriter();
SAX3WriterXml writer = new SAX3WriterXml(outputWriter); try (SAX3WriterXml writer = new SAX3WriterXml(outputWriter)) {
writer.startDocument(); writer.startDocument();
Assertions.assertThrows(SAXException.class, () -> {
Assertions.assertThrows(SAXException.class, () -> { AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", "", "", "", "junit");
writer.startElementEnd("", "test", "", atts);
});
Assertions.assertThrows(SAXException.class, () -> {
AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", " ", "", "", "junit");
writer.startElementEnd("", "test", "", atts);
});
Assertions.assertThrows(SAXException.class, () -> {
AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", "foo bar", "", "", "junit");
writer.startElementEnd("", "test", "", atts);
});
Assertions.assertThrows(SAXException.class, () -> {
AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", "foobar", "", "", "junit");
writer.startElementEnd("", "test junit", "", atts);
});
AttributesImpl atts = new AttributesImpl(); AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", "", "", "", "junit"); atts.addAttribute ("", "abc", "", "", " junit ");
writer.startElementEnd("", "test", "", atts); writer.startElementEnd("", "root", "", atts);
}); writer.endDocument();
Assertions.assertThrows(SAXException.class, () -> { }
AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", " ", "", "", "junit");
writer.startElementEnd("", "test", "", atts);
});
Assertions.assertThrows(SAXException.class, () -> {
AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", "foo bar", "", "", "junit");
writer.startElementEnd("", "test", "", atts);
});
Assertions.assertThrows(SAXException.class, () -> {
AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", "foobar", "", "", "junit");
writer.startElementEnd("", "test junit", "", atts);
});
AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", "abc", "", "", " junit ");
writer.startElementEnd("", "root", "", atts);
writer.endDocument();
String output = outputWriter.toString(); String output = outputWriter.toString();
Assertions.assertNotNull(output); Assertions.assertNotNull(output);
Assertions.assertTrue(output.length()>0); Assertions.assertTrue(output.length()>0);

View file

@ -103,65 +103,49 @@ public class SAX3WriterXmlTest {
@Test @Test
public void testXmlInvalid() throws Exception { public void testXmlInvalid() throws Exception {
StringWriter outputWriter = new StringWriter(); StringWriter outputWriter = new StringWriter();
SAX3WriterXml writer = new SAX3WriterXml(outputWriter); try (SAX3WriterXml writer = new SAX3WriterXml(outputWriter)) {
AttributesImpl atts = new AttributesImpl(); SAXException e = Assertions.assertThrows(SAXException.class, () -> {
AttributesImpl atts = new AttributesImpl();
Exception e = null; writer.startDocument();
try { writer.startElement("", "test", "", atts);
writer.startDocument(); writer.startElement("", "foobar", "", atts);
writer.startElement("", "test", "", atts); writer.endElement("", "test", "");
writer.startElement("", "foobar", "", atts); writer.endDocument();
writer.endElement("", "test", ""); });
writer.endDocument(); Assertions.assertTrue(e.getMessage().contains("tag"));
} catch (Exception catchE) { Assertions.assertTrue(e.getMessage().contains("foobar"));
e = catchE;
} }
Assertions.assertNotNull(e);
Assertions.assertEquals(SAXException.class, e.getClass());
Assertions.assertTrue(e.getMessage().contains("tag"));
Assertions.assertTrue(e.getMessage().contains("foobar"));
} }
@Test @Test
public void testXmlInvalidEnd() throws Exception { public void testXmlInvalidEnd() throws Exception {
StringWriter outputWriter = new StringWriter(); StringWriter outputWriter = new StringWriter();
SAX3WriterXml writer = new SAX3WriterXml(outputWriter); try (SAX3WriterXml writer = new SAX3WriterXml(outputWriter)) {
AttributesImpl atts = new AttributesImpl(); SAXException e = Assertions.assertThrows(SAXException.class, () -> {
AttributesImpl atts = new AttributesImpl();
Exception e = null; writer.startDocument();
try { writer.startElement("", "test", "", atts);
writer.startDocument(); writer.startElement("", "foobar", "", atts);
writer.startElement("", "test", "", atts); writer.endDocument();
writer.startElement("", "foobar", "", atts); });
writer.endDocument(); Assertions.assertTrue(e.getMessage().contains("Invalid"));
} catch (Exception catchE) { Assertions.assertTrue(e.getMessage().contains("2"));
e = catchE; Assertions.assertTrue(e.getMessage().contains("open"));
} }
Assertions.assertNotNull(e);
Assertions.assertEquals(SAXException.class, e.getClass());
Assertions.assertTrue(e.getMessage().contains("Invalid"));
Assertions.assertTrue(e.getMessage().contains("2"));
Assertions.assertTrue(e.getMessage().contains("open"));
} }
@Test @Test
public void testProcessingInstruction() throws Exception { public void testProcessingInstruction() throws Exception {
StringWriter outputWriter = new StringWriter(); StringWriter outputWriter = new StringWriter();
SAX3WriterXml writer = new SAX3WriterXml(outputWriter); try (SAX3WriterXml writer = new SAX3WriterXml(outputWriter)) {
AttributesImpl atts = new AttributesImpl(); AttributesImpl atts = new AttributesImpl();
Exception e = null;
try {
writer.startDocument(); writer.startDocument();
writer.processingInstruction("target", "data"); writer.processingInstruction("target", "data");
writer.startElement("", "test", "", atts); writer.startElement("", "test", "", atts);
writer.endElement("", "test", ""); writer.endElement("", "test", "");
writer.endDocument(); writer.endDocument();
} catch (Exception catchE) {
e = catchE;
} }
String output = outputWriter.toString(); String output = outputWriter.toString();
Assertions.assertNull(e);
Assertions.assertNotNull(output); Assertions.assertNotNull(output);
Assertions.assertTrue(output.length()>0); Assertions.assertTrue(output.length()>0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?target data?>\n<test/>"), output); Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?target data?>\n<test/>"), output);
@ -170,22 +154,16 @@ public class SAX3WriterXmlTest {
@Test @Test
public void testProcessingInstructionInline() throws Exception { public void testProcessingInstructionInline() throws Exception {
StringWriter outputWriter = new StringWriter(); StringWriter outputWriter = new StringWriter();
SAX3WriterXml writer = new SAX3WriterXml(outputWriter); try (SAX3WriterXml writer = new SAX3WriterXml(outputWriter)) {
AttributesImpl atts = new AttributesImpl(); AttributesImpl atts = new AttributesImpl();
Exception e = null;
try {
writer.startDocument(); writer.startDocument();
writer.processingInstruction("target", "data"); writer.processingInstruction("target", "data");
writer.startElement("", "test", "", atts); writer.startElement("", "test", "", atts);
writer.processingInstruction("target-doc", "data-doc"); writer.processingInstruction("target-doc", "data-doc");
writer.endElement("", "test", ""); writer.endElement("", "test", "");
writer.endDocument(); writer.endDocument();
} catch (Exception catchE) {
e = catchE;
} }
String output = outputWriter.toString(); String output = outputWriter.toString();
Assertions.assertNull(e);
Assertions.assertNotNull(output); Assertions.assertNotNull(output);
Assertions.assertTrue(output.length()>0); Assertions.assertTrue(output.length()>0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?target data?>\n<test>\n\t<?target-doc data-doc?>\n</test>\n"), output); Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?target data?>\n<test>\n\t<?target-doc data-doc?>\n</test>\n"), output);
@ -194,53 +172,41 @@ public class SAX3WriterXmlTest {
@Test @Test
public void testProcessingInstructionTargetXmlPrefix() throws Exception { public void testProcessingInstructionTargetXmlPrefix() throws Exception {
StringWriter outputWriter = new StringWriter(); StringWriter outputWriter = new StringWriter();
SAX3WriterXml writer = new SAX3WriterXml(outputWriter); try (SAX3WriterXml writer = new SAX3WriterXml(outputWriter)) {
Exception e = null; SAXException e = Assertions.assertThrows(SAXException.class, () -> {
try { writer.startDocument();
writer.startDocument(); writer.processingInstruction("xmlPrefix", "isInvalid");
writer.processingInstruction("xmlPrefix", "isInvalid"); });
} catch (Exception catchE) { Assertions.assertTrue(e.getMessage().contains("instruction"));
e = catchE; Assertions.assertTrue(e.getMessage().contains("start with xml"));
} }
Assertions.assertNotNull(e);
Assertions.assertEquals(SAXException.class, e.getClass());
Assertions.assertTrue(e.getMessage().contains("instruction"));
Assertions.assertTrue(e.getMessage().contains("start with xml"));
} }
@Test @Test
public void testProcessingInstructionTargetNoneNameChar() throws Exception { public void testProcessingInstructionTargetNoneNameChar() throws Exception {
StringWriter outputWriter = new StringWriter(); StringWriter outputWriter = new StringWriter();
SAX3WriterXml writer = new SAX3WriterXml(outputWriter); try (SAX3WriterXml writer = new SAX3WriterXml(outputWriter)) {
Exception e = null; SAXException e = Assertions.assertThrows(SAXException.class, () -> {
try { writer.startDocument();
writer.startDocument(); writer.processingInstruction("4Prefix", "isInvalid");
writer.processingInstruction("4Prefix", "isInvalid"); });
} catch (Exception catchE) { Assertions.assertTrue(e.getMessage().contains("instruction"));
e = catchE; Assertions.assertTrue(e.getMessage().contains("invalid name"));
Assertions.assertTrue(e.getMessage().contains("4Prefix"));
} }
Assertions.assertNotNull(e);
Assertions.assertEquals(SAXException.class, e.getClass());
Assertions.assertTrue(e.getMessage().contains("instruction"));
Assertions.assertTrue(e.getMessage().contains("invalid name"));
Assertions.assertTrue(e.getMessage().contains("4Prefix"));
} }
@Test @Test
public void testProcessingInstructionDataNoneChar() throws Exception { public void testProcessingInstructionDataNoneChar() throws Exception {
StringWriter outputWriter = new StringWriter(); StringWriter outputWriter = new StringWriter();
SAX3WriterXml writer = new SAX3WriterXml(outputWriter); try (SAX3WriterXml writer = new SAX3WriterXml(outputWriter)) {
Exception e = null; SAXException e = Assertions.assertThrows(SAXException.class, () -> {
try { writer.startDocument();
writer.startDocument(); writer.processingInstruction("target", "isInvalidChar="+0xD800);
writer.processingInstruction("target", "isInvalidChar="+0xD800); });
} catch (Exception catchE) { Assertions.assertTrue(e.getMessage().contains("instruction"));
e = catchE; Assertions.assertTrue(e.getMessage().contains("invalid char"));
Assertions.assertTrue(e.getMessage().contains("isInvalidChar=55296"));
} }
Assertions.assertNotNull(e);
Assertions.assertEquals(SAXException.class, e.getClass());
Assertions.assertTrue(e.getMessage().contains("instruction"));
Assertions.assertTrue(e.getMessage().contains("invalid char"));
Assertions.assertTrue(e.getMessage().contains("isInvalidChar=55296"));
} }
} }