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 2020/09/14 12:36:38 UTC

[incubator-daffodil] 01/03: Throw SDE in the case of a choice with no branches

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

commit 055f5b4136c794a33035baf2086c445a842a7469
Author: Steinberger <rs...@tresys.com>
AuthorDate: Fri Sep 11 10:10:40 2020 -0400

    Throw SDE in the case of a choice with no branches
    
    Section 15 of the spec satates that a choice that declares no branches
    in the DFDL schema is a schema definition error.
    
    Daffodil-1032
---
 .../org/apache/daffodil/dsom/ChoiceGroup.scala     |  7 +++
 .../daffodil/section15/choice_groups/choice.tdml   | 51 ++++++++++++++--------
 .../section15/choice_groups/noBranches.dfdl.xsd    | 43 ------------------
 .../section15/choice_groups/TestChoice.scala       |  4 +-
 4 files changed, 41 insertions(+), 64 deletions(-)

diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/dsom/ChoiceGroup.scala b/daffodil-core/src/main/scala/org/apache/daffodil/dsom/ChoiceGroup.scala
index 35a90fd..ec88686 100644
--- a/daffodil-core/src/main/scala/org/apache/daffodil/dsom/ChoiceGroup.scala
+++ b/daffodil-core/src/main/scala/org/apache/daffodil/dsom/ChoiceGroup.scala
@@ -100,6 +100,7 @@ abstract class ChoiceTermBase(
   with ChoiceAGMixin
   with HasOptRepTypeMixinImpl {
 
+  requiredEvaluationsIfActivated(noBranchesFound)
   requiredEvaluationsIfActivated(branchesAreNonOptional)
   requiredEvaluationsIfActivated(branchesAreNotIVCElements)
   requiredEvaluationsIfActivated(modelGroupRuntimeData.preSerialization)
@@ -201,6 +202,12 @@ abstract class ChoiceTermBase(
     }
   }.value
 
+  final lazy val noBranchesFound = LV('noBranchesFound) {
+    if(groupMembers.size.equals(0)) {
+      SDE("choice element must contain one or more branches")
+    }
+  }.value
+
   /**
    * The DFDL Spec as of this writing 2015-06-02 says that branches cannot be optional, but then
    * specifies that what it means is that if a choice branch root is an element/element-ref, it
diff --git a/daffodil-test/src/test/resources/org/apache/daffodil/section15/choice_groups/choice.tdml b/daffodil-test/src/test/resources/org/apache/daffodil/section15/choice_groups/choice.tdml
index d549e03..e29e7ff 100644
--- a/daffodil-test/src/test/resources/org/apache/daffodil/section15/choice_groups/choice.tdml
+++ b/daffodil-test/src/test/resources/org/apache/daffodil/section15/choice_groups/choice.tdml
@@ -1320,19 +1320,6 @@
 
   </tdml:parserTestCase>
 
-  <tdml:parserTestCase name="choice_noBranch" root="choice_noBranches"
-    model="noBranches.dfdl.xsd"
-    description="Section 15 - Choice Groups - A choice that declares no branches in the DFDL schema is a schema definition error..">
-
-    <tdml:document><![CDATA[test]]></tdml:document>
-
-    <tdml:errors>
-      <tdml:error>Schema Definition Error</tdml:error>
-      <tdml:error>Placeholder</tdml:error>
-    </tdml:errors>
-
-  </tdml:parserTestCase>
-  
   <tdml:defineSchema name="optionalChoices">
     <xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
     <dfdl:format ref="ex:GeneralFormat" lengthKind="delimited" />
@@ -1349,6 +1336,19 @@
         </xs:sequence>
       </xs:complexType>
 
+    <xs:element name="e0">
+      <xs:complexType>
+        <xs:sequence>
+          <xs:element name="redOrBlue" minOccurs="0" dfdl:occursCountKind="implicit">
+            <xs:complexType>
+              <xs:choice>
+              </xs:choice>
+            </xs:complexType>
+          </xs:element>
+        </xs:sequence>
+      </xs:complexType>
+    </xs:element>
+
       <xs:element name="e1">
         <xs:complexType>
           <xs:sequence dfdl:separator="/%NL;" dfdl:separatorPosition="postfix" dfdl:separatorSuppressionPolicy="trailingEmptyStrict">
@@ -1399,11 +1399,26 @@
 
   </tdml:defineSchema>
 
-<!--
-     Test Name: optionalChoice01
-     Schema: optionalChoices
-     Purpose: This test demonstrates the scenario where you have a choice of two initiated elements in an optional choice. In this case the first element should be selected.
--->
+  <!--
+   Test Name: optionalChoice0
+   Schema: optionalChoices
+   Purpose: This test demonstrates the SDE where you have a choice with no branches.
+  -->
+
+  <tdml:parserTestCase name="optionalChoice00" root="e0"
+                       model="optionalChoices">
+    <tdml:document>red</tdml:document>
+    <tdml:errors>
+      <tdml:error>Schema Definition Error</tdml:error>
+      <tdml:error>choice element must contain one or more branches</tdml:error>
+    </tdml:errors>
+  </tdml:parserTestCase>
+
+  <!--
+       Test Name: optionalChoice01
+       Schema: optionalChoices
+       Purpose: This test demonstrates the scenario where you have a choice of two initiated elements in an optional choice. In this case the first element should be selected.
+  -->
   
   <tdml:parserTestCase name="optionalChoice01" root="e1"
     model="optionalChoices">
diff --git a/daffodil-test/src/test/resources/org/apache/daffodil/section15/choice_groups/noBranches.dfdl.xsd b/daffodil-test/src/test/resources/org/apache/daffodil/section15/choice_groups/noBranches.dfdl.xsd
deleted file mode 100644
index d35efc8..0000000
--- a/daffodil-test/src/test/resources/org/apache/daffodil/section15/choice_groups/noBranches.dfdl.xsd
+++ /dev/null
@@ -1,43 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
-  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.
--->
-
-<schema xmlns="http://www.w3.org/2001/XMLSchema"
-  targetNamespace="http://www.example.org/example1/" xmlns:ex="http://www.example.org/example1/"
-  xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/" xmlns:xs="http://www.w3.org/2001/XMLSchema"
-  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-
-  <include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
-
-  <annotation>
-    <appinfo source="http://www.ogf.org/dfdl/">
-      <dfdl:format ref="ex:GeneralFormat" initiator=""
-        leadingSkip="0" trailingSkip="0" separator=""
-        encoding="utf-8" occursCountKind="parsed" separatorSuppressionPolicy="anyEmpty"
-        textNumberRep="standard" ignoreCase="no" representation="text"
-        lengthUnits="characters" lengthKind="delimited" initiatedContent="no" />
-    </appinfo>
-  </annotation>
-  
-  <xs:element name="choice_noBranches">
-    <xs:complexType>
-      <xs:choice>
-      </xs:choice>
-    </xs:complexType>
-  </xs:element>
-
-</schema>
diff --git a/daffodil-test/src/test/scala/org/apache/daffodil/section15/choice_groups/TestChoice.scala b/daffodil-test/src/test/scala/org/apache/daffodil/section15/choice_groups/TestChoice.scala
index 6cb5687..97dae26 100644
--- a/daffodil-test/src/test/scala/org/apache/daffodil/section15/choice_groups/TestChoice.scala
+++ b/daffodil-test/src/test/scala/org/apache/daffodil/section15/choice_groups/TestChoice.scala
@@ -38,6 +38,7 @@ class TestChoice {
 
   import TestChoice._
 
+  @Test def test_optionalChoice00(): Unit = { runnerCH.runOneTest("optionalChoice00") }
   @Test def test_optionalChoice01(): Unit = { runnerCH.runOneTest("optionalChoice01") }
   @Test def test_optionalChoice02(): Unit = { runnerCH.runOneTest("optionalChoice02") }
   @Test def test_optionalChoice03(): Unit = { runnerCH.runOneTest("optionalChoice03") }
@@ -123,9 +124,6 @@ class TestChoice {
   @Test def test_direct_dispatch_16(): Unit = { runnerCH.runOneTest("direct_dispatch_16") }
   @Test def test_direct_dispatch_17(): Unit = { runnerCH.runOneTest("direct_dispatch_17") }
 
-  //@Test def test_choice_noBranch() { runnerCH.runOneTest("choice_noBranch") } - Test consumes no data, which causes a TDMLError
-
-
   @Test def test_explicit_01(): Unit = { runnerCE.runOneTest("explicit_01") }
   @Test def test_explicit_02(): Unit = { runnerCE.runOneTest("explicit_02") }
   @Test def test_explicit_03(): Unit = { runnerCE.runOneTest("explicit_03") }