From 5cc1a862b0f461463c306a79f0b887c3ea44de53 Mon Sep 17 00:00:00 2001 From: Jozef Gloncak Date: Wed, 11 Sep 2013 12:39:35 +0200 Subject: [PATCH] Tests for class BindingGeneratorUtil + methods with prefix 'parseTo' in class BindingGeneratorUtil were refactored. Change-Id: I7d93a3e73423305af6055d2e3cf5cd1266b073bb Signed-off-by: Jozef Gloncak --- code-generator/binding-generator-util/pom.xml | 69 ++++---- .../generator/util/BindingGeneratorUtil.java | 63 +++----- .../util/BindingGeneratorUtilTest.java | 150 ++++++++++++++++++ .../src/test/resources/module.yang | 22 +++ 4 files changed, 234 insertions(+), 70 deletions(-) create mode 100644 code-generator/binding-generator-util/src/test/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtilTest.java create mode 100644 code-generator/binding-generator-util/src/test/resources/module.yang diff --git a/code-generator/binding-generator-util/pom.xml b/code-generator/binding-generator-util/pom.xml index 01fa3f19c4..d28873ec8e 100644 --- a/code-generator/binding-generator-util/pom.xml +++ b/code-generator/binding-generator-util/pom.xml @@ -1,32 +1,37 @@ - - - - org.opendaylight.yangtools - binding-generator - 0.5.7-SNAPSHOT - - - 4.0.0 - binding-generator-util - - - - org.opendaylight.yangtools - binding-model-api - - - org.opendaylight.yangtools - yang-model-api - - - junit - junit - - - org.opendaylight.yangtools - yang-binding - - - - + + + + org.opendaylight.yangtools + binding-generator + 0.5.7-SNAPSHOT + + + 4.0.0 + binding-generator-util + + + + org.opendaylight.yangtools + binding-model-api + + + org.opendaylight.yangtools + yang-model-api + + + junit + junit + + + org.opendaylight.yangtools + yang-binding + + + org.opendaylight.yangtools + yang-parser-impl + ${yang.version} + + + + diff --git a/code-generator/binding-generator-util/src/main/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtil.java b/code-generator/binding-generator-util/src/main/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtil.java index 27961a9065..4b00648cfd 100644 --- a/code-generator/binding-generator-util/src/main/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtil.java +++ b/code-generator/binding-generator-util/src/main/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtil.java @@ -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 token 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 + * token should|shuldn't be uppercased * @return string in the cammel case format * @throws IllegalArgumentException * */ - 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 index 0000000000..f16ba50af9 --- /dev/null +++ b/code-generator/binding-generator-util/src/test/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtilTest.java @@ -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 loadTestResources(String testFile) { + final List testModels = new ArrayList(); + final File listModelFile = new File(BindingGeneratorUtilTest.class.getResource(testFile).getPath()); + testModels.add(listModelFile); + return testModels; + } + + /** + * Tests methods: + *
    + *
  • moduleNamespaceToPackageName
  • - with revision + *
  • packageNameForGeneratedType
  • + *
      + *
    • validateJavaPackage
    • + *
    + *
  • packageNameForTypeDefinition
  • moduleNamespaceToPackageName
  • + * - without revision
+ */ + @Test + public void testBindingGeneratorUtilMethods() { + List testModels = loadTestResources("/module.yang"); + final YangModelParser parser = new YangParserImpl(); + final Set 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 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> 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 + *
    + *
  • {@link BindingGeneratorUtil#validateParameterName() + * validateParameterName()}
  • + *
      + */ + @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: + *
        + *
      • parseToClassName
      • + *
          + *
        • parseToCamelCase
        • + *
            + *
          • replaceWithCamelCase
          • + *
          + *
      • parseToValidParamName
      • + *
          + *
        • parseToCamelCase
        • + *
            + *
          • replaceWithCamelCase
          • + *
          + *
        + *
          + */ + @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 index 0000000000..928efa6f57 --- /dev/null +++ b/code-generator/binding-generator-util/src/test/resources/module.yang @@ -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 -- 2.36.6