b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yangutils.translator.tojava.utils; |
| 18 | |
| 19 | import org.junit.Test; |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.utils.UtilConstants; |
| 21 | |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 22 | import static org.junit.Assert.assertNotNull; |
| 23 | import static org.hamcrest.core.Is.is; |
| 24 | import static org.junit.Assert.assertThat; |
| 25 | import java.lang.reflect.Constructor; |
| 26 | import java.lang.reflect.InvocationTargetException; |
| 27 | |
| 28 | /** |
| 29 | * Unit tests for java identifier syntax. |
| 30 | */ |
| 31 | public final class JavaIdentifierSyntaxTest { |
| 32 | |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 33 | 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.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 51 | /** |
| 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.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 61 | @Test |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 62 | 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.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 77 | String pkgFromParent = JavaIdentifierSyntax.getPackageFromParent(PARENT_PACKAGE, CHILD_PACKAGE); |
| 78 | assertThat(pkgFromParent.equals(PARENT_WITH_PERIOD + UtilConstants.PERIOD + CHILD_WITH_PERIOD), is(true)); |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Unit test for root package generation with revision complexity. |
| 83 | */ |
| 84 | @Test |
| 85 | public void getRootPackageTest() { |
| 86 | |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 87 | 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.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /** |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 93 | * 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.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 108 | */ |
| 109 | @Test |
| 110 | public void getRootPackageWithRevTest() { |
| 111 | |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 112 | 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.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Unit test for capitalizing the incoming string. |
| 120 | */ |
| 121 | @Test |
| 122 | public void getCapitalCaseTest() { |
| 123 | |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 124 | String capitalCase = JavaIdentifierSyntax.getCaptialCase(WITHOUT_CAPITAL); |
| 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() { |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 133 | String camelCase = JavaIdentifierSyntax.getCamelCase(WITHOUT_CAMEL_CASE); |
| 134 | assertThat(camelCase.equals(WITH_CAMEL_CASE), is(true)); |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 135 | } |
| 136 | } |