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