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 | |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 19 | import static org.hamcrest.core.Is.is; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 20 | import static org.junit.Assert.assertNotNull; |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 21 | import static org.junit.Assert.assertThat; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 22 | |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 23 | import java.lang.reflect.Constructor; |
| 24 | import java.lang.reflect.InvocationTargetException; |
| 25 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 26 | import org.junit.Test; |
| 27 | import org.onosproject.yangutils.utils.UtilConstants; |
| 28 | |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 29 | /** |
| 30 | * Unit tests for java identifier syntax. |
| 31 | */ |
| 32 | public final class JavaIdentifierSyntaxTest { |
| 33 | |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 34 | 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 saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 40 | public static final String DATE_WITH_REV1 = "rev20000105"; |
| 41 | public static final String DATE_WITH_REV2 = "rev19920125"; |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 42 | 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.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 52 | /** |
| 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 S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 58 | * @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.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 64 | */ |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 65 | @Test |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 66 | public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 67 | InstantiationException, IllegalAccessException, InvocationTargetException { |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 68 | 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.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 77 | * Unit test for root package generation with revision complexity. |
| 78 | */ |
| 79 | @Test |
| 80 | public void getRootPackageTest() { |
| 81 | |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 82 | 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.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 88 | * 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.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 103 | */ |
| 104 | @Test |
| 105 | public void getRootPackageWithRevTest() { |
| 106 | |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 107 | 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.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Unit test for capitalizing the incoming string. |
| 115 | */ |
| 116 | @Test |
| 117 | public void getCapitalCaseTest() { |
| 118 | |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 119 | String capitalCase = JavaIdentifierSyntax.getCaptialCase(WITHOUT_CAPITAL); |
| 120 | assertThat(capitalCase.equals(WITH_CAPITAL), is(true)); |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Unit test for getting the camel case for the received string. |
| 125 | */ |
| 126 | @Test |
| 127 | public void getCamelCaseTest() { |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 128 | String camelCase = JavaIdentifierSyntax.getCamelCase(WITHOUT_CAMEL_CASE); |
| 129 | assertThat(camelCase.equals(WITH_CAMEL_CASE), is(true)); |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 130 | } |
| 131 | } |