Cleanup JavassistUtils
[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.CannotCompileException;
19 import javassist.ClassPool;
20 import javassist.CtClass;
21 import javassist.CtField;
22 import javassist.NotFoundException;
23 import javassist.bytecode.AccessFlag;
24 import org.junit.Test;
25
26 public class JavassistUtilsTest {
27
28     @Test
29     public void forClassPool() throws CannotCompileException, NotFoundException {
30         final JavassistUtils javassistUtils = JavassistUtils.forClassPool(ClassPool.getDefault());
31         final ClassGenerator classGenerator = mock(ClassGenerator.class);
32         doNothing().when(classGenerator).process(any());
33         final CtClass ctInterface = javassistUtils.createClass("TestInterface", classGenerator);
34         ctInterface.setModifiers(AccessFlag.INTERFACE);
35         final CtClass ctClass = javassistUtils.createClass("TestClass", ctInterface, classGenerator);
36         javassistUtils.ensureClassLoader(ctClass.getClass());
37         assertNotNull(ctClass);
38         assertNotNull(javassistUtils.get(ClassPool.getDefault(), ctClass.getClass()));
39
40         CtField ctField = javassistUtils.field(ctClass, "testField", Object.class);
41         assertEquals(ctField, ctClass.getField("testField"));
42         ctField = javassistUtils.staticField(ctClass, "testStaticField", Object.class);
43         assertEquals(ctField, ctClass.getField("testStaticField"));
44
45         final MethodGenerator methodGenerator = mock(MethodGenerator.class);
46         doNothing().when(methodGenerator).process(any());
47         javassistUtils.method(ctClass, Void.class, "testMethod", Object.class, methodGenerator);
48         javassistUtils.method(ctInterface, Void.class, "testInterfaceMethod", Object.class, methodGenerator);
49         javassistUtils.method(ctClass, Void.class, "testMethod2", ImmutableList.of(Object.class), methodGenerator);
50         javassistUtils.staticMethod(ctClass, Void.class, "testStaticMethod", Object.class, methodGenerator);
51         javassistUtils.implementMethodsFrom(ctClass, ctInterface, methodGenerator);
52         assertNotNull(ctClass.getDeclaredMethod("testMethod"));
53         assertNotNull(ctClass.getDeclaredMethod("testMethod2"));
54         assertNotNull(ctClass.getDeclaredMethod("testStaticMethod"));
55         assertNotNull(ctClass.getDeclaredMethod("testInterfaceMethod"));
56
57         final ClassCustomizer classCustomizer = mock(ClassCustomizer.class);
58         doNothing().when(classCustomizer).customizeClass(any());
59         assertNotNull(javassistUtils.instantiatePrototype("javassist.CtNewClass", "leWut", classCustomizer));
60     }
61
62     @Test
63     public void privateConstructTest() throws Exception {
64         assertFalse(JavassistUtils.class.getDeclaredConstructor(ClassPool.class).isAccessible());
65     }
66 }