blob: 7ca84c7a0e074f910981adef0fa02bedc0c4eccf [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
Vinod Kumar S38046502016-03-23 15:30:27 +053019import static org.hamcrest.core.Is.is;
20import static org.junit.Assert.assertNotNull;
21import static org.junit.Assert.assertThat;
b.janani8b8ebdc2016-02-25 12:25:55 +053022
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053023import java.lang.reflect.Constructor;
24import java.lang.reflect.InvocationTargetException;
Vinod Kumar S38046502016-03-23 15:30:27 +053025
26import org.junit.Test;
27import org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType;
28import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes;
29import org.onosproject.yangutils.translator.tojava.TraversalType;
30import org.onosproject.yangutils.utils.UtilConstants;
b.janani8b8ebdc2016-02-25 12:25:55 +053031
32/**
33 * Unit tests for class definition generator for generated files.
34 */
35public final class ClassDefinitionGeneratorTest {
36
37 /**
38 * Unit test for private constructor.
39 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053040 * @throws SecurityException if any security violation is observed
41 * @throws NoSuchMethodException if when the method is not found
42 * @throws IllegalArgumentException if there is illegal argument found
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053043 * @throws InstantiationException if instantiation is provoked for the private constructor
44 * @throws IllegalAccessException if instance is provoked or a method is provoked
45 * @throws InvocationTargetException when an exception occurs by the method or constructor
b.janani8b8ebdc2016-02-25 12:25:55 +053046 */
47 @Test
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053048 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S38046502016-03-23 15:30:27 +053049 InstantiationException, IllegalAccessException, InvocationTargetException {
50
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053051 Class<?>[] classesToConstruct = {ClassDefinitionGenerator.class };
b.janani8b8ebdc2016-02-25 12:25:55 +053052 for (Class<?> clazz : classesToConstruct) {
53 Constructor<?> constructor = clazz.getDeclaredConstructor();
54 constructor.setAccessible(true);
55 assertNotNull(constructor.newInstance());
56 }
57 }
58
59 /**
60 * Unit test for builder class definition.
61 */
62 @Test
63 public void generateBuilderClassDefinitionTest() {
64
65 String builderClassDefinition = ClassDefinitionGenerator
Vinod Kumar S38046502016-03-23 15:30:27 +053066 .generateClassDefinition(GeneratedJavaFileType.BUILDER_CLASS_MASK, "BuilderClass");
b.janani8b8ebdc2016-02-25 12:25:55 +053067 assertThat(true, is(builderClassDefinition.contains(UtilConstants.BUILDER)));
68 assertThat(true, is(builderClassDefinition.contains(UtilConstants.CLASS)));
69 }
70
71 /**
72 * Unit test for builder interface definition.
73 */
74 @Test
75 public void generateBuilderInterfaceDefinitionTest() {
76
77 String builderInterfaceDefinition = ClassDefinitionGenerator
Vinod Kumar S38046502016-03-23 15:30:27 +053078 .generateClassDefinition(GeneratedJavaFileType.BUILDER_INTERFACE_MASK, "BuilderInterfaceClass");
b.janani8b8ebdc2016-02-25 12:25:55 +053079 assertThat(true, is(builderInterfaceDefinition.contains(UtilConstants.BUILDER)));
80 }
81
82 /**
83 * Unit test for impl class definition.
84 */
85 @Test
86 public void generateImplDefinitionTest() {
87
Vinod Kumar S38046502016-03-23 15:30:27 +053088 String implDefinition = ClassDefinitionGenerator.generateClassDefinition(GeneratedJavaFileType.IMPL_CLASS_MASK,
Vinod Kumar Sc4216002016-03-03 19:55:30 +053089 "ImplClass");
b.janani8b8ebdc2016-02-25 12:25:55 +053090 assertThat(true, is(implDefinition.contains(UtilConstants.IMPL)));
91 }
92
93 /**
94 * Unit test for interface definition.
95 */
96 @Test
97 public void generateinterfaceDefinitionTest() {
98
Vinod Kumar S38046502016-03-23 15:30:27 +053099 String interfaceDefinition = ClassDefinitionGenerator.generateClassDefinition(
100 GeneratedJavaFileType.INTERFACE_MASK,
b.janani8b8ebdc2016-02-25 12:25:55 +0530101 "InterfaceClass");
102 assertThat(true, is(interfaceDefinition.contains(UtilConstants.INTERFACE)));
103 }
104
105 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530106 * Unit test for typedef generated type.
b.janani8b8ebdc2016-02-25 12:25:55 +0530107 */
108 @Test
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530109 public void generateTypeDefTest() {
b.janani8b8ebdc2016-02-25 12:25:55 +0530110
Vinod Kumar S38046502016-03-23 15:30:27 +0530111 String typeDef = ClassDefinitionGenerator.generateClassDefinition(GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS,
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530112 "invalid");
113 assertThat(true, is(typeDef.contains(UtilConstants.CLASS)));
b.janani8b8ebdc2016-02-25 12:25:55 +0530114 }
115
116 /**
117 * Unit test for enum data types.
118 */
119 @Test
120 public void enumDataTypesTest() {
121
122 TraversalType.valueOf(TraversalType.CHILD.toString());
123 GeneratedMethodTypes.valueOf(GeneratedMethodTypes.CONSTRUCTOR.toString());
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530124 TempDataStoreTypes.valueOf(TempDataStoreTypes.CONSTRUCTOR.toString());
b.janani8b8ebdc2016-02-25 12:25:55 +0530125 }
126}