blob: 7b3c932375b1535dd17c27cb2d365f4fbf098491 [file] [log] [blame]
b.janani8b8ebdc2016-02-25 12:25:55 +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 saraswal2f00b4b2016-03-04 20:08:09 +053019import java.lang.reflect.Constructor;
20import java.lang.reflect.InvocationTargetException;
Vinod Kumar S38046502016-03-23 15:30:27 +053021
22import org.junit.Test;
Bharat saraswal2f11f652016-03-25 18:19:46 +053023
24import static org.hamcrest.core.Is.is;
25import static org.junit.Assert.assertNotNull;
26import static org.junit.Assert.assertThat;
27import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
28import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
29import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
30import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
31import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
32import static org.onosproject.yangutils.translator.tojava.utils.ClassDefinitionGenerator.generateClassDefinition;
33import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
34import static org.onosproject.yangutils.utils.UtilConstants.CLASS;
35import static org.onosproject.yangutils.utils.UtilConstants.FINAL;
36import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
37import static org.onosproject.yangutils.utils.UtilConstants.IMPLEMENTS;
38import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
39import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
40import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
41import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
42import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC;
43import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
b.janani8b8ebdc2016-02-25 12:25:55 +053044
45/**
46 * Unit tests for class definition generator for generated files.
47 */
48public final class ClassDefinitionGeneratorTest {
49
Bharat saraswal2f11f652016-03-25 18:19:46 +053050 private static final String CLASS_NAME = "testclass";
51
b.janani8b8ebdc2016-02-25 12:25:55 +053052 /**
53 * Unit test for private constructor.
54 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053055 * @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
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053058 * @throws InstantiationException if instantiation is provoked for the private constructor
59 * @throws IllegalAccessException if instance is provoked or a method is provoked
60 * @throws InvocationTargetException when an exception occurs by the method or constructor
b.janani8b8ebdc2016-02-25 12:25:55 +053061 */
62 @Test
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053063 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S38046502016-03-23 15:30:27 +053064 InstantiationException, IllegalAccessException, InvocationTargetException {
65
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053066 Class<?>[] classesToConstruct = {ClassDefinitionGenerator.class };
b.janani8b8ebdc2016-02-25 12:25:55 +053067 for (Class<?> clazz : classesToConstruct) {
68 Constructor<?> constructor = clazz.getDeclaredConstructor();
69 constructor.setAccessible(true);
70 assertNotNull(constructor.newInstance());
71 }
72 }
73
74 /**
75 * Unit test for builder class definition.
76 */
77 @Test
78 public void generateBuilderClassDefinitionTest() {
79
Bharat saraswal2f11f652016-03-25 18:19:46 +053080 String builderClassDefinition = generateClassDefinition(BUILDER_CLASS_MASK, CLASS_NAME);
81 assertThat(true, is(builderClassDefinition.equals(
82 PUBLIC + SPACE + CLASS + SPACE + CLASS_NAME + BUILDER + SPACE + IMPLEMENTS + SPACE + CLASS_NAME + PERIOD
83 + CLASS_NAME + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE)));
b.janani8b8ebdc2016-02-25 12:25:55 +053084 }
85
86 /**
87 * Unit test for builder interface definition.
88 */
89 @Test
90 public void generateBuilderInterfaceDefinitionTest() {
91
Bharat saraswal2f11f652016-03-25 18:19:46 +053092 String builderInterfaceDefinition = generateClassDefinition(BUILDER_INTERFACE_MASK, CLASS_NAME);
93 assertThat(true, is(builderInterfaceDefinition
94 .equals(INTERFACE + SPACE + CLASS_NAME + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + NEW_LINE)));
b.janani8b8ebdc2016-02-25 12:25:55 +053095 }
96
97 /**
98 * Unit test for impl class definition.
99 */
100 @Test
101 public void generateImplDefinitionTest() {
102
Bharat saraswal2f11f652016-03-25 18:19:46 +0530103 String implDefinition = generateClassDefinition(IMPL_CLASS_MASK, CLASS_NAME);
104 assertThat(true, is(implDefinition.equals(
105 PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + CLASS_NAME + IMPL + SPACE + IMPLEMENTS + SPACE
106 + CLASS_NAME + SPACE + OPEN_CURLY_BRACKET + NEW_LINE)));
b.janani8b8ebdc2016-02-25 12:25:55 +0530107 }
108
109 /**
110 * Unit test for interface definition.
111 */
112 @Test
113 public void generateinterfaceDefinitionTest() {
114
Bharat saraswal2f11f652016-03-25 18:19:46 +0530115 String interfaceDefinition = generateClassDefinition(INTERFACE_MASK, CLASS_NAME);
116 assertThat(true, is(interfaceDefinition
117 .equals(PUBLIC + SPACE + INTERFACE + SPACE + CLASS_NAME + SPACE + OPEN_CURLY_BRACKET + NEW_LINE)));
b.janani8b8ebdc2016-02-25 12:25:55 +0530118 }
119
120 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530121 * Unit test for typedef generated type.
b.janani8b8ebdc2016-02-25 12:25:55 +0530122 */
123 @Test
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530124 public void generateTypeDefTest() {
b.janani8b8ebdc2016-02-25 12:25:55 +0530125
Bharat saraswal2f11f652016-03-25 18:19:46 +0530126 String typeDef = generateClassDefinition(GENERATE_TYPEDEF_CLASS, CLASS_NAME);
127 assertThat(true, is(typeDef.equals(
128 PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + CLASS_NAME + SPACE + OPEN_CURLY_BRACKET + NEW_LINE)));
b.janani8b8ebdc2016-02-25 12:25:55 +0530129 }
130}