Committed by
Gerrit Code Review
Defect Fix for namespace with special character support in YANG
Change-Id: I8cc5b9dce58023c5965b07ac36cc4b5858f91699
Showing
6 changed files
with
241 additions
and
26 deletions
| 1 | +/* | ||
| 2 | + * Copyright 2016 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +package org.onosproject.yangutils.translator.exception; | ||
| 18 | + | ||
| 19 | +/** | ||
| 20 | + * Provides custom translator exception for translator's operations. | ||
| 21 | + */ | ||
| 22 | +public class TranslatorException extends RuntimeException { | ||
| 23 | + | ||
| 24 | + private static final long serialVersionUID = 20160311L; | ||
| 25 | + private String fileName; | ||
| 26 | + | ||
| 27 | + /** | ||
| 28 | + * Create a new translator exception. | ||
| 29 | + */ | ||
| 30 | + public TranslatorException() { | ||
| 31 | + super(); | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + /** | ||
| 35 | + * Creates a new translator exception with given message. | ||
| 36 | + * | ||
| 37 | + * @param message the detail of exception in string | ||
| 38 | + */ | ||
| 39 | + public TranslatorException(String message) { | ||
| 40 | + super(message); | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + /** | ||
| 44 | + * Creates a new translator exception from given message and cause. | ||
| 45 | + * | ||
| 46 | + * @param message the detail of exception in string | ||
| 47 | + * @param cause underlying cause of the error | ||
| 48 | + */ | ||
| 49 | + public TranslatorException(final String message, final Throwable cause) { | ||
| 50 | + super(message, cause); | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + /** | ||
| 54 | + * Creates a new translator exception from cause. | ||
| 55 | + * | ||
| 56 | + * @param cause underlying cause of the error | ||
| 57 | + */ | ||
| 58 | + public TranslatorException(final Throwable cause) { | ||
| 59 | + super(cause); | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + /** | ||
| 63 | + * Returns generated file name for the exception. | ||
| 64 | + * | ||
| 65 | + * @return generated file name for the exception | ||
| 66 | + */ | ||
| 67 | + public String getFileName() { | ||
| 68 | + return this.fileName; | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + /** | ||
| 72 | + * Sets file name in translator exception. | ||
| 73 | + * | ||
| 74 | + * @param fileName generated file name | ||
| 75 | + */ | ||
| 76 | + public void setFileName(String fileName) { | ||
| 77 | + this.fileName = fileName; | ||
| 78 | + } | ||
| 79 | +} |
utils/yangutils/src/main/java/org/onosproject/yangutils/translator/exception/package-info.java
0 → 100644
| 1 | +/* | ||
| 2 | + * Copyright 2016 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +/** | ||
| 18 | + * Custom exception for translator. | ||
| 19 | + */ | ||
| 20 | +package org.onosproject.yangutils.translator.exception; |
| ... | @@ -18,6 +18,7 @@ package org.onosproject.yangutils.translator.tojava.utils; | ... | @@ -18,6 +18,7 @@ package org.onosproject.yangutils.translator.tojava.utils; |
| 18 | 18 | ||
| 19 | import java.util.ArrayList; | 19 | import java.util.ArrayList; |
| 20 | 20 | ||
| 21 | +import org.onosproject.yangutils.translator.exception.TranslatorException; | ||
| 21 | import org.onosproject.yangutils.utils.UtilConstants; | 22 | import org.onosproject.yangutils.utils.UtilConstants; |
| 22 | 23 | ||
| 23 | /** | 24 | /** |
| ... | @@ -25,8 +26,15 @@ import org.onosproject.yangutils.utils.UtilConstants; | ... | @@ -25,8 +26,15 @@ import org.onosproject.yangutils.utils.UtilConstants; |
| 25 | */ | 26 | */ |
| 26 | public final class JavaIdentifierSyntax { | 27 | public final class JavaIdentifierSyntax { |
| 27 | 28 | ||
| 29 | + private static final int MAX_MONTHS = 12; | ||
| 30 | + private static final int MAX_DAYS = 31; | ||
| 31 | + private static final int INDEX_ZERO = 0; | ||
| 32 | + private static final int INDEX_ONE = 1; | ||
| 33 | + private static final int INDEX_TWO = 2; | ||
| 34 | + private static final int INDEX_THREE = 3; | ||
| 35 | + | ||
| 28 | /** | 36 | /** |
| 29 | - * Util class, with static functions only. | 37 | + * Default constructor. |
| 30 | */ | 38 | */ |
| 31 | private JavaIdentifierSyntax() { | 39 | private JavaIdentifierSyntax() { |
| 32 | } | 40 | } |
| ... | @@ -71,9 +79,9 @@ public final class JavaIdentifierSyntax { | ... | @@ -71,9 +79,9 @@ public final class JavaIdentifierSyntax { |
| 71 | */ | 79 | */ |
| 72 | public static String getPkgFromNameSpace(String nameSpace) { | 80 | public static String getPkgFromNameSpace(String nameSpace) { |
| 73 | ArrayList<String> pkgArr = new ArrayList<String>(); | 81 | ArrayList<String> pkgArr = new ArrayList<String>(); |
| 74 | - nameSpace = nameSpace.replace("\"", ""); | 82 | + nameSpace = nameSpace.replace(UtilConstants.QUOTES, UtilConstants.EMPTY_STRING); |
| 75 | - | 83 | + String properNameSpace = nameSpace.replaceAll(UtilConstants.REGEX_WITH_SPECIAL_CHAR, UtilConstants.COLAN); |
| 76 | - String[] nameSpaceArr = nameSpace.split(UtilConstants.COLAN); | 84 | + String[] nameSpaceArr = properNameSpace.split(UtilConstants.COLAN); |
| 77 | 85 | ||
| 78 | for (String nameSpaceString : nameSpaceArr) { | 86 | for (String nameSpaceString : nameSpaceArr) { |
| 79 | pkgArr.add(nameSpaceString); | 87 | pkgArr.add(nameSpaceString); |
| ... | @@ -86,19 +94,31 @@ public final class JavaIdentifierSyntax { | ... | @@ -86,19 +94,31 @@ public final class JavaIdentifierSyntax { |
| 86 | * | 94 | * |
| 87 | * @param date YANG module revision | 95 | * @param date YANG module revision |
| 88 | * @return revision string | 96 | * @return revision string |
| 97 | + * @throws TranslatorException when date is invalid. | ||
| 89 | */ | 98 | */ |
| 90 | - public static String getYangRevisionStr(String date) { | 99 | + public static String getYangRevisionStr(String date) throws TranslatorException { |
| 91 | String[] revisionArr = date.split(UtilConstants.HYPHEN); | 100 | String[] revisionArr = date.split(UtilConstants.HYPHEN); |
| 92 | 101 | ||
| 93 | String rev = "rev"; | 102 | String rev = "rev"; |
| 94 | - for (String element : revisionArr) { | 103 | + String year = revisionArr[INDEX_ZERO]; |
| 95 | - Integer val = Integer.parseInt(element); | 104 | + char[] yearBytes = year.toCharArray(); |
| 96 | - if (val < 10) { | 105 | + rev = rev + yearBytes[INDEX_TWO] + yearBytes[INDEX_THREE]; |
| 97 | - rev = rev + "0"; | 106 | + |
| 107 | + if ((Integer.parseInt(revisionArr[INDEX_ONE]) <= MAX_MONTHS) | ||
| 108 | + && Integer.parseInt(revisionArr[INDEX_TWO]) <= MAX_DAYS) { | ||
| 109 | + for (int i = INDEX_ONE; i < revisionArr.length; i++) { | ||
| 110 | + | ||
| 111 | + Integer val = Integer.parseInt(revisionArr[i]); | ||
| 112 | + if (val < 10) { | ||
| 113 | + rev = rev + "0"; | ||
| 114 | + } | ||
| 115 | + rev = rev + val; | ||
| 98 | } | 116 | } |
| 99 | - rev = rev + val; | 117 | + |
| 118 | + return rev; | ||
| 119 | + } else { | ||
| 120 | + throw new TranslatorException("Date in revision is not proper: " + date); | ||
| 100 | } | 121 | } |
| 101 | - return rev; | ||
| 102 | } | 122 | } |
| 103 | 123 | ||
| 104 | /** | 124 | /** |
| ... | @@ -109,10 +129,14 @@ public final class JavaIdentifierSyntax { | ... | @@ -109,10 +129,14 @@ public final class JavaIdentifierSyntax { |
| 109 | */ | 129 | */ |
| 110 | public static String getPkgFrmArr(ArrayList<String> pkgArr) { | 130 | public static String getPkgFrmArr(ArrayList<String> pkgArr) { |
| 111 | 131 | ||
| 112 | - String pkg = ""; | 132 | + String pkg = UtilConstants.EMPTY_STRING; |
| 113 | int size = pkgArr.size(); | 133 | int size = pkgArr.size(); |
| 114 | int i = 0; | 134 | int i = 0; |
| 115 | for (String member : pkgArr) { | 135 | for (String member : pkgArr) { |
| 136 | + boolean presenceOfKeyword = UtilConstants.JAVA_KEY_WORDS.contains(member); | ||
| 137 | + if (presenceOfKeyword || (member.matches(UtilConstants.REGEX_FOR_FIRST_DIGIT))) { | ||
| 138 | + member = UtilConstants.UNDER_SCORE + member; | ||
| 139 | + } | ||
| 116 | pkg = pkg + member; | 140 | pkg = pkg + member; |
| 117 | if (i != size - 1) { | 141 | if (i != size - 1) { |
| 118 | pkg = pkg + UtilConstants.PERIOD; | 142 | pkg = pkg + UtilConstants.PERIOD; |
| ... | @@ -174,4 +198,14 @@ public final class JavaIdentifierSyntax { | ... | @@ -174,4 +198,14 @@ public final class JavaIdentifierSyntax { |
| 174 | public static String getCaptialCase(String yangIdentifier) { | 198 | public static String getCaptialCase(String yangIdentifier) { |
| 175 | return yangIdentifier.substring(0, 1).toUpperCase() + yangIdentifier.substring(1); | 199 | return yangIdentifier.substring(0, 1).toUpperCase() + yangIdentifier.substring(1); |
| 176 | } | 200 | } |
| 201 | + | ||
| 202 | + /** | ||
| 203 | + * Translate the YANG identifier name to java identifier with first letter in small. | ||
| 204 | + * | ||
| 205 | + * @param yangIdentifier identifier in YANG file. | ||
| 206 | + * @return corresponding java identifier | ||
| 207 | + */ | ||
| 208 | + public static String getLowerCase(String yangIdentifier) { | ||
| 209 | + return yangIdentifier.substring(0, 1).toLowerCase() + yangIdentifier.substring(1); | ||
| 210 | + } | ||
| 177 | } | 211 | } | ... | ... |
| ... | @@ -16,6 +16,9 @@ | ... | @@ -16,6 +16,9 @@ |
| 16 | 16 | ||
| 17 | package org.onosproject.yangutils.utils; | 17 | package org.onosproject.yangutils.utils; |
| 18 | 18 | ||
| 19 | +import java.util.Arrays; | ||
| 20 | +import java.util.List; | ||
| 21 | + | ||
| 19 | /** | 22 | /** |
| 20 | * Provides utility constants while generating java files. | 23 | * Provides utility constants while generating java files. |
| 21 | */ | 24 | */ |
| ... | @@ -41,6 +44,8 @@ public final class UtilConstants { | ... | @@ -41,6 +44,8 @@ public final class UtilConstants { |
| 41 | public static final String JAVA_DOC_RETURN = " * @return "; | 44 | public static final String JAVA_DOC_RETURN = " * @return "; |
| 42 | public static final String JAVA_DOC_THROWS = " * @throws "; | 45 | public static final String JAVA_DOC_THROWS = " * @throws "; |
| 43 | public static final String JAVA_DOC_SETTERS = " * Returns the builder object of "; | 46 | public static final String JAVA_DOC_SETTERS = " * Returns the builder object of "; |
| 47 | + public static final String JAVA_DOC_OF = " * Returns the object of "; | ||
| 48 | + public static final String JAVA_DOC_SETTERS_COMMON = " * Sets the value of "; | ||
| 44 | public static final String JAVA_DOC_GETTERS = " * Returns the attribute "; | 49 | public static final String JAVA_DOC_GETTERS = " * Returns the attribute "; |
| 45 | public static final String JAVA_DOC_DEFAULT_CONSTRUCTOR = " * Default Constructor.\n"; | 50 | public static final String JAVA_DOC_DEFAULT_CONSTRUCTOR = " * Default Constructor.\n"; |
| 46 | public static final String JAVA_DOC_CONSTRUCTOR = " * Construct the object of "; | 51 | public static final String JAVA_DOC_CONSTRUCTOR = " * Construct the object of "; |
| ... | @@ -51,9 +56,11 @@ public final class UtilConstants { | ... | @@ -51,9 +56,11 @@ public final class UtilConstants { |
| 51 | * Basic requirements. | 56 | * Basic requirements. |
| 52 | */ | 57 | */ |
| 53 | public static final String NEW_LINE = "\n"; | 58 | public static final String NEW_LINE = "\n"; |
| 59 | + public static final String EMPTY_STRING = ""; | ||
| 54 | public static final String NEW_LINE_ESTRIC = " *\n"; | 60 | public static final String NEW_LINE_ESTRIC = " *\n"; |
| 55 | public static final String PERIOD = "."; | 61 | public static final String PERIOD = "."; |
| 56 | public static final String COLAN = ":"; | 62 | public static final String COLAN = ":"; |
| 63 | + public static final String UNDER_SCORE = "_"; | ||
| 57 | public static final String SEMI_COLAN = ";"; | 64 | public static final String SEMI_COLAN = ";"; |
| 58 | public static final String HYPHEN = "-"; | 65 | public static final String HYPHEN = "-"; |
| 59 | public static final String SPACE = " "; | 66 | public static final String SPACE = " "; |
| ... | @@ -63,6 +70,25 @@ public final class UtilConstants { | ... | @@ -63,6 +70,25 @@ public final class UtilConstants { |
| 63 | public static final String ADD = "+"; | 70 | public static final String ADD = "+"; |
| 64 | public static final String ASTERISK = "*"; | 71 | public static final String ASTERISK = "*"; |
| 65 | public static final String AT = "@"; | 72 | public static final String AT = "@"; |
| 73 | + public static final String QUOTES = "\""; | ||
| 74 | + public static final String AND = "&"; | ||
| 75 | + public static final String COMMA = ","; | ||
| 76 | + public static final String ADD_STRING = "add"; | ||
| 77 | + public static final String CHECK_NOT_NULL_STRING = "checkNotNull"; | ||
| 78 | + public static final String HASH_CODE_STRING = "hashCode"; | ||
| 79 | + public static final String EQUALS_STRING = "equals"; | ||
| 80 | + public static final String OBJECT_STRING = "Object"; | ||
| 81 | + public static final String INSTANCE_OF = " instanceof "; | ||
| 82 | + | ||
| 83 | + public static final String VALUE = "value"; | ||
| 84 | + | ||
| 85 | + public static final String IF = "if"; | ||
| 86 | + public static final String FOR = "for"; | ||
| 87 | + public static final String WHILE = "while"; | ||
| 88 | + public static final String OF = "of"; | ||
| 89 | + | ||
| 90 | + public static final String TRUE = "true"; | ||
| 91 | + public static final String FALSE = "false"; | ||
| 66 | 92 | ||
| 67 | /** | 93 | /** |
| 68 | * For brackets. | 94 | * For brackets. |
| ... | @@ -193,7 +219,23 @@ public final class UtilConstants { | ... | @@ -193,7 +219,23 @@ public final class UtilConstants { |
| 193 | public static final String DOUBLE_WRAPPER = "Double"; | 219 | public static final String DOUBLE_WRAPPER = "Double"; |
| 194 | 220 | ||
| 195 | /** | 221 | /** |
| 196 | - * For idenifiers. | 222 | + * List of keywords in java, this is used for checking if the input does not contain these keywords. |
| 223 | + */ | ||
| 224 | + public static final List JAVA_KEY_WORDS = Arrays.asList("abstract", "assert", "boolean", "break", "byte", "case", | ||
| 225 | + "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "extends", "false", | ||
| 226 | + "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", | ||
| 227 | + "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", | ||
| 228 | + "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", | ||
| 229 | + "void", "volatile", "while"); | ||
| 230 | + | ||
| 231 | + /** | ||
| 232 | + * Defining regular expression. | ||
| 233 | + */ | ||
| 234 | + public static final String REGEX_WITH_SPECIAL_CHAR = "[ : / - @ $ # ' * + , ; = ]+"; | ||
| 235 | + public static final String REGEX_FOR_FIRST_DIGIT = "\\d.*"; | ||
| 236 | + | ||
| 237 | + /** | ||
| 238 | + * For identifiers. | ||
| 197 | */ | 239 | */ |
| 198 | public static final String CLASS = "class"; | 240 | public static final String CLASS = "class"; |
| 199 | public static final String BUILDER = "Builder"; | 241 | public static final String BUILDER = "Builder"; |
| ... | @@ -218,7 +260,9 @@ public final class UtilConstants { | ... | @@ -218,7 +260,9 @@ public final class UtilConstants { |
| 218 | /** | 260 | /** |
| 219 | * For collections. | 261 | * For collections. |
| 220 | */ | 262 | */ |
| 221 | - public static final String COLLECTION_IMPORTS = "import java.util."; | 263 | + public static final String COLLECTION_IMPORTS = "java.util"; |
| 264 | + public static final String MORE_OBJECT_IMPORT = "import com.google.common.base.MoreObjects;\n"; | ||
| 265 | + public static final String JAVA_UTIL_OBJECTS_IMPORT = "import java.util.Objects;\n"; | ||
| 222 | public static final String ABSTRACT_COLLECTION = "AbstractCollection"; | 266 | public static final String ABSTRACT_COLLECTION = "AbstractCollection"; |
| 223 | 267 | ||
| 224 | public static final String LIST = "List"; | 268 | public static final String LIST = "List"; | ... | ... |
| ... | @@ -17,6 +17,8 @@ | ... | @@ -17,6 +17,8 @@ |
| 17 | package org.onosproject.yangutils.translator.tojava.utils; | 17 | package org.onosproject.yangutils.translator.tojava.utils; |
| 18 | 18 | ||
| 19 | import org.junit.Test; | 19 | import org.junit.Test; |
| 20 | +import org.onosproject.yangutils.utils.UtilConstants; | ||
| 21 | + | ||
| 20 | import static org.junit.Assert.assertNotNull; | 22 | import static org.junit.Assert.assertNotNull; |
| 21 | import static org.hamcrest.core.Is.is; | 23 | import static org.hamcrest.core.Is.is; |
| 22 | import static org.junit.Assert.assertThat; | 24 | import static org.junit.Assert.assertThat; |
| ... | @@ -28,6 +30,24 @@ import java.lang.reflect.InvocationTargetException; | ... | @@ -28,6 +30,24 @@ import java.lang.reflect.InvocationTargetException; |
| 28 | */ | 30 | */ |
| 29 | public final class JavaIdentifierSyntaxTest { | 31 | public final class JavaIdentifierSyntaxTest { |
| 30 | 32 | ||
| 33 | + public static final String PARENT_PACKAGE = "test5.test6.test7"; | ||
| 34 | + public static final String CHILD_PACKAGE = "test1:test2:test3"; | ||
| 35 | + public static final String DATE1 = "2000-1-5"; | ||
| 36 | + public static final String DATE2 = "1992-01-25"; | ||
| 37 | + public static final String PARENT_WITH_PERIOD = "test5.test6.test7"; | ||
| 38 | + public static final String CHILD_WITH_PERIOD = "test1.test2.test3"; | ||
| 39 | + public static final String DATE_WITH_REV1 = "rev000105"; | ||
| 40 | + public static final String DATE_WITH_REV2 = "rev920125"; | ||
| 41 | + public static final String VERSION_NUMBER = "v1"; | ||
| 42 | + public static final String INVALID_NAME_SPACE1 = "byte:#test2:9test3"; | ||
| 43 | + public static final String INVALID_NAME_SPACE2 = "const:#test2://9test3"; | ||
| 44 | + public static final String VALID_NAME_SPACE1 = "_byte.test2._9test3"; | ||
| 45 | + public static final String VALID_NAME_SPACE2 = "_const.test2._9test3"; | ||
| 46 | + public static final String WITHOUT_CAMEL_CASE = "test-camel-case-identifier"; | ||
| 47 | + public static final String WITH_CAMEL_CASE = "testCamelCaseIdentifier"; | ||
| 48 | + public static final String WITHOUT_CAPITAL = "test_this"; | ||
| 49 | + public static final String WITH_CAPITAL = "Test_this"; | ||
| 50 | + | ||
| 31 | /** | 51 | /** |
| 32 | * Unit test for private constructor. | 52 | * Unit test for private constructor. |
| 33 | * | 53 | * |
| ... | @@ -38,6 +58,7 @@ public final class JavaIdentifierSyntaxTest { | ... | @@ -38,6 +58,7 @@ public final class JavaIdentifierSyntaxTest { |
| 38 | * @throws IllegalAccessException if instance is provoked or a method is provoked. | 58 | * @throws IllegalAccessException if instance is provoked or a method is provoked. |
| 39 | * @throws InvocationTargetException when an exception occurs by the method or constructor. | 59 | * @throws InvocationTargetException when an exception occurs by the method or constructor. |
| 40 | */ | 60 | */ |
| 61 | + @Test | ||
| 41 | public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException, | 62 | public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
| 42 | InstantiationException, IllegalAccessException, InvocationTargetException { | 63 | InstantiationException, IllegalAccessException, InvocationTargetException { |
| 43 | Class<?>[] classesToConstruct = {JavaIdentifierSyntax.class }; | 64 | Class<?>[] classesToConstruct = {JavaIdentifierSyntax.class }; |
| ... | @@ -53,8 +74,8 @@ public final class JavaIdentifierSyntaxTest { | ... | @@ -53,8 +74,8 @@ public final class JavaIdentifierSyntaxTest { |
| 53 | */ | 74 | */ |
| 54 | @Test | 75 | @Test |
| 55 | public void getPackageFromParentTest() { | 76 | public void getPackageFromParentTest() { |
| 56 | - String pkgFromParent = JavaIdentifierSyntax.getPackageFromParent("test5.test6.test7", "test1:test2:test3"); | 77 | + String pkgFromParent = JavaIdentifierSyntax.getPackageFromParent(PARENT_PACKAGE, CHILD_PACKAGE); |
| 57 | - assertThat(pkgFromParent.equals("test5.test6.test7.test1.test2.test3"), is(true)); | 78 | + assertThat(pkgFromParent.equals(PARENT_WITH_PERIOD + UtilConstants.PERIOD + CHILD_WITH_PERIOD), is(true)); |
| 58 | } | 79 | } |
| 59 | 80 | ||
| 60 | /** | 81 | /** |
| ... | @@ -63,18 +84,35 @@ public final class JavaIdentifierSyntaxTest { | ... | @@ -63,18 +84,35 @@ public final class JavaIdentifierSyntaxTest { |
| 63 | @Test | 84 | @Test |
| 64 | public void getRootPackageTest() { | 85 | public void getRootPackageTest() { |
| 65 | 86 | ||
| 66 | - String rootPackage = JavaIdentifierSyntax.getRootPackage((byte) 0, "test1:test2:test3", "5-1-2000"); | 87 | + String rootPackage = JavaIdentifierSyntax.getRootPackage((byte) 1, CHILD_PACKAGE, DATE1); |
| 67 | - assertThat(rootPackage.equals("org.onosproject.yang.gen.v0.test1.test2.test3.rev05012000"), is(true)); | 88 | + assertThat(rootPackage.equals(UtilConstants.DEFAULT_BASE_PKG + UtilConstants.PERIOD + VERSION_NUMBER |
| 89 | + + UtilConstants.PERIOD + CHILD_WITH_PERIOD + UtilConstants.PERIOD + DATE_WITH_REV1), is(true)); | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + /** | ||
| 93 | + * Unit test for root package generation with special characters presence. | ||
| 94 | + */ | ||
| 95 | + @Test | ||
| 96 | + public void getRootPackageWithSpecialCharactersTest() { | ||
| 97 | + | ||
| 98 | + String rootPackage = JavaIdentifierSyntax.getRootPackage((byte) 1, INVALID_NAME_SPACE1, DATE1); | ||
| 99 | + assertThat(rootPackage.equals(UtilConstants.DEFAULT_BASE_PKG + UtilConstants.PERIOD + VERSION_NUMBER | ||
| 100 | + + UtilConstants.PERIOD + VALID_NAME_SPACE1 + UtilConstants.PERIOD + DATE_WITH_REV1), is(true)); | ||
| 101 | + String rootPackage1 = JavaIdentifierSyntax.getRootPackage((byte) 1, INVALID_NAME_SPACE2, DATE1); | ||
| 102 | + assertThat(rootPackage1.equals(UtilConstants.DEFAULT_BASE_PKG + UtilConstants.PERIOD + VERSION_NUMBER | ||
| 103 | + + UtilConstants.PERIOD + VALID_NAME_SPACE2 + UtilConstants.PERIOD + DATE_WITH_REV1), is(true)); | ||
| 68 | } | 104 | } |
| 69 | 105 | ||
| 70 | /** | 106 | /** |
| 71 | - * Unit test for root package generation without revision complexity. | 107 | + * Unit test for root package generation without complexity in revision. |
| 72 | */ | 108 | */ |
| 73 | @Test | 109 | @Test |
| 74 | public void getRootPackageWithRevTest() { | 110 | public void getRootPackageWithRevTest() { |
| 75 | 111 | ||
| 76 | - String rootPkgWithRev = JavaIdentifierSyntax.getRootPackage((byte) 0, "test1:test2:test3", "25-01-1992"); | 112 | + String rootPkgWithRev = JavaIdentifierSyntax.getRootPackage((byte) 1, CHILD_PACKAGE, DATE2); |
| 77 | - assertThat(rootPkgWithRev.equals("org.onosproject.yang.gen.v0.test1.test2.test3.rev25011992"), is(true)); | 113 | + assertThat(rootPkgWithRev.equals(UtilConstants.DEFAULT_BASE_PKG + UtilConstants.PERIOD |
| 114 | + + VERSION_NUMBER + UtilConstants.PERIOD + CHILD_WITH_PERIOD + UtilConstants.PERIOD + DATE_WITH_REV2), | ||
| 115 | + is(true)); | ||
| 78 | } | 116 | } |
| 79 | 117 | ||
| 80 | /** | 118 | /** |
| ... | @@ -83,8 +121,8 @@ public final class JavaIdentifierSyntaxTest { | ... | @@ -83,8 +121,8 @@ public final class JavaIdentifierSyntaxTest { |
| 83 | @Test | 121 | @Test |
| 84 | public void getCapitalCaseTest() { | 122 | public void getCapitalCaseTest() { |
| 85 | 123 | ||
| 86 | - String capitalCase = JavaIdentifierSyntax.getCaptialCase("test_this"); | 124 | + String capitalCase = JavaIdentifierSyntax.getCaptialCase(WITHOUT_CAPITAL); |
| 87 | - assertThat(capitalCase.equals("Test_this"), is(true)); | 125 | + assertThat(capitalCase.equals(WITH_CAPITAL), is(true)); |
| 88 | } | 126 | } |
| 89 | 127 | ||
| 90 | /** | 128 | /** |
| ... | @@ -92,7 +130,7 @@ public final class JavaIdentifierSyntaxTest { | ... | @@ -92,7 +130,7 @@ public final class JavaIdentifierSyntaxTest { |
| 92 | */ | 130 | */ |
| 93 | @Test | 131 | @Test |
| 94 | public void getCamelCaseTest() { | 132 | public void getCamelCaseTest() { |
| 95 | - String camelCase = JavaIdentifierSyntax.getCamelCase("test-camel-case-identifier"); | 133 | + String camelCase = JavaIdentifierSyntax.getCamelCase(WITHOUT_CAMEL_CASE); |
| 96 | - assertThat(camelCase.equals("testCamelCaseIdentifier"), is(true)); | 134 | + assertThat(camelCase.equals(WITH_CAMEL_CASE), is(true)); |
| 97 | } | 135 | } |
| 98 | } | 136 | } | ... | ... |
| ... | @@ -70,7 +70,7 @@ public final class YangIoUtilsTest { | ... | @@ -70,7 +70,7 @@ public final class YangIoUtilsTest { |
| 70 | @Test | 70 | @Test |
| 71 | public void addPackageInfoWithEmptyPathTest() throws IOException { | 71 | public void addPackageInfoWithEmptyPathTest() throws IOException { |
| 72 | 72 | ||
| 73 | - File dirPath = new File(""); | 73 | + File dirPath = new File("invalid/check"); |
| 74 | thrown.expect(IOException.class); | 74 | thrown.expect(IOException.class); |
| 75 | thrown.expectMessage("Exception occured while creating package info file."); | 75 | thrown.expectMessage("Exception occured while creating package info file."); |
| 76 | YangIoUtils.addPackageInfo(dirPath, "check1", createPath); | 76 | YangIoUtils.addPackageInfo(dirPath, "check1", createPath); | ... | ... |
-
Please register or login to post a comment