blob: 6d0056ebe844819d06e2a6c02ba66c2718c9b8c0 [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 Sc4216002016-03-03 19:55:30 +053019import java.lang.reflect.Constructor;
20import java.lang.reflect.InvocationTargetException;
21
b.janani8b8ebdc2016-02-25 12:25:55 +053022import org.junit.Test;
23import org.onosproject.yangutils.translator.GeneratedFileType;
24import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes;
25import org.onosproject.yangutils.translator.tojava.TraversalType;
26import org.onosproject.yangutils.utils.UtilConstants;
27
b.janani8b8ebdc2016-02-25 12:25:55 +053028import static org.hamcrest.core.Is.is;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053029import static org.junit.Assert.assertNotNull;
b.janani8b8ebdc2016-02-25 12:25:55 +053030import static org.junit.Assert.assertThat;
31
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
43 * @throws InstantiationException if instantiation is provoked for the
44 * private constructor
45 * @throws IllegalAccessException if instance is provoked or a method is
46 * provoked
47 * @throws InvocationTargetException when an exception occurs by the method
48 * or constructor
b.janani8b8ebdc2016-02-25 12:25:55 +053049 */
50 @Test
51 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException,
Vinod Kumar Sc4216002016-03-03 19:55:30 +053052 IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
53 Class<?>[] classesToConstruct = {
54 ClassDefinitionGenerator.class };
b.janani8b8ebdc2016-02-25 12:25:55 +053055 for (Class<?> clazz : classesToConstruct) {
56 Constructor<?> constructor = clazz.getDeclaredConstructor();
57 constructor.setAccessible(true);
58 assertNotNull(constructor.newInstance());
59 }
60 }
61
62 /**
63 * Unit test for builder class definition.
64 */
65 @Test
66 public void generateBuilderClassDefinitionTest() {
67
68 String builderClassDefinition = ClassDefinitionGenerator
Vinod Kumar Sc4216002016-03-03 19:55:30 +053069 .generateClassDefinition(GeneratedFileType.BUILDER_CLASS_MASK, "BuilderClass");
b.janani8b8ebdc2016-02-25 12:25:55 +053070 assertThat(true, is(builderClassDefinition.contains(UtilConstants.BUILDER)));
71 assertThat(true, is(builderClassDefinition.contains(UtilConstants.CLASS)));
72 }
73
74 /**
75 * Unit test for builder interface definition.
76 */
77 @Test
78 public void generateBuilderInterfaceDefinitionTest() {
79
80 String builderInterfaceDefinition = ClassDefinitionGenerator
Vinod Kumar Sc4216002016-03-03 19:55:30 +053081 .generateClassDefinition(GeneratedFileType.BUILDER_INTERFACE_MASK, "BuilderInterfaceClass");
b.janani8b8ebdc2016-02-25 12:25:55 +053082 assertThat(true, is(builderInterfaceDefinition.contains(UtilConstants.BUILDER)));
83 }
84
85 /**
86 * Unit test for impl class definition.
87 */
88 @Test
89 public void generateImplDefinitionTest() {
90
Vinod Kumar Sc4216002016-03-03 19:55:30 +053091 String implDefinition = ClassDefinitionGenerator.generateClassDefinition(GeneratedFileType.IMPL_CLASS_MASK,
92 "ImplClass");
b.janani8b8ebdc2016-02-25 12:25:55 +053093 assertThat(true, is(implDefinition.contains(UtilConstants.IMPL)));
94 }
95
96 /**
97 * Unit test for interface definition.
98 */
99 @Test
100 public void generateinterfaceDefinitionTest() {
101
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530102 String interfaceDefinition = ClassDefinitionGenerator.generateClassDefinition(GeneratedFileType.INTERFACE_MASK,
b.janani8b8ebdc2016-02-25 12:25:55 +0530103 "InterfaceClass");
104 assertThat(true, is(interfaceDefinition.contains(UtilConstants.INTERFACE)));
105 }
106
107 /**
108 * Unit test for invalid generated type.
109 */
110 @Test
111 public void generateInvalidDefinitionTest() {
112
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530113 // String invalidDefinition = ClassDefinitionGenerator
114 // .generateClassDefinition(GeneratedFileType.GENERATE_INTERFACE_WITH_BUILDER, "invalid");
115 // assertThat(true, is(invalidDefinition == null));
b.janani8b8ebdc2016-02-25 12:25:55 +0530116 }
117
118 /**
119 * Unit test for enum data types.
120 */
121 @Test
122 public void enumDataTypesTest() {
123
124 TraversalType.valueOf(TraversalType.CHILD.toString());
125 GeneratedMethodTypes.valueOf(GeneratedMethodTypes.CONSTRUCTOR.toString());
126 }
127}