blob: a3b9200987252a60c6f4ae53c276bdec89e9e948 [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
Vinod Kumar S38046502016-03-23 15:30:27 +053022import org.junit.Test;
Bharat saraswal2f11f652016-03-25 18:19:46 +053023
24import static org.hamcrest.core.Is.is;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053025import static org.hamcrest.core.IsNot.not;
Bharat saraswal2f11f652016-03-25 18:19:46 +053026import static org.junit.Assert.assertThat;
27import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
janani b4a6711a2016-05-17 13:12:22 +053028import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase;
Bharat saraswal2f11f652016-03-25 18:19:46 +053029import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getJavaPackageFromPackagePath;
30import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
31import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
32import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase;
Bharat saraswal2f11f652016-03-25 18:19:46 +053033import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_BASE_PKG;
34import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
Vinod Kumar S38046502016-03-23 15:30:27 +053035
b.janani8b8ebdc2016-02-25 12:25:55 +053036/**
37 * Unit tests for java identifier syntax.
38 */
39public final class JavaIdentifierSyntaxTest {
40
Bharat saraswal2f11f652016-03-25 18:19:46 +053041 private static final String PARENT_PACKAGE = "test5/test6/test7";
42 private static final String CHILD_PACKAGE = "test1:test2:test3";
43 private static final String DATE1 = "2000-1-5";
44 private static final String DATE2 = "1992-01-25";
45 private static final String PARENT_WITH_PERIOD = "test5.test6.test7";
46 private static final String CHILD_WITH_PERIOD = "test1.test2.test3";
47 private static final String DATE_WITH_REV1 = "rev20000105";
48 private static final String DATE_WITH_REV2 = "rev19920125";
49 private static final String VERSION_NUMBER = "v1";
50 private static final String INVALID_NAME_SPACE1 = "byte:#test2:9test3";
51 private static final String INVALID_NAME_SPACE2 = "const:#test2://9test3";
janani bde4ffab2016-04-15 16:18:30 +053052 private static final String VALID_NAME_SPACE1 = "yangautoprefixbyte.test2.yangautoprefix9test3";
53 private static final String VALID_NAME_SPACE2 = "yangautoprefixconst.test2.yangautoprefix9test3";
Bharat saraswal2f11f652016-03-25 18:19:46 +053054 private static final String WITHOUT_CAMEL_CASE = "test-camel-case-identifier";
55 private static final String WITH_CAMEL_CASE = "testCamelCaseIdentifier";
janani bde4ffab2016-04-15 16:18:30 +053056 private static final String WITHOUT_CAMEL_CASE1 = ".-_try-._-.123";
57 private static final String WITH_CAMEL_CASE1 = "try123";
58 private static final String WITHOUT_CAMEL_CASE2 = "_try";
59 private static final String WITH_CAMEL_CASE2 = "yangAutoPrefixTry";
60 private static final String WITHOUT_CAMEL_CASE3 = "-1-123g-123ga-a";
61 private static final String WITH_CAMEL_CASE3 = "yangAutoPrefix1123G123Gaa";
62 private static final String WITHOUT_CAMEL_CASE4 = "a-b-c-d-e-f-g-h";
63 private static final String WITH_CAMEL_CASE4 = "aBcDeFgh";
janani b4a6711a2016-05-17 13:12:22 +053064 private static final String WITHOUT_CAMEL_CASE5 = "TestName";
65 private static final String WITH_CAMEL_CASE5 = "testName";
66 private static final String WITHOUT_CAMEL_CASE6 = "TEST-NAME";
67 private static final String WITH_CAMEL_CASE6 = "testName";
68 private static final String WITHOUT_CAMEL_CASE7 = "TESTNAME";
69 private static final String WITH_CAMEL_CASE7 = "testname";
70 private static final String WITHOUT_CAMEL_CASE8 = "TE-ST-NA-ME";
71 private static final String WITH_CAMEL_CASE8 = "teStNaMe";
72 private static final String WITHOUT_CAMEL_CASE9 = "TEST3NAME";
73 private static final String WITH_CAMEL_CASE9 = "test3Name";
74 private static final String WITHOUT_CAMEL_CASE10 = "TEST3";
75 private static final String WITH_CAMEL_CASE10 = "test3";
76 private static final String WITHOUT_CAMEL_CASE11 = "TEST3nAMe";
77 private static final String WITH_CAMEL_CASE11 = "test3Name";
78 private static final String WITHOUT_CAMEL_CASE12 = "TEST3name";
79 private static final String WITH_CAMEL_CASE12 = "test3Name";
Bharat saraswal2f11f652016-03-25 18:19:46 +053080 private static final String WITHOUT_CAPITAL = "test_this";
81 private static final String WITH_CAPITAL = "Test_this";
82 private static final String WITH_SMALL = "test_this";
b.janani1fef2732016-03-04 12:29:05 +053083
b.janani8b8ebdc2016-02-25 12:25:55 +053084 /**
85 * Unit test for private constructor.
86 *
87 * @throws SecurityException if any security violation is observed.
88 * @throws NoSuchMethodException if when the method is not found.
89 * @throws IllegalArgumentException if there is illegal argument found.
Vinod Kumar S38046502016-03-23 15:30:27 +053090 * @throws InstantiationException if instantiation is provoked for the
91 * private constructor.
92 * @throws IllegalAccessException if instance is provoked or a method is
93 * provoked.
94 * @throws InvocationTargetException when an exception occurs by the method
95 * or constructor.
b.janani8b8ebdc2016-02-25 12:25:55 +053096 */
b.janani1fef2732016-03-04 12:29:05 +053097 @Test
b.janani8b8ebdc2016-02-25 12:25:55 +053098 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S38046502016-03-23 15:30:27 +053099 InstantiationException, IllegalAccessException, InvocationTargetException {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530100
b.janani8b8ebdc2016-02-25 12:25:55 +0530101 Class<?>[] classesToConstruct = {JavaIdentifierSyntax.class };
102 for (Class<?> clazz : classesToConstruct) {
103 Constructor<?> constructor = clazz.getDeclaredConstructor();
104 constructor.setAccessible(true);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530105 assertThat(null, not(constructor.newInstance()));
b.janani8b8ebdc2016-02-25 12:25:55 +0530106 }
107 }
108
109 /**
b.janani8b8ebdc2016-02-25 12:25:55 +0530110 * Unit test for root package generation with revision complexity.
111 */
112 @Test
113 public void getRootPackageTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530114 String rootPackage = getRootPackage((byte) 1, CHILD_PACKAGE, DATE1);
115 assertThat(rootPackage.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
116 + PERIOD + CHILD_WITH_PERIOD + PERIOD + DATE_WITH_REV1), is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +0530117 }
118
119 /**
b.janani1fef2732016-03-04 12:29:05 +0530120 * Unit test for root package generation with special characters presence.
121 */
122 @Test
123 public void getRootPackageWithSpecialCharactersTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530124 String rootPackage = getRootPackage((byte) 1, INVALID_NAME_SPACE1, DATE1);
125 assertThat(rootPackage.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
126 + PERIOD + VALID_NAME_SPACE1 + PERIOD + DATE_WITH_REV1), is(true));
127 String rootPackage1 = getRootPackage((byte) 1, INVALID_NAME_SPACE2, DATE1);
128 assertThat(rootPackage1.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
129 + PERIOD + VALID_NAME_SPACE2 + PERIOD + DATE_WITH_REV1), is(true));
b.janani1fef2732016-03-04 12:29:05 +0530130 }
131
132 /**
133 * Unit test for root package generation without complexity in revision.
b.janani8b8ebdc2016-02-25 12:25:55 +0530134 */
135 @Test
136 public void getRootPackageWithRevTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530137 String rootPkgWithRev = getRootPackage((byte) 1, CHILD_PACKAGE, DATE2);
138 assertThat(rootPkgWithRev.equals(
139 DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER + PERIOD + CHILD_WITH_PERIOD + PERIOD + DATE_WITH_REV2),
b.janani1fef2732016-03-04 12:29:05 +0530140 is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +0530141 }
142
143 /**
144 * Unit test for capitalizing the incoming string.
145 */
146 @Test
147 public void getCapitalCaseTest() {
janani b4a6711a2016-05-17 13:12:22 +0530148 String capitalCase = getCapitalCase(WITHOUT_CAPITAL);
b.janani1fef2732016-03-04 12:29:05 +0530149 assertThat(capitalCase.equals(WITH_CAPITAL), is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +0530150 }
151
152 /**
153 * Unit test for getting the camel case for the received string.
154 */
155 @Test
156 public void getCamelCaseTest() {
janani bde4ffab2016-04-15 16:18:30 +0530157 String camelCase = getCamelCase(WITHOUT_CAMEL_CASE, null);
b.janani1fef2732016-03-04 12:29:05 +0530158 assertThat(camelCase.equals(WITH_CAMEL_CASE), is(true));
janani bde4ffab2016-04-15 16:18:30 +0530159 String camelCase1 = getCamelCase(WITHOUT_CAMEL_CASE1, null);
160 assertThat(camelCase1.equals(WITH_CAMEL_CASE1), is(true));
161 String camelCase2 = getCamelCase(WITHOUT_CAMEL_CASE2, null);
162 assertThat(camelCase2.equals(WITH_CAMEL_CASE2), is(true));
163 String camelCase3 = getCamelCase(WITHOUT_CAMEL_CASE3, null);
164 assertThat(camelCase3.equals(WITH_CAMEL_CASE3), is(true));
165 String camelCase4 = getCamelCase(WITHOUT_CAMEL_CASE4, null);
166 assertThat(camelCase4.equals(WITH_CAMEL_CASE4), is(true));
janani b4a6711a2016-05-17 13:12:22 +0530167 String camelCase5 = getCamelCase(WITHOUT_CAMEL_CASE5, null);
168 assertThat(camelCase5.equals(WITH_CAMEL_CASE5), is(true));
169 String camelCase6 = getCamelCase(WITHOUT_CAMEL_CASE6, null);
170 assertThat(camelCase6.equals(WITH_CAMEL_CASE6), is(true));
171 String camelCase7 = getCamelCase(WITHOUT_CAMEL_CASE7, null);
172 assertThat(camelCase7.equals(WITH_CAMEL_CASE7), is(true));
173 String camelCase8 = getCamelCase(WITHOUT_CAMEL_CASE8, null);
174 assertThat(camelCase8.equals(WITH_CAMEL_CASE8), is(true));
175 String camelCase9 = getCamelCase(WITHOUT_CAMEL_CASE9, null);
176 assertThat(camelCase9.equals(WITH_CAMEL_CASE9), is(true));
177 String camelCase10 = getCamelCase(WITHOUT_CAMEL_CASE10, null);
178 assertThat(camelCase10.equals(WITH_CAMEL_CASE10), is(true));
179 String camelCase11 = getCamelCase(WITHOUT_CAMEL_CASE11, null);
180 assertThat(camelCase11.equals(WITH_CAMEL_CASE11), is(true));
181 String camelCase12 = getCamelCase(WITHOUT_CAMEL_CASE12, null);
182 assertThat(camelCase12.equals(WITH_CAMEL_CASE12), is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +0530183 }
Bharat saraswal2f11f652016-03-25 18:19:46 +0530184
185 /**
186 * Unit test for getting the camel case for the received string.
187 */
188 @Test
189 public void getSmallCaseTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530190 String smallCase = getSmallCase(WITHOUT_CAPITAL);
191 assertThat(smallCase.equals(WITH_SMALL), is(true));
192 }
193
194 /**
195 * Unit test for getting the camel case for the received string.
196 */
197 @Test
198 public void getPackageFromPathTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530199 String pkg = getJavaPackageFromPackagePath(PARENT_PACKAGE);
200 assertThat(pkg.equals(PARENT_WITH_PERIOD), is(true));
201 }
202
203 /**
204 * Unit test for getting the camel case for the received string.
205 */
206 @Test
207 public void getPathFromPackageTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530208 String path = getPackageDirPathFromJavaJPackage(PARENT_WITH_PERIOD);
209 assertThat(path.equals(PARENT_PACKAGE), is(true));
210 }
b.janani8b8ebdc2016-02-25 12:25:55 +0530211}