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.yangtools.sal.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;
24 public class JavassistUtilsTest {
27 public void forClassPool() throws Exception {
28 final JavassistUtils javassistUtils = JavassistUtils.forClassPool(ClassPool.getDefault());
29 final ClassGenerator classGenerator = mock(ClassGenerator.class);
30 doNothing().when(classGenerator).process(any());
31 final CtClass ctInterface = javassistUtils.createClass("TestInterface", classGenerator);
32 ctInterface.setModifiers(AccessFlag.INTERFACE);
33 final CtClass ctClass = javassistUtils.createClass("TestClass", ctInterface, classGenerator);
34 javassistUtils.ensureClassLoader(ctClass.getClass());
35 assertNotNull(ctClass);
36 assertNotNull(javassistUtils.get(ClassPool.getDefault(), ctClass.getClass()));
38 CtField ctField = javassistUtils.field(ctClass, "testField", Object.class);
39 assertEquals(ctField, ctClass.getField("testField"));
40 ctField = javassistUtils.staticField(ctClass, "testStaticField", Object.class);
41 assertEquals(ctField, ctClass.getField("testStaticField"));
43 final MethodGenerator methodGenerator = mock(MethodGenerator.class);
44 doNothing().when(methodGenerator).process(any());
45 javassistUtils.method(ctClass, Void.class, "testMethod", Object.class, methodGenerator);
46 javassistUtils.method(ctInterface, Void.class, "testInterfaceMethod", Object.class, methodGenerator);
47 javassistUtils.method(ctClass, Void.class, "testMethod2", ImmutableList.of(Object.class), methodGenerator);
48 javassistUtils.staticMethod(ctClass, Void.class, "testStaticMethod", Object.class, methodGenerator);
49 javassistUtils.implementMethodsFrom(ctClass, ctInterface, methodGenerator);
50 assertNotNull(ctClass.getDeclaredMethod("testMethod"));
51 assertNotNull(ctClass.getDeclaredMethod("testMethod2"));
52 assertNotNull(ctClass.getDeclaredMethod("testStaticMethod"));
53 assertNotNull(ctClass.getDeclaredMethod("testInterfaceMethod"));
55 final ClassCustomizer classCustomizer = mock(ClassCustomizer.class);
56 doNothing().when(classCustomizer).customizeClass(any());
57 assertNotNull(javassistUtils.instantiatePrototype("javassist.CtNewClass", "leWut", classCustomizer));
61 public void privateConstructTest() throws Exception {
62 assertFalse(JavassistUtils.class.getDeclaredConstructor(ClassPool.class).isAccessible());