-
Notifications
You must be signed in to change notification settings - Fork 2
/
NukkitExtension.java
151 lines (133 loc) · 5.73 KB
/
NukkitExtension.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
This file is part of "nukkit xWorlds test tools".
"nukkit xWorlds test tools" is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
"nukkit xWorlds test tools" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with "nukkit xWorlds test tools". If not, see <http://www.gnu.org/licenses/>.
*/
package eu.xworlds.nukkit.test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Parameter;
import org.junit.gen5.api.extension.AfterEachExtensionPoint;
import org.junit.gen5.api.extension.BeforeEachExtensionPoint;
import org.junit.gen5.api.extension.ExtensionContext;
import org.junit.gen5.api.extension.ExtensionContext.Namespace;
import org.junit.gen5.api.extension.ExtensionContext.Store;
import org.junit.gen5.api.extension.InstancePostProcessor;
import org.junit.gen5.api.extension.MethodInvocationContext;
import org.junit.gen5.api.extension.MethodParameterResolver;
import org.junit.gen5.api.extension.ParameterResolutionException;
import org.junit.gen5.api.extension.TestExtensionContext;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import cn.nukkit.Server;
import cn.nukkit.command.CommandReader;
import cn.nukkit.scheduler.ServerScheduler;
import eu.xworlds.nukkit.test.api.NukkitInject;
import eu.xworlds.nukkit.test.sample.PowermockExtension;
import eu.xworlds.nukkit.test.sample.PowermockState;
import jline.console.ConsoleReader;
/**
* JUnit5 Extension for nukkit
*
* @author mepeisen
*/
public class NukkitExtension implements InstancePostProcessor, MethodParameterResolver, AfterEachExtensionPoint, BeforeEachExtensionPoint
{
/** the extension namespace */
private static final Namespace NS = Namespace.of(NukkitExtension.class);
@Override
public void postProcessTestInstance(TestExtensionContext context) throws Exception
{
final PowermockState powermock = PowermockExtension.getState(context);
powermock.getClassesToPrepare().add(Server.class);
powermock.getClassesToPrepare().add(ConsoleReader.class);
powermock.getClassesToPrepare().add(CommandReader.class);
powermock.getClassesToPrepare().add(ServerScheduler.class);
powermock.getPackagesToIgnore().add("org.mockito.*");
}
/**
* @see org.junit.gen5.api.extension.AfterEachExtensionPoint#afterEach(org.junit.gen5.api.extension.TestExtensionContext)
*/
@Override
public void afterEach(TestExtensionContext context) throws Exception
{
// currently empty
}
/**
* @see org.junit.gen5.api.extension.BeforeEachExtensionPoint#beforeEach(org.junit.gen5.api.extension.TestExtensionContext)
*/
@Override
public void beforeEach(TestExtensionContext context) throws Exception
{
// mokito support
// final Class<?> mockitoClazz = context.getTestInstance().getClass().getClassLoader().loadClass(MockitoAnnotations.class.getName());
// final Method initMocksMethod = mockitoClazz.getDeclaredMethod("initMocks", Object.class);
// initMocksMethod.invoke(null, context.getTestInstance());
MockitoAnnotations.initMocks(context.getTestInstance());
}
@Override
public Object resolve(Parameter param, MethodInvocationContext methodInvocationContext, ExtensionContext extensionContext) throws ParameterResolutionException
{
final Store mocks = extensionContext.getStore(NS);
return getMockWithoutCast(extensionContext, param.getType(), mocks, methodInvocationContext.getInstance().getClass().getClassLoader());
}
@Override
public boolean supports(Parameter param, MethodInvocationContext methodInvocationContext, ExtensionContext extensionContext) throws ParameterResolutionException
{
for (final Annotation annot : param.getAnnotations())
{
if (annot.annotationType().getName().equals(NukkitInject.class.getName()))
return true;
}
return false;
}
/**
* Calculates the mocking class
*
* @param extensionContext
* @param mockType
* @param mocks
* @param loader
* @return mocking class
*/
private Object getMockWithoutCast(ExtensionContext extensionContext, Class<?> mockType, Store mocks, ClassLoader loader)
{
return mocks.getOrComputeIfAbsent(mockType, type -> mock(extensionContext, mockType, mocks, loader));
}
/**
* Creates a mock
*
* @param extensionContext
* @param mockType
* @param mocks
* @param loader
* @return mocking class
*/
private Object mock(ExtensionContext extensionContext, Class<?> mockType, Store mocks, ClassLoader loader)
{
try
{
if (mockType.getName().equals(NukkitTestSession.class.getName()))
{
return loader.loadClass(NukkitTestSession.class.getName()).newInstance();
}
// TODO support additional nukkit classes
// fall back to mockito
// final Class<?> mockitoClazz = loader.loadClass(Mockito.class.getName());
// final Method mockMethod = mockitoClazz.getDeclaredMethod("mock", Class.class);
// return mockMethod.invoke(null, mockType);
return Mockito.mock(mockType);
}
catch (Exception e)
{
throw new IllegalStateException(e);
}
}
}