blob: 5ee059cd4f395eda04da628335dd151e21e3ed96 [file] [log] [blame]
b.janani0d4517a2016-02-25 12:25:55 +05301/*
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
17package org.onosproject.yangutils.translator.tojava.utils;
18
Vinod Kumar S08710982016-03-03 19:55:30 +053019import java.lang.reflect.Constructor;
20import java.lang.reflect.InvocationTargetException;
21
b.janani0d4517a2016-02-25 12:25:55 +053022import org.junit.Test;
b.janani0d4517a2016-02-25 12:25:55 +053023import org.onosproject.yangutils.datamodel.YangType;
24import org.onosproject.yangutils.translator.GeneratedFileType;
25import org.onosproject.yangutils.translator.tojava.AttributeInfo;
b.janani0d4517a2016-02-25 12:25:55 +053026
Vinod Kumar S08710982016-03-03 19:55:30 +053027import static org.hamcrest.core.Is.is;
28import static org.junit.Assert.assertNotNull;
29import static org.junit.Assert.assertThat;
b.janani0d4517a2016-02-25 12:25:55 +053030
31/**
32 * Unit tests for generated methods from the file type.
33 */
34public final class MethodsGeneratorTest {
35
36 public static AttributeInfo testAttr = new AttributeInfo();
37 public static YangType<?> attrType = new YangType<>();
38
39 /**
40 * Unit test for private constructor.
41 *
Vinod Kumar S08710982016-03-03 19:55:30 +053042 * @throws SecurityException if any security violation is observed
43 * @throws NoSuchMethodException if when the method is not found
44 * @throws IllegalArgumentException if there is illegal argument found
45 * @throws InstantiationException if instantiation is provoked for the
46 * private constructor
47 * @throws IllegalAccessException if instance is provoked or a method is
48 * provoked
49 * @throws InvocationTargetException when an exception occurs by the method
50 * or constructor
b.janani0d4517a2016-02-25 12:25:55 +053051 */
52 @Test
53 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
Vinod Kumar S08710982016-03-03 19:55:30 +053054 InstantiationException, IllegalAccessException, InvocationTargetException {
b.janani0d4517a2016-02-25 12:25:55 +053055
Vinod Kumar S08710982016-03-03 19:55:30 +053056 Class<?>[] classesToConstruct = {
57 MethodsGenerator.class };
b.janani0d4517a2016-02-25 12:25:55 +053058 for (Class<?> clazz : classesToConstruct) {
59 Constructor<?> constructor = clazz.getDeclaredConstructor();
60 constructor.setAccessible(true);
61 assertNotNull(constructor.newInstance());
62 }
63 }
64
65 /**
Vinod Kumar S08710982016-03-03 19:55:30 +053066 * Unit test for checking the values received from constructor, default
67 * constructor and build string formation.
b.janani0d4517a2016-02-25 12:25:55 +053068 */
69 @Test
70 public void getValuesTest() {
71 String stringConstructor = MethodsGenerator.getConstructorString("testname");
72 assertThat(
73 stringConstructor.contains("Construct the object of testnameImpl.")
Vinod Kumar S08710982016-03-03 19:55:30 +053074 && stringConstructor.contains("@param testnameObject builder object of testname")
75 && stringConstructor.contains("public testnameImpl(testnameBuilder testnameObject) {"),
b.janani0d4517a2016-02-25 12:25:55 +053076 is(true));
Vinod Kumar S08710982016-03-03 19:55:30 +053077 String stringDefaultConstructor = MethodsGenerator.getDefaultConstructorString(
78 GeneratedFileType.BUILDER_CLASS_MASK,
b.janani0d4517a2016-02-25 12:25:55 +053079 "testname");
80 assertThat(stringDefaultConstructor.contains("Default Constructor.")
81 && stringDefaultConstructor.contains("public testnameBuilder() {")
82 && stringDefaultConstructor.contains("}"), is(true));
83 String stringBuild = MethodsGenerator.getBuildString("testname");
84 assertThat(
85 stringBuild.contains("public testname build() {")
Vinod Kumar S08710982016-03-03 19:55:30 +053086 && stringBuild.contains("return new testnameImpl(this);") && stringBuild.contains("}"),
b.janani0d4517a2016-02-25 12:25:55 +053087 is(true));
88 }
89}