Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve Main Schema URI used for Validation #1358

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

package org.apache.daffodil.core.runtime1

import java.io.FileNotFoundException

import org.apache.daffodil.core.dsom.SchemaSet
import org.apache.daffodil.core.dsom.SequenceTermBase
import org.apache.daffodil.lib.util.Logger
import org.apache.daffodil.lib.xml.XMLUtils
import org.apache.daffodil.runtime1.api.DFDL
import org.apache.daffodil.runtime1.layers.LayerRuntimeCompiler
import org.apache.daffodil.runtime1.layers.LayerRuntimeData
Expand Down Expand Up @@ -79,7 +82,14 @@ trait SchemaSetRuntime1Mixin {
"The root element cannot have the dfdl:outputValueCalc property."
)
// stored transiently in the SSRD, only used for full validation
val mainSchemaURI = self.schemaSource.uriForLoading
val mainSchemaURI =
XMLUtils.resolveSchemaLocation(self.schemaSource.uriForLoading.toString, None) match {
case Some((uss, _)) => uss.uri
case None =>
throw new FileNotFoundException(
s"Could not find file or resource ${self.schemaSource.uriForLoading}"
)
}
val p = if (!root.isError) parser else null
val u = if (!root.isError) unparser else null
val ssrd =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
Expand Down Expand Up @@ -1444,5 +1446,51 @@ public void testJavaAPICompileResource() throws IOException, ClassNotFoundExcept
}
}

@Test
public void testJavaAPICompileSource1() throws IOException, URISyntaxException, InvalidUsageException {
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
URI uri = new URI("/test/japi/mySchema1.dfdl.xsd");
ProcessorFactory pf = c.compileSource(uri);
DataProcessor dp = pf.onPath("/").withValidationMode(ValidationMode.Full);

java.io.File file = getResource("/test/japi/myDataBroken.dat");
java.io.FileInputStream fis = new java.io.FileInputStream(file);
try (InputSourceDataInputStream dis = new InputSourceDataInputStream(fis)) {
JDOMInfosetOutputter outputter = new JDOMInfosetOutputter();
ParseResult res = dp.parse(dis, outputter);
assertTrue(res.isError());

Diagnostic d = res.getDiagnostics().get(0);
LocationInSchemaFile loc = d.getLocationsInSchemaFiles().get(0);
assertTrue(loc.toString().replace("\\", "/").contains("in " + uri.getPath()));
}
}

// intended to test the case where compileSource succeeds, but onPath
// can't find the file when it tries to resolve the schemaLocation
// takes care of coverage for this case
@Test
public void testJavaAPICompileSource2() throws IOException {
org.apache.daffodil.japi.Compiler c = Daffodil.compiler();
File tempFile = File.createTempFile("testJavaAPI", ".schema");
File schemaFile = getResource("/test/japi/mySchema2.dfdl.xsd");
FileUtils.copyFile(schemaFile, tempFile);
ProcessorFactory pf = c.compileSource(tempFile.toURI());
try {
assertFalse(pf.isError());
// delete file needed by Xerces for full validation
tempFile.delete();
// should throw FileNotFoundException because onPath calls resolveSchemaLocation
// on the URI backed by the deleted file
pf.onPath("/");
// fail if exception was not thrown
fail();
} catch (Exception e) {
assertTrue(e.getMessage().contains("Could not find file or resource"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this supposed to fail? Looks to me like testJavaAPI.schema should exist, so compileSource and onPath should succeed. Feels like this catch block wants to just contain fail();, or alternatively just remove the try/catch so that any thrown exceptions will cause the test to fail. Maybe we just need a finally block to delete the tmp file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this test is intended to test the case where compileSource succeeds, but onPath can't find the file when it tries to resolve the schemaLocation. It's mainly a coverage test. That's also why we don't call withValidationMode(Full) here, since onPath is where we do the resolution

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, can you add a comment to that affect? Also, in that case I think this needs a fail() and the end of the try block in case the exception isn't thrown. It might be a bit cleaner to rewrite it like this:

try {
  assertFalse(pf.isError)
  // delete file needed by Xerces for full validation
  tempFile.delete()
  // should throw FileNotFoundException because ...
  pf.onPath("/")
  // fail if exception was not thrown
  fail() 
} catch (Exception e) {
  assertTrue(e.getMessage().contains(...))
} finally {
  if (tempFile.exists) tempFile.delete()
}

Removes the conditionals to make it a bit easier to see what's expected.

} finally {
if (tempFile.exists()) tempFile.delete();
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import java.io.ByteArrayOutputStream
import java.io.File
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.net.URI
import java.nio.ByteBuffer
import java.nio.channels.Channels
import java.nio.charset.StandardCharsets
Expand Down Expand Up @@ -1407,4 +1408,50 @@ class TestScalaAPI {
}
}

@Test
def testScalaAPICompileSource1(): Unit = {
val c = Daffodil.compiler()
val uri = new URI("/test/sapi/mySchema1.dfdl.xsd")
val pf = c.compileSource(uri)
val dp = pf.onPath("/").withValidationMode(ValidationMode.Full)

val file = getResource("/test/sapi/myDataBroken.dat")
val fis = new java.io.FileInputStream(file)
using(new InputSourceDataInputStream(fis)) { input =>
val outputter = new ScalaXMLInfosetOutputter()
val res = dp.parse(input, outputter)
assertTrue(res.isError())

val d = res.getDiagnostics.head
val loc = d.getLocationsInSchemaFiles.head
assertTrue(loc.toString().replace("\\", "/").contains("in " + uri.getPath))
}
}

// intended to test the case where compileSource succeeds, but onPath
// can't find the file when it tries to resolve the schemaLocation
// takes care of coverage for this case
@Test
def testScalaAPICompileSource2(): Unit = {
val c = Daffodil.compiler()
val tempFile = File.createTempFile("testScalaAPI", ".schema")
val schemaFile = getResource("/test/sapi/mySchema2.dfdl.xsd")
FileUtils.copyFile(schemaFile, tempFile)
val pf = c.compileSource(tempFile.toURI)
try {
assertFalse(pf.isError())
// delete file needed by Xerces for full validation
tempFile.delete()
// should throw FileNotFoundException because onPath calls resolveSchemaLocation
// on the URI backed by the deleted file
pf.onPath("/")
// fail if exception was not thrown
fail()
} catch {
case e: Exception =>
assertTrue(e.getMessage.contains("Could not find file or resource"))
} finally {
if (tempFile.exists) tempFile.delete()
}
}
}
Loading