Bug 6859 #3 Binding generator v1 refactoring
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / mdsal / binding / generator / util / JavassistUtilsTest.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 org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.mock;
16
17 import com.google.common.collect.ImmutableList;
18 import javassist.ClassPool;
19 import javassist.CtClass;
20 import javassist.CtField;
21 import javassist.bytecode.AccessFlag;
22 import org.junit.Test;
23 import org.opendaylight.mdsal.binding.generator.util.ClassCustomizer;
24 import org.opendaylight.mdsal.binding.generator.util.ClassGenerator;
25 import org.opendaylight.mdsal.binding.generator.util.JavassistUtils;
26 import org.opendaylight.mdsal.binding.generator.util.MethodGenerator;
27
28 public class JavassistUtilsTest {
29
30     @Test
31     public void forClassPool() throws Exception {
32         final JavassistUtils javassistUtils = JavassistUtils.forClassPool(ClassPool.getDefault());
33         final ClassGenerator classGenerator = mock(ClassGenerator.class);
34         doNothing().when(classGenerator).process(any());
35         final CtClass ctInterface = javassistUtils.createClass("TestInterface", classGenerator);
36         ctInterface.setModifiers(AccessFlag.INTERFACE);
37         final CtClass ctClass = javassistUtils.createClass("TestClass", ctInterface, classGenerator);
38         javassistUtils.ensureClassLoader(ctClass.getClass());
39         assertNotNull(ctClass);
40         assertNotNull(javassistUtils.get(ClassPool.getDefault(), ctClass.getClass()));
41
42         CtField ctField = javassistUtils.field(ctClass, "testField", Object.class);
43         assertEquals(ctField, ctClass.getField("testField"));
44         ctField = javassistUtils.staticField(ctClass, "testStaticField", Object.class);
45         assertEquals(ctField, ctClass.getField("testStaticField"));
46
47         final MethodGenerator methodGenerator = mock(MethodGenerator.class);
48         doNothing().when(methodGenerator).process(any());
49         javassistUtils.method(ctClass, Void.class, "testMethod", Object.class, methodGenerator);
50         javassistUtils.method(ctInterface, Void.class, "testInterfaceMethod", Object.class, methodGenerator);
51         javassistUtils.method(ctClass, Void.class, "testMethod2", ImmutableList.of(Object.class), methodGenerator);
52         javassistUtils.staticMethod(ctClass, Void.class, "testStaticMethod", Object.class, methodGenerator);
53         javassistUtils.implementMethodsFrom(ctClass, ctInterface, methodGenerator);
54         assertNotNull(ctClass.getDeclaredMethod("testMethod"));
55         assertNotNull(ctClass.getDeclaredMethod("testMethod2"));
56         assertNotNull(ctClass.getDeclaredMethod("testStaticMethod"));
57         assertNotNull(ctClass.getDeclaredMethod("testInterfaceMethod"));
58
59         final ClassCustomizer classCustomizer = mock(ClassCustomizer.class);
60         doNothing().when(classCustomizer).customizeClass(any());
61         assertNotNull(javassistUtils.instantiatePrototype("javassist.CtNewClass", "leWut", classCustomizer));
62     }
63
64     @Test
65     public void privateConstructTest() throws Exception {
66         assertFalse(JavassistUtils.class.getDeclaredConstructor(ClassPool.class).isAccessible());
67     }
68 }