Tests for class BindingGeneratorUtil 54/1154/2
authorJozef Gloncak <jgloncak@cisco.com>
Wed, 11 Sep 2013 10:39:35 +0000 (12:39 +0200)
committerJozef Gloncak <jgloncak@cisco.com>
Wed, 11 Sep 2013 10:47:01 +0000 (12:47 +0200)
+ methods with prefix 'parseTo' in class BindingGeneratorUtil were
refactored.

Change-Id: I7d93a3e73423305af6055d2e3cf5cd1266b073bb
Signed-off-by: Jozef Gloncak <jgloncak@cisco.com>
code-generator/binding-generator-util/pom.xml
code-generator/binding-generator-util/src/main/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtil.java
code-generator/binding-generator-util/src/test/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtilTest.java [new file with mode: 0644]
code-generator/binding-generator-util/src/test/resources/module.yang [new file with mode: 0644]

index 01fa3f19c4e34a96e3a8db7d6e810a2c3554e118..d28873ec8e1e14236f4024ff3ff30510fc79637a 100644 (file)
@@ -1,32 +1,37 @@
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\r
-    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">\r
-\r
-    <parent>\r
-        <groupId>org.opendaylight.yangtools</groupId>\r
-        <artifactId>binding-generator</artifactId>\r
-        <version>0.5.7-SNAPSHOT</version>\r
-    </parent>\r
-\r
-    <modelVersion>4.0.0</modelVersion>\r
-    <artifactId>binding-generator-util</artifactId>\r
-\r
-    <dependencies>\r
-        <dependency>\r
-            <groupId>org.opendaylight.yangtools</groupId>\r
-            <artifactId>binding-model-api</artifactId>\r
-        </dependency>\r
-        <dependency>\r
-            <groupId>org.opendaylight.yangtools</groupId>\r
-            <artifactId>yang-model-api</artifactId>\r
-        </dependency>\r
-        <dependency>\r
-            <groupId>junit</groupId>\r
-            <artifactId>junit</artifactId>\r
-        </dependency>\r
-        <dependency>\r
-            <groupId>org.opendaylight.yangtools</groupId>\r
-            <artifactId>yang-binding</artifactId>\r
-        </dependency>\r
-    </dependencies>\r
-\r
-</project>\r
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+    <parent>
+        <groupId>org.opendaylight.yangtools</groupId>
+        <artifactId>binding-generator</artifactId>
+        <version>0.5.7-SNAPSHOT</version>
+    </parent>
+
+    <modelVersion>4.0.0</modelVersion>
+    <artifactId>binding-generator-util</artifactId>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.opendaylight.yangtools</groupId>
+            <artifactId>binding-model-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.opendaylight.yangtools</groupId>
+            <artifactId>yang-model-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.opendaylight.yangtools</groupId>
+            <artifactId>yang-binding</artifactId>
+        </dependency>
+        <dependency>
+               <groupId>org.opendaylight.yangtools</groupId>
+               <artifactId>yang-parser-impl</artifactId>
+               <version>${yang.version}</version>
+        </dependency>
+    </dependencies>
+
+</project>
index 27961a9065f8f12820aae6ffb06d09ccf2b8e328..4b00648cfd045f9020c0bc968412c807c9cf1073 100644 (file)
@@ -21,7 +21,7 @@ import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 public final class BindingGeneratorUtil {
 
     private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyMMdd");
-    
+
     /**
      * Array of strings values which represents JAVA reserved words.
      */
@@ -87,7 +87,7 @@ public final class BindingGeneratorUtil {
      *            string with the parameter name
      * @return string with the admissible parameter name
      */
-    public static String validateParameterName(final String parameterName) {
+    public static String resolveJavaReservedWordEquivalency(final String parameterName) {
         if (parameterName != null && JAVA_RESERVED_WORDS.contains(parameterName)) {
             return "_" + parameterName;
         }
@@ -141,7 +141,7 @@ public final class BindingGeneratorUtil {
         packageNameBuilder.append(namespace);
         packageNameBuilder.append(".rev");
         packageNameBuilder.append(DATE_FORMAT.format(module.getRevision()));
-        
+
         return validateJavaPackage(packageNameBuilder.toString());
     }
 
@@ -223,18 +223,7 @@ public final class BindingGeneratorUtil {
      *         name.
      */
     public static String parseToClassName(String token) {
-        String correctStr = token.replace(".", "");
-        correctStr = parseToCamelCase(correctStr);
-
-        // make first char upper-case
-        char first = Character.toUpperCase(correctStr.charAt(0));
-        if (first >= '0' && first <= '9') {
-
-            correctStr = "_" + correctStr;
-        } else {
-            correctStr = first + correctStr.substring(1);
-        }
-        return correctStr;
+        return parseToCamelCase(token, true);
     }
 
     /**
@@ -248,28 +237,7 @@ public final class BindingGeneratorUtil {
      *         parameter name.
      */
     public static String parseToValidParamName(final String token) {
-        final String validToken = token.replace(".", "");
-        String correctStr = parseToCamelCase(validToken);
-
-        // make first char lower-case
-        char first = Character.toLowerCase(correctStr.charAt(0));
-        correctStr = first + correctStr.substring(1);
-        return validateParameterName(correctStr);
-    }
-
-    /**
-     * Converts <code>token</code> to capital letters and removes invalid
-     * characters.
-     * 
-     * @param token
-     *            string with characters which should be conversed to capital
-     * @return string with capital letters
-     */
-    public static String convertToCapitalLetters(final String token) {
-        String convertedStr = token.replace(" ", "_");
-        convertedStr = convertedStr.replace(".", "_");
-        convertedStr = convertedStr.toUpperCase();
-        return convertedStr;
+        return resolveJavaReservedWordEquivalency(parseToCamelCase(token, false));
     }
 
     /**
@@ -278,6 +246,9 @@ public final class BindingGeneratorUtil {
      * 
      * @param token
      *            string which should be converted to the cammel case format
+     * @param uppercase
+     *            boolean value which says whether the first character of the
+     *            <code>token</code> should|shuldn't be uppercased
      * @return string in the cammel case format
      * @throws IllegalArgumentException
      *             <ul>
@@ -285,12 +256,15 @@ public final class BindingGeneratorUtil {
      *             <li>if <code>token</code> equals null</li>
      *             </ul>
      */
-    private static String parseToCamelCase(String token) {
+
+    private static String parseToCamelCase(final String token, final boolean uppercase) {
         if (token == null) {
             throw new IllegalArgumentException("Name can not be null");
         }
 
         String correctStr = token.trim();
+        correctStr = correctStr.replace(".", "");
+
         if (correctStr.isEmpty()) {
             throw new IllegalArgumentException("Name can not be emty");
         }
@@ -298,6 +272,19 @@ public final class BindingGeneratorUtil {
         correctStr = replaceWithCamelCase(correctStr, ' ');
         correctStr = replaceWithCamelCase(correctStr, '-');
         correctStr = replaceWithCamelCase(correctStr, '_');
+
+        String firstChar = correctStr.substring(0, 1);
+        if (uppercase) {
+            firstChar = firstChar.toUpperCase();
+        } else {
+            firstChar = firstChar.toLowerCase();
+        }
+
+        if (firstChar.matches("[0-9]")) {
+            correctStr = "_" + correctStr;
+        } else {
+            correctStr = firstChar + correctStr.substring(1);
+        }
         return correctStr;
     }
 
diff --git a/code-generator/binding-generator-util/src/test/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtilTest.java b/code-generator/binding-generator-util/src/test/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtilTest.java
new file mode 100644 (file)
index 0000000..f16ba50
--- /dev/null
@@ -0,0 +1,150 @@
+package org.opendaylight.yangtools.binding.generator.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
+import org.opendaylight.yangtools.yang.model.util.DataNodeIterator;
+import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
+import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
+
+public class BindingGeneratorUtilTest {
+
+    private static List<File> loadTestResources(String testFile) {
+        final List<File> testModels = new ArrayList<File>();
+        final File listModelFile = new File(BindingGeneratorUtilTest.class.getResource(testFile).getPath());
+        testModels.add(listModelFile);
+        return testModels;
+    }
+
+    /**
+     * Tests methods:
+     * <ul>
+     * <li>moduleNamespaceToPackageName</li> - with revision
+     * <li>packageNameForGeneratedType</li>
+     * <ul>
+     * <li>validateJavaPackage</li>
+     * </ul>
+     * <li>packageNameForTypeDefinition</li> <li>moduleNamespaceToPackageName</li>
+     * - without revision </ul>
+     */
+    @Test
+    public void testBindingGeneratorUtilMethods() {
+        List<File> testModels = loadTestResources("/module.yang");
+        final YangModelParser parser = new YangParserImpl();
+        final Set<Module> modules = parser.parseYangModels(testModels);
+        String packageName = "";
+        Module module = null;
+        for (Module m : modules) {
+            module = m;
+            break;
+        }
+        assertNotNull("Module can't be null", module);
+
+        // test of the method moduleNamespaceToPackageName()
+        packageName = BindingGeneratorUtil.moduleNamespaceToPackageName(module);
+        assertEquals("Generated package name is incorrect.",
+                "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910", packageName);
+
+        // test of the method packageNameForGeneratedType()
+        DataNodeIterator it = new DataNodeIterator(module);
+        List<ContainerSchemaNode> schemaContainers = it.allContainers();
+        String subPackageNameForDataNode = "";
+        for (ContainerSchemaNode containerSchemaNode : schemaContainers) {
+            if (containerSchemaNode.getQName().getLocalName().equals("cont-inner")) {
+                subPackageNameForDataNode = BindingGeneratorUtil.packageNameForGeneratedType(packageName,
+                        containerSchemaNode.getPath());
+                break;
+            }
+        }
+        assertEquals("The name of the subpackage is incorrect.",
+                "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910.cont.outter",
+                subPackageNameForDataNode);
+
+        // test of the method packageNameForTypeDefinition
+        Set<TypeDefinition<?>> typeDefinitions = module.getTypeDefinitions();
+        String subPackageNameForTypeDefinition = "";
+        for (TypeDefinition<?> tpDef : typeDefinitions) {
+            if (tpDef.getQName().getLocalName().equals("tpdf")) {
+                subPackageNameForTypeDefinition = BindingGeneratorUtil.packageNameForTypeDefinition(packageName, tpDef);
+                break;
+            }
+        }
+        assertEquals("The name of the subpackage is incorrect.",
+                "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910",
+                subPackageNameForTypeDefinition);
+
+        // test of exception part of the method moduleNamespaceToPackageName()
+        ModuleBuilder moduleBuilder = new ModuleBuilder("module-withut-revision");
+        Module moduleWithoutRevision = moduleBuilder.build();
+        boolean passedSuccesfully = false;
+        try {
+            BindingGeneratorUtil.moduleNamespaceToPackageName(moduleWithoutRevision);
+            passedSuccesfully = true;
+        } catch (IllegalArgumentException e) {
+        }
+        assertFalse("Exception 'IllegalArgumentException' wasn't raised", passedSuccesfully);
+
+    }
+
+    /**
+     * Test for the method
+     * <ul>
+     * <li>{@link BindingGeneratorUtil#validateParameterName()
+     * validateParameterName()}</li>
+     * <ul>
+     */
+    @Test
+    public void testValidateParameterName() {
+        assertNull("Return value is incorrect.", BindingGeneratorUtil.resolveJavaReservedWordEquivalency(null));
+        assertEquals("Return value is incorrect.", "whatever",
+                BindingGeneratorUtil.resolveJavaReservedWordEquivalency("whatever"));
+        assertEquals("Return value is incorrect.", "_case",
+                BindingGeneratorUtil.resolveJavaReservedWordEquivalency("case"));
+    }
+
+    /**
+     * Tests the methods:
+     * <ul>
+     * <li>parseToClassName</li>
+     * <ul>
+     * <li>parseToCamelCase</li>
+     * <ul>
+     * <li>replaceWithCamelCase</li>
+     * </ul>
+     * </ul> <li>parseToValidParamName</li>
+     * <ul>
+     * <li>parseToCamelCase</li>
+     * <ul>
+     * <li>replaceWithCamelCase</li>
+     * </ul>
+     * </ul>
+     * <ul>
+     */
+    @Test
+    public void testParsingMethods() {
+        // parseToClassName method testing
+        assertEquals("Class name has incorrect format", "SomeTestingClassName",
+                BindingGeneratorUtil.parseToClassName("  some-testing_class name   "));
+        assertEquals("Class name has incorrect format", "_0SomeTestingClassName",
+                BindingGeneratorUtil.parseToClassName("  0 some-testing_class name   "));
+
+        // parseToValidParamName
+        assertEquals("Parameter name has incorrect format", "someTestingParameterName",
+                BindingGeneratorUtil.parseToValidParamName("  some-testing_parameter   name   "));
+        assertEquals("Parameter name has incorrect format", "_0someTestingParameterName",
+                BindingGeneratorUtil.parseToValidParamName("  0some-testing_parameter   name   "));
+    }
+
+}
diff --git a/code-generator/binding-generator-util/src/test/resources/module.yang b/code-generator/binding-generator-util/src/test/resources/module.yang
new file mode 100644 (file)
index 0000000..928efa6
--- /dev/null
@@ -0,0 +1,22 @@
+module module-name-test {
+
+    namespace "urn:m*o+d,u;l=e.n/a-m@e.t$e#s't.case.1digit";
+    prefix "pref";
+
+    organization "OPEN DAYLIGHT";
+    contact "http://www.opendaylight.org/";
+
+    revision 2013-09-10 {
+    }
+    
+    typedef tpdf {
+       type string;
+    }
+    
+    container cont-outter {
+       container cont-inner {
+       }    
+    }
+    
+    
+}
\ No newline at end of file