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 | |
| 19 | import org.junit.Test; |
| 20 | import org.onosproject.yangutils.translator.GeneratedFileType; |
| 21 | import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes; |
| 22 | import org.onosproject.yangutils.translator.tojava.TraversalType; |
| 23 | import org.onosproject.yangutils.utils.UtilConstants; |
| 24 | |
| 25 | import java.lang.reflect.Constructor; |
| 26 | import java.lang.reflect.InvocationTargetException; |
| 27 | import static org.junit.Assert.assertNotNull; |
| 28 | import static org.hamcrest.core.Is.is; |
| 29 | import static org.junit.Assert.assertThat; |
| 30 | |
| 31 | /** |
| 32 | * Unit tests for class definition generator for generated files. |
| 33 | */ |
| 34 | public final class ClassDefinitionGeneratorTest { |
| 35 | |
| 36 | /** |
| 37 | * Unit test for private constructor. |
| 38 | * |
| 39 | * @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. |
| 42 | * @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. |
| 45 | */ |
| 46 | @Test |
| 47 | public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, |
| 48 | IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { |
| 49 | Class<?>[] classesToConstruct = {ClassDefinitionGenerator.class }; |
| 50 | 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 |
| 64 | .generateClassDefinition(GeneratedFileType.BUILDER_CLASS, "BuilderClass"); |
| 65 | 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 |
| 76 | .generateClassDefinition(GeneratedFileType.BUILDER_INTERFACE, "BuilderInterfaceClass"); |
| 77 | 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 | |
| 86 | String implDefinition = ClassDefinitionGenerator.generateClassDefinition(GeneratedFileType.IMPL, "ImplClass"); |
| 87 | assertThat(true, is(implDefinition.contains(UtilConstants.IMPL))); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Unit test for interface definition. |
| 92 | */ |
| 93 | @Test |
| 94 | public void generateinterfaceDefinitionTest() { |
| 95 | |
| 96 | String interfaceDefinition = ClassDefinitionGenerator.generateClassDefinition(GeneratedFileType.INTERFACE, |
| 97 | "InterfaceClass"); |
| 98 | assertThat(true, is(interfaceDefinition.contains(UtilConstants.INTERFACE))); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Unit test for invalid generated type. |
| 103 | */ |
| 104 | @Test |
| 105 | public void generateInvalidDefinitionTest() { |
| 106 | |
| 107 | String invalidDefinition = ClassDefinitionGenerator.generateClassDefinition(GeneratedFileType.ALL, "invalid"); |
| 108 | assertThat(true, is(invalidDefinition == null)); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Unit test for enum data types. |
| 113 | */ |
| 114 | @Test |
| 115 | public void enumDataTypesTest() { |
| 116 | |
| 117 | TraversalType.valueOf(TraversalType.CHILD.toString()); |
| 118 | GeneratedMethodTypes.valueOf(GeneratedMethodTypes.CONSTRUCTOR.toString()); |
| 119 | } |
| 120 | } |