blob: d2cb823b1f33245b08c14931c1fd0036238ed5d6 [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
19import org.junit.Test;
20import org.onosproject.yangutils.translator.GeneratedFileType;
21import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes;
22import org.onosproject.yangutils.translator.tojava.TraversalType;
23import org.onosproject.yangutils.utils.UtilConstants;
24
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053025import java.lang.reflect.Constructor;
26import java.lang.reflect.InvocationTargetException;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053027import static org.junit.Assert.assertNotNull;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053028import static org.hamcrest.core.Is.is;
b.janani8b8ebdc2016-02-25 12:25:55 +053029import static org.junit.Assert.assertThat;
30
31/**
32 * Unit tests for class definition generator for generated files.
33 */
34public final class ClassDefinitionGeneratorTest {
35
36 /**
37 * Unit test for private constructor.
38 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053039 * @throws SecurityException if any security violation is observed
40 * @throws NoSuchMethodException if when the method is not found
41 * @throws IllegalArgumentException if there is illegal argument found
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053042 * @throws InstantiationException if instantiation is provoked for the private constructor
43 * @throws IllegalAccessException if instance is provoked or a method is provoked
44 * @throws InvocationTargetException when an exception occurs by the method or constructor
b.janani8b8ebdc2016-02-25 12:25:55 +053045 */
46 @Test
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053047 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
48 InstantiationException, IllegalAccessException, InvocationTargetException {
49 Class<?>[] classesToConstruct = {ClassDefinitionGenerator.class };
b.janani8b8ebdc2016-02-25 12:25:55 +053050 for (Class<?> clazz : classesToConstruct) {
51 Constructor<?> constructor = clazz.getDeclaredConstructor();
52 constructor.setAccessible(true);
53 assertNotNull(constructor.newInstance());
54 }
55 }
56
57 /**
58 * Unit test for builder class definition.
59 */
60 @Test
61 public void generateBuilderClassDefinitionTest() {
62
63 String builderClassDefinition = ClassDefinitionGenerator
Vinod Kumar Sc4216002016-03-03 19:55:30 +053064 .generateClassDefinition(GeneratedFileType.BUILDER_CLASS_MASK, "BuilderClass");
b.janani8b8ebdc2016-02-25 12:25:55 +053065 assertThat(true, is(builderClassDefinition.contains(UtilConstants.BUILDER)));
66 assertThat(true, is(builderClassDefinition.contains(UtilConstants.CLASS)));
67 }
68
69 /**
70 * Unit test for builder interface definition.
71 */
72 @Test
73 public void generateBuilderInterfaceDefinitionTest() {
74
75 String builderInterfaceDefinition = ClassDefinitionGenerator
Vinod Kumar Sc4216002016-03-03 19:55:30 +053076 .generateClassDefinition(GeneratedFileType.BUILDER_INTERFACE_MASK, "BuilderInterfaceClass");
b.janani8b8ebdc2016-02-25 12:25:55 +053077 assertThat(true, is(builderInterfaceDefinition.contains(UtilConstants.BUILDER)));
78 }
79
80 /**
81 * Unit test for impl class definition.
82 */
83 @Test
84 public void generateImplDefinitionTest() {
85
Vinod Kumar Sc4216002016-03-03 19:55:30 +053086 String implDefinition = ClassDefinitionGenerator.generateClassDefinition(GeneratedFileType.IMPL_CLASS_MASK,
87 "ImplClass");
b.janani8b8ebdc2016-02-25 12:25:55 +053088 assertThat(true, is(implDefinition.contains(UtilConstants.IMPL)));
89 }
90
91 /**
92 * Unit test for interface definition.
93 */
94 @Test
95 public void generateinterfaceDefinitionTest() {
96
Vinod Kumar Sc4216002016-03-03 19:55:30 +053097 String interfaceDefinition = ClassDefinitionGenerator.generateClassDefinition(GeneratedFileType.INTERFACE_MASK,
b.janani8b8ebdc2016-02-25 12:25:55 +053098 "InterfaceClass");
99 assertThat(true, is(interfaceDefinition.contains(UtilConstants.INTERFACE)));
100 }
101
102 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530103 * Unit test for typedef generated type.
b.janani8b8ebdc2016-02-25 12:25:55 +0530104 */
105 @Test
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530106 public void generateTypeDefTest() {
b.janani8b8ebdc2016-02-25 12:25:55 +0530107
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530108 String typeDef = ClassDefinitionGenerator.generateClassDefinition(GeneratedFileType.GENERATE_TYPEDEF_CLASS,
109 "invalid");
110 assertThat(true, is(typeDef.contains(UtilConstants.CLASS)));
b.janani8b8ebdc2016-02-25 12:25:55 +0530111 }
112
113 /**
114 * Unit test for enum data types.
115 */
116 @Test
117 public void enumDataTypesTest() {
118
119 TraversalType.valueOf(TraversalType.CHILD.toString());
120 GeneratedMethodTypes.valueOf(GeneratedMethodTypes.CONSTRUCTOR.toString());
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530121 TempDataStoreTypes.valueOf(TempDataStoreTypes.CONSTRUCTOR.toString());
b.janani8b8ebdc2016-02-25 12:25:55 +0530122 }
123}