2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.sal.java.api.generator;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.spy;
16 import java.lang.reflect.Field;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
22 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
23 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;
24 import org.opendaylight.yangtools.sal.binding.model.api.Type;
26 @SuppressWarnings("deprecation")
27 public class BuilderGeneratorTest {
29 private static final String PROPERTIES_FIELD_NAME = "properties";
30 private final String firstPartOfGenToStringMethod =
31 "@Override\npublic java.lang.String toString() {\n java.lang.StringBuilder builder = new java.lang.StringBuilder ("
33 private final String lastPartOfGenToStringMethode = "\n return builder.append(']').toString();\n}";
34 private final String argPartOfGenToStringMethode =
35 "\n builder.append(\"augmentation=\");\n builder.append(augmentation.values());";
36 private final String comaPartOfAppend = "builder.append(\", \");";
37 private final String TEST = "test";
40 public void basicTest() throws Exception {
41 assertEquals("", new BuilderGenerator().generate(mock(Type.class)));
45 public void builderTemplateGenerateToStringWithPropertyTest() throws Exception {
46 final GeneratedType genType = mockGenType("get" + this.TEST);
47 final String generateToString = genToString(genType).toString();
48 assertTrue(generateToString.contains(this.firstPartOfGenToStringMethod
49 + "\n if (_test != null) {\n builder.append(\"_test=\");\n builder.append(_test);\n }"
50 + this.lastPartOfGenToStringMethode));
54 public void builderTemplateGenerateToStringWithoutAnyPropertyTest() throws Exception {
55 final GeneratedType genType = mockGenType(this.TEST);
56 final String generateToString = genToString(genType).toString();
57 assertTrue(generateToString
58 .contains(this.firstPartOfGenToStringMethod + this.lastPartOfGenToStringMethode));
62 public void builderTemplateGenerateToStringWithMorePropertiesTest() throws Exception {
63 final GeneratedType genType = mockGenTypeMoreMeth("get" + this.TEST);
64 final String generateToString = genToString(genType).toString();
65 assertTrue(generateToString.contains(this.firstPartOfGenToStringMethod
66 + "\n if (_test1 != null) {\n builder.append(\"_test1=\");\n builder.append(_test1);"
67 + "\n " + this.comaPartOfAppend + "\n }"
68 + "\n if (_test2 != null) {\n builder.append(\"_test2=\");\n builder.append(_test2);\n }"
69 + this.lastPartOfGenToStringMethode));
73 public void builderTemplateGenerateToStringWithoutPropertyWithAugmentTest() throws Exception {
74 final GeneratedType genType = mockGenType(this.TEST);
76 final String generateToString = genToString(genType).toString();
77 assertTrue(generateToString.contains(this.firstPartOfGenToStringMethod + this.argPartOfGenToStringMethode
78 + this.lastPartOfGenToStringMethode));
82 public void builderTemplateGenerateToStringWithPropertyWithAugmentTest() throws Exception {
83 final GeneratedType genType = mockGenType("get" + this.TEST);
85 final String generateToString = genToString(genType).toString();
86 assertTrue(generateToString.contains(this.firstPartOfGenToStringMethod
87 + "\n if (_test != null) {\n builder.append(\"_test=\");\n builder.append(_test);\n }"
88 + "\n " + this.comaPartOfAppend + this.argPartOfGenToStringMethode
89 + this.lastPartOfGenToStringMethode));
93 public void builderTemplateGenerateToStringWithMorePropertiesWithAugmentTest() throws Exception {
94 final GeneratedType genType = mockGenTypeMoreMeth("get" + this.TEST);
96 final String generateToString = genToString(genType).toString();
97 assertTrue(generateToString.contains(this.firstPartOfGenToStringMethod
98 + "\n if (_test1 != null) {\n builder.append(\"_test1=\");\n builder.append(_test1);\n "
99 + this.comaPartOfAppend + "\n }"
100 + "\n if (_test2 != null) {\n builder.append(\"_test2=\");\n builder.append(_test2);\n }"
101 + "\n " + this.comaPartOfAppend
102 + this.argPartOfGenToStringMethode + this.lastPartOfGenToStringMethode));
105 private void mockAugment(final GeneratedType genType) {
106 final List<Type> impls = new ArrayList<>();
107 final Type impl = mock(Type.class);
108 doReturn("org.opendaylight.yangtools.yang.binding.Augmentable").when(impl).getFullyQualifiedName();
110 doReturn(impls).when(genType).getImplements();
113 private GeneratedType mockGenTypeMoreMeth(final String methodeName) {
114 final GeneratedType genType = spy(GeneratedType.class);
115 doReturn(this.TEST).when(genType).getName();
116 doReturn(this.TEST).when(genType).getPackageName();
118 final List<MethodSignature> listMethodSign = new ArrayList<>();
119 for (int i = 0; i < 2; i++) {
120 final MethodSignature methSign = mockMethSign(methodeName + (i + 1));
121 listMethodSign.add(methSign);
123 doReturn(listMethodSign).when(genType).getMethodDefinitions();
125 final List<Type> impls = new ArrayList<>();
126 doReturn(impls).when(genType).getImplements();
130 @SuppressWarnings("unchecked")
131 private CharSequence genToString(final GeneratedType genType)
132 throws NoSuchFieldException, IllegalAccessException {
133 final BuilderTemplate bt = new BuilderTemplate(genType);
134 final Field propertiesField = bt.getClass().getDeclaredField(PROPERTIES_FIELD_NAME);
135 propertiesField.setAccessible(true);
136 return bt.generateToString((Collection<GeneratedProperty>) propertiesField.get(bt));
139 private GeneratedType mockGenType(final String methodeName) {
140 final GeneratedType genType = spy(GeneratedType.class);
141 doReturn(this.TEST).when(genType).getName();
142 doReturn(this.TEST).when(genType).getPackageName();
144 final List<MethodSignature> listMethodSign = new ArrayList<>();
145 final MethodSignature methSign = mockMethSign(methodeName);
146 listMethodSign.add(methSign);
147 doReturn(listMethodSign).when(genType).getMethodDefinitions();
149 final List<Type> impls = new ArrayList<>();
150 doReturn(impls).when(genType).getImplements();
154 private MethodSignature mockMethSign(final String methodeName) {
155 final MethodSignature methSign = mock(MethodSignature.class);
156 doReturn(methodeName).when(methSign).getName();
157 final Type methType = mock(Type.class);
158 doReturn(this.TEST).when(methType).getName();
159 doReturn(this.TEST).when(methType).getPackageName();
160 doReturn(methType).when(methSign).getReturnType();