olabusayoT commented on a change in pull request #436:
URL: https://github.com/apache/incubator-daffodil/pull/436#discussion_r512207455



##########
File path: 
daffodil-core/src/test/scala/org/apache/daffodil/processor/TestSAXParseAPI.scala
##########
@@ -0,0 +1,218 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.daffodil.processor
+
+import java.io.ByteArrayInputStream
+import java.io.ByteArrayOutputStream
+import java.io.IOException
+
+import scala.xml.Elem
+import scala.xml.SAXParseException
+
+import org.apache.daffodil.Implicits.intercept
+import org.apache.daffodil.compiler.Compiler
+import org.apache.daffodil.infoset.DaffodilOutputContentHandler
+import org.apache.daffodil.processors.DataProcessor
+import org.apache.daffodil.processors.ParseResult
+import org.apache.daffodil.util.SchemaUtils
+import org.apache.daffodil.xml.XMLUtils
+import org.jdom2.input.sax.BuilderErrorHandler
+import org.jdom2.input.sax.SAXHandler
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertNotEquals
+import org.junit.Assert.assertNull
+import org.junit.Assert.assertTrue
+import org.junit.Assert.fail
+import org.junit.Test
+import org.xml.sax.InputSource
+import org.xml.sax.SAXNotRecognizedException
+import org.xml.sax.SAXNotSupportedException
+
+
+object TestSAXParseAPI {
+
+  val testSchema: Elem = SchemaUtils.dfdlTestSchema(
+      <xs:include 
schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>,
+      <dfdl:format ref="tns:GeneralFormat"/>,
+      <xs:element name="list" type="tns:example1"/>
+      <xs:complexType name="example1">
+        <xs:sequence>
+          <xs:element name="w" type="xs:int" dfdl:length="1" 
dfdl:lengthKind="explicit" maxOccurs="unbounded"/>
+        </xs:sequence>
+      </xs:complexType>
+  )
+  val testInfoset: Elem = <list 
xmlns="http://example.com";><w>9</w><w>1</w><w>0</w></list>
+  val testInfosetString: String = testInfoset.toString()
+  val testData = "910"
+
+  lazy val dp: DataProcessor = testDataprocessor(testSchema)
+
+  def testDataprocessor(testSchema: scala.xml.Elem): DataProcessor = {
+    val schemaCompiler = Compiler()
+    val pf = schemaCompiler.compileNode(testSchema)
+    if (pf.isError) {
+      val msgs = pf.getDiagnostics.map { _.getMessage() }.mkString("\n")
+      fail("pf compile errors: " + msgs)
+    }
+    pf.sset.root.erd.preSerialization // force evaluation of all compile-time 
constructs
+    val dp = pf.onPath("/").asInstanceOf[DataProcessor]
+    if (dp.isError) {
+      val msgs = dp.getDiagnostics.map { _.getMessage() }.mkString("\n")
+      fail("dp compile errors: " + msgs)
+    }
+    dp
+  }
+}
+
+class TestSAXParseAPI {
+  import TestSAXParseAPI._
+
+  @Test def testDaffodilParseXMLReader_setFeatureUnsupported(): Unit = {
+    val xmlReader = dp.newXMLReaderInstance
+    val snr = intercept[SAXNotRecognizedException] {
+      xmlReader.setFeature("http://xml.org/sax/features/validation";, true)
+    }
+    assertTrue(snr.getMessage.contains("Feature unsupported"))
+    assertTrue(snr.getMessage.contains("Supported features are:"))
+  }
+
+  @Test def testDaffodilParseXMLReader_get_setFeature(): Unit = {
+    val xmlReader = dp.newXMLReaderInstance
+    val feature = "http://xml.org/sax/features/namespace-prefixes";
+    val origValue = xmlReader.getFeature(feature)
+    assertFalse(origValue)
+    xmlReader.setFeature(feature, true)
+    val newValue = xmlReader.getFeature(feature)
+    assertTrue(newValue)
+  }
+
+  @Test def testDaffodilParseXMLReader_setProperty_unsupported(): Unit = {
+    val xmlReader = dp.newXMLReaderInstance
+    val property: AnyRef = "Hello"
+    val snr = intercept[SAXNotRecognizedException] {
+      xmlReader.setProperty("http://xml.org/sax/properties/xml-string";, 
property)
+    }
+    assertTrue(snr.getMessage.contains("Property unsupported"))
+  }
+
+  @Test def testDaffodilParseXMLReader_setProperty_badValue(): Unit = {
+    val xmlReader = dp.newXMLReaderInstance
+    val property: String = XMLUtils.DAFFODIL_SAX_URN_BLOBDIRECTORY
+    val propertyVal: AnyRef = "/tmp/i/am/a/directory"
+    val sns = intercept[SAXNotSupportedException](
+      xmlReader.setProperty(property, propertyVal)
+    )
+    assertTrue(sns.getMessage.contains("Unsupported value for property"))
+  }
+
+  @Test def testDaffodilParseXMLReader_get_setProperty(): Unit = {
+    val xmlReader = dp.newXMLReaderInstance
+    val property: String = XMLUtils.DAFFODIL_SAX_URN_BLOBPREFIX
+    val propertyVal: AnyRef ="testing-blobs"
+    val origValue = xmlReader.getProperty(property)
+    assertNotEquals(propertyVal, origValue)
+    xmlReader.setProperty(property, propertyVal)
+    val newValue = xmlReader.getProperty(property)
+    assertEquals(propertyVal, newValue.asInstanceOf[String])
+  }
+
+  @Test def testDaffodilParseXMLReader_get_setContentHandler(): Unit = {
+    val xmlReader = dp.newXMLReaderInstance
+    val ch = new SAXHandler()
+    val origValue = xmlReader.getContentHandler
+    assertNull(origValue)
+    xmlReader.setContentHandler(ch)
+    val newValue = xmlReader.getContentHandler
+    assertTrue(newValue.isInstanceOf[SAXHandler])
+  }
+
+  @Test def testDaffodilParseXMLReader_get_setErrorHandler(): Unit = {
+    val xmlReader = dp.newXMLReaderInstance
+    val eh = new BuilderErrorHandler()
+    val origValue = xmlReader.getErrorHandler
+    assertNull(origValue)
+    xmlReader.setErrorHandler(eh)
+    val newValue = xmlReader.getErrorHandler
+    assertTrue(newValue.isInstanceOf[BuilderErrorHandler])
+  }
+
+  @Test def testDaffodilParseXMLReader_parse_inputSource_no_backing_stream(): 
Unit = {
+    val xmlReader = dp.newXMLReaderInstance
+    val input = new InputSource()
+    val ioe = intercept[IOException](
+      xmlReader.parse(input)
+    )
+    assertTrue(ioe.getMessage.contains("Inputsource must be backed by 
Inputstream"))
+  }
+
+  @Test def 
testDaffodilParseXMLReader_parse_inputSource_with_backing_stream(): Unit = {
+    val xmlReader = dp.newXMLReaderInstance
+    val baos = new ByteArrayOutputStream()
+    val ch = new DaffodilOutputContentHandler(baos)

Review comment:
       I also added tests directly connecting SAX parse/unparse and 
Dataprocessor parse/unparse, as was mentioned in original comment




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to