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.Matchers.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;
30 public class SourceCodeGeneratorFactoryTest {
32 private static final SourceCodeGeneratorFactory FACTORY = new SourceCodeGeneratorFactory();
33 private SourceCodeGenerator generator;
34 private String propKey;
37 public void basicFactoryTest() throws Exception {
38 final Field propField = SourceCodeGeneratorFactory.class.getDeclaredField("GENERATE_CODEC_SOURCE_PROP");
39 propField.setAccessible(true);
40 propKey = (String) propField.get(FACTORY);
41 final String propValue;
42 final boolean present;
44 propValue = System.getProperty(propKey, null);
45 present = propValue != null;
47 testWithPropertyPresent();
48 testWithoutPropertyPresent();
51 System.setProperty(propKey, propValue);
53 System.clearProperty(propKey);
57 private void testWithPropertyPresent() throws Exception {
58 System.clearProperty(propKey);
59 System.setProperty(propKey, "true");
60 generator = FACTORY.getInstance(null);
61 assertTrue(generator instanceof DefaultSourceCodeGenerator);
64 private void testWithoutPropertyPresent() throws Exception {
65 System.clearProperty(propKey);
66 generator = FACTORY.getInstance(null);
67 assertTrue(generator instanceof NullSourceCodeGenerator);
71 public void nullSourceCodeGenTest() throws Exception {
72 generator = new NullSourceCodeGenerator();
73 generator.appendField(null, null);
77 public void defaultSourceCodeGenTest() throws Exception {
78 final File dir = new File("testDir");
79 assertTrue(cleanup(dir));
81 generator = new DefaultSourceCodeGenerator(dir.getName());
82 final CtClass ctClass = mock(CtClass.class, CALLS_REAL_METHODS);
83 doReturn(Boolean.FALSE).when(ctClass).isFrozen();
84 ctClass.setName("TestClass");
85 final ClassPool classPool = mock(ClassPool.class);
86 doReturn(ctClass).when(classPool).get((String) any());
87 doNothing().when(ctClass).setName("TestClass");
88 doReturn(ctClass).when(ctClass).getSuperclass();
89 doReturn(new CtClass[] {ctClass,ctClass}).when(ctClass).getInterfaces();
90 doReturn(classPool).when(ctClass).getClassPool();
91 doReturn(Boolean.FALSE).when(ctClass).isArray();
92 doReturn(Boolean.FALSE).when(ctClass).isPrimitive();
93 doReturn(AccessFlag.toModifier(AccessFlag.PUBLIC)).when(ctClass).getModifiers();
94 final ClassFile classFile = new ClassFile(false,"test", null);
95 doReturn(classFile).when(ctClass).getClassFile2();
96 doReturn(Boolean.FALSE).when(ctClass).isFrozen();
97 doReturn("testClass").when(ctClass).getName();
98 final CtField ctField = mock(CtField.class);
99 doReturn(AccessFlag.toModifier(AccessFlag.PUBLIC)).when(ctField).getModifiers();
100 doReturn(ctClass).when(ctField).getType();
101 doReturn("testField").when(ctField).getName();
102 final CtMethod ctMethod = new CtMethod(ctClass,"method", new CtClass[] { ctClass , ctClass }, ctClass);
104 generator.appendField(ctField, "b");
105 generator.appendMethod(ctMethod, "c");
106 generator.outputGeneratedSource(ctClass);
108 assertTrue(dir.exists());
109 assertTrue(dir.isDirectory());
110 assertFalse(ImmutableList.of(dir.listFiles()).isEmpty());
112 assertTrue(cleanup(dir));
115 private static boolean cleanup(final File dir) {
120 stream(dir.listFiles()).forEach(File::delete);