BUG-6316: Fix Bit and EnumPair's position/value types 14/44614/2
authorIgor Foltin <ifoltin@cisco.com>
Tue, 16 Aug 2016 13:06:58 +0000 (15:06 +0200)
committerRobert Varga <nite@hq.sk>
Tue, 30 Aug 2016 14:23:47 +0000 (14:23 +0000)
yang.model.api.type defines the effective model of the world,
where these attributes cannot ever be null, which is in contrast
to yang.model.api.stmt, which is the declared model -- where they
can in fact be null.

Fix this discrepancy by forcing the methods to return simple types

Change-Id: I27b9d6d283cd14b2044890d9ccd19bf92647f27e
Signed-off-by: Robert Varga <rovarga@cisco.com>
Signed-off-by: Igor Foltin <ifoltin@cisco.com>
Signed-off-by: Peter Kajsa <pkajsa@cisco.com>
13 files changed:
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/type/BitsTypeDefinition.java
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/type/EnumTypeDefinition.java
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/BitImpl.java
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/BitsTypeBuilder.java
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/EnumPairImpl.java
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/EnumerationTypeBuilder.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BitEffectiveStatementImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BitsSpecificationEffectiveStatementImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/EnumEffectiveStatementImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/EnumSpecificationEffectiveStatementImpl.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6316.java [new file with mode: 0644]
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/EffectiveStatementTypeTest.java
yang/yang-parser-impl/src/test/resources/bugs/bug6316/foo.yang [new file with mode: 0644]

index 7d13921ffe0228d30b87732f602517cf13d53fcc..bbaa5c23efcb81bccf105ca4ce000872d84d5205 100644 (file)
@@ -13,9 +13,7 @@ import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 
 /**
- *
  * Makes is possible to access to the individual bits values of this type.
- *
  */
 public interface BitsTypeDefinition extends TypeDefinition<BitsTypeDefinition> {
     /**
@@ -33,18 +31,18 @@ public interface BitsTypeDefinition extends TypeDefinition<BitsTypeDefinition> {
      */
     interface Bit extends SchemaNode {
         /**
-         * The position value MUST be in the range 0 to 4294967295, and it MUST
-         * be unique within the bits type.
+         * Returns the name of the concrete bit.
          *
-         * @return The position value of bit in range from 0 to 4294967295.
+         * @return string with the name of the concrete bit
          */
-        Long getPosition();
+        @Nonnull String getName();
 
         /**
-         * Returns the name of the concrete bit.
+         * The position value MUST be in the range 0 to 4294967295, and it MUST
+         * be unique within the bits type.
          *
-         * @return string with the name of the concrete bit
+         * @return The position value of bit in range from 0 to 4294967295.
          */
-        @Nonnull String getName();
+        long getPosition();
     }
 }
index eaa3fa6c98d9aaea297d61bc6f1120dee845dc99..770b5de84a5aff78a53d3622b3c1f09a97b5fa61 100644 (file)
@@ -13,10 +13,8 @@ import org.opendaylight.yangtools.yang.model.api.DocumentedNode;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 
 /**
- *
  * Makes is possible to access to the individual enumeration values of this
  * type.
- *
  */
 public interface EnumTypeDefinition extends TypeDefinition<EnumTypeDefinition> {
     /**
@@ -44,11 +42,10 @@ public interface EnumTypeDefinition extends TypeDefinition<EnumTypeDefinition> {
         /**
          * The "value" statement, which is optional, is used to associate an
          * integer value with the assigned name for the enum. This integer value
-         * MUST be in the range -2147483648 to 2147483647, and it MUST be unique
-         * within the enumeration type.
+         * MUST be unique within the enumeration type.
          *
          * @return integer value assigned to enumeration
          */
-        Integer getValue();
+        int getValue();
     }
 }
index f7edfe22cf665bb6c951ae4cf1e5c989d12a469b..8828f4dda3c45060e4cab328a537c6415440f4bf 100644 (file)
@@ -23,12 +23,14 @@ final class BitImpl implements Bit, Immutable {
     private final String description;
     private final String reference;
     private final Status status;
-    private final Long position;
+    private final long position;
 
-    BitImpl(final SchemaPath schemaPath, final Long position, final String description,
+    BitImpl(final SchemaPath schemaPath, final long position, final String description,
             final String reference, final Status status, final List<UnknownSchemaNode> unknownNodes) {
         this.schemaPath = Preconditions.checkNotNull(schemaPath, "Schema Path should not be null");
-        this.position = Preconditions.checkNotNull(position, "Position should not be null");
+
+        Preconditions.checkArgument(position >= 0L && position <= 4294967295L, "Invalid position %s", position);
+        this.position = position;
         this.description = description;
         this.reference = reference;
         this.status = Preconditions.checkNotNull(status);
@@ -66,7 +68,7 @@ final class BitImpl implements Bit, Immutable {
     }
 
     @Override
-    public Long getPosition() {
+    public long getPosition() {
         return position;
     }
 
@@ -81,7 +83,7 @@ final class BitImpl implements Bit, Immutable {
         int result = 1;
         result = prime * result + getQName().hashCode();
         result = prime * result + schemaPath.hashCode();
-        result = prime * result + position.hashCode();
+        result = prime * result + Long.hashCode(position);
         result = prime * result + unknownNodes.hashCode();
         return result;
     }
@@ -97,7 +99,7 @@ final class BitImpl implements Bit, Immutable {
         if (getClass() != obj.getClass()) {
             return false;
         }
-        Bit other = (Bit) obj;
+        final Bit other = (Bit) obj;
         return Objects.equals(schemaPath, other.getPath());
     }
 
index 04ee96a53f4075c7a02d83a99ba59e8d3f8b14f0..016a5d2d1deb5f2b2731f5b0311d9a0c682a12ec 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.yangtools.yang.model.util.type;
 
-import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableMap.Builder;
 import java.util.HashMap;
@@ -25,8 +24,6 @@ public final class BitsTypeBuilder extends TypeBuilder<BitsTypeDefinition> {
     }
 
     public BitsTypeBuilder addBit(@Nonnull final Bit item) {
-        Preconditions.checkArgument(item.getPosition() != null, "Bit %s has null position", item);
-
         builder.put(item.getName(), item);
         return this;
     }
index c2d19a0ed20c0388460e1f1391ada8d93ab12db5..5ba72ffe69f752b176ad7d92c7dfb229cc119098 100644 (file)
@@ -21,13 +21,13 @@ final class EnumPairImpl implements EnumPair, Immutable {
     private final String description;
     private final String reference;
     private final Status status;
-    private final Integer value;
     private final String name;
+    private final int value;
 
-    EnumPairImpl(final String name, final Integer value, final String description, final String reference,
+    EnumPairImpl(final String name, final int value, final String description, final String reference,
             final Status status, final List<UnknownSchemaNode> unknownSchemaNodes) {
-        this.value = Preconditions.checkNotNull(value);
         this.name = Preconditions.checkNotNull(name);
+        this.value = value;
         this.description = description;
         this.reference = reference;
         this.status = Preconditions.checkNotNull(status);
@@ -60,7 +60,7 @@ final class EnumPairImpl implements EnumPair, Immutable {
     }
 
     @Override
-    public Integer getValue() {
+    public int getValue() {
         return value;
     }
 
@@ -70,7 +70,7 @@ final class EnumPairImpl implements EnumPair, Immutable {
         int result = 1;
         result = prime * result + unknownSchemaNodes.hashCode();
         result = prime * result + name.hashCode();
-        result = prime * result + value.hashCode();
+        result = prime * result + Integer.hashCode(value);
         return result;
     }
 
@@ -87,8 +87,7 @@ final class EnumPairImpl implements EnumPair, Immutable {
             return false;
         }
 
-        return Objects.equals(value, other.getValue()) &&
-                Objects.equals(unknownSchemaNodes, other.getUnknownSchemaNodes());
+        return value == other.getValue() && Objects.equals(unknownSchemaNodes, other.getUnknownSchemaNodes());
     }
 
     @Override
index b2658372e4b1454474198ba730375c16bda1b70b..fa272b84f4ba299a4942fac1155246093f356a1b 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.yangtools.yang.model.util.type;
 
-import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableMap.Builder;
 import java.util.HashMap;
@@ -25,7 +24,6 @@ public final class EnumerationTypeBuilder extends TypeBuilder<EnumTypeDefinition
     }
 
     public EnumerationTypeBuilder addEnum(@Nonnull final EnumPair item) {
-        Preconditions.checkArgument(item.getValue() != null, "Enum %s has null value", item);
         builder.put(item.getName(), item);
         return this;
     }
index 81782a08284372ae12a475f563ce16b51045c2bf..381a3ed16104bf127f07b24f03009a2d201f57d8 100644 (file)
@@ -17,54 +17,50 @@ import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.BitStatement;
 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.AbstractEffectiveDocumentedNode;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.PositionEffectiveStatementImpl;
 
-public class BitEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, BitStatement> implements Bit {
+public class BitEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, BitStatement> {
 
     private final QName qName;
     private final SchemaPath schemaPath;
-    private Long position;
+    private final Long declaredPosition;
     private final List<UnknownSchemaNode> unknownSchemaNodes;
 
     public BitEffectiveStatementImpl(final StmtContext<QName, BitStatement, ?> ctx) {
         super(ctx);
 
-        List<UnknownSchemaNode> unknownSchemaNodesInit = new ArrayList<>();
-
         qName = ctx.getStatementArgument();
         schemaPath = ctx.getSchemaPath().get();
 
+        final List<UnknownSchemaNode> unknownSchemaNodesInit = new ArrayList<>();
+        Long declaredPositionInit = null;
         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
             if (effectiveStatement instanceof PositionEffectiveStatementImpl) {
-                position = ((PositionEffectiveStatementImpl) effectiveStatement).argument();
+                declaredPositionInit = ((PositionEffectiveStatementImpl) effectiveStatement).argument();
             }
             if (effectiveStatement instanceof UnknownSchemaNode) {
                 unknownSchemaNodesInit.add((UnknownSchemaNode) effectiveStatement);
             }
         }
 
+        declaredPosition = declaredPositionInit;
         unknownSchemaNodes = ImmutableList.copyOf(unknownSchemaNodesInit);
     }
 
-    @Override
-    public Long getPosition() {
-        return position;
+    public Long getDeclaredPosition() {
+        return declaredPosition;
     }
 
-    @Override
     public String getName() {
         return qName.getLocalName();
     }
 
-    @Override
     public QName getQName() {
         return qName;
     }
 
-    @Override
     public SchemaPath getPath() {
         return schemaPath;
     }
@@ -80,7 +76,7 @@ public class BitEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<Q
         int result = 1;
         result = prime * result + qName.hashCode();
         result = prime * result + schemaPath.hashCode();
-        result = prime * result + Objects.hashCode(position);
+        result = prime * result + Objects.hashCode(declaredPosition);
         result = prime * result + Objects.hashCode(unknownSchemaNodes);
         return result;
     }
@@ -96,13 +92,13 @@ public class BitEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<Q
         if (getClass() != obj.getClass()) {
             return false;
         }
-        BitsTypeDefinition.Bit other = (BitsTypeDefinition.Bit) obj;
+        final BitsTypeDefinition.Bit other = (BitsTypeDefinition.Bit) obj;
         return Objects.equals(qName, other.getQName()) && Objects.equals(schemaPath, other.getPath());
     }
 
     @Override
     public String toString() {
         return BitEffectiveStatementImpl.class.getSimpleName() + "[name=" + qName.getLocalName() + ", position="
-                + position + "]";
+                + declaredPosition + "]";
     }
 }
index 582695b8ec5ed0cd650ce6d948e7cbded3e525c9..35222b89167dfb84f5f0bef9ad5d398f09671aff 100644 (file)
@@ -25,31 +25,34 @@ public final class BitsSpecificationEffectiveStatementImpl extends
 
     private final BitsTypeDefinition typeDefinition;
 
-    public BitsSpecificationEffectiveStatementImpl(final StmtContext<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> ctx) {
+    public BitsSpecificationEffectiveStatementImpl(
+            final StmtContext<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> ctx) {
         super(ctx);
 
         final BitsTypeBuilder builder = BaseTypes.bitsTypeBuilder(ctx.getSchemaPath().get());
         Long highestPosition = null;
         for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
-            if (stmt instanceof Bit) {
-                Bit b = (Bit) stmt;
+            if (stmt instanceof BitEffectiveStatementImpl) {
+                final BitEffectiveStatementImpl bitSubStmt = (BitEffectiveStatementImpl) stmt;
 
-                if (b.getPosition() == null) {
-                    final Long newPos;
-                    if (highestPosition == null) {
-                        newPos = 0L;
-                    } else if (highestPosition != 4294967295L) {
-                        newPos = highestPosition + 1;
+                final long effectivePos;
+                if (bitSubStmt.getDeclaredPosition() == null) {
+                    if (highestPosition != null) {
+                        SourceException.throwIf(highestPosition == 4294967295L, ctx.getStatementSourceReference(),
+                                "Bit %s must have a position statement", bitSubStmt);
+                        effectivePos = highestPosition + 1;
                     } else {
-                        throw new SourceException(ctx.getStatementSourceReference(),
-                            "Bit %s must have a position statement", b);
+                        effectivePos = 0L;
                     }
-
-                    b = BitBuilder.create(b.getPath(), newPos).setDescription(b.getDescription())
-                            .setReference(b.getReference()).setStatus(b.getStatus())
-                            .setUnknownSchemaNodes(b.getUnknownSchemaNodes()).build();
+                } else {
+                    effectivePos = bitSubStmt.getDeclaredPosition();
                 }
 
+                final Bit b = BitBuilder.create(bitSubStmt.getPath(), effectivePos)
+                        .setDescription(bitSubStmt.getDescription()).setReference(bitSubStmt.getReference())
+                        .setStatus(bitSubStmt.getStatus()).setUnknownSchemaNodes(bitSubStmt.getUnknownSchemaNodes())
+                        .build();
+
                 SourceException.throwIf(b.getPosition() < 0L && b.getPosition() > 4294967295L,
                         ctx.getStatementSourceReference(), "Bit %s has illegal position", b);
 
@@ -60,7 +63,7 @@ public final class BitsSpecificationEffectiveStatementImpl extends
                 builder.addBit(b);
             }
             if (stmt instanceof UnknownEffectiveStatementImpl) {
-                builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl)stmt);
+                builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl) stmt);
             }
         }
 
index a9497b545490ada22fdd51151e11aeff04b36a30..dafa786f16bb54f5decfe041b82c5977e2b5ad81 100644 (file)
@@ -13,16 +13,14 @@ import java.util.List;
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.EnumStatement;
-import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.AbstractEffectiveDocumentedNode;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ValueEffectiveStatementImpl;
 
-public class EnumEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<String, EnumStatement>
-        implements EnumPair {
+public class EnumEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<String, EnumStatement> {
     private final List<UnknownSchemaNode> unknownSchemaNodes;
     private final String name;
-    private Integer value;
+    private final Integer declaredValue;
 
     public EnumEffectiveStatementImpl(final StmtContext<String, EnumStatement, ?> ctx) {
         super(ctx);
@@ -30,26 +28,26 @@ public class EnumEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<
         name = ctx.rawStatementArgument();
 
         final List<UnknownSchemaNode> unknownSchemaNodesInit = new ArrayList<>();
+        Integer declaredValueInit = null;
         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
             if (effectiveStatement instanceof ValueEffectiveStatementImpl) {
-                value = ((ValueEffectiveStatementImpl) effectiveStatement).argument();
+                declaredValueInit = ((ValueEffectiveStatementImpl) effectiveStatement).argument();
             }
             if (effectiveStatement instanceof UnknownSchemaNode) {
                 unknownSchemaNodesInit.add((UnknownSchemaNode) effectiveStatement);
             }
         }
 
+        declaredValue = declaredValueInit;
         unknownSchemaNodes = ImmutableList.copyOf(unknownSchemaNodesInit);
     }
 
-    @Override
     public String getName() {
         return name;
     }
 
-    @Override
-    public Integer getValue() {
-        return value;
+    public Integer getDeclaredValue() {
+        return declaredValue;
     }
 
     @Override
index 09937c15b5ef0c956469bbad26d66b0355eff5ac..425d6e74e8ac51d5ce664b2682fce2f5d93ebad8 100644 (file)
@@ -26,31 +26,34 @@ public final class EnumSpecificationEffectiveStatementImpl extends
 
     private final EnumTypeDefinition typeDefinition;
 
-    public EnumSpecificationEffectiveStatementImpl(final StmtContext<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> ctx) {
+    public EnumSpecificationEffectiveStatementImpl(
+            final StmtContext<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> ctx) {
         super(ctx);
 
         final EnumerationTypeBuilder builder = BaseTypes.enumerationTypeBuilder(ctx.getSchemaPath().get());
         Integer highestValue = null;
         for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
-            if (stmt instanceof EnumPair) {
-                EnumPair p = (EnumPair) stmt;
+            if (stmt instanceof EnumEffectiveStatementImpl) {
+                final EnumEffectiveStatementImpl enumSubStmt = (EnumEffectiveStatementImpl) stmt;
 
-                if (p.getValue() == null) {
-                    final Integer newValue;
-                    if (highestValue == null) {
-                        newValue = 0;
-                    } else if (highestValue != 2147483647) {
-                        newValue = highestValue + 1;
+                final int effectiveValue;
+                if (enumSubStmt.getDeclaredValue() == null) {
+                    if (highestValue != null) {
+                        SourceException.throwIf(highestValue == 2147483647, ctx.getStatementSourceReference(),
+                                "Enum '%s' must have a value statement", enumSubStmt);
+                        effectiveValue = highestValue + 1;
                     } else {
-                        throw new SourceException(ctx.getStatementSourceReference(),
-                            "Enum '%s' must have a value statement", p);
+                        effectiveValue = 0;
                     }
-
-                    p = EnumPairBuilder.create(p.getName(), newValue).setDescription(p.getDescription())
-                            .setReference(p.getReference()).setStatus(p.getStatus())
-                            .setUnknownSchemaNodes(p.getUnknownSchemaNodes()).build();
+                } else {
+                    effectiveValue = enumSubStmt.getDeclaredValue();
                 }
 
+                final EnumPair p = EnumPairBuilder.create(enumSubStmt.getName(), effectiveValue)
+                        .setDescription(enumSubStmt.getDescription()).setReference(enumSubStmt.getReference())
+                        .setStatus(enumSubStmt.getStatus()).setUnknownSchemaNodes(enumSubStmt.getUnknownSchemaNodes())
+                        .build();
+
                 if (highestValue == null || highestValue < p.getValue()) {
                     highestValue = p.getValue();
                 }
@@ -58,7 +61,7 @@ public final class EnumSpecificationEffectiveStatementImpl extends
                 builder.addEnum(p);
             }
             if (stmt instanceof UnknownEffectiveStatementImpl) {
-                builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl)stmt);
+                builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl) stmt);
             }
         }
 
diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6316.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6316.java
new file mode 100644 (file)
index 0000000..eb60e0a
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.stmt;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.FileNotFoundException;
+import java.net.URISyntaxException;
+import java.util.List;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
+import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
+
+public class Bug6316 {
+    @Test
+    public void test() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
+        final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug6316");
+        assertNotNull(context);
+        verifyEnumTypedefinition(context);
+        verifyBitsTypedefinition(context);
+    }
+
+    private void verifyEnumTypedefinition(final SchemaContext context) {
+        final DataSchemaNode dataChildByName = context.getDataChildByName(QName
+                .create("foo", "1970-01-01", "enum-leaf"));
+        assertTrue(dataChildByName instanceof LeafSchemaNode);
+        final LeafSchemaNode enumLeaf = (LeafSchemaNode) dataChildByName;
+        final TypeDefinition<? extends TypeDefinition<?>> type = enumLeaf.getType();
+        assertTrue(type instanceof EnumTypeDefinition);
+        final EnumTypeDefinition myEnumeration = (EnumTypeDefinition) type;
+        final List<EnumPair> values = myEnumeration.getValues();
+        for (final EnumPair enumPair : values) {
+            final String name = enumPair.getName();
+            switch (name) {
+            case "zero":
+                assertEquals(0, enumPair.getValue());
+                break;
+            case "twenty":
+                assertEquals(20, enumPair.getValue());
+                break;
+            case "twenty-one":
+                assertEquals(21, enumPair.getValue());
+                break;
+            case "two":
+                assertEquals(2, enumPair.getValue());
+                break;
+            case "twenty-two":
+                assertEquals(22, enumPair.getValue());
+                break;
+            default:
+                fail("Unexpected enum name.");
+            }
+        }
+    }
+
+    private void verifyBitsTypedefinition(final SchemaContext context) {
+        final DataSchemaNode dataChildByName = context.getDataChildByName(QName
+                .create("foo", "1970-01-01", "bits-leaf"));
+        assertTrue(dataChildByName instanceof LeafSchemaNode);
+        final LeafSchemaNode bitsLeaf = (LeafSchemaNode) dataChildByName;
+        final TypeDefinition<? extends TypeDefinition<?>> type = bitsLeaf.getType();
+        assertTrue(type instanceof BitsTypeDefinition);
+        final BitsTypeDefinition myBits = (BitsTypeDefinition) type;
+        final List<Bit> positions = myBits.getBits();
+        for (final Bit bit : positions) {
+            final String name = bit.getName();
+            switch (name) {
+            case "zero":
+                assertEquals(0, bit.getPosition());
+                break;
+            case "twenty":
+                assertEquals(20, bit.getPosition());
+                break;
+            case "twenty-one":
+                assertEquals(21, bit.getPosition());
+                break;
+            case "two":
+                assertEquals(2, bit.getPosition());
+                break;
+            case "twenty-two":
+                assertEquals(22, bit.getPosition());
+                break;
+            default:
+                fail("Unexpected bit name.");
+            }
+        }
+    }
+}
index 529d2a99269ab5195cf55604d5c6daae3150e2cc..b839c2b8b63aef394634b0993c522c447768605b 100644 (file)
@@ -17,14 +17,18 @@ import static org.junit.Assert.assertTrue;
 import java.util.List;
 import org.junit.Before;
 import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
@@ -37,10 +41,8 @@ import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.LeafEffectiveStatementImpl;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BitEffectiveStatementImpl;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BitsSpecificationEffectiveStatementImpl;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.Decimal64SpecificationEffectiveStatementImpl;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.EnumEffectiveStatementImpl;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.EnumSpecificationEffectiveStatementImpl;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.IdentityRefSpecificationEffectiveStatementImpl;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.LeafrefSpecificationEffectiveStatementImpl;
@@ -53,22 +55,24 @@ public class EffectiveStatementTypeTest {
             "/type-tests/types.yang", false);
     private static EffectiveSchemaContext effectiveSchemaContext;
     private static LeafSchemaNode currentLeaf;
+    private static Module types;
 
     @Before
     public void setup() throws ReactorException {
-        CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
+        final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
         reactor.addSource(IMPORTED_MODULE);
         effectiveSchemaContext = reactor.buildEffective();
+        types = effectiveSchemaContext.findModuleByName("types", null);
+        assertNotNull(types);
     }
 
     @Test
     public void testBinary() {
-        currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null)
-                .getDataChildByName("leaf-binary");
+        currentLeaf = (LeafSchemaNode) types.getDataChildByName(QName.create(types.getQNameModule(), "leaf-binary"));
         assertNotNull(currentLeaf.getType());
 
-        BinaryTypeDefinition binaryEff = (BinaryTypeDefinition) ((TypeEffectiveStatement<?>)
-                ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next()).getTypeDefinition();
+        final BinaryTypeDefinition binaryEff = (BinaryTypeDefinition) ((TypeEffectiveStatement<?>) ((LeafEffectiveStatementImpl) currentLeaf)
+                .effectiveSubstatements().iterator().next()).getTypeDefinition();
 
         assertNull(binaryEff.getBaseType());
         assertNull(binaryEff.getUnits());
@@ -84,16 +88,15 @@ public class EffectiveStatementTypeTest {
 
     @Test
     public void testBits() {
-        currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null)
-                .getDataChildByName("leaf-bits");
+        currentLeaf = (LeafSchemaNode) types.getDataChildByName(QName.create(types.getQNameModule(), "leaf-bits"));
         assertNotNull(currentLeaf.getType());
 
-        List<BitsTypeDefinition.Bit> bitsEffIter = ((BitsTypeDefinition) currentLeaf.getType()).getBits();
-        BitEffectiveStatementImpl bitEff = (BitEffectiveStatementImpl) bitsEffIter.get(0);
-        BitEffectiveStatementImpl bitEffSecond = (BitEffectiveStatementImpl) bitsEffIter.get(1);
+        final List<BitsTypeDefinition.Bit> bitsEffIter = ((BitsTypeDefinition) currentLeaf.getType()).getBits();
+        final Bit bitEff = bitsEffIter.get(0);
+        final Bit bitEffSecond = bitsEffIter.get(1);
 
-        BitsTypeDefinition bitsEff = ((BitsSpecificationEffectiveStatementImpl)
-                ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next()).getTypeDefinition();
+        final BitsTypeDefinition bitsEff = ((BitsSpecificationEffectiveStatementImpl) ((LeafEffectiveStatementImpl) currentLeaf)
+                .effectiveSubstatements().iterator().next()).getTypeDefinition();
 
         assertNull(bitsEff.getBaseType());
         assertNotNull(bitsEff.getQName());
@@ -124,17 +127,15 @@ public class EffectiveStatementTypeTest {
         assertNotNull(bitEff.toString());
         assertEquals("one", bitEff.getName());
         assertNotNull(bitEff.getQName());
-        assertEquals("0", bitEff.getPosition().toString());
-        assertEquals(0, bitEff.getPosition().longValue());
+        assertEquals(0, bitEff.getPosition());
     }
 
     @Test
     public void testBoolean() {
-        currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null)
-                .getDataChildByName("leaf-boolean");
+        currentLeaf = (LeafSchemaNode) types.getDataChildByName(QName.create(types.getQNameModule(), "leaf-boolean"));
         assertNotNull(currentLeaf.getType());
-        BooleanTypeDefinition booleanEff = (BooleanTypeDefinition) ((TypeEffectiveStatement<?>)
-                ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next()).getTypeDefinition();
+        final BooleanTypeDefinition booleanEff = (BooleanTypeDefinition) ((TypeEffectiveStatement<?>) ((LeafEffectiveStatementImpl) currentLeaf)
+                .effectiveSubstatements().iterator().next()).getTypeDefinition();
 
         assertNull(booleanEff.getBaseType());
         assertNull(booleanEff.getUnits());
@@ -150,11 +151,10 @@ public class EffectiveStatementTypeTest {
 
     @Test
     public void testDecimal64() {
-        currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null)
-                .getDataChildByName("leaf-decimal64");
+        currentLeaf = (LeafSchemaNode) types.getDataChildByName(QName.create(types.getQNameModule(), "leaf-decimal64"));
         assertNotNull(currentLeaf.getType());
-        DecimalTypeDefinition decimal64Eff = ((Decimal64SpecificationEffectiveStatementImpl)
-                ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next()).getTypeDefinition();
+        final DecimalTypeDefinition decimal64Eff = ((Decimal64SpecificationEffectiveStatementImpl) ((LeafEffectiveStatementImpl) currentLeaf)
+                .effectiveSubstatements().iterator().next()).getTypeDefinition();
 
         assertNull(decimal64Eff.getBaseType());
         assertNull(decimal64Eff.getUnits());
@@ -162,7 +162,8 @@ public class EffectiveStatementTypeTest {
         assertEquals("decimal64", decimal64Eff.getQName().getLocalName());
         assertNotNull(decimal64Eff.getUnknownSchemaNodes());
 
-        // FIXME: The model is wrong: description/reference/status are not allowed under 'type', how come we parse it?
+        // FIXME: The yang model api is wrong: description/reference/status are not allowed under 'type', how come we parse it?
+        // allowed under 'type', how come we parse it?
         assertNull(decimal64Eff.getDescription());
         assertNull(decimal64Eff.getReference());
         assertEquals("CURRENT", decimal64Eff.getStatus().toString());
@@ -179,11 +180,10 @@ public class EffectiveStatementTypeTest {
 
     @Test
     public void testEmpty() {
-        currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null)
-                .getDataChildByName("leaf-empty");
+        currentLeaf = (LeafSchemaNode) types.getDataChildByName(QName.create(types.getQNameModule(), "leaf-empty"));
         assertNotNull(currentLeaf.getType());
-        EmptyTypeDefinition emptyEff = (EmptyTypeDefinition) ((TypeEffectiveStatement<?>)
-                ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next()).getTypeDefinition();
+        final EmptyTypeDefinition emptyEff = (EmptyTypeDefinition) ((TypeEffectiveStatement<?>) ((LeafEffectiveStatementImpl) currentLeaf)
+                .effectiveSubstatements().iterator().next()).getTypeDefinition();
 
         assertNull(emptyEff.getUnits());
         assertNull(emptyEff.getDefaultValue());
@@ -199,14 +199,13 @@ public class EffectiveStatementTypeTest {
 
     @Test
     public void testEnum() {
-        currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null)
-                .getDataChildByName("leaf-enum");
+        currentLeaf = (LeafSchemaNode) types.getDataChildByName(QName.create(types.getQNameModule(), "leaf-enum"));
         assertNotNull(currentLeaf.getType());
-        List<EnumTypeDefinition.EnumPair> enumEffIter = ((EnumTypeDefinition) currentLeaf.getType()).getValues();
-        EnumEffectiveStatementImpl enumEff = (EnumEffectiveStatementImpl) enumEffIter.iterator().next();
+        final List<EnumTypeDefinition.EnumPair> enumEffIter = ((EnumTypeDefinition) currentLeaf.getType()).getValues();
+        final EnumPair enumEff = enumEffIter.iterator().next();
 
-        EnumTypeDefinition enumSpecEff = ((EnumSpecificationEffectiveStatementImpl)
-                ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next()).getTypeDefinition();
+        final EnumTypeDefinition enumSpecEff = ((EnumSpecificationEffectiveStatementImpl) ((LeafEffectiveStatementImpl) currentLeaf)
+                .effectiveSubstatements().iterator().next()).getTypeDefinition();
 
         assertEquals("enumeration", enumSpecEff.getQName().getLocalName());
         assertEquals("enumeration", enumSpecEff.getPath().getLastComponent().getLocalName());
@@ -229,16 +228,16 @@ public class EffectiveStatementTypeTest {
         assertEquals("test enum", enumEff.getDescription());
         assertEquals("test enum ref", enumEff.getReference());
         assertEquals("CURRENT", enumEff.getStatus().toString());
-        assertEquals("0", enumEff.getValue().toString());
+        assertEquals(0, enumEff.getValue());
     }
 
     @Test
     public void testIdentityRef() {
-        currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null)
-                .getDataChildByName("leaf-identityref");
+        currentLeaf = (LeafSchemaNode) types
+                .getDataChildByName(QName.create(types.getQNameModule(), "leaf-identityref"));
         assertNotNull(currentLeaf.getType());
-        IdentityrefTypeDefinition identityRefEff = ((IdentityRefSpecificationEffectiveStatementImpl)
-                ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next()).getTypeDefinition();
+        final IdentityrefTypeDefinition identityRefEff = ((IdentityRefSpecificationEffectiveStatementImpl) ((LeafEffectiveStatementImpl) currentLeaf)
+                .effectiveSubstatements().iterator().next()).getTypeDefinition();
 
         assertNull(identityRefEff.getDefaultValue());
         assertEquals("identityref", identityRefEff.getQName().getLocalName());
@@ -257,12 +256,11 @@ public class EffectiveStatementTypeTest {
 
     @Test
     public void testInstanceIdentifier() {
-        currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null)
-                .getDataChildByName("leaf-instance-identifier");
+        currentLeaf = (LeafSchemaNode) types.getDataChildByName(QName.create(types.getQNameModule(),
+                "leaf-instance-identifier"));
         assertNotNull(currentLeaf.getType());
-        InstanceIdentifierTypeDefinition instanceIdentEff = (InstanceIdentifierTypeDefinition)
-                ((TypeEffectiveStatement<?>) ((LeafEffectiveStatementImpl) currentLeaf)
-                        .effectiveSubstatements().iterator().next()).getTypeDefinition();
+        final InstanceIdentifierTypeDefinition instanceIdentEff = (InstanceIdentifierTypeDefinition) ((TypeEffectiveStatement<?>) ((LeafEffectiveStatementImpl) currentLeaf)
+                .effectiveSubstatements().iterator().next()).getTypeDefinition();
         assertNotNull(instanceIdentEff.toString());
 
         assertFalse(instanceIdentEff.requireInstance());
@@ -283,12 +281,11 @@ public class EffectiveStatementTypeTest {
 
     @Test
     public void testLeafref() {
-        currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null)
-                .getDataChildByName("leaf-leafref");
+        currentLeaf = (LeafSchemaNode) types.getDataChildByName(QName.create(types.getQNameModule(), "leaf-leafref"));
         assertNotNull(currentLeaf.getType());
 
-        LeafrefTypeDefinition leafrefEff = ((LeafrefSpecificationEffectiveStatementImpl)
-                ((LeafEffectiveStatementImpl) currentLeaf).effectiveSubstatements().iterator().next()).getTypeDefinition();
+        final LeafrefTypeDefinition leafrefEff = ((LeafrefSpecificationEffectiveStatementImpl) ((LeafEffectiveStatementImpl) currentLeaf)
+                .effectiveSubstatements().iterator().next()).getTypeDefinition();
 
         assertEquals("/container-test/leaf-test", leafrefEff.getPathStatement().toString());
         assertNull(leafrefEff.getBaseType());
@@ -309,73 +306,61 @@ public class EffectiveStatementTypeTest {
 
     @Test
     public void testIntAll() {
-        currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null)
-                .getDataChildByName("leaf-int8");
+        currentLeaf = (LeafSchemaNode) types.getDataChildByName(QName.create(types.getQNameModule(), "leaf-int8"));
         assertNotNull(currentLeaf.getType());
-        TypeEffectiveStatement<?> int8Eff = (TypeEffectiveStatement<?>) ((LeafEffectiveStatementImpl) currentLeaf)
+        final TypeEffectiveStatement<?> int8Eff = (TypeEffectiveStatement<?>) ((LeafEffectiveStatementImpl) currentLeaf)
                 .effectiveSubstatements().iterator().next();
         assertNotNull(int8Eff.toString());
 
-        currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null)
-                .getDataChildByName("leaf-int16");
+        currentLeaf = (LeafSchemaNode) types.getDataChildByName(QName.create(types.getQNameModule(), "leaf-int16"));
         assertNotNull(currentLeaf.getType());
-        TypeEffectiveStatement<?> int16Eff = (TypeEffectiveStatement<?>) ((LeafEffectiveStatementImpl) currentLeaf)
+        final TypeEffectiveStatement<?> int16Eff = (TypeEffectiveStatement<?>) ((LeafEffectiveStatementImpl) currentLeaf)
                 .effectiveSubstatements().iterator().next();
         assertNotNull(int16Eff.toString());
 
-        currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null)
-                .getDataChildByName("leaf-int32");
+        currentLeaf = (LeafSchemaNode) types.getDataChildByName(QName.create(types.getQNameModule(), "leaf-int32"));
         assertNotNull(currentLeaf.getType());
-        TypeEffectiveStatement<?> int32Eff = (TypeEffectiveStatement<?>) ((LeafEffectiveStatementImpl) currentLeaf)
+        final TypeEffectiveStatement<?> int32Eff = (TypeEffectiveStatement<?>) ((LeafEffectiveStatementImpl) currentLeaf)
                 .effectiveSubstatements().iterator().next();
         assertNotNull(int32Eff.toString());
 
-        currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null)
-                .getDataChildByName("leaf-int64");
+        currentLeaf = (LeafSchemaNode) types.getDataChildByName(QName.create(types.getQNameModule(), "leaf-int64"));
         assertNotNull(currentLeaf.getType());
-        TypeEffectiveStatement<?> int64Eff = (TypeEffectiveStatement<?>) ((LeafEffectiveStatementImpl) currentLeaf)
+        final TypeEffectiveStatement<?> int64Eff = (TypeEffectiveStatement<?>) ((LeafEffectiveStatementImpl) currentLeaf)
                 .effectiveSubstatements().iterator().next();
         assertNotNull(int64Eff.toString());
 
-        currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null)
-                .getDataChildByName("leaf-uint8");
+        currentLeaf = (LeafSchemaNode) types.getDataChildByName(QName.create(types.getQNameModule(), "leaf-uint8"));
         assertNotNull(currentLeaf.getType());
-        TypeEffectiveStatement<?> uint8Eff = (TypeEffectiveStatement<?>) ((LeafEffectiveStatementImpl) currentLeaf)
+        final TypeEffectiveStatement<?> uint8Eff = (TypeEffectiveStatement<?>) ((LeafEffectiveStatementImpl) currentLeaf)
                 .effectiveSubstatements().iterator().next();
         assertNotNull(uint8Eff.toString());
 
-        currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null)
-                .getDataChildByName("leaf-uint16");
+        currentLeaf = (LeafSchemaNode) types.getDataChildByName(QName.create(types.getQNameModule(), "leaf-uint16"));
         assertNotNull(currentLeaf.getType());
-        TypeEffectiveStatement<?> uint16Eff = (TypeEffectiveStatement<?>) ((LeafEffectiveStatementImpl)
-                currentLeaf)
+        final TypeEffectiveStatement<?> uint16Eff = (TypeEffectiveStatement<?>) ((LeafEffectiveStatementImpl) currentLeaf)
                 .effectiveSubstatements().iterator().next();
         assertNotNull(uint16Eff.toString());
 
-        currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null)
-                .getDataChildByName("leaf-uint32");
+        currentLeaf = (LeafSchemaNode) types.getDataChildByName(QName.create(types.getQNameModule(), "leaf-uint32"));
         assertNotNull(currentLeaf.getType());
-        TypeEffectiveStatement<?> uint32Eff = (TypeEffectiveStatement<?>) ((LeafEffectiveStatementImpl)
-                currentLeaf)
+        final TypeEffectiveStatement<?> uint32Eff = (TypeEffectiveStatement<?>) ((LeafEffectiveStatementImpl) currentLeaf)
                 .effectiveSubstatements().iterator().next();
         assertNotNull(uint32Eff.toString());
 
-        currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null)
-                .getDataChildByName("leaf-uint64");
+        currentLeaf = (LeafSchemaNode) types.getDataChildByName(QName.create(types.getQNameModule(), "leaf-uint64"));
         assertNotNull(currentLeaf.getType());
-        TypeEffectiveStatement<?> uint64Eff = (TypeEffectiveStatement<?>) ((LeafEffectiveStatementImpl)
-                currentLeaf)
+        final TypeEffectiveStatement<?> uint64Eff = (TypeEffectiveStatement<?>) ((LeafEffectiveStatementImpl) currentLeaf)
                 .effectiveSubstatements().iterator().next();
         assertNotNull(uint64Eff.toString());
     }
 
     @Test
     public void testUnion() {
-        currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null)
-                .getDataChildByName("leaf-union");
+        currentLeaf = (LeafSchemaNode) types.getDataChildByName(QName.create(types.getQNameModule(), "leaf-union"));
         assertNotNull(currentLeaf.getType());
-        UnionTypeDefinition unionEff = ((UnionSpecificationEffectiveStatementImpl)
-                ((LeafEffectiveStatementImpl)currentLeaf).effectiveSubstatements().iterator().next()).getTypeDefinition();
+        final UnionTypeDefinition unionEff = ((UnionSpecificationEffectiveStatementImpl) ((LeafEffectiveStatementImpl) currentLeaf)
+                .effectiveSubstatements().iterator().next()).getTypeDefinition();
 
         assertEquals(2, unionEff.getTypes().size());
         assertEquals("union", unionEff.getQName().getLocalName());
@@ -396,15 +381,18 @@ public class EffectiveStatementTypeTest {
 
     @Test
     public void testLengthConstraint() {
-        currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null)
-                .getDataChildByName("leaf-length-pattern");
+        currentLeaf = (LeafSchemaNode) types.getDataChildByName(QName.create(types.getQNameModule(),
+                "leaf-length-pattern"));
         assertNotNull(currentLeaf.getType());
-        LengthConstraint lengthConstraint = ((StringTypeDefinition) (currentLeaf.getType())).getLengthConstraints().get(0);
-        LengthConstraint lengthConstraintThird = ((StringTypeDefinition) (currentLeaf.getType())).getLengthConstraints().get(0);
-        currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null)
-                .getDataChildByName("leaf-length-pattern-second");
+        final LengthConstraint lengthConstraint = ((StringTypeDefinition) (currentLeaf.getType()))
+                .getLengthConstraints().get(0);
+        final LengthConstraint lengthConstraintThird = ((StringTypeDefinition) (currentLeaf.getType()))
+                .getLengthConstraints().get(0);
+        currentLeaf = (LeafSchemaNode) types.getDataChildByName(QName.create(types.getQNameModule(),
+                "leaf-length-pattern-second"));
         assertNotNull(currentLeaf.getType());
-        LengthConstraint lengthConstraintSecond = ((StringTypeDefinition) (currentLeaf.getType())).getLengthConstraints().get(0);
+        final LengthConstraint lengthConstraintSecond = ((StringTypeDefinition) (currentLeaf.getType()))
+                .getLengthConstraints().get(0);
 
         assertEquals(1, lengthConstraint.getMin().intValue());
         assertEquals(255, lengthConstraint.getMax().intValue());
@@ -422,18 +410,18 @@ public class EffectiveStatementTypeTest {
 
     @Test
     public void testPatternConstraint() {
-        currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null)
-                .getDataChildByName("leaf-length-pattern");
+        currentLeaf = (LeafSchemaNode) types.getDataChildByName(QName.create(types.getQNameModule(),
+                "leaf-length-pattern"));
         assertNotNull(currentLeaf.getType());
-        PatternConstraintEffectiveImpl lengthConstraint = (PatternConstraintEffectiveImpl)
-                ((StringTypeDefinition) (currentLeaf.getType())).getPatternConstraints().get(0);
-        PatternConstraintEffectiveImpl lengthConstraintThird = (PatternConstraintEffectiveImpl)
-                ((StringTypeDefinition) (currentLeaf.getType())).getPatternConstraints().get(0);
-        currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null)
-                .getDataChildByName("leaf-length-pattern-second");
+        final PatternConstraintEffectiveImpl lengthConstraint = (PatternConstraintEffectiveImpl) ((StringTypeDefinition) (currentLeaf
+                .getType())).getPatternConstraints().get(0);
+        final PatternConstraintEffectiveImpl lengthConstraintThird = (PatternConstraintEffectiveImpl) ((StringTypeDefinition) (currentLeaf
+                .getType())).getPatternConstraints().get(0);
+        currentLeaf = (LeafSchemaNode) types.getDataChildByName(QName.create(types.getQNameModule(),
+                "leaf-length-pattern-second"));
         assertNotNull(currentLeaf.getType());
-        PatternConstraintEffectiveImpl lengthConstraintSecond = (PatternConstraintEffectiveImpl)
-                ((StringTypeDefinition) (currentLeaf.getType())).getPatternConstraints().get(0);
+        final PatternConstraintEffectiveImpl lengthConstraintSecond = (PatternConstraintEffectiveImpl) ((StringTypeDefinition) (currentLeaf
+                .getType())).getPatternConstraints().get(0);
 
         assertEquals("^[0-9a-fA-F]*$", lengthConstraint.getRegularExpression());
         assertNull(lengthConstraint.getReference());
@@ -450,11 +438,10 @@ public class EffectiveStatementTypeTest {
 
     @Test
     public void testString() {
-        currentLeaf = (LeafSchemaNode) effectiveSchemaContext.findModuleByName("types", null)
-                .getDataChildByName("leaf-string");
+        currentLeaf = (LeafSchemaNode) types.getDataChildByName(QName.create(types.getQNameModule(), "leaf-string"));
         assertNotNull(currentLeaf.getType());
-        StringTypeDefinition stringEff = (StringTypeDefinition) ((TypeEffectiveStatement<?>)
-                ((LeafEffectiveStatementImpl)currentLeaf).effectiveSubstatements().iterator().next()).getTypeDefinition();
+        final StringTypeDefinition stringEff = (StringTypeDefinition) ((TypeEffectiveStatement<?>) ((LeafEffectiveStatementImpl) currentLeaf)
+                .effectiveSubstatements().iterator().next()).getTypeDefinition();
 
         assertEquals("string", stringEff.getQName().getLocalName());
         assertEquals("CURRENT", stringEff.getStatus().toString());
diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug6316/foo.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug6316/foo.yang
new file mode 100644 (file)
index 0000000..1a56c05
--- /dev/null
@@ -0,0 +1,40 @@
+module foo {
+    namespace "foo";
+    prefix foo;
+
+    typedef my-enumeration {
+        type enumeration {
+            enum zero;
+            enum twenty {
+                value 20;
+            }
+            enum twenty-one;
+            enum two {
+                value 2;
+            }
+            enum twenty-two;
+        }
+    }
+
+    typedef my-bits {
+        type bits {
+            bit zero;
+            bit twenty {
+                position 20;
+            }
+            bit twenty-one;
+            bit two {
+                position 2;
+            }
+            bit twenty-two;
+        }
+    }
+
+    leaf enum-leaf {
+        type my-enumeration;
+    }
+
+    leaf bits-leaf {
+        type my-bits;
+    }
+}