eb061a430443df27d283031e912ac22932074d3b
[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
30 public class SourceCodeGeneratorFactoryTest {
31
32     private static final SourceCodeGeneratorFactory FACTORY = new SourceCodeGeneratorFactory();
33     private SourceCodeGenerator generator;
34     private String propKey;
35
36     @Test
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;
43
44         propValue = System.getProperty(propKey, null);
45         present = propValue != null;
46
47         testWithPropertyPresent();
48         testWithoutPropertyPresent();
49
50         if (present) {
51             System.setProperty(propKey, propValue);
52         } else {
53             System.clearProperty(propKey);
54         }
55     }
56
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);
62     }
63
64     private void testWithoutPropertyPresent() throws Exception {
65         System.clearProperty(propKey);
66         generator = FACTORY.getInstance(null);
67         assertTrue(generator instanceof NullSourceCodeGenerator);
68     }
69
70     @Test
71     public void nullSourceCodeGenTest() throws Exception {
72         generator = new NullSourceCodeGenerator();
73         generator.appendField(null, null);
74     }
75
76     @Test
77     public void defaultSourceCodeGenTest() throws Exception {
78         final File dir = new File("testDir");
79         assertTrue(cleanup(dir));
80
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);
103
104         generator.appendField(ctField, "b");
105         generator.appendMethod(ctMethod, "c");
106         generator.outputGeneratedSource(ctClass);
107
108         assertTrue(dir.exists());
109         assertTrue(dir.isDirectory());
110         assertFalse(ImmutableList.of(dir.listFiles()).isEmpty());
111
112         assertTrue(cleanup(dir));
113     }
114
115     private static boolean cleanup(final File dir) {
116         if (!dir.exists()) {
117             return true;
118         }
119
120         stream(dir.listFiles()).forEach(File::delete);
121         return dir.delete();
122     }
123 }