2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.generator.util;
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;
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;
28 public class JavassistUtilsTest {
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()));
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"));
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"));
59 final ClassCustomizer classCustomizer = mock(ClassCustomizer.class);
60 doNothing().when(classCustomizer).customizeClass(any());
61 assertNotNull(javassistUtils.instantiatePrototype("javassist.CtNewClass", "leWut", classCustomizer));
65 public void privateConstructTest() throws Exception {
66 assertFalse(JavassistUtils.class.getDeclaredConstructor(ClassPool.class).isAccessible());