blob: 1ae9143625e13e9cadbb193c8fa357cf26477ae6 [file] [log] [blame]
Bharat saraswalc46ee2a2016-02-25 02:26:43 +05301/*
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
17package org.onosproject.yangutils.translator.tojava.utils;
18
Bharat saraswalc46ee2a2016-02-25 02:26:43 +053019import static org.hamcrest.MatcherAssert.assertThat;
20import static org.hamcrest.core.Is.is;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053021import static org.junit.Assert.assertNotNull;
Bharat saraswalc46ee2a2016-02-25 02:26:43 +053022
Bharat saraswal022dae92016-03-04 20:08:09 +053023import java.lang.reflect.Constructor;
24import java.lang.reflect.InvocationTargetException;
25
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053026import org.junit.Test;
27import org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType;
28import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes;
29import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
30import org.onosproject.yangutils.utils.UtilConstants;
31
Bharat saraswalc46ee2a2016-02-25 02:26:43 +053032/**
33 * Unit test cases for java code snippet generator.
34 */
35public class JavaCodeSnippetGenTest {
36
37 private static final String PKG_INFO = "org.onosproject.unittest";
38 private static final String CLASS_INFO = "JavaCodeSnippetGenTest";
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053039 private static final int FILE_GEN_TYPE = GeneratedJavaFileType.INTERFACE_MASK;
Bharat saraswalc46ee2a2016-02-25 02:26:43 +053040 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 saraswal022dae92016-03-04 20:08:09 +053045 * 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 saraswalc46ee2a2016-02-25 02:26:43 +053066 * Unit test case for import text.
67 */
68 @Test
69 public void testForImportText() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053070 JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo();
Bharat saraswalc46ee2a2016-02-25 02:26:43 +053071 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 saraswalc46ee2a2016-02-25 02:26:43 +053090
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 saraswal022dae92016-03-04 20:08:09 +0530104 * Unit test case for java class interface definition close.
Bharat saraswalc46ee2a2016-02-25 02:26:43 +0530105 */
106 @Test
Bharat saraswal022dae92016-03-04 20:08:09 +0530107 public void testForJavaClassDefInterfaceClose() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530108 String interfaceDef = JavaCodeSnippetGen.getJavaClassDefClose();
Bharat saraswal022dae92016-03-04 20:08:09 +0530109 assertThat(true, is(interfaceDef.equals(UtilConstants.CLOSE_CURLY_BRACKET)));
Bharat saraswalc46ee2a2016-02-25 02:26:43 +0530110 }
111
112 /**
Bharat saraswal022dae92016-03-04 20:08:09 +0530113 * Unit test case for java class builder class definition close.
Bharat saraswalc46ee2a2016-02-25 02:26:43 +0530114 */
115 @Test
Bharat saraswal022dae92016-03-04 20:08:09 +0530116 public void testForJavaClassDefBuilderClassClose() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530117 String builderClassDef = JavaCodeSnippetGen.getJavaClassDefClose();
Bharat saraswal022dae92016-03-04 20:08:09 +0530118 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 S9f26ae52016-03-23 15:30:27 +0530126 String typeDef = JavaCodeSnippetGen.getJavaClassDefClose();
Bharat saraswal022dae92016-03-04 20:08:09 +0530127 assertThat(true, is(typeDef.equals(UtilConstants.CLOSE_CURLY_BRACKET)));
128 }
129
130 /**
131 * Unit test case for java attribute info.
132 */
Bharat saraswal022dae92016-03-04 20:08:09 +0530133 @Test
134 public void testForJavaAttributeInfo() {
135
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530136 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 saraswal022dae92016-03-04 20:08:09 +0530149 assertThat(true,
150 is(attributeWithListPkg.equals(UtilConstants.PRIVATE + UtilConstants.SPACE + UtilConstants.LIST
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530151 + 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 saraswal022dae92016-03-04 20:08:09 +0530156 assertThat(true,
157 is(attributeWithListWithoutPkg.equals(UtilConstants.PRIVATE + UtilConstants.SPACE + UtilConstants.LIST
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530158 + 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 saraswalc46ee2a2016-02-25 02:26:43 +0530161 }
162}