Defect Fix for namespace with special character support in YANG
Change-Id: I8cc5b9dce58023c5965b07ac36cc4b5858f91699
diff --git a/src/main/java/org/onosproject/yangutils/translator/exception/TranslatorException.java b/src/main/java/org/onosproject/yangutils/translator/exception/TranslatorException.java
new file mode 100644
index 0000000..218e24d
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/translator/exception/TranslatorException.java
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.translator.exception;
+
+/**
+ * Provides custom translator exception for translator's operations.
+ */
+public class TranslatorException extends RuntimeException {
+
+ private static final long serialVersionUID = 20160311L;
+ private String fileName;
+
+ /**
+ * Create a new translator exception.
+ */
+ public TranslatorException() {
+ super();
+ }
+
+ /**
+ * Creates a new translator exception with given message.
+ *
+ * @param message the detail of exception in string
+ */
+ public TranslatorException(String message) {
+ super(message);
+ }
+
+ /**
+ * Creates a new translator exception from given message and cause.
+ *
+ * @param message the detail of exception in string
+ * @param cause underlying cause of the error
+ */
+ public TranslatorException(final String message, final Throwable cause) {
+ super(message, cause);
+ }
+
+ /**
+ * Creates a new translator exception from cause.
+ *
+ * @param cause underlying cause of the error
+ */
+ public TranslatorException(final Throwable cause) {
+ super(cause);
+ }
+
+ /**
+ * Returns generated file name for the exception.
+ *
+ * @return generated file name for the exception
+ */
+ public String getFileName() {
+ return this.fileName;
+ }
+
+ /**
+ * Sets file name in translator exception.
+ *
+ * @param fileName generated file name
+ */
+ public void setFileName(String fileName) {
+ this.fileName = fileName;
+ }
+}
diff --git a/src/main/java/org/onosproject/yangutils/translator/exception/package-info.java b/src/main/java/org/onosproject/yangutils/translator/exception/package-info.java
new file mode 100644
index 0000000..7432328
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/translator/exception/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Custom exception for translator.
+ */
+package org.onosproject.yangutils.translator.exception;
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntax.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntax.java
index 00e953b..dd543ca 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntax.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntax.java
@@ -18,6 +18,7 @@
import java.util.ArrayList;
+import org.onosproject.yangutils.translator.exception.TranslatorException;
import org.onosproject.yangutils.utils.UtilConstants;
/**
@@ -25,8 +26,15 @@
*/
public final class JavaIdentifierSyntax {
+ private static final int MAX_MONTHS = 12;
+ private static final int MAX_DAYS = 31;
+ private static final int INDEX_ZERO = 0;
+ private static final int INDEX_ONE = 1;
+ private static final int INDEX_TWO = 2;
+ private static final int INDEX_THREE = 3;
+
/**
- * Util class, with static functions only.
+ * Default constructor.
*/
private JavaIdentifierSyntax() {
}
@@ -71,9 +79,9 @@
*/
public static String getPkgFromNameSpace(String nameSpace) {
ArrayList<String> pkgArr = new ArrayList<String>();
- nameSpace = nameSpace.replace("\"", "");
-
- String[] nameSpaceArr = nameSpace.split(UtilConstants.COLAN);
+ nameSpace = nameSpace.replace(UtilConstants.QUOTES, UtilConstants.EMPTY_STRING);
+ String properNameSpace = nameSpace.replaceAll(UtilConstants.REGEX_WITH_SPECIAL_CHAR, UtilConstants.COLAN);
+ String[] nameSpaceArr = properNameSpace.split(UtilConstants.COLAN);
for (String nameSpaceString : nameSpaceArr) {
pkgArr.add(nameSpaceString);
@@ -86,19 +94,31 @@
*
* @param date YANG module revision
* @return revision string
+ * @throws TranslatorException when date is invalid.
*/
- public static String getYangRevisionStr(String date) {
+ public static String getYangRevisionStr(String date) throws TranslatorException {
String[] revisionArr = date.split(UtilConstants.HYPHEN);
String rev = "rev";
- for (String element : revisionArr) {
- Integer val = Integer.parseInt(element);
- if (val < 10) {
- rev = rev + "0";
+ String year = revisionArr[INDEX_ZERO];
+ char[] yearBytes = year.toCharArray();
+ rev = rev + yearBytes[INDEX_TWO] + yearBytes[INDEX_THREE];
+
+ if ((Integer.parseInt(revisionArr[INDEX_ONE]) <= MAX_MONTHS)
+ && Integer.parseInt(revisionArr[INDEX_TWO]) <= MAX_DAYS) {
+ for (int i = INDEX_ONE; i < revisionArr.length; i++) {
+
+ Integer val = Integer.parseInt(revisionArr[i]);
+ if (val < 10) {
+ rev = rev + "0";
+ }
+ rev = rev + val;
}
- rev = rev + val;
+
+ return rev;
+ } else {
+ throw new TranslatorException("Date in revision is not proper: " + date);
}
- return rev;
}
/**
@@ -109,10 +129,14 @@
*/
public static String getPkgFrmArr(ArrayList<String> pkgArr) {
- String pkg = "";
+ String pkg = UtilConstants.EMPTY_STRING;
int size = pkgArr.size();
int i = 0;
for (String member : pkgArr) {
+ boolean presenceOfKeyword = UtilConstants.JAVA_KEY_WORDS.contains(member);
+ if (presenceOfKeyword || (member.matches(UtilConstants.REGEX_FOR_FIRST_DIGIT))) {
+ member = UtilConstants.UNDER_SCORE + member;
+ }
pkg = pkg + member;
if (i != size - 1) {
pkg = pkg + UtilConstants.PERIOD;
@@ -174,4 +198,14 @@
public static String getCaptialCase(String yangIdentifier) {
return yangIdentifier.substring(0, 1).toUpperCase() + yangIdentifier.substring(1);
}
+
+ /**
+ * Translate the YANG identifier name to java identifier with first letter in small.
+ *
+ * @param yangIdentifier identifier in YANG file.
+ * @return corresponding java identifier
+ */
+ public static String getLowerCase(String yangIdentifier) {
+ return yangIdentifier.substring(0, 1).toLowerCase() + yangIdentifier.substring(1);
+ }
}
diff --git a/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java b/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java
index f596bf8..63c1fe7 100644
--- a/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java
+++ b/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java
@@ -16,6 +16,9 @@
package org.onosproject.yangutils.utils;
+import java.util.Arrays;
+import java.util.List;
+
/**
* Provides utility constants while generating java files.
*/
@@ -41,6 +44,8 @@
public static final String JAVA_DOC_RETURN = " * @return ";
public static final String JAVA_DOC_THROWS = " * @throws ";
public static final String JAVA_DOC_SETTERS = " * Returns the builder object of ";
+ public static final String JAVA_DOC_OF = " * Returns the object of ";
+ public static final String JAVA_DOC_SETTERS_COMMON = " * Sets the value of ";
public static final String JAVA_DOC_GETTERS = " * Returns the attribute ";
public static final String JAVA_DOC_DEFAULT_CONSTRUCTOR = " * Default Constructor.\n";
public static final String JAVA_DOC_CONSTRUCTOR = " * Construct the object of ";
@@ -51,9 +56,11 @@
* Basic requirements.
*/
public static final String NEW_LINE = "\n";
+ public static final String EMPTY_STRING = "";
public static final String NEW_LINE_ESTRIC = " *\n";
public static final String PERIOD = ".";
public static final String COLAN = ":";
+ public static final String UNDER_SCORE = "_";
public static final String SEMI_COLAN = ";";
public static final String HYPHEN = "-";
public static final String SPACE = " ";
@@ -63,6 +70,25 @@
public static final String ADD = "+";
public static final String ASTERISK = "*";
public static final String AT = "@";
+ public static final String QUOTES = "\"";
+ public static final String AND = "&";
+ public static final String COMMA = ",";
+ public static final String ADD_STRING = "add";
+ public static final String CHECK_NOT_NULL_STRING = "checkNotNull";
+ public static final String HASH_CODE_STRING = "hashCode";
+ public static final String EQUALS_STRING = "equals";
+ public static final String OBJECT_STRING = "Object";
+ public static final String INSTANCE_OF = " instanceof ";
+
+ public static final String VALUE = "value";
+
+ public static final String IF = "if";
+ public static final String FOR = "for";
+ public static final String WHILE = "while";
+ public static final String OF = "of";
+
+ public static final String TRUE = "true";
+ public static final String FALSE = "false";
/**
* For brackets.
@@ -193,7 +219,23 @@
public static final String DOUBLE_WRAPPER = "Double";
/**
- * For idenifiers.
+ * List of keywords in java, this is used for checking if the input does not contain these keywords.
+ */
+ public static final List JAVA_KEY_WORDS = Arrays.asList("abstract", "assert", "boolean", "break", "byte", "case",
+ "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "extends", "false",
+ "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface",
+ "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static",
+ "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try",
+ "void", "volatile", "while");
+
+ /**
+ * Defining regular expression.
+ */
+ public static final String REGEX_WITH_SPECIAL_CHAR = "[ : / - @ $ # ' * + , ; = ]+";
+ public static final String REGEX_FOR_FIRST_DIGIT = "\\d.*";
+
+ /**
+ * For identifiers.
*/
public static final String CLASS = "class";
public static final String BUILDER = "Builder";
@@ -218,7 +260,9 @@
/**
* For collections.
*/
- public static final String COLLECTION_IMPORTS = "import java.util.";
+ public static final String COLLECTION_IMPORTS = "java.util";
+ public static final String MORE_OBJECT_IMPORT = "import com.google.common.base.MoreObjects;\n";
+ public static final String JAVA_UTIL_OBJECTS_IMPORT = "import java.util.Objects;\n";
public static final String ABSTRACT_COLLECTION = "AbstractCollection";
public static final String LIST = "List";
diff --git a/src/test/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntaxTest.java b/src/test/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntaxTest.java
index bd55acb..6e89d93 100644
--- a/src/test/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntaxTest.java
+++ b/src/test/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntaxTest.java
@@ -17,6 +17,8 @@
package org.onosproject.yangutils.translator.tojava.utils;
import org.junit.Test;
+import org.onosproject.yangutils.utils.UtilConstants;
+
import static org.junit.Assert.assertNotNull;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
@@ -28,6 +30,24 @@
*/
public final class JavaIdentifierSyntaxTest {
+ public static final String PARENT_PACKAGE = "test5.test6.test7";
+ public static final String CHILD_PACKAGE = "test1:test2:test3";
+ public static final String DATE1 = "2000-1-5";
+ public static final String DATE2 = "1992-01-25";
+ public static final String PARENT_WITH_PERIOD = "test5.test6.test7";
+ public static final String CHILD_WITH_PERIOD = "test1.test2.test3";
+ public static final String DATE_WITH_REV1 = "rev000105";
+ public static final String DATE_WITH_REV2 = "rev920125";
+ public static final String VERSION_NUMBER = "v1";
+ public static final String INVALID_NAME_SPACE1 = "byte:#test2:9test3";
+ public static final String INVALID_NAME_SPACE2 = "const:#test2://9test3";
+ public static final String VALID_NAME_SPACE1 = "_byte.test2._9test3";
+ public static final String VALID_NAME_SPACE2 = "_const.test2._9test3";
+ public static final String WITHOUT_CAMEL_CASE = "test-camel-case-identifier";
+ public static final String WITH_CAMEL_CASE = "testCamelCaseIdentifier";
+ public static final String WITHOUT_CAPITAL = "test_this";
+ public static final String WITH_CAPITAL = "Test_this";
+
/**
* Unit test for private constructor.
*
@@ -38,6 +58,7 @@
* @throws IllegalAccessException if instance is provoked or a method is provoked.
* @throws InvocationTargetException when an exception occurs by the method or constructor.
*/
+ @Test
public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
InstantiationException, IllegalAccessException, InvocationTargetException {
Class<?>[] classesToConstruct = {JavaIdentifierSyntax.class };
@@ -53,8 +74,8 @@
*/
@Test
public void getPackageFromParentTest() {
- String pkgFromParent = JavaIdentifierSyntax.getPackageFromParent("test5.test6.test7", "test1:test2:test3");
- assertThat(pkgFromParent.equals("test5.test6.test7.test1.test2.test3"), is(true));
+ String pkgFromParent = JavaIdentifierSyntax.getPackageFromParent(PARENT_PACKAGE, CHILD_PACKAGE);
+ assertThat(pkgFromParent.equals(PARENT_WITH_PERIOD + UtilConstants.PERIOD + CHILD_WITH_PERIOD), is(true));
}
/**
@@ -63,18 +84,35 @@
@Test
public void getRootPackageTest() {
- String rootPackage = JavaIdentifierSyntax.getRootPackage((byte) 0, "test1:test2:test3", "5-1-2000");
- assertThat(rootPackage.equals("org.onosproject.yang.gen.v0.test1.test2.test3.rev05012000"), is(true));
+ String rootPackage = JavaIdentifierSyntax.getRootPackage((byte) 1, CHILD_PACKAGE, DATE1);
+ assertThat(rootPackage.equals(UtilConstants.DEFAULT_BASE_PKG + UtilConstants.PERIOD + VERSION_NUMBER
+ + UtilConstants.PERIOD + CHILD_WITH_PERIOD + UtilConstants.PERIOD + DATE_WITH_REV1), is(true));
}
/**
- * Unit test for root package generation without revision complexity.
+ * Unit test for root package generation with special characters presence.
+ */
+ @Test
+ public void getRootPackageWithSpecialCharactersTest() {
+
+ String rootPackage = JavaIdentifierSyntax.getRootPackage((byte) 1, INVALID_NAME_SPACE1, DATE1);
+ assertThat(rootPackage.equals(UtilConstants.DEFAULT_BASE_PKG + UtilConstants.PERIOD + VERSION_NUMBER
+ + UtilConstants.PERIOD + VALID_NAME_SPACE1 + UtilConstants.PERIOD + DATE_WITH_REV1), is(true));
+ String rootPackage1 = JavaIdentifierSyntax.getRootPackage((byte) 1, INVALID_NAME_SPACE2, DATE1);
+ assertThat(rootPackage1.equals(UtilConstants.DEFAULT_BASE_PKG + UtilConstants.PERIOD + VERSION_NUMBER
+ + UtilConstants.PERIOD + VALID_NAME_SPACE2 + UtilConstants.PERIOD + DATE_WITH_REV1), is(true));
+ }
+
+ /**
+ * Unit test for root package generation without complexity in revision.
*/
@Test
public void getRootPackageWithRevTest() {
- String rootPkgWithRev = JavaIdentifierSyntax.getRootPackage((byte) 0, "test1:test2:test3", "25-01-1992");
- assertThat(rootPkgWithRev.equals("org.onosproject.yang.gen.v0.test1.test2.test3.rev25011992"), is(true));
+ String rootPkgWithRev = JavaIdentifierSyntax.getRootPackage((byte) 1, CHILD_PACKAGE, DATE2);
+ assertThat(rootPkgWithRev.equals(UtilConstants.DEFAULT_BASE_PKG + UtilConstants.PERIOD
+ + VERSION_NUMBER + UtilConstants.PERIOD + CHILD_WITH_PERIOD + UtilConstants.PERIOD + DATE_WITH_REV2),
+ is(true));
}
/**
@@ -83,8 +121,8 @@
@Test
public void getCapitalCaseTest() {
- String capitalCase = JavaIdentifierSyntax.getCaptialCase("test_this");
- assertThat(capitalCase.equals("Test_this"), is(true));
+ String capitalCase = JavaIdentifierSyntax.getCaptialCase(WITHOUT_CAPITAL);
+ assertThat(capitalCase.equals(WITH_CAPITAL), is(true));
}
/**
@@ -92,7 +130,7 @@
*/
@Test
public void getCamelCaseTest() {
- String camelCase = JavaIdentifierSyntax.getCamelCase("test-camel-case-identifier");
- assertThat(camelCase.equals("testCamelCaseIdentifier"), is(true));
+ String camelCase = JavaIdentifierSyntax.getCamelCase(WITHOUT_CAMEL_CASE);
+ assertThat(camelCase.equals(WITH_CAMEL_CASE), is(true));
}
}
diff --git a/src/test/java/org/onosproject/yangutils/utils/io/impl/YangIoUtilsTest.java b/src/test/java/org/onosproject/yangutils/utils/io/impl/YangIoUtilsTest.java
index a7e9860..2cfc3b9 100644
--- a/src/test/java/org/onosproject/yangutils/utils/io/impl/YangIoUtilsTest.java
+++ b/src/test/java/org/onosproject/yangutils/utils/io/impl/YangIoUtilsTest.java
@@ -70,7 +70,7 @@
@Test
public void addPackageInfoWithEmptyPathTest() throws IOException {
- File dirPath = new File("");
+ File dirPath = new File("invalid/check");
thrown.expect(IOException.class);
thrown.expectMessage("Exception occured while creating package info file.");
YangIoUtils.addPackageInfo(dirPath, "check1", createPath);