b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 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.utils.io.impl; |
| 18 | |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 19 | import java.lang.reflect.Constructor; |
| 20 | import java.lang.reflect.InvocationTargetException; |
| 21 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 22 | import org.junit.Rule; |
| 23 | import org.junit.Test; |
| 24 | import org.junit.rules.ExpectedException; |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.utils.io.YangPluginConfig; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 26 | |
| 27 | import static org.hamcrest.core.Is.is; |
| 28 | import static org.hamcrest.core.IsNot.not; |
| 29 | import static org.junit.Assert.assertThat; |
| 30 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc; |
| 31 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILDER_CLASS; |
| 32 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILDER_INTERFACE; |
| 33 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILD_METHOD; |
| 34 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.CONSTRUCTOR; |
| 35 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.DEFAULT_CONSTRUCTOR; |
| 36 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD; |
| 37 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.IMPL_CLASS; |
| 38 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.INTERFACE; |
| 39 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO; |
| 40 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.SETTER_METHOD; |
| 41 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.TYPE_DEF_SETTER_METHOD; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Tests the java doc that is generated. |
| 45 | */ |
| 46 | public final class JavaDocGenTest { |
| 47 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 48 | private static final String TEST_NAME = "testName"; |
| 49 | private static final String END_STRING = " */\n"; |
| 50 | |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 51 | @Rule |
| 52 | public ExpectedException thrown = ExpectedException.none(); |
| 53 | |
| 54 | /** |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 55 | * This test case checks the content received for the builder class java doc. |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 56 | */ |
| 57 | @Test |
| 58 | public void builderClassGenerationTest() { |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 59 | String builderClassJavaDoc = getJavaDoc(BUILDER_CLASS, TEST_NAME, false, getStubPluginConfig(), null); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 60 | assertThat(true, is(builderClassJavaDoc.contains("Represents the builder implementation of") |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 61 | && builderClassJavaDoc.contains(END_STRING))); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 65 | * This test case checks the content received for the builder interface ge java doc. |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 66 | */ |
| 67 | @Test |
| 68 | public void builderInterfaceGenerationTest() { |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 69 | String builderInterfaceJavaDoc = getJavaDoc(BUILDER_INTERFACE, TEST_NAME, false, getStubPluginConfig(), null); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 70 | assertThat(true, |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 71 | is(builderInterfaceJavaDoc.contains("Builder for") |
| 72 | && builderInterfaceJavaDoc.contains(END_STRING))); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 76 | * This test case checks the content received for the build java doc. |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 77 | */ |
| 78 | @Test |
| 79 | public void buildGenerationTest() { |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 80 | String buildDoc = getJavaDoc(BUILD_METHOD, TEST_NAME, false, getStubPluginConfig(), null); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 81 | assertThat(true, is(buildDoc.contains("Builds object of") && buildDoc.contains(END_STRING))); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
| 85 | * A private constructor is tested. |
| 86 | * |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 87 | * @throws SecurityException if any security violation is observed |
| 88 | * @throws NoSuchMethodException if when the method is not found |
| 89 | * @throws IllegalArgumentException if there is illegal argument found |
| 90 | * @throws InstantiationException if instantiation is provoked for the private constructor |
| 91 | * @throws IllegalAccessException if instance is provoked or a method is provoked |
| 92 | * @throws InvocationTargetException when an exception occurs by the method or constructor |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 93 | */ |
| 94 | @Test |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 95 | public void callPrivateConstructors() |
| 96 | throws SecurityException, NoSuchMethodException, IllegalArgumentException, |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 97 | InstantiationException, IllegalAccessException, InvocationTargetException { |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 98 | |
| 99 | Class<?>[] classesToConstruct = {JavaDocGen.class }; |
| 100 | for (Class<?> clazz : classesToConstruct) { |
| 101 | Constructor<?> constructor = clazz.getDeclaredConstructor(); |
| 102 | constructor.setAccessible(true); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 103 | assertThat(null, not(constructor.newInstance())); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 108 | * This test case checks the content received for the constructor java doc. |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 109 | */ |
| 110 | @Test |
| 111 | public void constructorGenerationTest() { |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 112 | String constructorDoc = getJavaDoc(CONSTRUCTOR, TEST_NAME, false, getStubPluginConfig(), null); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 113 | assertThat(true, |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 114 | is(constructorDoc.contains("Creates an instance of ") |
| 115 | && constructorDoc.contains("builder object of") |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 116 | && constructorDoc.contains("@param") && constructorDoc.contains("*/\n"))); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 120 | * This test case checks the content received for the default constructor java doc. |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 121 | */ |
| 122 | @Test |
| 123 | public void defaultConstructorGenerationTest() { |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 124 | String defaultConstructorDoc = getJavaDoc(DEFAULT_CONSTRUCTOR, TEST_NAME, false, getStubPluginConfig(), null); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 125 | assertThat(true, is(defaultConstructorDoc.contains("Creates an instance of ") |
| 126 | && defaultConstructorDoc.contains(END_STRING))); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 130 | * This test case checks the content received for the getter java doc. |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 131 | */ |
| 132 | @Test |
| 133 | public void getterGenerationTest() { |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 134 | String getterJavaDoc = getJavaDoc(GETTER_METHOD, TEST_NAME, false, getStubPluginConfig(), null); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 135 | assertThat(true, |
| 136 | is(getterJavaDoc.contains("Returns the attribute") && getterJavaDoc.contains(END_STRING))); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 140 | * This test case checks the content received for the impl class java doc. |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 141 | */ |
| 142 | @Test |
| 143 | public void implClassGenerationTest() { |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 144 | String implClassJavaDoc = getJavaDoc(IMPL_CLASS, TEST_NAME, false, getStubPluginConfig(), null); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 145 | assertThat(true, |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 146 | is(implClassJavaDoc.contains("Represents the implementation of") |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 147 | && implClassJavaDoc.contains(END_STRING))); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 151 | * This test case checks the content received for the interface java doc. |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 152 | */ |
| 153 | @Test |
| 154 | public void interfaceGenerationTest() { |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 155 | String interfaceJavaDoc = getJavaDoc(INTERFACE, TEST_NAME, false, getStubPluginConfig(), null); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 156 | assertThat(true, |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 157 | is(interfaceJavaDoc.contains("Abstraction of an entity which represents the functionality of") |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 158 | && interfaceJavaDoc.contains(END_STRING))); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 162 | * This test case checks the content received for the package info java doc. |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 163 | */ |
| 164 | @Test |
| 165 | public void packageInfoGenerationTest() { |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 166 | String packageInfo = getJavaDoc(PACKAGE_INFO, TEST_NAME, false, getStubPluginConfig(), null); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 167 | assertThat(true, |
| 168 | is(packageInfo.contains("Implementation of YANG node") && packageInfo.contains(END_STRING))); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 172 | * This test case checks the content received for the package info java doc. |
| 173 | */ |
| 174 | @Test |
| 175 | public void packageInfoGenerationForChildNodeTest() { |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 176 | String packageInfo = getJavaDoc(PACKAGE_INFO, TEST_NAME, true, getStubPluginConfig(), null); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 177 | assertThat(true, is(packageInfo.contains("Implementation of YANG node testName's children nodes") |
| 178 | && packageInfo.contains(END_STRING))); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * This test case checks the content received for the setter java doc. |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 183 | */ |
| 184 | @Test |
| 185 | public void setterGenerationTest() { |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 186 | String setterJavaDoc = getJavaDoc(SETTER_METHOD, TEST_NAME, false, getStubPluginConfig(), null); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 187 | assertThat(true, |
| 188 | is(setterJavaDoc.contains("Returns the builder object of") && setterJavaDoc.contains(END_STRING))); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 189 | } |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 190 | |
| 191 | /** |
| 192 | * This test case checks the content received for the typedef setter java doc. |
| 193 | */ |
| 194 | @Test |
| 195 | public void typeDefSetterGenerationTest() { |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 196 | String typeDefSetter = getJavaDoc(TYPE_DEF_SETTER_METHOD, TEST_NAME, false, getStubPluginConfig(), null); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 197 | assertThat(true, is(typeDefSetter.contains("Sets the value of") && typeDefSetter.contains(END_STRING))); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 198 | } |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 199 | |
| 200 | /** |
| 201 | * Returns stub pluginConfig. |
| 202 | * |
| 203 | * @return stub pluginConfig |
| 204 | */ |
| 205 | private YangPluginConfig getStubPluginConfig() { |
| 206 | YangPluginConfig pluginConfig = new YangPluginConfig(); |
| 207 | pluginConfig.setConflictResolver(null); |
| 208 | return pluginConfig; |
| 209 | } |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 210 | } |