blob: fdf1f21b38c427eb8d2ded31b07641a56f538114 [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";
52 private static final String VALID_NAME_SPACE1 = "_byte.test2._9test3";
53 private static final String VALID_NAME_SPACE2 = "_const.test2._9test3";
54 private static final String WITHOUT_CAMEL_CASE = "test-camel-case-identifier";
55 private static final String WITH_CAMEL_CASE = "testCamelCaseIdentifier";
56 private static final String WITHOUT_CAPITAL = "test_this";
57 private static final String WITH_CAPITAL = "Test_this";
58 private static final String WITH_SMALL = "test_this";
b.janani1fef2732016-03-04 12:29:05 +053059
b.janani8b8ebdc2016-02-25 12:25:55 +053060 /**
61 * Unit test for private constructor.
62 *
63 * @throws SecurityException if any security violation is observed.
64 * @throws NoSuchMethodException if when the method is not found.
65 * @throws IllegalArgumentException if there is illegal argument found.
Vinod Kumar S38046502016-03-23 15:30:27 +053066 * @throws InstantiationException if instantiation is provoked for the
67 * private constructor.
68 * @throws IllegalAccessException if instance is provoked or a method is
69 * provoked.
70 * @throws InvocationTargetException when an exception occurs by the method
71 * or constructor.
b.janani8b8ebdc2016-02-25 12:25:55 +053072 */
b.janani1fef2732016-03-04 12:29:05 +053073 @Test
b.janani8b8ebdc2016-02-25 12:25:55 +053074 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S38046502016-03-23 15:30:27 +053075 InstantiationException, IllegalAccessException, InvocationTargetException {
Bharat saraswal2f11f652016-03-25 18:19:46 +053076
b.janani8b8ebdc2016-02-25 12:25:55 +053077 Class<?>[] classesToConstruct = {JavaIdentifierSyntax.class };
78 for (Class<?> clazz : classesToConstruct) {
79 Constructor<?> constructor = clazz.getDeclaredConstructor();
80 constructor.setAccessible(true);
Bharat saraswal6ef0b762016-04-05 12:45:45 +053081 assertThat(null, not(constructor.newInstance()));
b.janani8b8ebdc2016-02-25 12:25:55 +053082 }
83 }
84
85 /**
b.janani8b8ebdc2016-02-25 12:25:55 +053086 * Unit test for root package generation with revision complexity.
87 */
88 @Test
89 public void getRootPackageTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +053090 String rootPackage = getRootPackage((byte) 1, CHILD_PACKAGE, DATE1);
91 assertThat(rootPackage.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
92 + PERIOD + CHILD_WITH_PERIOD + PERIOD + DATE_WITH_REV1), is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +053093 }
94
95 /**
b.janani1fef2732016-03-04 12:29:05 +053096 * Unit test for root package generation with special characters presence.
97 */
98 @Test
99 public void getRootPackageWithSpecialCharactersTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530100 String rootPackage = getRootPackage((byte) 1, INVALID_NAME_SPACE1, DATE1);
101 assertThat(rootPackage.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
102 + PERIOD + VALID_NAME_SPACE1 + PERIOD + DATE_WITH_REV1), is(true));
103 String rootPackage1 = getRootPackage((byte) 1, INVALID_NAME_SPACE2, DATE1);
104 assertThat(rootPackage1.equals(DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER
105 + PERIOD + VALID_NAME_SPACE2 + PERIOD + DATE_WITH_REV1), is(true));
b.janani1fef2732016-03-04 12:29:05 +0530106 }
107
108 /**
109 * Unit test for root package generation without complexity in revision.
b.janani8b8ebdc2016-02-25 12:25:55 +0530110 */
111 @Test
112 public void getRootPackageWithRevTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530113 String rootPkgWithRev = getRootPackage((byte) 1, CHILD_PACKAGE, DATE2);
114 assertThat(rootPkgWithRev.equals(
115 DEFAULT_BASE_PKG + PERIOD + VERSION_NUMBER + PERIOD + CHILD_WITH_PERIOD + PERIOD + DATE_WITH_REV2),
b.janani1fef2732016-03-04 12:29:05 +0530116 is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +0530117 }
118
119 /**
120 * Unit test for capitalizing the incoming string.
121 */
122 @Test
123 public void getCapitalCaseTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530124 String capitalCase = getCaptialCase(WITHOUT_CAPITAL);
b.janani1fef2732016-03-04 12:29:05 +0530125 assertThat(capitalCase.equals(WITH_CAPITAL), is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +0530126 }
127
128 /**
129 * Unit test for getting the camel case for the received string.
130 */
131 @Test
132 public void getCamelCaseTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530133 String camelCase = getCamelCase(WITHOUT_CAMEL_CASE);
b.janani1fef2732016-03-04 12:29:05 +0530134 assertThat(camelCase.equals(WITH_CAMEL_CASE), is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +0530135 }
Bharat saraswal2f11f652016-03-25 18:19:46 +0530136
137 /**
138 * Unit test for getting the camel case for the received string.
139 */
140 @Test
141 public void getSmallCaseTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530142 String smallCase = getSmallCase(WITHOUT_CAPITAL);
143 assertThat(smallCase.equals(WITH_SMALL), is(true));
144 }
145
146 /**
147 * Unit test for getting the camel case for the received string.
148 */
149 @Test
150 public void getPackageFromPathTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530151 String pkg = getJavaPackageFromPackagePath(PARENT_PACKAGE);
152 assertThat(pkg.equals(PARENT_WITH_PERIOD), is(true));
153 }
154
155 /**
156 * Unit test for getting the camel case for the received string.
157 */
158 @Test
159 public void getPathFromPackageTest() {
Bharat saraswal2f11f652016-03-25 18:19:46 +0530160 String path = getPackageDirPathFromJavaJPackage(PARENT_WITH_PERIOD);
161 assertThat(path.equals(PARENT_PACKAGE), is(true));
162 }
b.janani8b8ebdc2016-02-25 12:25:55 +0530163}