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.mdsal.binding.generator.util;
10 import static java.util.Arrays.stream;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.Mockito.CALLS_REAL_METHODS;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
19 import com.google.common.collect.ImmutableList;
21 import java.lang.reflect.Field;
22 import javassist.ClassPool;
23 import javassist.CtClass;
24 import javassist.CtField;
25 import javassist.CtMethod;
26 import javassist.bytecode.AccessFlag;
27 import javassist.bytecode.ClassFile;
28 import org.junit.Test;
31 public class SourceCodeGeneratorFactoryTest {
33 private static final SourceCodeGeneratorFactory FACTORY = new SourceCodeGeneratorFactory();
34 private SourceCodeGenerator generator;
35 private String propKey;
38 public void basicFactoryTest() throws Exception {
39 final Field propField = SourceCodeGeneratorFactory.class.getDeclaredField("GENERATE_CODEC_SOURCE_PROP");
40 propField.setAccessible(true);
41 propKey = (String) propField.get(FACTORY);
42 final String propValue;
43 final boolean present;
45 propValue = System.getProperty(propKey, null);
46 present = propValue != null;
48 testWithPropertyPresent();
49 testWithoutPropertyPresent();
52 System.setProperty(propKey, propValue);
54 System.clearProperty(propKey);
58 private void testWithPropertyPresent() throws Exception {
59 System.clearProperty(propKey);
60 System.setProperty(propKey, "true");
61 generator = FACTORY.getInstance(null);
62 assertTrue(generator instanceof DefaultSourceCodeGenerator);
65 private void testWithoutPropertyPresent() throws Exception {
66 System.clearProperty(propKey);
67 generator = FACTORY.getInstance(null);
68 assertTrue(generator instanceof NullSourceCodeGenerator);
72 public void nullSourceCodeGenTest() throws Exception {
73 generator = new NullSourceCodeGenerator();
74 generator.appendField(null, null);
78 public void defaultSourceCodeGenTest() throws Exception {
79 final File dir = new File("testDir");
80 assertTrue(cleanup(dir));
82 generator = new DefaultSourceCodeGenerator(dir.getName());
83 final CtClass ctClass = mock(CtClass.class, CALLS_REAL_METHODS);
84 doReturn(Boolean.FALSE).when(ctClass).isFrozen();
85 ctClass.setName("TestClass");
86 final ClassPool classPool = mock(ClassPool.class);
87 doReturn(ctClass).when(classPool).get((String) any());
88 doNothing().when(ctClass).setName("TestClass");
89 doReturn(ctClass).when(ctClass).getSuperclass();
90 doReturn(new CtClass[] {ctClass,ctClass}).when(ctClass).getInterfaces();
91 doReturn(classPool).when(ctClass).getClassPool();
92 doReturn(Boolean.FALSE).when(ctClass).isArray();
93 doReturn(Boolean.FALSE).when(ctClass).isPrimitive();
94 doReturn(AccessFlag.toModifier(AccessFlag.PUBLIC)).when(ctClass).getModifiers();
95 final ClassFile classFile = new ClassFile(false,"test", null);
96 doReturn(classFile).when(ctClass).getClassFile2();
97 doReturn(Boolean.FALSE).when(ctClass).isFrozen();
98 doReturn("testClass").when(ctClass).getName();
99 final CtField ctField = mock(CtField.class);
100 doReturn(AccessFlag.toModifier(AccessFlag.PUBLIC)).when(ctField).getModifiers();
101 doReturn(ctClass).when(ctField).getType();
102 doReturn("testField").when(ctField).getName();
103 final CtMethod ctMethod = new CtMethod(ctClass,"method", new CtClass[] { ctClass , ctClass }, ctClass);
105 generator.appendField(ctField, "b");
106 generator.appendMethod(ctMethod, "c");
107 generator.outputGeneratedSource(ctClass);
109 assertTrue(dir.exists());
110 assertTrue(dir.isDirectory());
111 assertFalse(ImmutableList.of(dir.listFiles()).isEmpty());
113 assertTrue(cleanup(dir));
116 private static boolean cleanup(final File dir) {
121 stream(dir.listFiles()).forEach(File::delete);