Bug 6859 #2 Binding generator v1 refactoring
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / mdsal / binding / generator / util / SourceCodeGeneratorFactoryTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.generator.util;
9
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;
18
19 import com.google.common.collect.ImmutableList;
20 import java.io.File;
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;
33
34 public class SourceCodeGeneratorFactoryTest {
35
36     private static final SourceCodeGeneratorFactory FACTORY = new SourceCodeGeneratorFactory();
37     private SourceCodeGenerator generator;
38     private String propKey;
39
40     @Test
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;
47
48         propValue = System.getProperty(propKey, null);
49         present = propValue != null;
50
51         testWithPropertyPresent();
52         testWithoutPropertyPresent();
53
54         if (present) {
55             System.setProperty(propKey, propValue);
56         } else {
57             System.clearProperty(propKey);
58         }
59     }
60
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);
66     }
67
68     private void testWithoutPropertyPresent() throws Exception {
69         System.clearProperty(propKey);
70         generator = FACTORY.getInstance(null);
71         assertTrue(generator instanceof NullSourceCodeGenerator);
72     }
73
74     @Test
75     public void nullSourceCodeGenTest() throws Exception {
76         generator = new NullSourceCodeGenerator();
77         generator.appendField(null, null);
78     }
79
80     @Test
81     public void defaultSourceCodeGenTest() throws Exception {
82         final File dir = new File("testDir");
83         assertTrue(cleanup(dir));
84
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);
107
108         generator.appendField(ctField, "b");
109         generator.appendMethod(ctMethod, "c");
110         generator.outputGeneratedSource(ctClass);
111
112         assertTrue(dir.exists());
113         assertTrue(dir.isDirectory());
114         assertFalse(ImmutableList.of(dir.listFiles()).isEmpty());
115
116         assertTrue(cleanup(dir));
117     }
118
119     private boolean cleanup(File dir) {
120         if (!dir.exists()) return true;
121
122         stream(dir.listFiles()).forEach(File::delete);
123         return dir.delete();
124     }
125 }