b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.yangutils.translator.tojava.utils; |
| 18 | |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 19 | import java.lang.reflect.Constructor; |
| 20 | import java.lang.reflect.InvocationTargetException; |
| 21 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 22 | import org.junit.Test; |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 23 | |
| 24 | import static org.hamcrest.core.Is.is; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 25 | import static org.hamcrest.core.IsNot.not; |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 26 | import static org.junit.Assert.assertThat; |
| 27 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase; |
| 28 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase; |
| 29 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getJavaPackageFromPackagePath; |
| 30 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage; |
| 31 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage; |
| 32 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase; |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_BASE_PKG; |
| 34 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 35 | |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 36 | /** |
| 37 | * Unit tests for java identifier syntax. |
| 38 | */ |
| 39 | public final class JavaIdentifierSyntaxTest { |
| 40 | |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 41 | 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.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 59 | |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 60 | /** |
| 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 S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 66 | * @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.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 72 | */ |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 73 | @Test |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 74 | public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 75 | InstantiationException, IllegalAccessException, InvocationTargetException { |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 76 | |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 77 | Class<?>[] classesToConstruct = {JavaIdentifierSyntax.class }; |
| 78 | for (Class<?> clazz : classesToConstruct) { |
| 79 | Constructor<?> constructor = clazz.getDeclaredConstructor(); |
| 80 | constructor.setAccessible(true); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 81 | assertThat(null, not(constructor.newInstance())); |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 86 | * Unit test for root package generation with revision complexity. |
| 87 | */ |
| 88 | @Test |
| 89 | public void getRootPackageTest() { |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 90 | 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.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 96 | * Unit test for root package generation with special characters presence. |
| 97 | */ |
| 98 | @Test |
| 99 | public void getRootPackageWithSpecialCharactersTest() { |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 100 | 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.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Unit test for root package generation without complexity in revision. |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 110 | */ |
| 111 | @Test |
| 112 | public void getRootPackageWithRevTest() { |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 113 | 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.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 116 | is(true)); |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Unit test for capitalizing the incoming string. |
| 121 | */ |
| 122 | @Test |
| 123 | public void getCapitalCaseTest() { |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 124 | String capitalCase = getCaptialCase(WITHOUT_CAPITAL); |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 125 | assertThat(capitalCase.equals(WITH_CAPITAL), is(true)); |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Unit test for getting the camel case for the received string. |
| 130 | */ |
| 131 | @Test |
| 132 | public void getCamelCaseTest() { |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 133 | String camelCase = getCamelCase(WITHOUT_CAMEL_CASE); |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 134 | assertThat(camelCase.equals(WITH_CAMEL_CASE), is(true)); |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 135 | } |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 136 | |
| 137 | /** |
| 138 | * Unit test for getting the camel case for the received string. |
| 139 | */ |
| 140 | @Test |
| 141 | public void getSmallCaseTest() { |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 142 | 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 saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 151 | 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 saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 160 | String path = getPackageDirPathFromJavaJPackage(PARENT_WITH_PERIOD); |
| 161 | assertThat(path.equals(PARENT_PACKAGE), is(true)); |
| 162 | } |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 163 | } |