You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by sl...@apache.org on 2019/02/14 14:46:19 UTC

[incubator-daffodil] branch master updated: Remove JUnit4 compile dependency

This is an automated email from the ASF dual-hosted git repository.

slawrence pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-daffodil.git


The following commit(s) were added to refs/heads/master by this push:
     new 3e497fa  Remove JUnit4 compile dependency
3e497fa is described below

commit 3e497fa25b458de131e873c713b3fa90209e8a4c
Author: Steve Lawrence <sl...@apache.org>
AuthorDate: Wed Feb 13 12:46:31 2019 -0500

    Remove JUnit4 compile dependency
    
    We currently have a JUnit4 compile dependency because the TDML Runner
    throws a JUnit-based exception to indicate that a test should be skipped
    when the current implementation does not support that test. The problem
    with this is 1) we now have a compile dependency that is traditionally
    only for tests, which is odd, and 2) JUnit is now distributed in our
    binary helpers, and so requires licensing changes.
    
    Neither of these are ideal, so this patch removes that compile
    dependency. It does so by using reflection to create the appropriate
    JUnit "skip this test" exception if JUnit is on the classpath (i.e.
    we're probably running a JUnit test). If JUnit is not on the classpath,
    then we just throws a standard TDML exception and lets the caller handle
    it however it makes sense for them.
    
    DAFFODIL-2067
---
 build.sbt                                          |  1 -
 .../org/apache/daffodil/tdml/TDMLException.scala   | 13 +++------
 .../org/apache/daffodil/tdml/TDMLRunner.scala      | 34 +++++++++++++++++-----
 project/Dependencies.scala                         |  5 +---
 4 files changed, 32 insertions(+), 21 deletions(-)

diff --git a/build.sbt b/build.sbt
index 11e1d89..2726207 100644
--- a/build.sbt
+++ b/build.sbt
@@ -65,7 +65,6 @@ lazy val sapi             = Project("daffodil-sapi", file("daffodil-sapi")).conf
 lazy val tdmlLib             = Project("daffodil-tdml-lib", file("daffodil-tdml-lib")).configs(IntegrationTest, TestDebug, IntegrationTestDebug)
                               .dependsOn(macroLib % "compile-internal", lib, io, io % "test->test")
                               .settings(commonSettings)
-                              .settings(libraryDependencies += Dependencies.junit)
 
 lazy val tdmlProc         = Project("daffodil-tdml-processor", file("daffodil-tdml-processor")).configs(IntegrationTest, TestDebug, IntegrationTestDebug)
                               .dependsOn(tdmlLib, core)
diff --git a/daffodil-tdml-lib/src/main/scala/org/apache/daffodil/tdml/TDMLException.scala b/daffodil-tdml-lib/src/main/scala/org/apache/daffodil/tdml/TDMLException.scala
index 0cc1f31..a77adb4 100644
--- a/daffodil-tdml-lib/src/main/scala/org/apache/daffodil/tdml/TDMLException.scala
+++ b/daffodil-tdml-lib/src/main/scala/org/apache/daffodil/tdml/TDMLException.scala
@@ -19,7 +19,6 @@ package org.apache.daffodil.tdml
 
 import org.apache.daffodil.api.Diagnostic
 import org.apache.daffodil.util.Maybe
-import org.junit.AssumptionViolatedException
 import org.apache.daffodil.util.Misc
 
 object TDMLException {
@@ -87,14 +86,10 @@ class TDMLDiagnostic(diag: String, implementation: Option[String])
 }
 
 /**
- * Causes a Junit test to be skipped, but otherwise is a TDMLException like
- * others thrown by the TDML runner.
+ * Used to determine when a test will not be run due to not being compatible
+ * with the implementation. Useful since this isn't necessarily a failure and
+ * may want to be treated differently in some cases.
  */
 class TDMLTestNotCompatibleException(testName: String, override val implementation: Option[String])
-  extends AssumptionViolatedException(
-    implementation.map { impl =>
-      "Test '%s' not compatible with implementation '%s'.".format(testName, impl)
-    }.get) with TDMLException {
-  override def msg = getMessage()
-  override def causes: Seq[Throwable] = Nil
+  extends TDMLExceptionImpl("Test '%s' not compatible with implementation.".format(testName), implementation) {
 }
diff --git a/daffodil-tdml-lib/src/main/scala/org/apache/daffodil/tdml/TDMLRunner.scala b/daffodil-tdml-lib/src/main/scala/org/apache/daffodil/tdml/TDMLRunner.scala
index 687506d..aee4bcd 100644
--- a/daffodil-tdml-lib/src/main/scala/org/apache/daffodil/tdml/TDMLRunner.scala
+++ b/daffodil-tdml-lib/src/main/scala/org/apache/daffodil/tdml/TDMLRunner.scala
@@ -664,6 +664,31 @@ abstract class TestCase(testCaseXML: NodeSeq, val parent: DFDLTestSuite)
     optDefinedConfig
   }
 
+  // Throws an exception marking a test as not compatible based on what classes
+  // are on the classpath. If Junit is on the classpath, assume we are in a
+  // Junit test and throw the AssumptionViolatedException to mark a test as
+  // skipped rather than failed. Otherwise, if Junit is not on the classpath
+  // just throw a standard "not compatible" exception and let the caller figure
+  // out the right way to handle it.
+  private def testNotCompatible(testName: String, implementationName: Option[String]) = {
+    import scala.language.reflectiveCalls
+    import scala.language.existentials
+
+    val tdmlException = new TDMLTestNotCompatibleException(testName, implementationName)
+
+    val junitExceptionClassName = "org.junit.AssumptionViolatedException"
+    val junitExceptionClass = Try(Class.forName(junitExceptionClassName))
+    val junitExceptionConstructor = junitExceptionClass.map {
+      _.getDeclaredConstructor(classOf[String], classOf[Throwable])
+    }
+    val junitExceptionInstance = junitExceptionConstructor.map {
+      _.newInstance(tdmlException.getMessage, tdmlException).asInstanceOf[Exception]
+    }
+
+    val exceptionToThrow = junitExceptionInstance.getOrElse(tdmlException)
+    throw exceptionToThrow
+  }
+
   def run(schemaArg: Option[Node] = None): Unit = {
     val suppliedSchema = getSuppliedSchema(schemaArg)
 
@@ -701,13 +726,8 @@ abstract class TestCase(testCaseXML: NodeSeq, val parent: DFDLTestSuite)
     val istrings = implementationStrings
     val useThisImpl = istrings.contains(implName)
     if (!useThisImpl) {
-      //
-      // skip the test
-      //
-      // JUnit framework will mark as skipped (and count them) tests that throw this.
-      // But it is not considered a failure.
-      //
-      throw new TDMLTestNotCompatibleException(this.tcName, implString)
+      // throws an exception marking a test as not compatible
+      testNotCompatible(this.tcName, implString)
     } else {
       // run the test.
 
diff --git a/project/Dependencies.scala b/project/Dependencies.scala
index 84df48b..c848bb6 100644
--- a/project/Dependencies.scala
+++ b/project/Dependencies.scala
@@ -31,9 +31,6 @@ object Dependencies {
     "jline" % "jline" % "2.14.6",
   )
 
-  lazy val junit = "junit" % "junit" % "4.12" // needed for TDML runner use of AssertionViolationException
-  
-
   lazy val infoset = Seq(
     "org.jdom" % "jdom2" % "2.0.6",
     "com.fasterxml.woodstox" % "woodstox-core" % "5.1.0",
@@ -47,7 +44,7 @@ object Dependencies {
   )
 
   lazy val test = Seq(
-    junit % "it,test", 
+    "junit" % "junit" % "4.12" % "it,test",
     "com.novocode" % "junit-interface" % "0.11" % "it,test",
     "org.scalacheck" %% "scalacheck" % "1.14.0" % "it,test"
   )