blob: 0284ea0e271c645342a859422a69ff1ec9479695 [file] [log] [blame]
b.janani68c55e12016-02-24 12:23:03 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
b.janani68c55e12016-02-24 12:23:03 +05303 *
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.utils.io.impl;
18
b.janani68c55e12016-02-24 12:23:03 +053019import java.lang.reflect.Constructor;
20import java.lang.reflect.InvocationTargetException;
21
Bharat saraswal6ef0b762016-04-05 12:45:45 +053022import org.junit.Rule;
23import org.junit.Test;
24import org.junit.rules.ExpectedException;
25
26import static org.hamcrest.core.Is.is;
27import static org.hamcrest.core.IsNot.not;
28import static org.junit.Assert.assertThat;
29import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
30import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILDER_CLASS;
31import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILDER_INTERFACE;
32import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILD_METHOD;
33import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.CONSTRUCTOR;
34import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.DEFAULT_CONSTRUCTOR;
35import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
36import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.IMPL_CLASS;
37import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.INTERFACE;
38import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO;
39import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.SETTER_METHOD;
40import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.TYPE_DEF_SETTER_METHOD;
b.janani68c55e12016-02-24 12:23:03 +053041
42/**
43 * Tests the java doc that is generated.
44 */
45public final class JavaDocGenTest {
46
Bharat saraswal6ef0b762016-04-05 12:45:45 +053047 private static final String TEST_NAME = "testName";
48 private static final String END_STRING = " */\n";
49
b.janani68c55e12016-02-24 12:23:03 +053050 @Rule
51 public ExpectedException thrown = ExpectedException.none();
52
53 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +053054 * This test case checks the content received for the builder class java doc.
b.janani68c55e12016-02-24 12:23:03 +053055 */
56 @Test
57 public void builderClassGenerationTest() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053058 String builderClassJavaDoc = getJavaDoc(BUILDER_CLASS, TEST_NAME, false);
Gaurav Agrawal338735b2016-04-18 18:53:11 +053059 assertThat(true, is(builderClassJavaDoc.contains("Represents the builder implementation of")
Bharat saraswal6ef0b762016-04-05 12:45:45 +053060 && builderClassJavaDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +053061 }
62
63 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +053064 * This test case checks the content received for the builder interface ge java doc.
b.janani68c55e12016-02-24 12:23:03 +053065 */
66 @Test
67 public void builderInterfaceGenerationTest() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053068 String builderInterfaceJavaDoc = getJavaDoc(BUILDER_INTERFACE, TEST_NAME, false);
69 assertThat(true,
70 is(builderInterfaceJavaDoc.contains("Builder for") && builderInterfaceJavaDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +053071 }
72
73 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +053074 * This test case checks the content received for the build java doc.
b.janani68c55e12016-02-24 12:23:03 +053075 */
76 @Test
77 public void buildGenerationTest() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053078 String buildDoc = getJavaDoc(BUILD_METHOD, TEST_NAME, false);
79 assertThat(true, is(buildDoc.contains("Builds object of") && buildDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +053080 }
81
82 /**
83 * A private constructor is tested.
84 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053085 * @throws SecurityException if any security violation is observed
86 * @throws NoSuchMethodException if when the method is not found
87 * @throws IllegalArgumentException if there is illegal argument found
88 * @throws InstantiationException if instantiation is provoked for the private constructor
89 * @throws IllegalAccessException if instance is provoked or a method is provoked
90 * @throws InvocationTargetException when an exception occurs by the method or constructor
b.janani68c55e12016-02-24 12:23:03 +053091 */
92 @Test
93 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Bharat saraswal6ef0b762016-04-05 12:45:45 +053094 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani68c55e12016-02-24 12:23:03 +053095
96 Class<?>[] classesToConstruct = {JavaDocGen.class };
97 for (Class<?> clazz : classesToConstruct) {
98 Constructor<?> constructor = clazz.getDeclaredConstructor();
99 constructor.setAccessible(true);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530100 assertThat(null, not(constructor.newInstance()));
b.janani68c55e12016-02-24 12:23:03 +0530101 }
102 }
103
104 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530105 * This test case checks the content received for the constructor java doc.
b.janani68c55e12016-02-24 12:23:03 +0530106 */
107 @Test
108 public void constructorGenerationTest() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530109 String constructorDoc = getJavaDoc(CONSTRUCTOR, TEST_NAME, false);
110 assertThat(true,
111 is(constructorDoc.contains("Creates an instance of ") && constructorDoc.contains("builder object of")
112 && constructorDoc.contains("@param") && constructorDoc.contains("*/\n")));
b.janani68c55e12016-02-24 12:23:03 +0530113 }
114
115 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530116 * This test case checks the content received for the default constructor java doc.
b.janani68c55e12016-02-24 12:23:03 +0530117 */
118 @Test
119 public void defaultConstructorGenerationTest() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530120 String defaultConstructorDoc = getJavaDoc(DEFAULT_CONSTRUCTOR, TEST_NAME, false);
121 assertThat(true, is(defaultConstructorDoc.contains("Creates an instance of ")
122 && defaultConstructorDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +0530123 }
124
125 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530126 * This test case checks the content received for the getter java doc.
b.janani68c55e12016-02-24 12:23:03 +0530127 */
128 @Test
129 public void getterGenerationTest() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530130 String getterJavaDoc = getJavaDoc(GETTER_METHOD, TEST_NAME, false);
131 assertThat(true, is(getterJavaDoc.contains("Returns the attribute") && getterJavaDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +0530132 }
133
134 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530135 * This test case checks the content received for the impl class java doc.
b.janani68c55e12016-02-24 12:23:03 +0530136 */
137 @Test
138 public void implClassGenerationTest() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530139 String implClassJavaDoc = getJavaDoc(IMPL_CLASS, TEST_NAME, false);
140 assertThat(true,
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530141 is(implClassJavaDoc.contains("Represents the implementation of")
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530142 && implClassJavaDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +0530143 }
144
145 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530146 * This test case checks the content received for the interface java doc.
b.janani68c55e12016-02-24 12:23:03 +0530147 */
148 @Test
149 public void interfaceGenerationTest() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530150 String interfaceJavaDoc = getJavaDoc(INTERFACE, TEST_NAME, false);
151 assertThat(true,
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530152 is(interfaceJavaDoc.contains("Abstraction of an entity which represents the functionality of")
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530153 && interfaceJavaDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +0530154 }
155
156 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530157 * This test case checks the content received for the package info java doc.
b.janani68c55e12016-02-24 12:23:03 +0530158 */
159 @Test
160 public void packageInfoGenerationTest() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530161 String packageInfo = getJavaDoc(PACKAGE_INFO, TEST_NAME, false);
Bharat saraswald72411a2016-04-19 01:00:16 +0530162 assertThat(true, is(packageInfo.contains("Implementation of YANG node") && packageInfo.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +0530163 }
164
165 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530166 * This test case checks the content received for the package info java doc.
167 */
168 @Test
169 public void packageInfoGenerationForChildNodeTest() {
170 String packageInfo = getJavaDoc(PACKAGE_INFO, TEST_NAME, true);
171 assertThat(true, is(packageInfo.contains("Implementation of YANG node testName's children nodes")
172 && packageInfo.contains(END_STRING)));
173 }
174
175 /**
176 * This test case checks the content received for the setter java doc.
b.janani68c55e12016-02-24 12:23:03 +0530177 */
178 @Test
179 public void setterGenerationTest() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530180 String setterJavaDoc = getJavaDoc(SETTER_METHOD, TEST_NAME, false);
181 assertThat(true,
182 is(setterJavaDoc.contains("Returns the builder object of") && setterJavaDoc.contains(END_STRING)));
b.janani68c55e12016-02-24 12:23:03 +0530183 }
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530184
185 /**
186 * This test case checks the content received for the typedef setter java doc.
187 */
188 @Test
189 public void typeDefSetterGenerationTest() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530190 String typeDefSetter = getJavaDoc(TYPE_DEF_SETTER_METHOD, TEST_NAME, false);
191 assertThat(true, is(typeDefSetter.contains("Sets the value of") && typeDefSetter.contains(END_STRING)));
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530192 }
b.janani68c55e12016-02-24 12:23:03 +0530193}