blob: 7d66c7bd54cf7c750f447f941d00c612f6d532e0 [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 static org.hamcrest.core.Is.is;
Vinod Kumar S38046502016-03-23 15:30:27 +053020import static org.junit.Assert.assertNotNull;
b.janani8b8ebdc2016-02-25 12:25:55 +053021import static org.junit.Assert.assertThat;
Vinod Kumar S38046502016-03-23 15:30:27 +053022
b.janani8b8ebdc2016-02-25 12:25:55 +053023import java.lang.reflect.Constructor;
24import java.lang.reflect.InvocationTargetException;
25
Vinod Kumar S38046502016-03-23 15:30:27 +053026import org.junit.Test;
27import org.onosproject.yangutils.utils.UtilConstants;
28
b.janani8b8ebdc2016-02-25 12:25:55 +053029/**
30 * Unit tests for java identifier syntax.
31 */
32public final class JavaIdentifierSyntaxTest {
33
b.janani1fef2732016-03-04 12:29:05 +053034 public static final String PARENT_PACKAGE = "test5.test6.test7";
35 public static final String CHILD_PACKAGE = "test1:test2:test3";
36 public static final String DATE1 = "2000-1-5";
37 public static final String DATE2 = "1992-01-25";
38 public static final String PARENT_WITH_PERIOD = "test5.test6.test7";
39 public static final String CHILD_WITH_PERIOD = "test1.test2.test3";
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053040 public static final String DATE_WITH_REV1 = "rev20000105";
41 public static final String DATE_WITH_REV2 = "rev19920125";
b.janani1fef2732016-03-04 12:29:05 +053042 public static final String VERSION_NUMBER = "v1";
43 public static final String INVALID_NAME_SPACE1 = "byte:#test2:9test3";
44 public static final String INVALID_NAME_SPACE2 = "const:#test2://9test3";
45 public static final String VALID_NAME_SPACE1 = "_byte.test2._9test3";
46 public static final String VALID_NAME_SPACE2 = "_const.test2._9test3";
47 public static final String WITHOUT_CAMEL_CASE = "test-camel-case-identifier";
48 public static final String WITH_CAMEL_CASE = "testCamelCaseIdentifier";
49 public static final String WITHOUT_CAPITAL = "test_this";
50 public static final String WITH_CAPITAL = "Test_this";
51
b.janani8b8ebdc2016-02-25 12:25:55 +053052 /**
53 * Unit test for private constructor.
54 *
55 * @throws SecurityException if any security violation is observed.
56 * @throws NoSuchMethodException if when the method is not found.
57 * @throws IllegalArgumentException if there is illegal argument found.
Vinod Kumar S38046502016-03-23 15:30:27 +053058 * @throws InstantiationException if instantiation is provoked for the
59 * private constructor.
60 * @throws IllegalAccessException if instance is provoked or a method is
61 * provoked.
62 * @throws InvocationTargetException when an exception occurs by the method
63 * or constructor.
b.janani8b8ebdc2016-02-25 12:25:55 +053064 */
b.janani1fef2732016-03-04 12:29:05 +053065 @Test
b.janani8b8ebdc2016-02-25 12:25:55 +053066 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S38046502016-03-23 15:30:27 +053067 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani8b8ebdc2016-02-25 12:25:55 +053068 Class<?>[] classesToConstruct = {JavaIdentifierSyntax.class };
69 for (Class<?> clazz : classesToConstruct) {
70 Constructor<?> constructor = clazz.getDeclaredConstructor();
71 constructor.setAccessible(true);
72 assertNotNull(constructor.newInstance());
73 }
74 }
75
76 /**
b.janani8b8ebdc2016-02-25 12:25:55 +053077 * Unit test for root package generation with revision complexity.
78 */
79 @Test
80 public void getRootPackageTest() {
81
b.janani1fef2732016-03-04 12:29:05 +053082 String rootPackage = JavaIdentifierSyntax.getRootPackage((byte) 1, CHILD_PACKAGE, DATE1);
83 assertThat(rootPackage.equals(UtilConstants.DEFAULT_BASE_PKG + UtilConstants.PERIOD + VERSION_NUMBER
84 + UtilConstants.PERIOD + CHILD_WITH_PERIOD + UtilConstants.PERIOD + DATE_WITH_REV1), is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +053085 }
86
87 /**
b.janani1fef2732016-03-04 12:29:05 +053088 * Unit test for root package generation with special characters presence.
89 */
90 @Test
91 public void getRootPackageWithSpecialCharactersTest() {
92
93 String rootPackage = JavaIdentifierSyntax.getRootPackage((byte) 1, INVALID_NAME_SPACE1, DATE1);
94 assertThat(rootPackage.equals(UtilConstants.DEFAULT_BASE_PKG + UtilConstants.PERIOD + VERSION_NUMBER
95 + UtilConstants.PERIOD + VALID_NAME_SPACE1 + UtilConstants.PERIOD + DATE_WITH_REV1), is(true));
96 String rootPackage1 = JavaIdentifierSyntax.getRootPackage((byte) 1, INVALID_NAME_SPACE2, DATE1);
97 assertThat(rootPackage1.equals(UtilConstants.DEFAULT_BASE_PKG + UtilConstants.PERIOD + VERSION_NUMBER
98 + UtilConstants.PERIOD + VALID_NAME_SPACE2 + UtilConstants.PERIOD + DATE_WITH_REV1), is(true));
99 }
100
101 /**
102 * Unit test for root package generation without complexity in revision.
b.janani8b8ebdc2016-02-25 12:25:55 +0530103 */
104 @Test
105 public void getRootPackageWithRevTest() {
106
b.janani1fef2732016-03-04 12:29:05 +0530107 String rootPkgWithRev = JavaIdentifierSyntax.getRootPackage((byte) 1, CHILD_PACKAGE, DATE2);
108 assertThat(rootPkgWithRev.equals(UtilConstants.DEFAULT_BASE_PKG + UtilConstants.PERIOD
109 + VERSION_NUMBER + UtilConstants.PERIOD + CHILD_WITH_PERIOD + UtilConstants.PERIOD + DATE_WITH_REV2),
110 is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +0530111 }
112
113 /**
114 * Unit test for capitalizing the incoming string.
115 */
116 @Test
117 public void getCapitalCaseTest() {
118
b.janani1fef2732016-03-04 12:29:05 +0530119 String capitalCase = JavaIdentifierSyntax.getCaptialCase(WITHOUT_CAPITAL);
120 assertThat(capitalCase.equals(WITH_CAPITAL), is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +0530121 }
122
123 /**
124 * Unit test for getting the camel case for the received string.
125 */
126 @Test
127 public void getCamelCaseTest() {
b.janani1fef2732016-03-04 12:29:05 +0530128 String camelCase = JavaIdentifierSyntax.getCamelCase(WITHOUT_CAMEL_CASE);
129 assertThat(camelCase.equals(WITH_CAMEL_CASE), is(true));
b.janani8b8ebdc2016-02-25 12:25:55 +0530130 }
131}