From 275c4aa1619c13eaf0fc6c5a8efc4b4f468ef642 Mon Sep 17 00:00:00 2001 From: Willem Date: Wed, 29 Jan 2025 17:20:43 +0100 Subject: [PATCH] Removed some dead code and updated exception unit tests to lastest spec --- .../org/x4o/sax3/io/SAX3XMLConstants.java | 18 +-- .../sax3/io/SAX3WriterXmlAttributeTest.java | 54 ++++--- .../org/x4o/sax3/io/SAX3WriterXmlTest.java | 132 +++++++----------- 3 files changed, 76 insertions(+), 128 deletions(-) diff --git a/nx01-x4o-sax3/src/main/java/org/x4o/sax3/io/SAX3XMLConstants.java b/nx01-x4o-sax3/src/main/java/org/x4o/sax3/io/SAX3XMLConstants.java index c1ed87d..2f400a8 100644 --- a/nx01-x4o-sax3/src/main/java/org/x4o/sax3/io/SAX3XMLConstants.java +++ b/nx01-x4o-sax3/src/main/java/org/x4o/sax3/io/SAX3XMLConstants.java @@ -261,10 +261,6 @@ public final class SAX3XMLConstants { return true; } - //static public boolean isCharRef(String c) { - // &#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';' - //} - static private boolean escapeXMLValueAppend(int codePoint, StringBuilder result) { if (codePoint == '<') { result.append("<"); @@ -300,17 +296,7 @@ public final class SAX3XMLConstants { if (escapeXMLValueAppend(c,result)) { continue; } - if (/*isNameChar(c)*/true==false) {// TODO: add correct - // 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); - } + result.appendCodePoint(c); } return result.toString(); } @@ -320,8 +306,6 @@ public final class SAX3XMLConstants { PrimitiveIterator.OfInt iterator = value.codePoints().iterator(); while (iterator.hasNext()) { int c = iterator.nextInt(); - //for (int i=0;i { + try (SAX3WriterXml writer = new SAX3WriterXml(outputWriter)) { + writer.startDocument(); + 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(); - 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(); - atts.addAttribute ("", "abc", "", "", " junit "); - writer.startElementEnd("", "root", "", atts); - writer.endDocument(); - + atts.addAttribute ("", "abc", "", "", " junit "); + writer.startElementEnd("", "root", "", atts); + writer.endDocument(); + } String output = outputWriter.toString(); Assertions.assertNotNull(output); Assertions.assertTrue(output.length()>0); diff --git a/nx01-x4o-sax3/src/test/java/org/x4o/sax3/io/SAX3WriterXmlTest.java b/nx01-x4o-sax3/src/test/java/org/x4o/sax3/io/SAX3WriterXmlTest.java index 04ec171..9c57f71 100644 --- a/nx01-x4o-sax3/src/test/java/org/x4o/sax3/io/SAX3WriterXmlTest.java +++ b/nx01-x4o-sax3/src/test/java/org/x4o/sax3/io/SAX3WriterXmlTest.java @@ -103,65 +103,49 @@ public class SAX3WriterXmlTest { @Test public void testXmlInvalid() throws Exception { StringWriter outputWriter = new StringWriter(); - SAX3WriterXml writer = new SAX3WriterXml(outputWriter); - AttributesImpl atts = new AttributesImpl(); - - Exception e = null; - try { - writer.startDocument(); - writer.startElement("", "test", "", atts); - writer.startElement("", "foobar", "", atts); - writer.endElement("", "test", ""); - writer.endDocument(); - } catch (Exception catchE) { - e = catchE; + try (SAX3WriterXml writer = new SAX3WriterXml(outputWriter)) { + SAXException e = Assertions.assertThrows(SAXException.class, () -> { + AttributesImpl atts = new AttributesImpl(); + writer.startDocument(); + writer.startElement("", "test", "", atts); + writer.startElement("", "foobar", "", atts); + writer.endElement("", "test", ""); + writer.endDocument(); + }); + Assertions.assertTrue(e.getMessage().contains("tag")); + Assertions.assertTrue(e.getMessage().contains("foobar")); } - Assertions.assertNotNull(e); - Assertions.assertEquals(SAXException.class, e.getClass()); - Assertions.assertTrue(e.getMessage().contains("tag")); - Assertions.assertTrue(e.getMessage().contains("foobar")); } @Test public void testXmlInvalidEnd() throws Exception { StringWriter outputWriter = new StringWriter(); - SAX3WriterXml writer = new SAX3WriterXml(outputWriter); - AttributesImpl atts = new AttributesImpl(); - - Exception e = null; - try { - writer.startDocument(); - writer.startElement("", "test", "", atts); - writer.startElement("", "foobar", "", atts); - writer.endDocument(); - } catch (Exception catchE) { - e = catchE; + try (SAX3WriterXml writer = new SAX3WriterXml(outputWriter)) { + SAXException e = Assertions.assertThrows(SAXException.class, () -> { + AttributesImpl atts = new AttributesImpl(); + writer.startDocument(); + writer.startElement("", "test", "", atts); + writer.startElement("", "foobar", "", atts); + writer.endDocument(); + }); + Assertions.assertTrue(e.getMessage().contains("Invalid")); + Assertions.assertTrue(e.getMessage().contains("2")); + 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 public void testProcessingInstruction() throws Exception { StringWriter outputWriter = new StringWriter(); - SAX3WriterXml writer = new SAX3WriterXml(outputWriter); - AttributesImpl atts = new AttributesImpl(); - - Exception e = null; - try { + try (SAX3WriterXml writer = new SAX3WriterXml(outputWriter)) { + AttributesImpl atts = new AttributesImpl(); writer.startDocument(); writer.processingInstruction("target", "data"); writer.startElement("", "test", "", atts); writer.endElement("", "test", ""); writer.endDocument(); - } catch (Exception catchE) { - e = catchE; } String output = outputWriter.toString(); - Assertions.assertNull(e); Assertions.assertNotNull(output); Assertions.assertTrue(output.length()>0); Assertions.assertTrue(output.equals("\n\n"), output); @@ -170,22 +154,16 @@ public class SAX3WriterXmlTest { @Test public void testProcessingInstructionInline() throws Exception { StringWriter outputWriter = new StringWriter(); - SAX3WriterXml writer = new SAX3WriterXml(outputWriter); - AttributesImpl atts = new AttributesImpl(); - - Exception e = null; - try { + try (SAX3WriterXml writer = new SAX3WriterXml(outputWriter)) { + AttributesImpl atts = new AttributesImpl(); writer.startDocument(); writer.processingInstruction("target", "data"); writer.startElement("", "test", "", atts); writer.processingInstruction("target-doc", "data-doc"); writer.endElement("", "test", ""); writer.endDocument(); - } catch (Exception catchE) { - e = catchE; } String output = outputWriter.toString(); - Assertions.assertNull(e); Assertions.assertNotNull(output); Assertions.assertTrue(output.length()>0); Assertions.assertTrue(output.equals("\n\n\n\t\n\n"), output); @@ -194,53 +172,41 @@ public class SAX3WriterXmlTest { @Test public void testProcessingInstructionTargetXmlPrefix() throws Exception { StringWriter outputWriter = new StringWriter(); - SAX3WriterXml writer = new SAX3WriterXml(outputWriter); - Exception e = null; - try { - writer.startDocument(); - writer.processingInstruction("xmlPrefix", "isInvalid"); - } catch (Exception catchE) { - e = catchE; + try (SAX3WriterXml writer = new SAX3WriterXml(outputWriter)) { + SAXException e = Assertions.assertThrows(SAXException.class, () -> { + writer.startDocument(); + writer.processingInstruction("xmlPrefix", "isInvalid"); + }); + Assertions.assertTrue(e.getMessage().contains("instruction")); + 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 public void testProcessingInstructionTargetNoneNameChar() throws Exception { StringWriter outputWriter = new StringWriter(); - SAX3WriterXml writer = new SAX3WriterXml(outputWriter); - Exception e = null; - try { - writer.startDocument(); - writer.processingInstruction("4Prefix", "isInvalid"); - } catch (Exception catchE) { - e = catchE; + try (SAX3WriterXml writer = new SAX3WriterXml(outputWriter)) { + SAXException e = Assertions.assertThrows(SAXException.class, () -> { + writer.startDocument(); + writer.processingInstruction("4Prefix", "isInvalid"); + }); + Assertions.assertTrue(e.getMessage().contains("instruction")); + 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 public void testProcessingInstructionDataNoneChar() throws Exception { StringWriter outputWriter = new StringWriter(); - SAX3WriterXml writer = new SAX3WriterXml(outputWriter); - Exception e = null; - try { - writer.startDocument(); - writer.processingInstruction("target", "isInvalidChar="+0xD800); - } catch (Exception catchE) { - e = catchE; + try (SAX3WriterXml writer = new SAX3WriterXml(outputWriter)) { + SAXException e = Assertions.assertThrows(SAXException.class, () -> { + writer.startDocument(); + writer.processingInstruction("target", "isInvalidChar="+0xD800); + }); + Assertions.assertTrue(e.getMessage().contains("instruction")); + 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")); } }