blob: 6b86b9c4480d1e9e788f120cca797f38eb60b92a [file] [log] [blame]
b.janani8b8ebdc2016-02-25 12:25:55 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
b.janani8b8ebdc2016-02-25 12:25:55 +05303 *
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
17package org.onosproject.yangutils.translator.tojava.utils;
18
b.janani8b8ebdc2016-02-25 12:25:55 +053019import java.lang.reflect.Constructor;
20import java.lang.reflect.InvocationTargetException;
21
janani bdd1314f2016-05-19 17:39:50 +053022import org.junit.Rule;
Vinod Kumar S38046502016-03-23 15:30:27 +053023import org.junit.Test;
janani bdd1314f2016-05-19 17:39:50 +053024import org.junit.rules.ExpectedException;
25import org.onosproject.yangutils.translator.exception.TranslatorException;
Bharat saraswal2f11f652016-03-25 18:19:46 +053026
27import static org.hamcrest.core.Is.is;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053028import static org.hamcrest.core.IsNot.not;
Bharat saraswal2f11f652016-03-25 18:19:46 +053029import static org.junit.Assert.assertThat;
30import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
janani b4a6711a2016-05-17 13:12:22 +053031import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase;
Bharat saraswal2f11f652016-03-25 18:19:46 +053032import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getJavaPackageFromPackagePath;
33import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
34import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
35import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase;
Bharat saraswal2f11f652016-03-25 18:19:46 +053036import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_BASE_PKG;
37import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
Vinod Kumar S38046502016-03-23 15:30:27 +053038
b.janani8b8ebdc2016-02-25 12:25:55 +053039/**
40 * Unit tests for java identifier syntax.
41 */
42public final class JavaIdentifierSyntaxTest {
43
janani bdd1314f2016-05-19 17:39:50 +053044 @Rule
45 public ExpectedException thrown = ExpectedException.none();
46
Bharat saraswal2f11f652016-03-25 18:19:46 +053047 private static final String PARENT_PACKAGE = "test5/test6/test7";
48 private static final String CHILD_PACKAGE = "test1:test2:test3";
49 private static final String DATE1 = "2000-1-5";
50 private static final String DATE2 = "1992-01-25";
51 private static final String PARENT_WITH_PERIOD = "test5.test6.test7";
52 private static final String CHILD_WITH_PERIOD = "test1.test2.test3";
53 private static final String DATE_WITH_REV1 = "rev20000105";
54 private static final String DATE_WITH_REV2 = "rev19920125";
55 private static final String VERSION_NUMBER = "v1";
janani bdd1314f2016-05-19 17:39:50 +053056 private static final String VALID_PREFIX = "123add-prefix";
57 private static final String INVALID_PREFIX = "-*()&^&#$%";
58 private static final String INVALID_PREFIX1 = "abc~!@#$%^&*()_+}{:<>?`1234567890-=[]''|,./SS";
59 private static final String INVALID_NAME_SPACE_FOR_INVALID_PREFIX = "try:#test3:9case3";
Bharat saraswal2f11f652016-03-25 18:19:46 +053060 private static final String INVALID_NAME_SPACE1 = "byte:#test2:9test3";
61 private static final String INVALID_NAME_SPACE2 = "const:#test2://9test3";
janani bdd1314f2016-05-19 17:39:50 +053062 private static final String INVALID_NAME_SPACE3 = "CONST:TRY://9test3";
63 private static final String VALID_NAME_SPACE1 = "123addprefixbyte.test2.123addprefix9test3";
janani bde4ffab2016-04-15 16:18:30 +053064 private static final String VALID_NAME_SPACE2 = "yangautoprefixconst.test2.yangautoprefix9test3";
janani bdd1314f2016-05-19 17:39:50 +053065 private static final String VALID_NAME_SPACE3 = "abc1234567890ssconst.test2.abc1234567890ss9test3";
66 private static final String VALID_NAME_SPACE4 = "yangautoprefixconst.yangautoprefixtry.yangautoprefix9test3";
Bharat saraswal2f11f652016-03-25 18:19:46 +053067 private static final String WITHOUT_CAMEL_CASE = "test-camel-case-identifier";
68 private static final String WITH_CAMEL_CASE = "testCamelCaseIdentifier";
janani bde4ffab2016-04-15 16:18:30 +053069 private static final String WITHOUT_CAMEL_CASE1 = ".-_try-._-.123";
70 private static final String WITH_CAMEL_CASE1 = "try123";
71 private static final String WITHOUT_CAMEL_CASE2 = "_try";
72 private static final String WITH_CAMEL_CASE2 = "yangAutoPrefixTry";
73 private static final String WITHOUT_CAMEL_CASE3 = "-1-123g-123ga-a";
74 private static final String WITH_CAMEL_CASE3 = "yangAutoPrefix1123G123Gaa";
75 private static final String WITHOUT_CAMEL_CASE4 = "a-b-c-d-e-f-g-h";
76 private static final String WITH_CAMEL_CASE4 = "aBcDeFgh";
janani b4a6711a2016-05-17 13:12:22 +053077 private static final String WITHOUT_CAMEL_CASE5 = "TestName";
78 private static final String WITH_CAMEL_CASE5 = "testName";
79 private static final String WITHOUT_CAMEL_CASE6 = "TEST-NAME";
80 private static final String WITH_CAMEL_CASE6 = "testName";
81 private static final String WITHOUT_CAMEL_CASE7 = "TESTNAME";
82 private static final String WITH_CAMEL_CASE7 = "testname";
83 private static final String WITHOUT_CAMEL_CASE8 = "TE-ST-NA-ME";
84 private static final String WITH_CAMEL_CASE8 = "teStNaMe";
85 private static final String WITHOUT_CAMEL_CASE9 = "TEST3NAME";
86 private static final String WITH_CAMEL_CASE9 = "test3Name";
87 private static final String WITHOUT_CAMEL_CASE10 = "TEST3";
88 private static final String WITH_CAMEL_CASE10 = "test3";
89 private static final String WITHOUT_CAMEL_CASE11 = "TEST3nAMe";
90 private static final String WITH_CAMEL_CASE11 = "test3Name";
91 private static final String WITHOUT_CAMEL_CASE12 = "TEST3name";
92 private static final String WITH_CAMEL_CASE12 = "test3Name";
janani bdd1314f2016-05-19 17:39:50 +053093 private static final String WITHOUT_CAMEL_CASE13 = "t-RY";
94 private static final String WITH_CAMEL_CASE13 = "tRy";
95 private static final String WITHOUT_CAMEL_CASE14 = "TRY";
96 private static final String WITH_CAMEL_CASE14 = "yangAutoPrefixTry";
Bharat saraswal2f11f652016-03-25 18:19:46 +053097 private static final String WITHOUT_CAPITAL = "test_this";
98 private static final String WITH_CAPITAL = "Test_this";
99 private static final String WITH_SMALL = "test_this";
janani bdd1314f2016-05-19 17:39:50 +0530100 private static final String WITH_CAMEL_CASE_WITH_PREFIX = "123addPrefixTry";
101 private static final String WITH_CAMEL_CASE_WITH_PREFIX1 = "abc1234567890Ss1123G123Gaa";
102 private static YangToJavaNamingConflictUtil conflictResolver = new YangToJavaNamingConflictUtil();
b.janani1fef2732016-03-04 12:29:05 +0530103
b.janani8b8ebdc2016-02-25 12:25:55 +0530104 /**
105 * Unit test for private constructor.
106 *
107 * @throws SecurityException if any security violation is observed.
108 * @throws NoSuchMethodException if when the method is not found.
109 * @throws IllegalArgumentException if there is illegal argument found.
Vinod Kumar S38046502016-03-23 15:30:27 +0530110 * @throws InstantiationException if instantiation is provoked for the
111 * private constructor.
112 * @throws IllegalAccessException if instance is provoked or a method is
113 * provoked.
114 * @throws InvocationTargetException when an exception occurs by the method
115 * or constructor.
b.janani8b8ebdc2016-02-25 12:25:55 +0530116 */
b.janani1fef2732016-03-04 12:29:05 +0530117 @Test
b.janani8b8ebdc2016-02-25 12:25:55 +0530118 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S38046502016-03-23 15:30:27 +0530119 InstantiationException, IllegalAccessException, InvocationTargetException {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530120
b.janani8b8ebdc2016-02-25 12:25:55 +0530121 Class<?>[] classesToConstruct = {JavaIdentifierSyntax.class };
122 for (Class<?> clazz : classesToConstruct) {
123 Constructor<?> constructor = clazz.getDeclaredConstructor();
124 constructor.setAccessible(true);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530125 assertThat(null, not(constructor.newInstance()));
b.janani8b8ebdc2016-02-25 12:25:55 +0530126 }
127 }
128
129 /**
b.janani8b8ebdc2016-02-25 12:25:55 +0530130 * Unit test for root package generation with revision complexity.
131 */
132 @Test
133 public void getRootPackageTest() {
janani bdd1314f2016-05-19 17:39:50 +0530134 conflictResolver.setPrefixForIdentifier(null);
135 String rootPackage = getRootPackage((byte) 1, CHILD_PACKAGE, DATE1, conflictResolver);
Bharat saraswal2f11f652016-03-25 18:19:46 +0530136 assertThat(rootPackage.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
137 + PERIOD + CHILD_WITH_PERIOD + PERIOD + DATE_WITH_REV1), is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +0530138 }
139
140 /**
janani bdd1314f2016-05-19 17:39:50 +0530141 * Unit test for root package generation with invalid prefix.
142 */
143 @Test
144 public void getRootPackageWithInvalidPrefix() throws TranslatorException {
145 thrown.expect(TranslatorException.class);
146 thrown.expectMessage("The given prefix in pom.xml is invalid.");
147 conflictResolver.setPrefixForIdentifier(INVALID_PREFIX);
148 String rootPackage1 = getRootPackage((byte) 1, INVALID_NAME_SPACE_FOR_INVALID_PREFIX, DATE1, conflictResolver);
149 }
150
151 /**
b.janani1fef2732016-03-04 12:29:05 +0530152 * Unit test for root package generation with special characters presence.
153 */
154 @Test
155 public void getRootPackageWithSpecialCharactersTest() {
janani bdd1314f2016-05-19 17:39:50 +0530156 conflictResolver.setPrefixForIdentifier(VALID_PREFIX);
157 String rootPackage = getRootPackage((byte) 1, INVALID_NAME_SPACE1, DATE1, conflictResolver);
Bharat saraswal2f11f652016-03-25 18:19:46 +0530158 assertThat(rootPackage.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
159 + PERIOD + VALID_NAME_SPACE1 + PERIOD + DATE_WITH_REV1), is(true));
janani bdd1314f2016-05-19 17:39:50 +0530160 conflictResolver.setPrefixForIdentifier(null);
161 String rootPackage1 = getRootPackage((byte) 1, INVALID_NAME_SPACE2, DATE1, conflictResolver);
Bharat saraswal2f11f652016-03-25 18:19:46 +0530162 assertThat(rootPackage1.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
163 + PERIOD + VALID_NAME_SPACE2 + PERIOD + DATE_WITH_REV1), is(true));
janani bdd1314f2016-05-19 17:39:50 +0530164 String rootPackage2 = getRootPackage((byte) 1, INVALID_NAME_SPACE3, DATE1, conflictResolver);
165 assertThat(rootPackage2.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
166 + PERIOD + VALID_NAME_SPACE4 + PERIOD + DATE_WITH_REV1), is(true));
167 conflictResolver.setPrefixForIdentifier(INVALID_PREFIX1);
168 String rootPackage3 = getRootPackage((byte) 1, INVALID_NAME_SPACE2, DATE1, conflictResolver);
169 assertThat(rootPackage3.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
170 + PERIOD + VALID_NAME_SPACE3 + PERIOD + DATE_WITH_REV1), is(true));
171
b.janani1fef2732016-03-04 12:29:05 +0530172 }
173
174 /**
175 * Unit test for root package generation without complexity in revision.
b.janani8b8ebdc2016-02-25 12:25:55 +0530176 */
177 @Test
178 public void getRootPackageWithRevTest() {
janani bdd1314f2016-05-19 17:39:50 +0530179 String rootPkgWithRev = getRootPackage((byte) 1, CHILD_PACKAGE, DATE2, null);
Bharat saraswal2f11f652016-03-25 18:19:46 +0530180 assertThat(rootPkgWithRev.equals(
181 DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER + PERIOD + CHILD_WITH_PERIOD + PERIOD + DATE_WITH_REV2),
b.janani1fef2732016-03-04 12:29:05 +0530182 is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +0530183 }
184
185 /**
186 * Unit test for capitalizing the incoming string.
187 */
188 @Test
189 public void getCapitalCaseTest() {
janani b4a6711a2016-05-17 13:12:22 +0530190 String capitalCase = getCapitalCase(WITHOUT_CAPITAL);
b.janani1fef2732016-03-04 12:29:05 +0530191 assertThat(capitalCase.equals(WITH_CAPITAL), is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +0530192 }
193
194 /**
195 * Unit test for getting the camel case for the received string.
196 */
197 @Test
198 public void getCamelCaseTest() {
janani bdd1314f2016-05-19 17:39:50 +0530199 conflictResolver.setPrefixForIdentifier(null);
200 String camelCase = getCamelCase(WITHOUT_CAMEL_CASE, conflictResolver);
b.janani1fef2732016-03-04 12:29:05 +0530201 assertThat(camelCase.equals(WITH_CAMEL_CASE), is(true));
janani bdd1314f2016-05-19 17:39:50 +0530202 String camelCase1 = getCamelCase(WITHOUT_CAMEL_CASE1, conflictResolver);
janani bde4ffab2016-04-15 16:18:30 +0530203 assertThat(camelCase1.equals(WITH_CAMEL_CASE1), is(true));
janani bdd1314f2016-05-19 17:39:50 +0530204 String camelCase2 = getCamelCase(WITHOUT_CAMEL_CASE2, conflictResolver);
janani bde4ffab2016-04-15 16:18:30 +0530205 assertThat(camelCase2.equals(WITH_CAMEL_CASE2), is(true));
janani bdd1314f2016-05-19 17:39:50 +0530206 String camelCase3 = getCamelCase(WITHOUT_CAMEL_CASE3, conflictResolver);
janani bde4ffab2016-04-15 16:18:30 +0530207 assertThat(camelCase3.equals(WITH_CAMEL_CASE3), is(true));
janani bdd1314f2016-05-19 17:39:50 +0530208 String camelCase4 = getCamelCase(WITHOUT_CAMEL_CASE4, conflictResolver);
janani bde4ffab2016-04-15 16:18:30 +0530209 assertThat(camelCase4.equals(WITH_CAMEL_CASE4), is(true));
janani bdd1314f2016-05-19 17:39:50 +0530210 String camelCase5 = getCamelCase(WITHOUT_CAMEL_CASE5, conflictResolver);
janani b4a6711a2016-05-17 13:12:22 +0530211 assertThat(camelCase5.equals(WITH_CAMEL_CASE5), is(true));
janani bdd1314f2016-05-19 17:39:50 +0530212 String camelCase6 = getCamelCase(WITHOUT_CAMEL_CASE6, conflictResolver);
janani b4a6711a2016-05-17 13:12:22 +0530213 assertThat(camelCase6.equals(WITH_CAMEL_CASE6), is(true));
janani bdd1314f2016-05-19 17:39:50 +0530214 String camelCase7 = getCamelCase(WITHOUT_CAMEL_CASE7, conflictResolver);
janani b4a6711a2016-05-17 13:12:22 +0530215 assertThat(camelCase7.equals(WITH_CAMEL_CASE7), is(true));
janani bdd1314f2016-05-19 17:39:50 +0530216 String camelCase8 = getCamelCase(WITHOUT_CAMEL_CASE8, conflictResolver);
janani b4a6711a2016-05-17 13:12:22 +0530217 assertThat(camelCase8.equals(WITH_CAMEL_CASE8), is(true));
janani bdd1314f2016-05-19 17:39:50 +0530218 String camelCase9 = getCamelCase(WITHOUT_CAMEL_CASE9, conflictResolver);
janani b4a6711a2016-05-17 13:12:22 +0530219 assertThat(camelCase9.equals(WITH_CAMEL_CASE9), is(true));
janani bdd1314f2016-05-19 17:39:50 +0530220 String camelCase10 = getCamelCase(WITHOUT_CAMEL_CASE10, conflictResolver);
janani b4a6711a2016-05-17 13:12:22 +0530221 assertThat(camelCase10.equals(WITH_CAMEL_CASE10), is(true));
janani bdd1314f2016-05-19 17:39:50 +0530222 String camelCase11 = getCamelCase(WITHOUT_CAMEL_CASE11, conflictResolver);
janani b4a6711a2016-05-17 13:12:22 +0530223 assertThat(camelCase11.equals(WITH_CAMEL_CASE11), is(true));
janani bdd1314f2016-05-19 17:39:50 +0530224 String camelCase12 = getCamelCase(WITHOUT_CAMEL_CASE12, conflictResolver);
janani b4a6711a2016-05-17 13:12:22 +0530225 assertThat(camelCase12.equals(WITH_CAMEL_CASE12), is(true));
janani bdd1314f2016-05-19 17:39:50 +0530226 String camelCase13 = getCamelCase(WITHOUT_CAMEL_CASE13, conflictResolver);
227 assertThat(camelCase13.equals(WITH_CAMEL_CASE13), is(true));
228 String camelCase14 = getCamelCase(WITHOUT_CAMEL_CASE14, conflictResolver);
229 assertThat(camelCase14.equals(WITH_CAMEL_CASE14), is(true));
230 }
231
232 /**
233 * Unit test for getting the camel case along with the prefix provided.
234 */
235 @Test
236 public void getCamelCaseWithPrefixTest() {
237
238 conflictResolver.setPrefixForIdentifier(VALID_PREFIX);
239 String camelCase = getCamelCase(WITHOUT_CAMEL_CASE2, conflictResolver);
240 assertThat(camelCase.equals(WITH_CAMEL_CASE_WITH_PREFIX), is(true));
241 conflictResolver.setPrefixForIdentifier(INVALID_PREFIX1);
242 String camelCase2 = getCamelCase(WITHOUT_CAMEL_CASE3, conflictResolver);
243 assertThat(camelCase2.equals(WITH_CAMEL_CASE_WITH_PREFIX1), is(true));
244 }
245
246 /**
247 * Unit test for getting the camel case along with the invalid prefix provided.
248 */
249 @Test
250 public void getCamelCaseWithInvalidPrefixTest() throws TranslatorException {
251
252 thrown.expect(TranslatorException.class);
253 thrown.expectMessage("The given prefix in pom.xml is invalid.");
254 conflictResolver.setPrefixForIdentifier(INVALID_PREFIX);
255 String camelCase = getCamelCase(WITHOUT_CAMEL_CASE3, conflictResolver);
b.janani8b8ebdc2016-02-25 12:25:55 +0530256 }
Bharat saraswal2f11f652016-03-25 18:19:46 +0530257
258 /**
259 * Unit test for getting the camel case for the received string.
260 */
261 @Test
262 public void getSmallCaseTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530263 String smallCase = getSmallCase(WITHOUT_CAPITAL);
264 assertThat(smallCase.equals(WITH_SMALL), is(true));
265 }
266
267 /**
268 * Unit test for getting the camel case for the received string.
269 */
270 @Test
271 public void getPackageFromPathTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530272 String pkg = getJavaPackageFromPackagePath(PARENT_PACKAGE);
273 assertThat(pkg.equals(PARENT_WITH_PERIOD), is(true));
274 }
275
276 /**
277 * Unit test for getting the camel case for the received string.
278 */
279 @Test
280 public void getPathFromPackageTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530281 String path = getPackageDirPathFromJavaJPackage(PARENT_WITH_PERIOD);
282 assertThat(path.equals(PARENT_PACKAGE), is(true));
283 }
b.janani8b8ebdc2016-02-25 12:25:55 +0530284}