blob: d4a51d8f34178c6d89e27a0d9f1957ff4fca1947 [file] [log] [blame]
b.janani8b8ebdc2016-02-25 12:25:55 +05301/*
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
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;
25import static org.junit.Assert.assertNotNull;
26import 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;
33import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getYangRevisionStr;
34import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_BASE_PKG;
35import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
Vinod Kumar S38046502016-03-23 15:30:27 +053036
b.janani8b8ebdc2016-02-25 12:25:55 +053037/**
38 * Unit tests for java identifier syntax.
39 */
40public final class JavaIdentifierSyntaxTest {
41
Bharat saraswal2f11f652016-03-25 18:19:46 +053042 private static final String PARENT_PACKAGE = "test5/test6/test7";
43 private static final String CHILD_PACKAGE = "test1:test2:test3";
44 private static final String DATE1 = "2000-1-5";
45 private static final String DATE2 = "1992-01-25";
46 private static final String PARENT_WITH_PERIOD = "test5.test6.test7";
47 private static final String CHILD_WITH_PERIOD = "test1.test2.test3";
48 private static final String DATE_WITH_REV1 = "rev20000105";
49 private static final String DATE_WITH_REV2 = "rev19920125";
50 private static final String VERSION_NUMBER = "v1";
51 private static final String INVALID_NAME_SPACE1 = "byte:#test2:9test3";
52 private static final String INVALID_NAME_SPACE2 = "const:#test2://9test3";
53 private static final String VALID_NAME_SPACE1 = "_byte.test2._9test3";
54 private static final String VALID_NAME_SPACE2 = "_const.test2._9test3";
55 private static final String WITHOUT_CAMEL_CASE = "test-camel-case-identifier";
56 private static final String WITH_CAMEL_CASE = "testCamelCaseIdentifier";
57 private static final String WITHOUT_CAPITAL = "test_this";
58 private static final String WITH_CAPITAL = "Test_this";
59 private static final String WITH_SMALL = "test_this";
b.janani1fef2732016-03-04 12:29:05 +053060
b.janani8b8ebdc2016-02-25 12:25:55 +053061 /**
62 * Unit test for private constructor.
63 *
64 * @throws SecurityException if any security violation is observed.
65 * @throws NoSuchMethodException if when the method is not found.
66 * @throws IllegalArgumentException if there is illegal argument found.
Vinod Kumar S38046502016-03-23 15:30:27 +053067 * @throws InstantiationException if instantiation is provoked for the
68 * private constructor.
69 * @throws IllegalAccessException if instance is provoked or a method is
70 * provoked.
71 * @throws InvocationTargetException when an exception occurs by the method
72 * or constructor.
b.janani8b8ebdc2016-02-25 12:25:55 +053073 */
b.janani1fef2732016-03-04 12:29:05 +053074 @Test
b.janani8b8ebdc2016-02-25 12:25:55 +053075 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S38046502016-03-23 15:30:27 +053076 InstantiationException, IllegalAccessException, InvocationTargetException {
Bharat saraswal2f11f652016-03-25 18:19:46 +053077
b.janani8b8ebdc2016-02-25 12:25:55 +053078 Class<?>[] classesToConstruct = {JavaIdentifierSyntax.class };
79 for (Class<?> clazz : classesToConstruct) {
80 Constructor<?> constructor = clazz.getDeclaredConstructor();
81 constructor.setAccessible(true);
82 assertNotNull(constructor.newInstance());
83 }
84 }
85
86 /**
b.janani8b8ebdc2016-02-25 12:25:55 +053087 * Unit test for root package generation with revision complexity.
88 */
89 @Test
90 public void getRootPackageTest() {
91
Bharat saraswal2f11f652016-03-25 18:19:46 +053092 String rootPackage = getRootPackage((byte) 1, CHILD_PACKAGE, DATE1);
93 assertThat(rootPackage.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
94 + PERIOD + CHILD_WITH_PERIOD + PERIOD + DATE_WITH_REV1), is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +053095 }
96
97 /**
b.janani1fef2732016-03-04 12:29:05 +053098 * Unit test for root package generation with special characters presence.
99 */
100 @Test
101 public void getRootPackageWithSpecialCharactersTest() {
102
Bharat saraswal2f11f652016-03-25 18:19:46 +0530103 String rootPackage = getRootPackage((byte) 1, INVALID_NAME_SPACE1, DATE1);
104 assertThat(rootPackage.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
105 + PERIOD + VALID_NAME_SPACE1 + PERIOD + DATE_WITH_REV1), is(true));
106 String rootPackage1 = getRootPackage((byte) 1, INVALID_NAME_SPACE2, DATE1);
107 assertThat(rootPackage1.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
108 + PERIOD + VALID_NAME_SPACE2 + PERIOD + DATE_WITH_REV1), is(true));
b.janani1fef2732016-03-04 12:29:05 +0530109 }
110
111 /**
112 * Unit test for root package generation without complexity in revision.
b.janani8b8ebdc2016-02-25 12:25:55 +0530113 */
114 @Test
115 public void getRootPackageWithRevTest() {
116
Bharat saraswal2f11f652016-03-25 18:19:46 +0530117 String rootPkgWithRev = getRootPackage((byte) 1, CHILD_PACKAGE, DATE2);
118 assertThat(rootPkgWithRev.equals(
119 DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER + PERIOD + CHILD_WITH_PERIOD + PERIOD + DATE_WITH_REV2),
b.janani1fef2732016-03-04 12:29:05 +0530120 is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +0530121 }
122
123 /**
124 * Unit test for capitalizing the incoming string.
125 */
126 @Test
127 public void getCapitalCaseTest() {
128
Bharat saraswal2f11f652016-03-25 18:19:46 +0530129 String capitalCase = getCaptialCase(WITHOUT_CAPITAL);
b.janani1fef2732016-03-04 12:29:05 +0530130 assertThat(capitalCase.equals(WITH_CAPITAL), is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +0530131 }
132
133 /**
134 * Unit test for getting the camel case for the received string.
135 */
136 @Test
137 public void getCamelCaseTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530138
139 String camelCase = getCamelCase(WITHOUT_CAMEL_CASE);
b.janani1fef2732016-03-04 12:29:05 +0530140 assertThat(camelCase.equals(WITH_CAMEL_CASE), is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +0530141 }
Bharat saraswal2f11f652016-03-25 18:19:46 +0530142
143 /**
144 * Unit test for getting the camel case for the received string.
145 */
146 @Test
147 public void getSmallCaseTest() {
148
149 String smallCase = getSmallCase(WITHOUT_CAPITAL);
150 assertThat(smallCase.equals(WITH_SMALL), is(true));
151 }
152
153 /**
154 * Unit test for getting the camel case for the received string.
155 */
156 @Test
157 public void getPackageFromPathTest() {
158
159 String pkg = getJavaPackageFromPackagePath(PARENT_PACKAGE);
160 assertThat(pkg.equals(PARENT_WITH_PERIOD), is(true));
161 }
162
163 /**
164 * Unit test for getting the camel case for the received string.
165 */
166 @Test
167 public void getPathFromPackageTest() {
168
169 String path = getPackageDirPathFromJavaJPackage(PARENT_WITH_PERIOD);
170 assertThat(path.equals(PARENT_PACKAGE), is(true));
171 }
172
173 /**
174 * Unit test for getting the camel case for the received string.
175 */
176 @Test
177 public void getYangRevTest() {
178
179 String rev = getYangRevisionStr(DATE1);
180 assertThat(rev.equals(DATE_WITH_REV1), is(true));
181 }
b.janani8b8ebdc2016-02-25 12:25:55 +0530182}