forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActTestBase.java
More file actions
140 lines (125 loc) · 4.86 KB
/
ActTestBase.java
File metadata and controls
140 lines (125 loc) · 4.86 KB
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
package act;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.app.ActionContext;
import act.app.App;
import act.app.SingletonRegistry;
import act.conf.AppConfig;
import act.event.EventBus;
import act.job.JobManager;
import act.metric.SimpleMetricPlugin;
import act.route.Router;
import act.session.SessionManager;
import act.util.ClassNames;
import org.junit.Ignore;
import org.junit.runner.JUnitCore;
import org.mockito.Matchers;
import org.mockito.internal.matchers.StartsWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.osgl.$;
import org.osgl.http.H;
import org.osgl.util.FastStr;
import org.osgl.util.IO;
import org.osgl.util.S;
import osgl.ut.TestBase;
import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Field;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@Ignore
public class ActTestBase extends TestBase {
protected void ceq(Object o1, Object o2) {
assertEquals(S.string(o1), S.string(o2));
}
protected static void run(Class<? extends ActTestBase> cls) {
new JUnitCore().run(cls);
}
protected static void println(String tmpl, Object... args) {
System.out.println(String.format(tmpl, args));
}
protected static <T> T fieldVal(Object entity, String field) {
return $.getProperty(entity, field);
}
public static File root() {
FastStr fs = FastStr.of(ActTestBase.class.getClassLoader().getResource("routes").getPath());
FastStr classRoot = fs.beforeLast("/");
FastStr target = classRoot.beforeLast("/");
return new File(target.toString());
}
protected Router mockRouter;
protected ActionContext mockActionContext;
protected AppConfig mockAppConfig;
protected JobManager mockJobManager;
protected SingletonRegistry mockSingletonRegistry;
protected App mockApp;
protected EventBus mockEventBus;
protected H.Request mockReq;
protected ActResponse mockResp;
protected void setup() throws Exception {
initActMetricPlugin();
mockApp = mock(App.class);
Field f = App.class.getDeclaredField("INST");
f.setAccessible(true);
f.set(null, mockApp);
mockSingletonRegistry = mock(SingletonRegistry.class);
//when(mockApp.singletonRegistry()).thenReturn(mockSingletonRegistry);
mockJobManager = mock(JobManager.class);
when(mockApp.jobManager()).thenReturn(mockJobManager);
mockEventBus = mock(EventBus.class);
when(mockApp.eventBus()).thenReturn(mockEventBus);
mockAppConfig = mock(AppConfig.class);
when(mockAppConfig.possibleControllerClass(argThat(new StartsWith("testapp.controller.")))).thenReturn(true);
mockActionContext = mock(ActionContext.class);
when(mockActionContext.app()).thenReturn(mockApp);
when(mockActionContext.config()).thenReturn(mockAppConfig);
mockRouter = mock(Router.class);
when(mockApp.config()).thenReturn(mockAppConfig);
when(mockApp.router()).thenReturn(mockRouter);
when(mockApp.router(Matchers.same(""))).thenReturn(mockRouter);
when(mockApp.getInstance(any(Class.class))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
Class<?> cls = (Class) args[0];
if (SessionManager.class == cls) {
return new SessionManager(mockAppConfig);
}
return $.newInstance((Class)args[0]);
}
});
mockReq = mock(H.Request.class);
mockResp = mock(ActResponse.class);
when(mockReq.method()).thenReturn(H.Method.GET);
}
protected byte[] loadBytecode(String className) {
String fileName = ClassNames.classNameToClassFileName(className);
InputStream is = getClass().getResourceAsStream(fileName);
return IO.readContent(is);
}
private void initActMetricPlugin() throws Exception {
Field f = Act.class.getDeclaredField("metricPlugin");
f.setAccessible(true);
f.set(null, new SimpleMetricPlugin());
}
}