b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yangutils.translator.tojava.utils; |
| 18 | |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 19 | import java.lang.reflect.Constructor; |
| 20 | import java.lang.reflect.InvocationTargetException; |
| 21 | |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 22 | import org.junit.Test; |
| 23 | import org.onosproject.yangutils.translator.GeneratedFileType; |
| 24 | import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes; |
| 25 | import org.onosproject.yangutils.translator.tojava.TraversalType; |
| 26 | import org.onosproject.yangutils.utils.UtilConstants; |
| 27 | |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 28 | import static org.hamcrest.core.Is.is; |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 29 | import static org.junit.Assert.assertNotNull; |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 30 | import static org.junit.Assert.assertThat; |
| 31 | |
| 32 | /** |
| 33 | * Unit tests for class definition generator for generated files. |
| 34 | */ |
| 35 | public final class ClassDefinitionGeneratorTest { |
| 36 | |
| 37 | /** |
| 38 | * Unit test for private constructor. |
| 39 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 40 | * @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.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 49 | */ |
| 50 | @Test |
| 51 | public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 52 | IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { |
| 53 | Class<?>[] classesToConstruct = { |
| 54 | ClassDefinitionGenerator.class }; |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 55 | 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 S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 69 | .generateClassDefinition(GeneratedFileType.BUILDER_CLASS_MASK, "BuilderClass"); |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 70 | 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 S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 81 | .generateClassDefinition(GeneratedFileType.BUILDER_INTERFACE_MASK, "BuilderInterfaceClass"); |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 82 | 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 S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 91 | String implDefinition = ClassDefinitionGenerator.generateClassDefinition(GeneratedFileType.IMPL_CLASS_MASK, |
| 92 | "ImplClass"); |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 93 | 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 S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 102 | String interfaceDefinition = ClassDefinitionGenerator.generateClassDefinition(GeneratedFileType.INTERFACE_MASK, |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 103 | "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 S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 113 | // String invalidDefinition = ClassDefinitionGenerator |
| 114 | // .generateClassDefinition(GeneratedFileType.GENERATE_INTERFACE_WITH_BUILDER, "invalid"); |
| 115 | // assertThat(true, is(invalidDefinition == null)); |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 116 | } |
| 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 | } |