Fix mockito deprecation warnings
[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 package org.opendaylight.mdsal.binding.javav2.runtime.javassist;
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.ArgumentMatchers.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
24 public class JavassistUtilsTest {
25
26     @Test
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()));
37
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"));
42
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"));
54
55         final ClassCustomizer classCustomizer = mock(ClassCustomizer.class);
56         doNothing().when(classCustomizer).customizeClass(any());
57         assertNotNull(javassistUtils.instantiatePrototype("javassist.CtNewClass", "leWut", classCustomizer));
58     }
59
60     @Test
61     public void privateConstructTest() throws Exception {
62         assertFalse(JavassistUtils.class.getDeclaredConstructor(ClassPool.class).isAccessible());
63     }
64 }