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 | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 19 | import static org.hamcrest.core.Is.is; |
| 20 | import static org.junit.Assert.assertNotNull; |
| 21 | import static org.junit.Assert.assertThat; |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 22 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 23 | import java.lang.reflect.Constructor; |
| 24 | import java.lang.reflect.InvocationTargetException; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 25 | |
| 26 | import org.junit.Test; |
| 27 | import org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType; |
| 28 | import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes; |
| 29 | import org.onosproject.yangutils.translator.tojava.TraversalType; |
| 30 | import org.onosproject.yangutils.utils.UtilConstants; |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 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 |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 43 | * @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.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 46 | */ |
| 47 | @Test |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 48 | public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 49 | InstantiationException, IllegalAccessException, InvocationTargetException { |
| 50 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 51 | Class<?>[] classesToConstruct = {ClassDefinitionGenerator.class }; |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 52 | 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 S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 66 | .generateClassDefinition(GeneratedJavaFileType.BUILDER_CLASS_MASK, "BuilderClass"); |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 67 | 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 S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 78 | .generateClassDefinition(GeneratedJavaFileType.BUILDER_INTERFACE_MASK, "BuilderInterfaceClass"); |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 79 | 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 S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 88 | String implDefinition = ClassDefinitionGenerator.generateClassDefinition(GeneratedJavaFileType.IMPL_CLASS_MASK, |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 89 | "ImplClass"); |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 90 | 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 S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 99 | String interfaceDefinition = ClassDefinitionGenerator.generateClassDefinition( |
| 100 | GeneratedJavaFileType.INTERFACE_MASK, |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 101 | "InterfaceClass"); |
| 102 | assertThat(true, is(interfaceDefinition.contains(UtilConstants.INTERFACE))); |
| 103 | } |
| 104 | |
| 105 | /** |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 106 | * Unit test for typedef generated type. |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 107 | */ |
| 108 | @Test |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 109 | public void generateTypeDefTest() { |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 110 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 111 | String typeDef = ClassDefinitionGenerator.generateClassDefinition(GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS, |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 112 | "invalid"); |
| 113 | assertThat(true, is(typeDef.contains(UtilConstants.CLASS))); |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 114 | } |
| 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 saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 124 | TempDataStoreTypes.valueOf(TempDataStoreTypes.CONSTRUCTOR.toString()); |
b.janani | 0d4517a | 2016-02-25 12:25:55 +0530 | [diff] [blame] | 125 | } |
| 126 | } |