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;
29 import org.opendaylight.mdsal.binding.generator.util.DefaultSourceCodeGenerator;
30 import org.opendaylight.mdsal.binding.generator.util.NullSourceCodeGenerator;
31 import org.opendaylight.mdsal.binding.generator.util.SourceCodeGenerator;
32 import org.opendaylight.mdsal.binding.generator.util.SourceCodeGeneratorFactory;
34 public class SourceCodeGeneratorFactoryTest {
36 private static final SourceCodeGeneratorFactory FACTORY = new SourceCodeGeneratorFactory();
37 private SourceCodeGenerator generator;
38 private String propKey;
41 public void basicFactoryTest() throws Exception {
42 final Field propField = SourceCodeGeneratorFactory.class.getDeclaredField("GENERATE_CODEC_SOURCE_PROP");
43 propField.setAccessible(true);
44 propKey = (String) propField.get(FACTORY);
45 final String propValue;
46 final boolean present;
48 propValue = System.getProperty(propKey, null);
49 present = propValue != null;
51 testWithPropertyPresent();
52 testWithoutPropertyPresent();
55 System.setProperty(propKey, propValue);
57 System.clearProperty(propKey);
61 private void testWithPropertyPresent() throws Exception {
62 System.clearProperty(propKey);
63 System.setProperty(propKey, "true");
64 generator = FACTORY.getInstance(null);
65 assertTrue(generator instanceof DefaultSourceCodeGenerator);
68 private void testWithoutPropertyPresent() throws Exception {
69 System.clearProperty(propKey);
70 generator = FACTORY.getInstance(null);
71 assertTrue(generator instanceof NullSourceCodeGenerator);
75 public void nullSourceCodeGenTest() throws Exception {
76 generator = new NullSourceCodeGenerator();
77 generator.appendField(null, null);
81 public void defaultSourceCodeGenTest() throws Exception {
82 final File dir = new File("testDir");
83 assertTrue(cleanup(dir));
85 generator = new DefaultSourceCodeGenerator(dir.getName());
86 final CtClass ctClass = mock(CtClass.class, CALLS_REAL_METHODS);
87 doReturn(false).when(ctClass).isFrozen();
88 ctClass.setName("TestClass");
89 final ClassPool classPool = mock(ClassPool.class);
90 doReturn(ctClass).when(classPool).get((String) any());
91 doNothing().when(ctClass).setName("TestClass");
92 doReturn(ctClass).when(ctClass).getSuperclass();
93 doReturn(new CtClass[] {ctClass,ctClass}).when(ctClass).getInterfaces();
94 doReturn(classPool).when(ctClass).getClassPool();
95 doReturn(false).when(ctClass).isArray();
96 doReturn(false).when(ctClass).isPrimitive();
97 doReturn(AccessFlag.toModifier(AccessFlag.PUBLIC)).when(ctClass).getModifiers();
98 final ClassFile classFile = new ClassFile(false,"test", null);
99 doReturn(classFile).when(ctClass).getClassFile2();
100 doReturn(false).when(ctClass).isFrozen();
101 doReturn("testClass").when(ctClass).getName();
102 final CtField ctField = mock(CtField.class);
103 doReturn(AccessFlag.toModifier(AccessFlag.PUBLIC)).when(ctField).getModifiers();
104 doReturn(ctClass).when(ctField).getType();
105 doReturn("testField").when(ctField).getName();
106 final CtMethod ctMethod = new CtMethod(ctClass,"method", new CtClass[] { ctClass , ctClass }, ctClass);
108 generator.appendField(ctField, "b");
109 generator.appendMethod(ctMethod, "c");
110 generator.outputGeneratedSource(ctClass);
112 assertTrue(dir.exists());
113 assertTrue(dir.isDirectory());
114 assertFalse(ImmutableList.of(dir.listFiles()).isEmpty());
116 assertTrue(cleanup(dir));
119 private boolean cleanup(File dir) {
120 if (!dir.exists()) return true;
122 stream(dir.listFiles()).forEach(File::delete);