Implemented typedef units statement in generated code. 21/2821/1
authorMartin Vitez <mvitez@cisco.com>
Mon, 18 Nov 2013 10:54:39 +0000 (11:54 +0100)
committerMartin Vitez <mvitez@cisco.com>
Mon, 18 Nov 2013 12:09:27 +0000 (13:09 +0100)
Added _UNITS constant to classes generated from typedefs with units statement (except typedef with enumeration as base type).
Updated tests.

Signed-off-by: Martin Vitez <mvitez@cisco.com>
code-generator/binding-generator-util/src/main/java/org/opendaylight/yangtools/binding/generator/util/Types.java
code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/AugmentToUsesInAugmentCompilationTest.java
code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/CascadeUsesCompilationTest.java
code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/CompilationTest.java
code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/CompilationTestUtils.java
code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/TypedefCompilationTest.java
code-generator/binding-java-api-generator/src/test/resources/compilation/typedef/foo.yang
code-generator/binding-type-provider/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java

index ffcbf175b6314817db87dbcffaf93a4a54c801f0..b658d2f2fa51f5bda45ee330484def38754f59a6 100644 (file)
@@ -30,6 +30,7 @@ public final class Types {
 
     public static final ConcreteType BOOLEAN = typeForClass(Boolean.class);
     public static final ConcreteType FUTURE = typeForClass(Future.class);
+    public static final ConcreteType STRING = typeForClass(String.class);
     public static final ConcreteType VOID = typeForClass(Void.class);
 
     /**
index 6f0aaae29e9ae7757f7b9040cee11bfd9c2f33ba..833fcab8545be0bc6b3533aafccafda5592bc3e1 100644 (file)
@@ -41,14 +41,14 @@ public class AugmentToUsesInAugmentCompilationTest extends BaseCompilationTest {
 
         // Test if all sources are generated from 'module foo'
         File fooParent = new File(sourcesOutputDir, NS_FOO);
-        testFilesCount(fooParent, 4);
+        assertFilesCount(fooParent, 4);
         assertTrue(new File(fooParent, "IgpLinkAttributes.java").exists());
         assertTrue(new File(fooParent, "Link1.java").exists());
         assertTrue(new File(fooParent, "Link1Builder.java").exists());
 
         // Test if all sources are generated from 'module bar'
         File barParent = new File(sourcesOutputDir, NS_BAR);
-        testFilesCount(barParent, 7);
+        assertFilesCount(barParent, 7);
         assertTrue(new File(barParent, "BarData.java").exists());
         assertTrue(new File(barParent, "NetworkTopology.java").exists());
         assertTrue(new File(barParent, "NetworkTopologyBuilder.java").exists());
@@ -56,22 +56,22 @@ public class AugmentToUsesInAugmentCompilationTest extends BaseCompilationTest {
         assertTrue(new File(barParent, "LinkAttributes.java").exists());
 
         File networkParent = new File(barParent, "network");
-        testFilesCount(networkParent, 1);
+        assertFilesCount(networkParent, 1);
         File topologyParent = new File(networkParent, "topology");
-        testFilesCount(topologyParent, 3);
+        assertFilesCount(topologyParent, 3);
         assertTrue(new File(topologyParent, "Topology.java").exists());
         assertTrue(new File(topologyParent, "TopologyBuilder.java").exists());
         assertTrue(new File(topologyParent, "TopologyKey.java").exists());
 
         File linkParent = new File(barParent, "link");
-        testFilesCount(linkParent, 3);
+        assertFilesCount(linkParent, 3);
         assertTrue(new File(linkParent, "Link.java").exists());
         assertTrue(new File(linkParent, "LinkBuilder.java").exists());
         assertTrue(new File(linkParent, "LinkKey.java").exists());
 
         // Test if all sources are generated from 'module baz'
         File bazParent = new File(sourcesOutputDir, NS_BAZ);
-        testFilesCount(bazParent, 4);
+        assertFilesCount(bazParent, 4);
         assertTrue(new File(bazParent, "IgpLinkAttributes1.java").exists());
         assertTrue(new File(bazParent, "IgpLinkAttributes1Builder.java").exists());
         assertTrue(new File(bazParent, "LinkAttributes.java").exists());
@@ -88,7 +88,7 @@ public class AugmentToUsesInAugmentCompilationTest extends BaseCompilationTest {
 
             Class<?> igpLinkAttributesClass = Class.forName(BASE_PKG
                     + ".urn.opendaylight.foo.rev131008.IgpLinkAttributes", true, loader);
-            testImplementsIfc(link1Class, igpLinkAttributesClass);
+            assertImplementsIfc(link1Class, igpLinkAttributesClass);
         } catch (ClassNotFoundException e) {
             throw new AssertionError("Class for augment wasn't generated");
         }
@@ -101,7 +101,7 @@ public class AugmentToUsesInAugmentCompilationTest extends BaseCompilationTest {
 
             Class<?> linkAttributesClass = Class.forName(BASE_PKG + ".urn.opendaylight.baz.rev131008.LinkAttributes",
                     true, loader);
-            testImplementsIfc(igpLinkAttributes1Class, linkAttributesClass);
+            assertImplementsIfc(igpLinkAttributes1Class, linkAttributesClass);
         } catch (ClassNotFoundException e) {
             throw new AssertionError("Class for augment wasn't generated");
         }
index 06ac678afe5c9a0e00abcbfb5f4579ee24eabfb7..a4f42fe8605c9b74e141ef9d7c671aafc8e5d571 100644 (file)
@@ -43,7 +43,7 @@ public class CascadeUsesCompilationTest extends BaseCompilationTest {
 
         // Test if all sources are generated from module foo
         File parent = new File(sourcesOutputDir, NS_FOO);
-        testFilesCount(parent, 5);
+        assertFilesCount(parent, 5);
         File fooData = new File(parent, "FooData.java");
         File foo_gr1 = new File(parent, "FooGr1.java");
         File nodes = new File(parent, "Nodes.java");
@@ -55,7 +55,7 @@ public class CascadeUsesCompilationTest extends BaseCompilationTest {
 
         // Test if all sources are generated from module bar
         parent = new File(sourcesOutputDir, NS_BAR);
-        testFilesCount(parent, 2);
+        assertFilesCount(parent, 2);
         File barGr1 = new File(parent, "BarGr1.java");
         File barGr2 = new File(parent, "BarGr2.java");
         assertTrue(barGr1.exists());
@@ -63,7 +63,7 @@ public class CascadeUsesCompilationTest extends BaseCompilationTest {
 
         // Test if all sources are generated from module baz
         parent = new File(sourcesOutputDir, NS_BAZ);
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         File bazGr1 = new File(parent, "BazGr1.java");
         assertTrue(bazGr1.exists());
 
@@ -81,8 +81,8 @@ public class CascadeUsesCompilationTest extends BaseCompilationTest {
         Class<?> bazGr1Class = Class.forName(BASE_PKG + ".urn.opendaylight.baz.rev131008.BazGr1", true, loader);
 
         // test generated interface from 'container nodes'
-        testImplementsIfc(nodesClass, fooGr1Class);
-        testImplementsIfc(nodesClass, barGr2Class);
+        assertImplementsIfc(nodesClass, fooGr1Class);
+        assertImplementsIfc(nodesClass, barGr2Class);
 
         // test generated builder for 'container nodes'
         assertFalse(nodesBuilderClass.isInterface());
index 1fe839cef992454a545de17ef17b0be9473985d0..3d9ece434ed9f6030ac373489ab94ad755443400 100644 (file)
@@ -64,7 +64,7 @@ public class CompilationTest extends BaseCompilationTest {
         assertTrue(linksBuilder.exists());
         assertTrue(linksKey.exists());
         assertTrue(testData.exists());
-        testFilesCount(parent, 6);
+        assertFilesCount(parent, 6);
 
         parent = new File(sourcesOutputDir, NS_TEST + FS + "links");
         File level = new File(parent, "Level.java");
@@ -81,7 +81,7 @@ public class CompilationTest extends BaseCompilationTest {
         assertTrue(nodeList.exists());
         assertTrue(nodeListBuilder.exists());
         assertTrue(nodesType.exists());
-        testFilesCount(parent, 7);
+        assertFilesCount(parent, 7);
 
         // Test if sources are compilable
         testCompilation(sourcesOutputDir, compiledOutputDir);
@@ -101,7 +101,7 @@ public class CompilationTest extends BaseCompilationTest {
         assertTrue(linksClass.isInterface());
         // TODO: anyxml
         assertEquals(6, linksClass.getDeclaredMethods().length);
-        testImplementsIfc(linksClass, keyArgsClass);
+        assertImplementsIfc(linksClass, keyArgsClass);
 
         // Test list key constructor arguments ordering
         assertContainsConstructor(linksKeyClass, Byte.class, String.class, Integer.class);
@@ -133,77 +133,77 @@ public class CompilationTest extends BaseCompilationTest {
         assertTrue(new File(parent, "OpenObject.java").exists());
         assertTrue(new File(parent, "ExplicitRouteObject.java").exists());
         assertTrue(new File(parent, "PathKeySubobject.java").exists());
-        testFilesCount(parent, 7);
+        assertFilesCount(parent, 7);
 
         parent = new File(parent, "object");
         assertTrue(new File(parent, "Nodes.java").exists());
         assertTrue(new File(parent, "NodesBuilder.java").exists());
-        testFilesCount(parent, 2);
+        assertFilesCount(parent, 2);
 
         parent = new File(sourcesOutputDir, NS_FOO + FS + "open");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
 
         parent = new File(parent, "object");
         assertTrue(new File(parent, "Nodes1.java").exists());
         assertTrue(new File(parent, "Nodes1Builder.java").exists());
-        testFilesCount(parent, 3);
+        assertFilesCount(parent, 3);
 
         parent = new File(parent, "nodes");
         assertTrue(new File(parent, "Links.java").exists());
         assertTrue(new File(parent, "LinksBuilder.java").exists());
-        testFilesCount(parent, 2);
+        assertFilesCount(parent, 2);
 
         parent = new File(sourcesOutputDir, NS_FOO + FS + "explicit");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         parent = new File(parent, "route");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         parent = new File(parent, "object");
         assertTrue(new File(parent, "Subobjects.java").exists());
         assertTrue(new File(parent, "SubobjectsBuilder.java").exists());
-        testFilesCount(parent, 3);
+        assertFilesCount(parent, 3);
 
         parent = new File(parent, "subobjects");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         parent = new File(parent, "subobject");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         parent = new File(parent, "type");
         assertTrue(new File(parent, "PathKey.java").exists());
         assertTrue(new File(parent, "PathKeyBuilder.java").exists());
-        testFilesCount(parent, 3);
+        assertFilesCount(parent, 3);
 
         parent = new File(parent, "path");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         parent = new File(parent, "key");
         assertTrue(new File(parent, "PathKey.java").exists());
         assertTrue(new File(parent, "PathKeyBuilder.java").exists());
-        testFilesCount(parent, 2);
+        assertFilesCount(parent, 2);
 
         // Test if all sources were generated from 'module bar'
         parent = new File(sourcesOutputDir, NS_BAR);
         assertTrue(new File(parent, "BasicExplicitRouteSubobjects.java").exists());
         assertTrue(new File(parent, "ExplicitRouteSubobjects.java").exists());
-        testFilesCount(parent, 3);
+        assertFilesCount(parent, 3);
 
         parent = new File(parent, "basic");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         parent = new File(parent, "explicit");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         parent = new File(parent, "route");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
 
         parent = new File(parent, "subobjects");
-        testFilesCount(parent, 2);
+        assertFilesCount(parent, 2);
         assertTrue(new File(parent, "SubobjectType.java").exists());
 
         parent = new File(parent, "subobject");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
 
         parent = new File(parent, "type");
         assertTrue(new File(parent, "IpPrefix.java").exists());
         assertTrue(new File(parent, "IpPrefixBuilder.java").exists());
         assertTrue(new File(parent, "Label.java").exists());
         assertTrue(new File(parent, "LabelBuilder.java").exists());
-        testFilesCount(parent, 4);
+        assertFilesCount(parent, 4);
 
         // Test if sources are compilable
         testCompilation(sourcesOutputDir, compiledOutputDir);
@@ -235,19 +235,19 @@ public class CompilationTest extends BaseCompilationTest {
         assertTrue(pathAttributes.exists());
         assertTrue(update.exists());
         assertTrue(updateBuilder.exists());
-        testFilesCount(parent, 6);
+        assertFilesCount(parent, 6);
 
         parent = new File(sourcesOutputDir, NS_FOO + FS + "path");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         parent = new File(parent, "attributes");
-        testFilesCount(parent, 2);
+        assertFilesCount(parent, 2);
         File origin = new File(parent, "Origin.java");
         File originBuilder = new File(parent, "OriginBuilder.java");
         assertTrue(origin.exists());
         assertTrue(originBuilder.exists());
 
         parent = new File(sourcesOutputDir, NS_FOO + FS + "update");
-        testFilesCount(parent, 2);
+        assertFilesCount(parent, 2);
         pathAttributes = new File(parent, "PathAttributes.java");
         File pathAttributesBuilder = new File(parent, "PathAttributesBuilder.java");
         assertTrue(pathAttributes.exists());
@@ -261,94 +261,94 @@ public class CompilationTest extends BaseCompilationTest {
         assertTrue(destination.exists());
         assertTrue(pathAttributes1.exists());
         assertTrue(pathAttributes1Builder.exists());
-        testFilesCount(parent, 5);
+        assertFilesCount(parent, 5);
 
         parent = new File(sourcesOutputDir, NS_BAR + FS + "destination");
-        testFilesCount(parent, 2);
+        assertFilesCount(parent, 2);
         File destinationType = new File(parent, "DestinationType.java");
         assertTrue(destinationType.exists());
 
         parent = new File(parent, "destination");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         parent = new File(parent, "type");
-        testFilesCount(parent, 2);
+        assertFilesCount(parent, 2);
         File destinationIpv4 = new File(parent, "DestinationIp.java");
         File destinationIpv4Builder = new File(parent, "DestinationIpBuilder.java");
         assertTrue(destinationIpv4.exists());
         assertTrue(destinationIpv4Builder.exists());
 
         parent = new File(sourcesOutputDir, NS_BAR + FS + "update");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         parent = new File(parent, "path");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         parent = new File(parent, "attributes");
         File mpUnreachNlri = new File(parent, "MpUnreachNlri.java");
         File mpUnreachNlriBuilder = new File(parent, "MpUnreachNlriBuilder.java");
         assertTrue(mpUnreachNlri.exists());
         assertTrue(mpUnreachNlriBuilder.exists());
-        testFilesCount(parent, 3);
+        assertFilesCount(parent, 3);
 
         parent = new File(parent, "mp");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         parent = new File(parent, "unreach");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         parent = new File(parent, "nlri");
         File withdrawnRoutes = new File(parent, "WithdrawnRoutes.java");
         File withdrawnRoutesBuilder = new File(parent, "WithdrawnRoutesBuilder.java");
         assertTrue(withdrawnRoutes.exists());
         assertTrue(withdrawnRoutesBuilder.exists());
-        testFilesCount(parent, 2);
+        assertFilesCount(parent, 2);
 
         // Test if all sources were generated from 'module baz'
         parent = new File(sourcesOutputDir, NS_BAZ);
-        testFilesCount(parent, 2);
+        assertFilesCount(parent, 2);
         File linkstateDestination = new File(parent, "LinkstateDestination.java");
         assertTrue(linkstateDestination.exists());
 
         parent = new File(sourcesOutputDir, NS_BAZ + FS + "update");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         parent = new File(parent, "path");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         parent = new File(parent, "attributes");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         parent = new File(parent, "mp");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         parent = new File(parent, "unreach");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         parent = new File(parent, "nlri");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         parent = new File(parent, "withdrawn");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         parent = new File(parent, "routes");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         parent = new File(parent, "destination");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         parent = new File(parent, "type");
         File destinationLinkstate = new File(parent, "DestinationLinkstate.java");
         File destinationLinkstateBuilder = new File(parent, "DestinationLinkstateBuilder.java");
         assertTrue(destinationLinkstate.exists());
         assertTrue(destinationLinkstateBuilder.exists());
-        testFilesCount(parent, 3);
+        assertFilesCount(parent, 3);
         parent = new File(parent, "destination");
-        testFilesCount(parent, 1);
+        assertFilesCount(parent, 1);
         parent = new File(parent, "linkstate");
         File links = new File(parent, "Links.java");
         File linksBuilder = new File(parent, "LinksBuilder.java");
         assertTrue(links.exists());
         assertTrue(linksBuilder.exists());
-        testFilesCount(parent, 3);
+        assertFilesCount(parent, 3);
         parent = new File(parent, "links");
         File source = new File(parent, "Source.java");
         File sourceBuilder = new File(parent, "SourceBuilder.java");
         assertTrue(source.exists());
         assertTrue(sourceBuilder.exists());
-        testFilesCount(parent, 3);
+        assertFilesCount(parent, 3);
         parent = new File(parent, "source");
         File address = new File(parent, "Address.java");
         File addressBuilder = new File(parent, "AddressBuilder.java");
         assertTrue(address.exists());
         assertTrue(addressBuilder.exists());
-        testFilesCount(parent, 2);
+        assertFilesCount(parent, 2);
 
         // Test if sources are compilable
         testCompilation(sourcesOutputDir, compiledOutputDir);
@@ -371,7 +371,7 @@ public class CompilationTest extends BaseCompilationTest {
         generator.generateToFile(sourcesOutputDir);
 
         File parent = new File(sourcesOutputDir, NS_TEST);
-        testFilesCount(parent, 4);
+        assertFilesCount(parent, 4);
         assertTrue(new File(parent, "TestData.java").exists());
         assertTrue(new File(parent, "Nodes.java").exists());
         assertTrue(new File(parent, "NodesBuilder.java").exists());
@@ -425,13 +425,13 @@ public class CompilationTest extends BaseCompilationTest {
 
         // Test if all sources are generated
         File fooParent = new File(sourcesOutputDir, NS_FOO);
-        testFilesCount(fooParent, 3);
+        assertFilesCount(fooParent, 3);
         assertTrue(new File(fooParent, "FooData.java").exists());
         assertTrue(new File(fooParent, "Nodes.java").exists());
         assertTrue(new File(fooParent, "NodesBuilder.java").exists());
 
         File barParent = new File(sourcesOutputDir, NS_BAR);
-        testFilesCount(barParent, 1);
+        assertFilesCount(barParent, 1);
         assertTrue(new File(barParent, "IdentityClass.java").exists());
 
         // Test if sources are compilable
index c20a52dbb033d4e62766b435d4cb89492651d72b..a9f85265964bfa3148e2ac3de7667930ad8bc66c 100644 (file)
@@ -11,6 +11,7 @@ import static org.junit.Assert.*;
 
 import java.io.File;
 import java.io.FileNotFoundException;
+import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.lang.reflect.ParameterizedType;
@@ -54,6 +55,17 @@ public class CompilationTestUtils {
         }
     }
 
+    /**
+     * Asserts that class contains field with fiven name and type.
+     *
+     * @param clazz
+     *            class to test
+     * @param name
+     *            field name
+     * @param type
+     *            field type
+     * @return field with given name if present in class
+     */
     static Field assertContainsField(Class<?> clazz, String name, Class<?> type) {
         try {
             Field f = clazz.getDeclaredField(name);
@@ -64,6 +76,71 @@ public class CompilationTestUtils {
         }
     }
 
+    /**
+     * Asserts that class contains field with given name and value. Method tries
+     * to create new instance of class and get value of field. If class
+     * constructor contains any arguments, class is instantiated with null
+     * values.
+     *
+     * @param clazz
+     *            class to test
+     * @param name
+     *            name of field
+     * @param returnType
+     *            return type of field
+     * @param expectedValue
+     *            expected value of field
+     * @param constructorArgs
+     *            constructor arguments of class to test
+     */
+    static void assertContainsFieldWithValue(Class<?> clazz, String name, Class<?> returnType, Object expectedValue,
+            Class<?>... constructorArgs) {
+        Object[] initargs = null;
+        if (constructorArgs != null && constructorArgs.length > 0) {
+            initargs = new Object[constructorArgs.length];
+            for (int i = 0; i < constructorArgs.length; i++) {
+                initargs[i] = null;
+            }
+        }
+        assertContainsFieldWithValue(clazz, name, returnType, expectedValue, constructorArgs, initargs);
+    }
+
+    /**
+     * Asserts that class contains field with given name, return type and value.
+     *
+     * @param clazz
+     *            class to test
+     * @param name
+     *            name of field
+     * @param returnType
+     *            return type of field
+     * @param expectedValue
+     *            expected value of field
+     * @param constructorArgs
+     *            array of constructor arguments classes
+     * @param initargs
+     *            array of constructor values
+     */
+    static void assertContainsFieldWithValue(Class<?> clazz, String name, Class<?> returnType, Object expectedValue,
+            Class<?>[] constructorArgs, Object... initargs) {
+        Field f = assertContainsField(clazz, name, returnType);
+        try {
+            Constructor<?> c = clazz.getDeclaredConstructor(constructorArgs);
+            Object o = c.newInstance(initargs);
+            assertEquals(expectedValue, f.get(o));
+        } catch (Exception e) {
+            throw new AssertionError("Failed to perform " + name + " field test", e);
+        }
+    }
+
+    /**
+     * Asserts that class contains constructor with parameter types.
+     *
+     * @param clazz
+     *            class to test
+     * @param args
+     *            array of argument classes
+     */
     static void assertContainsConstructor(Class<?> clazz, Class<?>... args) {
         try {
             clazz.getDeclaredConstructor(args);
@@ -73,6 +150,19 @@ public class CompilationTestUtils {
         }
     }
 
+    /**
+     * Asserts that class contains method with given name, return type and
+     * parameter types.
+     *
+     * @param clazz
+     *            class to test
+     * @param returnType
+     *            method return type
+     * @param name
+     *            method name
+     * @param args
+     *            array of parameter type classes
+     */
     static void assertContainsMethod(Class<?> clazz, Class<?> returnType, String name, Class<?>... args) {
         try {
             Method m = clazz.getDeclaredMethod(name, args);
@@ -83,8 +173,19 @@ public class CompilationTestUtils {
         }
     }
 
-    static void assertContainsMethod(Class<?> clazz, String returnTypeStr, String name, ClassLoader loader)
-            throws Exception {
+    /**
+     * Asserts that class contains method with given name and return type.
+     *
+     * @param clazz
+     *            class to test
+     * @param returnTypeStr
+     *            name of method return type
+     * @param name
+     *            method name
+     * @param loader
+     *            current class loader
+     */
+    static void assertContainsMethod(Class<?> clazz, String returnTypeStr, String name, ClassLoader loader) {
         Class<?> returnType;
         try {
             returnType = Class.forName(returnTypeStr, true, loader);
@@ -92,14 +193,16 @@ public class CompilationTestUtils {
             assertEquals(returnType, method.getReturnType());
         } catch (ClassNotFoundException e) {
             throw new AssertionError("Return type of method '" + name + "' not found");
+        } catch (NoSuchMethodException e) {
+            throw new AssertionError("Method " + name + " does not exists in class " + clazz.getSimpleName());
         }
     }
 
     /**
-     * Check for presence of hashCode, equals and toString methods.
+     * Asserts that class containes hashCode, equals and toString methods.
      *
      * @param clazz
-     *            class to check
+     *            class to test
      */
     static void assertContainsDefaultMethods(Class<?> clazz) {
         assertContainsMethod(clazz, Integer.TYPE, "hashCode");
@@ -108,12 +211,12 @@ public class CompilationTestUtils {
     }
 
     /**
-     * Check for presence of 'public static
+     * Asserts that class contains 'public static
      * java.util.List<com.google.common.collect.Range<java.lang.Integer>>
      * getLength()' method.
      *
      * @param clazz
-     *            class to check
+     *            class to test
      */
     static void assertContainsGetLength(Class<?> clazz) {
         try {
@@ -141,18 +244,18 @@ public class CompilationTestUtils {
     }
 
     /**
-     * Test if generated source implements interface.
+     * Asserts that class implements given interface.
      *
-     * @param classToTest
+     * @param clazz
      *            source to test
-     * @param ifcClass
-     *            expected interface type
+     * @param ifc
+     *            expected interface
      */
-    static void testImplementsIfc(Class<?> classToTest, Class<?> ifcClass) {
-        Class<?>[] interfaces = classToTest.getInterfaces();
+    static void assertImplementsIfc(Class<?> clazz, Class<?> ifc) {
+        Class<?>[] interfaces = clazz.getInterfaces();
         List<Class<?>> ifcsList = Arrays.asList(interfaces);
-        if (!ifcsList.contains(ifcClass)) {
-            throw new AssertionError(classToTest + " should implement " + ifcClass);
+        if (!ifcsList.contains(ifc)) {
+            throw new AssertionError(clazz + " should implement " + ifc);
         }
     }
 
@@ -160,31 +263,42 @@ public class CompilationTestUtils {
      * Test if interface generated from augment extends Augmentation interface
      * with correct generic type.
      *
-     * @param classToTest
+     * @param clazz
      *            interface generated from augment
-     * @param genericType
+     * @param genericTypeName
      *            fully qualified name of expected parameter type
      */
-    static void testAugmentation(Class<?> classToTest, String genericType) {
-        final String ifcToImplement = "interface org.opendaylight.yangtools.yang.binding.Augmentation";
-        testImplementParameterizedIfc(classToTest, ifcToImplement, genericType);
+    static void testAugmentation(Class<?> clazz, String genericTypeName) {
+        final String ifcName = "interface org.opendaylight.yangtools.yang.binding.Augmentation";
+        assertImplementsParameterizedIfc(clazz, ifcName, genericTypeName);
     }
 
-    static void testImplementParameterizedIfc(Class<?> classToTest, String ifcToImplement, String genericType) {
-        ParameterizedType augmentation = null;
-        for (java.lang.reflect.Type ifc : classToTest.getGenericInterfaces()) {
+    /**
+     * Asserts that class implements interface with given name and generic type
+     * parameter.
+     *
+     * @param clazz
+     *            class to test
+     * @param ifcName
+     *            name of interface
+     * @param genericTypeName
+     *            name of generic type
+     */
+    static void assertImplementsParameterizedIfc(Class<?> clazz, String ifcName, String genericTypeName) {
+        ParameterizedType ifcType = null;
+        for (java.lang.reflect.Type ifc : clazz.getGenericInterfaces()) {
             if (ifc instanceof ParameterizedType) {
                 ParameterizedType pt = (ParameterizedType) ifc;
-                if (ifcToImplement.equals(pt.getRawType().toString())) {
-                    augmentation = pt;
+                if (ifcName.equals(pt.getRawType().toString())) {
+                    ifcType = pt;
                 }
             }
         }
-        assertNotNull(augmentation);
+        assertNotNull(ifcType);
 
-        java.lang.reflect.Type[] typeArguments = augmentation.getActualTypeArguments();
+        java.lang.reflect.Type[] typeArguments = ifcType.getActualTypeArguments();
         assertEquals(1, typeArguments.length);
-        assertEquals("interface " + genericType, typeArguments[0].toString());
+        assertEquals("interface " + genericTypeName, typeArguments[0].toString());
     }
 
     /**
@@ -205,7 +319,15 @@ public class CompilationTestUtils {
         assertTrue(compiled);
     }
 
-    static void testFilesCount(File dir, int count) {
+    /**
+     * Asserts that directory contains exactly given count of files.
+     *
+     * @param dir
+     *            directory to test
+     * @param count
+     *            expected count of files in directory
+     */
+    static void assertFilesCount(File dir, int count) {
         File[] dirContent = dir.listFiles();
         if (dirContent == null) {
             throw new AssertionError("File " + dir + " doesn't exists or it's not a directory");
index d335633792b5bdf8efed378a40f41e2447413175..769452c4354c3d1f27c0ffcb9a2f48edfa7acf67 100644 (file)
@@ -30,6 +30,7 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext;
  */
 public class TypedefCompilationTest extends BaseCompilationTest {
     private static final String VAL = "_value";
+    private static final String UNITS = "_UNITS";
 
     @Test
     public void test() throws Exception {
@@ -66,7 +67,7 @@ public class TypedefCompilationTest extends BaseCompilationTest {
         assertTrue(unionExt2.exists());
         assertTrue(unionExt3.exists());
         assertTrue(unionExt4.exists());
-        testFilesCount(parent, 16);
+        assertFilesCount(parent, 16);
 
         // Test if sources are compilable
         testCompilation(sourcesOutputDir, compiledOutputDir);
@@ -95,12 +96,14 @@ public class TypedefCompilationTest extends BaseCompilationTest {
 
         // typedef int32-ext2
         assertFalse(int32Ext2Class.isInterface());
-        assertEquals(0, int32Ext2Class.getDeclaredFields().length);
+        assertContainsFieldWithValue(int32Ext2Class, UNITS, String.class, "mile", Integer.class);
+        assertEquals(1, int32Ext2Class.getDeclaredFields().length);
         assertContainsConstructor(int32Ext2Class, Integer.class);
         assertContainsConstructor(int32Ext2Class, int32Ext2Class);
         assertContainsConstructor(int32Ext2Class, int32Ext1Class);
         assertEquals(3, int32Ext2Class.getDeclaredConstructors().length);
-        assertEquals(0, int32Ext2Class.getDeclaredMethods().length);
+        assertContainsMethod(int32Ext2Class, String.class, "toString");
+        assertEquals(1, int32Ext2Class.getDeclaredMethods().length);
 
         // typedef string-ext1
         assertFalse(stringExt1Class.isInterface());
@@ -171,7 +174,8 @@ public class TypedefCompilationTest extends BaseCompilationTest {
         assertFalse(unionExt3Class.isInterface());
         assertContainsField(unionExt3Class, "_string", String.class);
         assertContainsField(unionExt3Class, "_unionExt2", unionExt2Class);
-        assertEquals(2, unionExt3Class.getDeclaredFields().length);
+        assertContainsFieldWithValue(unionExt3Class, UNITS, String.class, "object id", String.class);
+        assertEquals(3, unionExt3Class.getDeclaredFields().length);
         assertContainsMethod(unionExt3Class, String.class, "getString");
         assertContainsMethod(unionExt3Class, unionExt2Class, "getUnionExt2");
         assertContainsConstructor(unionExt3Class, String.class);
index 7a43a64458fc0f1d6a47317de4ba1f7cef5a5af0..3c6d45c05f81149cc752e6b62db4789313add878 100644 (file)
@@ -63,6 +63,7 @@ module foo {
             type union-ext2;
             type string;
         }
+        units "object id";
     }
 
     typedef union-ext4 {
index 712d079c32fd60b74f1375533a606e05085f7b9d..e90edea2eef054f2c1a8c7cd60617be66be0f9cc 100644 (file)
@@ -25,6 +25,7 @@ import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil;
 import org.opendaylight.yangtools.binding.generator.util.TypeConstants;
 import org.opendaylight.yangtools.binding.generator.util.Types;
 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.EnumerationBuilderImpl;
+import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedPropertyBuilderImpl;
 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
 import org.opendaylight.yangtools.sal.binding.generator.spi.TypeProvider;
 import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType;
@@ -617,15 +618,17 @@ public final class TypeProviderImpl implements TypeProvider {
                     final GeneratedTOBuilder genTOBuilder = provideGeneratedTOBuilderForUnionTypeDef(basePackageName,
                             (UnionTypeDefinition) innerTypeDefinition, typedefName, typedef);
                     genTOBuilder.setIsUnion(true);
+                    addUnitsToGenTO(genTOBuilder, typedef.getUnits());
                     returnType = genTOBuilder.toInstance();
                 } else if (innerTypeDefinition instanceof EnumTypeDefinition) {
                     final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) innerTypeDefinition;
+                    // TODO units for typedef enum
                     returnType = provideTypeForEnum(enumTypeDef, typedefName, typedef);
-
                 } else if (innerTypeDefinition instanceof BitsTypeDefinition) {
                     final BitsTypeDefinition bitsTypeDefinition = (BitsTypeDefinition) innerTypeDefinition;
                     final GeneratedTOBuilder genTOBuilder = provideGeneratedTOBuilderForBitsTypeDefinition(
                             basePackageName, bitsTypeDefinition, typedefName);
+                    addUnitsToGenTO(genTOBuilder, typedef.getUnits());
                     returnType = genTOBuilder.toInstance();
                 } else {
                     final Type javaType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(
@@ -671,6 +674,7 @@ public final class TypeProviderImpl implements TypeProvider {
                 final List<String> regExps = resolveRegExpressionsFromTypedef((ExtendedType) typedef);
                 addStringRegExAsConstant(genTOBuilder, regExps);
             }
+            addUnitsToGenTO(genTOBuilder, typedef.getUnits());
             return genTOBuilder.toInstance();
         }
         return null;
@@ -1105,6 +1109,7 @@ public final class TypeProviderImpl implements TypeProvider {
                 genTOBuilder.setExtendsType((GeneratedTransferObject) type);
             }
         }
+        addUnitsToGenTO(genTOBuilder, typedef.getUnits());
 
         return genTOBuilder.toInstance();
     }
@@ -1199,4 +1204,13 @@ public final class TypeProviderImpl implements TypeProvider {
         }
     }
 
+    private void addUnitsToGenTO(GeneratedTOBuilder to, String units) {
+        if (units != null && !units.isEmpty()) {
+            to.addConstant(Types.STRING, "_UNITS", "\"" + units + "\"");
+            GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("UNITS");
+            prop.setReturnType(Types.STRING);
+            to.addToStringProperty(prop);
+        }
+    }
+
 }