blob: cf922849c030420f1aaff2a23e448180caa908f3 [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;
28import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
29import 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";
Bharat saraswal2f11f652016-03-25 18:19:46 +053064 private static final String WITHOUT_CAPITAL = "test_this";
65 private static final String WITH_CAPITAL = "Test_this";
66 private static final String WITH_SMALL = "test_this";
b.janani1fef2732016-03-04 12:29:05 +053067
b.janani8b8ebdc2016-02-25 12:25:55 +053068 /**
69 * Unit test for private constructor.
70 *
71 * @throws SecurityException if any security violation is observed.
72 * @throws NoSuchMethodException if when the method is not found.
73 * @throws IllegalArgumentException if there is illegal argument found.
Vinod Kumar S38046502016-03-23 15:30:27 +053074 * @throws InstantiationException if instantiation is provoked for the
75 * private constructor.
76 * @throws IllegalAccessException if instance is provoked or a method is
77 * provoked.
78 * @throws InvocationTargetException when an exception occurs by the method
79 * or constructor.
b.janani8b8ebdc2016-02-25 12:25:55 +053080 */
b.janani1fef2732016-03-04 12:29:05 +053081 @Test
b.janani8b8ebdc2016-02-25 12:25:55 +053082 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S38046502016-03-23 15:30:27 +053083 InstantiationException, IllegalAccessException, InvocationTargetException {
Bharat saraswal2f11f652016-03-25 18:19:46 +053084
b.janani8b8ebdc2016-02-25 12:25:55 +053085 Class<?>[] classesToConstruct = {JavaIdentifierSyntax.class };
86 for (Class<?> clazz : classesToConstruct) {
87 Constructor<?> constructor = clazz.getDeclaredConstructor();
88 constructor.setAccessible(true);
Bharat saraswal6ef0b762016-04-05 12:45:45 +053089 assertThat(null, not(constructor.newInstance()));
b.janani8b8ebdc2016-02-25 12:25:55 +053090 }
91 }
92
93 /**
b.janani8b8ebdc2016-02-25 12:25:55 +053094 * Unit test for root package generation with revision complexity.
95 */
96 @Test
97 public void getRootPackageTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +053098 String rootPackage = getRootPackage((byte) 1, CHILD_PACKAGE, DATE1);
99 assertThat(rootPackage.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
100 + PERIOD + CHILD_WITH_PERIOD + PERIOD + DATE_WITH_REV1), is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +0530101 }
102
103 /**
b.janani1fef2732016-03-04 12:29:05 +0530104 * Unit test for root package generation with special characters presence.
105 */
106 @Test
107 public void getRootPackageWithSpecialCharactersTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530108 String rootPackage = getRootPackage((byte) 1, INVALID_NAME_SPACE1, DATE1);
109 assertThat(rootPackage.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
110 + PERIOD + VALID_NAME_SPACE1 + PERIOD + DATE_WITH_REV1), is(true));
111 String rootPackage1 = getRootPackage((byte) 1, INVALID_NAME_SPACE2, DATE1);
112 assertThat(rootPackage1.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
113 + PERIOD + VALID_NAME_SPACE2 + PERIOD + DATE_WITH_REV1), is(true));
b.janani1fef2732016-03-04 12:29:05 +0530114 }
115
116 /**
117 * Unit test for root package generation without complexity in revision.
b.janani8b8ebdc2016-02-25 12:25:55 +0530118 */
119 @Test
120 public void getRootPackageWithRevTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530121 String rootPkgWithRev = getRootPackage((byte) 1, CHILD_PACKAGE, DATE2);
122 assertThat(rootPkgWithRev.equals(
123 DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER + PERIOD + CHILD_WITH_PERIOD + PERIOD + DATE_WITH_REV2),
b.janani1fef2732016-03-04 12:29:05 +0530124 is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +0530125 }
126
127 /**
128 * Unit test for capitalizing the incoming string.
129 */
130 @Test
131 public void getCapitalCaseTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530132 String capitalCase = getCaptialCase(WITHOUT_CAPITAL);
b.janani1fef2732016-03-04 12:29:05 +0530133 assertThat(capitalCase.equals(WITH_CAPITAL), is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +0530134 }
135
136 /**
137 * Unit test for getting the camel case for the received string.
138 */
139 @Test
140 public void getCamelCaseTest() {
janani bde4ffab2016-04-15 16:18:30 +0530141 String camelCase = getCamelCase(WITHOUT_CAMEL_CASE, null);
b.janani1fef2732016-03-04 12:29:05 +0530142 assertThat(camelCase.equals(WITH_CAMEL_CASE), is(true));
janani bde4ffab2016-04-15 16:18:30 +0530143 String camelCase1 = getCamelCase(WITHOUT_CAMEL_CASE1, null);
144 assertThat(camelCase1.equals(WITH_CAMEL_CASE1), is(true));
145 String camelCase2 = getCamelCase(WITHOUT_CAMEL_CASE2, null);
146 assertThat(camelCase2.equals(WITH_CAMEL_CASE2), is(true));
147 String camelCase3 = getCamelCase(WITHOUT_CAMEL_CASE3, null);
148 assertThat(camelCase3.equals(WITH_CAMEL_CASE3), is(true));
149 String camelCase4 = getCamelCase(WITHOUT_CAMEL_CASE4, null);
150 assertThat(camelCase4.equals(WITH_CAMEL_CASE4), is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +0530151 }
Bharat saraswal2f11f652016-03-25 18:19:46 +0530152
153 /**
154 * Unit test for getting the camel case for the received string.
155 */
156 @Test
157 public void getSmallCaseTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530158 String smallCase = getSmallCase(WITHOUT_CAPITAL);
159 assertThat(smallCase.equals(WITH_SMALL), is(true));
160 }
161
162 /**
163 * Unit test for getting the camel case for the received string.
164 */
165 @Test
166 public void getPackageFromPathTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530167 String pkg = getJavaPackageFromPackagePath(PARENT_PACKAGE);
168 assertThat(pkg.equals(PARENT_WITH_PERIOD), is(true));
169 }
170
171 /**
172 * Unit test for getting the camel case for the received string.
173 */
174 @Test
175 public void getPathFromPackageTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530176 String path = getPackageDirPathFromJavaJPackage(PARENT_WITH_PERIOD);
177 assertThat(path.equals(PARENT_PACKAGE), is(true));
178 }
b.janani8b8ebdc2016-02-25 12:25:55 +0530179}