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