blob: 6e89d93600c6260e5c81489b5cfa43b06a6f066f [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
19import org.junit.Test;
b.janani1fef2732016-03-04 12:29:05 +053020import org.onosproject.yangutils.utils.UtilConstants;
21
b.janani8b8ebdc2016-02-25 12:25:55 +053022import static org.junit.Assert.assertNotNull;
23import static org.hamcrest.core.Is.is;
24import static org.junit.Assert.assertThat;
25import java.lang.reflect.Constructor;
26import java.lang.reflect.InvocationTargetException;
27
28/**
29 * Unit tests for java identifier syntax.
30 */
31public final class JavaIdentifierSyntaxTest {
32
b.janani1fef2732016-03-04 12:29:05 +053033 public static final String PARENT_PACKAGE = "test5.test6.test7";
34 public static final String CHILD_PACKAGE = "test1:test2:test3";
35 public static final String DATE1 = "2000-1-5";
36 public static final String DATE2 = "1992-01-25";
37 public static final String PARENT_WITH_PERIOD = "test5.test6.test7";
38 public static final String CHILD_WITH_PERIOD = "test1.test2.test3";
39 public static final String DATE_WITH_REV1 = "rev000105";
40 public static final String DATE_WITH_REV2 = "rev920125";
41 public static final String VERSION_NUMBER = "v1";
42 public static final String INVALID_NAME_SPACE1 = "byte:#test2:9test3";
43 public static final String INVALID_NAME_SPACE2 = "const:#test2://9test3";
44 public static final String VALID_NAME_SPACE1 = "_byte.test2._9test3";
45 public static final String VALID_NAME_SPACE2 = "_const.test2._9test3";
46 public static final String WITHOUT_CAMEL_CASE = "test-camel-case-identifier";
47 public static final String WITH_CAMEL_CASE = "testCamelCaseIdentifier";
48 public static final String WITHOUT_CAPITAL = "test_this";
49 public static final String WITH_CAPITAL = "Test_this";
50
b.janani8b8ebdc2016-02-25 12:25:55 +053051 /**
52 * Unit test for private constructor.
53 *
54 * @throws SecurityException if any security violation is observed.
55 * @throws NoSuchMethodException if when the method is not found.
56 * @throws IllegalArgumentException if there is illegal argument found.
57 * @throws InstantiationException if instantiation is provoked for the private constructor.
58 * @throws IllegalAccessException if instance is provoked or a method is provoked.
59 * @throws InvocationTargetException when an exception occurs by the method or constructor.
60 */
b.janani1fef2732016-03-04 12:29:05 +053061 @Test
b.janani8b8ebdc2016-02-25 12:25:55 +053062 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
63 InstantiationException, IllegalAccessException, InvocationTargetException {
64 Class<?>[] classesToConstruct = {JavaIdentifierSyntax.class };
65 for (Class<?> clazz : classesToConstruct) {
66 Constructor<?> constructor = clazz.getDeclaredConstructor();
67 constructor.setAccessible(true);
68 assertNotNull(constructor.newInstance());
69 }
70 }
71
72 /**
73 * Unit test for testing the package path generation from a parent package.
74 */
75 @Test
76 public void getPackageFromParentTest() {
b.janani1fef2732016-03-04 12:29:05 +053077 String pkgFromParent = JavaIdentifierSyntax.getPackageFromParent(PARENT_PACKAGE, CHILD_PACKAGE);
78 assertThat(pkgFromParent.equals(PARENT_WITH_PERIOD + UtilConstants.PERIOD + CHILD_WITH_PERIOD), is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +053079 }
80
81 /**
82 * Unit test for root package generation with revision complexity.
83 */
84 @Test
85 public void getRootPackageTest() {
86
b.janani1fef2732016-03-04 12:29:05 +053087 String rootPackage = JavaIdentifierSyntax.getRootPackage((byte) 1, CHILD_PACKAGE, DATE1);
88 assertThat(rootPackage.equals(UtilConstants.DEFAULT_BASE_PKG + UtilConstants.PERIOD + VERSION_NUMBER
89 + UtilConstants.PERIOD + CHILD_WITH_PERIOD + UtilConstants.PERIOD + DATE_WITH_REV1), is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +053090 }
91
92 /**
b.janani1fef2732016-03-04 12:29:05 +053093 * Unit test for root package generation with special characters presence.
94 */
95 @Test
96 public void getRootPackageWithSpecialCharactersTest() {
97
98 String rootPackage = JavaIdentifierSyntax.getRootPackage((byte) 1, INVALID_NAME_SPACE1, DATE1);
99 assertThat(rootPackage.equals(UtilConstants.DEFAULT_BASE_PKG + UtilConstants.PERIOD + VERSION_NUMBER
100 + UtilConstants.PERIOD + VALID_NAME_SPACE1 + UtilConstants.PERIOD + DATE_WITH_REV1), is(true));
101 String rootPackage1 = JavaIdentifierSyntax.getRootPackage((byte) 1, INVALID_NAME_SPACE2, DATE1);
102 assertThat(rootPackage1.equals(UtilConstants.DEFAULT_BASE_PKG + UtilConstants.PERIOD + VERSION_NUMBER
103 + UtilConstants.PERIOD + VALID_NAME_SPACE2 + UtilConstants.PERIOD + DATE_WITH_REV1), is(true));
104 }
105
106 /**
107 * Unit test for root package generation without complexity in revision.
b.janani8b8ebdc2016-02-25 12:25:55 +0530108 */
109 @Test
110 public void getRootPackageWithRevTest() {
111
b.janani1fef2732016-03-04 12:29:05 +0530112 String rootPkgWithRev = JavaIdentifierSyntax.getRootPackage((byte) 1, CHILD_PACKAGE, DATE2);
113 assertThat(rootPkgWithRev.equals(UtilConstants.DEFAULT_BASE_PKG + UtilConstants.PERIOD
114 + VERSION_NUMBER + UtilConstants.PERIOD + CHILD_WITH_PERIOD + UtilConstants.PERIOD + DATE_WITH_REV2),
115 is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +0530116 }
117
118 /**
119 * Unit test for capitalizing the incoming string.
120 */
121 @Test
122 public void getCapitalCaseTest() {
123
b.janani1fef2732016-03-04 12:29:05 +0530124 String capitalCase = JavaIdentifierSyntax.getCaptialCase(WITHOUT_CAPITAL);
125 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() {
b.janani1fef2732016-03-04 12:29:05 +0530133 String camelCase = JavaIdentifierSyntax.getCamelCase(WITHOUT_CAMEL_CASE);
134 assertThat(camelCase.equals(WITH_CAMEL_CASE), is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +0530135 }
136}