Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +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 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +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; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 24 | |
| 25 | import static org.hamcrest.MatcherAssert.assertThat; |
| 26 | import static org.hamcrest.core.Is.is; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 27 | import static org.hamcrest.core.IsNot.not; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 28 | import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getImportText; |
| 29 | import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination; |
| 30 | import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 31 | import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET; |
| 32 | import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_CLOSE_BRACKET; |
| 33 | import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_OPEN_BRACKET; |
| 34 | import static org.onosproject.yangutils.utils.UtilConstants.IMPORT; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG; |
| 36 | import static org.onosproject.yangutils.utils.UtilConstants.LIST; |
| 37 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
| 39 | import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN; |
| 41 | import static org.onosproject.yangutils.utils.UtilConstants.SPACE; |
| 42 | import static org.onosproject.yangutils.utils.UtilConstants.STRING_DATA_TYPE; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 43 | |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 44 | /** |
| 45 | * Unit test cases for java code snippet generator. |
| 46 | */ |
| 47 | public class JavaCodeSnippetGenTest { |
| 48 | |
| 49 | private static final String PKG_INFO = "org.onosproject.unittest"; |
| 50 | private static final String CLASS_INFO = "JavaCodeSnippetGenTest"; |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 51 | private static final String YANG_NAME = "Test"; |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 52 | |
| 53 | /** |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 54 | * Unit test for private constructor. |
| 55 | * |
| 56 | * @throws SecurityException if any security violation is observed |
| 57 | * @throws NoSuchMethodException if when the method is not found |
| 58 | * @throws IllegalArgumentException if there is illegal argument found |
| 59 | * @throws InstantiationException if instantiation is provoked for the private constructor |
| 60 | * @throws IllegalAccessException if instance is provoked or a method is provoked |
| 61 | * @throws InvocationTargetException when an exception occurs by the method or constructor |
| 62 | */ |
| 63 | @Test |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 64 | public void callPrivateConstructors() |
| 65 | throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 66 | InstantiationException, IllegalAccessException, InvocationTargetException { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 67 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 68 | Class<?>[] classesToConstruct = {JavaCodeSnippetGen.class }; |
| 69 | for (Class<?> clazz : classesToConstruct) { |
| 70 | Constructor<?> constructor = clazz.getDeclaredConstructor(); |
| 71 | constructor.setAccessible(true); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 72 | assertThat(null, not(constructor.newInstance())); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 77 | * Unit test case for import text. |
| 78 | */ |
| 79 | @Test |
| 80 | public void testForImportText() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 81 | JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo(); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 82 | importInfo.setPkgInfo(PKG_INFO); |
| 83 | importInfo.setClassInfo(CLASS_INFO); |
| 84 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 85 | String imports = getImportText(importInfo); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 86 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 87 | assertThat(true, is(imports.equals(IMPORT + PKG_INFO + PERIOD + CLASS_INFO + SEMI_COLAN + NEW_LINE))); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 91 | * Unit test case for java class interface definition close. |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 92 | */ |
| 93 | @Test |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 94 | public void testForJavaClassDefClose() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 95 | String interfaceDef = getJavaClassDefClose(); |
| 96 | assertThat(true, is(interfaceDef.equals(CLOSE_CURLY_BRACKET))); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Unit test case for java attribute info. |
| 101 | */ |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 102 | @Test |
| 103 | public void testForJavaAttributeInfo() { |
| 104 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 105 | String attributeWithoutTypePkg = getJavaAttributeDefination(null, STRING_DATA_TYPE, YANG_NAME, false); |
| 106 | assertThat(true, is(attributeWithoutTypePkg.equals( |
| 107 | PRIVATE + SPACE + STRING_DATA_TYPE + SPACE + YANG_NAME + SEMI_COLAN + NEW_LINE))); |
| 108 | |
| 109 | String attributeWithTypePkg = getJavaAttributeDefination(JAVA_LANG, STRING_DATA_TYPE, YANG_NAME, false); |
| 110 | assertThat(true, is(attributeWithTypePkg.equals(PRIVATE + SPACE + JAVA_LANG + PERIOD |
| 111 | + STRING_DATA_TYPE + SPACE + YANG_NAME + SEMI_COLAN + NEW_LINE))); |
| 112 | |
| 113 | String attributeWithListPkg = getJavaAttributeDefination(JAVA_LANG, STRING_DATA_TYPE, YANG_NAME, true); |
| 114 | assertThat(true, is(attributeWithListPkg.equals( |
| 115 | PRIVATE + SPACE + LIST + DIAMOND_OPEN_BRACKET + JAVA_LANG + PERIOD + STRING_DATA_TYPE |
| 116 | + DIAMOND_CLOSE_BRACKET + SPACE + YANG_NAME + SEMI_COLAN + NEW_LINE))); |
| 117 | |
| 118 | String attributeWithListWithoutPkg = getJavaAttributeDefination(null, STRING_DATA_TYPE, YANG_NAME, true); |
| 119 | assertThat(true, is(attributeWithListWithoutPkg.equals( |
| 120 | PRIVATE + SPACE + LIST + DIAMOND_OPEN_BRACKET + STRING_DATA_TYPE + DIAMOND_CLOSE_BRACKET + SPACE |
| 121 | + YANG_NAME + SEMI_COLAN + NEW_LINE))); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 122 | } |
| 123 | } |