Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +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 | |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 19 | import static org.hamcrest.MatcherAssert.assertThat; |
| 20 | import static org.hamcrest.core.Is.is; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 21 | import static org.junit.Assert.assertNotNull; |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 22 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +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.translator.tojava.GeneratedJavaFileType; |
| 28 | import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes; |
| 29 | import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo; |
| 30 | import org.onosproject.yangutils.utils.UtilConstants; |
| 31 | |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 32 | /** |
| 33 | * Unit test cases for java code snippet generator. |
| 34 | */ |
| 35 | public class JavaCodeSnippetGenTest { |
| 36 | |
| 37 | private static final String PKG_INFO = "org.onosproject.unittest"; |
| 38 | private static final String CLASS_INFO = "JavaCodeSnippetGenTest"; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 39 | private static final int FILE_GEN_TYPE = GeneratedJavaFileType.INTERFACE_MASK; |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 40 | private static final GeneratedMethodTypes METHOD_GEN_TYPE = GeneratedMethodTypes.GETTER; |
| 41 | private static final String YANG_NAME = "Test"; |
| 42 | private static final String STRING = "String"; |
| 43 | |
| 44 | /** |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 45 | * Unit test for private constructor. |
| 46 | * |
| 47 | * @throws SecurityException if any security violation is observed |
| 48 | * @throws NoSuchMethodException if when the method is not found |
| 49 | * @throws IllegalArgumentException if there is illegal argument found |
| 50 | * @throws InstantiationException if instantiation is provoked for the private constructor |
| 51 | * @throws IllegalAccessException if instance is provoked or a method is provoked |
| 52 | * @throws InvocationTargetException when an exception occurs by the method or constructor |
| 53 | */ |
| 54 | @Test |
| 55 | public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
| 56 | InstantiationException, IllegalAccessException, InvocationTargetException { |
| 57 | Class<?>[] classesToConstruct = {JavaCodeSnippetGen.class }; |
| 58 | for (Class<?> clazz : classesToConstruct) { |
| 59 | Constructor<?> constructor = clazz.getDeclaredConstructor(); |
| 60 | constructor.setAccessible(true); |
| 61 | assertNotNull(constructor.newInstance()); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 66 | * Unit test case for import text. |
| 67 | */ |
| 68 | @Test |
| 69 | public void testForImportText() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 70 | JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo(); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 71 | importInfo.setPkgInfo(PKG_INFO); |
| 72 | importInfo.setClassInfo(CLASS_INFO); |
| 73 | |
| 74 | String imports = JavaCodeSnippetGen.getImportText(importInfo); |
| 75 | |
| 76 | assertThat(true, is(imports.equals(UtilConstants.IMPORT + PKG_INFO + UtilConstants.PERIOD + CLASS_INFO |
| 77 | + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE))); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Unit test case for java class definition start. |
| 82 | */ |
| 83 | @Test |
| 84 | public void testForJavaClassDefStart() { |
| 85 | String classDef = JavaCodeSnippetGen.getJavaClassDefStart(FILE_GEN_TYPE, YANG_NAME); |
| 86 | assertThat(true, |
| 87 | is(classDef.equals(UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.INTERFACE |
| 88 | + UtilConstants.SPACE + YANG_NAME + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET |
| 89 | + UtilConstants.NEW_LINE))); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 90 | |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Unit test case for list attribute. |
| 95 | */ |
| 96 | @Test |
| 97 | public void testForListAttribute() { |
| 98 | String listAttribute = JavaCodeSnippetGen.getListAttribute(STRING); |
| 99 | assertThat(true, is(listAttribute.equals(UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET + STRING |
| 100 | + UtilConstants.DIAMOND_CLOSE_BRACKET))); |
| 101 | } |
| 102 | |
| 103 | /** |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 104 | * Unit test case for java class interface definition close. |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 105 | */ |
| 106 | @Test |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 107 | public void testForJavaClassDefInterfaceClose() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 108 | String interfaceDef = JavaCodeSnippetGen.getJavaClassDefClose(); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 109 | assertThat(true, is(interfaceDef.equals(UtilConstants.CLOSE_CURLY_BRACKET))); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 113 | * Unit test case for java class builder class definition close. |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 114 | */ |
| 115 | @Test |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 116 | public void testForJavaClassDefBuilderClassClose() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 117 | String builderClassDef = JavaCodeSnippetGen.getJavaClassDefClose(); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 118 | assertThat(true, is(builderClassDef.equals(UtilConstants.CLOSE_CURLY_BRACKET))); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Unit test case for java class typedef definition close. |
| 123 | */ |
| 124 | @Test |
| 125 | public void testForJavaClassDefTypeDefClose() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 126 | String typeDef = JavaCodeSnippetGen.getJavaClassDefClose(); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 127 | assertThat(true, is(typeDef.equals(UtilConstants.CLOSE_CURLY_BRACKET))); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Unit test case for java attribute info. |
| 132 | */ |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 133 | @Test |
| 134 | public void testForJavaAttributeInfo() { |
| 135 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 136 | String attributeWithoutTypePkg = JavaCodeSnippetGen.getJavaAttributeDefination(null, UtilConstants.STRING, |
| 137 | YANG_NAME, false); |
| 138 | assertThat(true, |
| 139 | is(attributeWithoutTypePkg.equals(UtilConstants.PRIVATE + UtilConstants.SPACE + UtilConstants.STRING |
| 140 | + UtilConstants.SPACE + YANG_NAME + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE))); |
| 141 | String attributeWithTypePkg = JavaCodeSnippetGen.getJavaAttributeDefination( |
| 142 | UtilConstants.JAVA_LANG, UtilConstants.STRING, YANG_NAME, false); |
| 143 | assertThat(true, is(attributeWithTypePkg |
| 144 | .equals(UtilConstants.PRIVATE + UtilConstants.SPACE + UtilConstants.JAVA_LANG + UtilConstants.PERIOD |
| 145 | + UtilConstants.STRING + UtilConstants.SPACE + YANG_NAME + UtilConstants.SEMI_COLAN |
| 146 | + UtilConstants.NEW_LINE))); |
| 147 | String attributeWithListPkg = JavaCodeSnippetGen.getJavaAttributeDefination( |
| 148 | UtilConstants.JAVA_LANG, UtilConstants.STRING, YANG_NAME, true); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 149 | assertThat(true, |
| 150 | is(attributeWithListPkg.equals(UtilConstants.PRIVATE + UtilConstants.SPACE + UtilConstants.LIST |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 151 | + UtilConstants.DIAMOND_OPEN_BRACKET + UtilConstants.JAVA_LANG + UtilConstants.PERIOD |
| 152 | + UtilConstants.STRING + UtilConstants.DIAMOND_CLOSE_BRACKET + UtilConstants.SPACE + YANG_NAME |
| 153 | + UtilConstants.SUFIX_S + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE))); |
| 154 | String attributeWithListWithoutPkg = JavaCodeSnippetGen.getJavaAttributeDefination(null, UtilConstants.STRING, |
| 155 | YANG_NAME, true); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 156 | assertThat(true, |
| 157 | is(attributeWithListWithoutPkg.equals(UtilConstants.PRIVATE + UtilConstants.SPACE + UtilConstants.LIST |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 158 | + UtilConstants.DIAMOND_OPEN_BRACKET + UtilConstants.STRING |
| 159 | + UtilConstants.DIAMOND_CLOSE_BRACKET + UtilConstants.SPACE + YANG_NAME + UtilConstants.SUFIX_S |
| 160 | + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE))); |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 161 | } |
| 162 | } |