forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestBase.java
More file actions
151 lines (128 loc) · 4.83 KB
/
TestBase.java
File metadata and controls
151 lines (128 loc) · 4.83 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
141
142
143
144
145
146
147
148
149
150
151
package act;
import act.app.ActionContext;
import act.app.App;
import act.app.SingletonRegistry;
import act.conf.AppConfig;
import act.event.EventBus;
import act.job.AppJobManager;
import act.metric.SimpleMetricPlugin;
import act.route.Router;
import act.util.ClassNames;
import org.junit.Assert;
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 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 TestBase extends Assert {
protected void same(Object a, Object b) {
assertSame(a, b);
}
protected void eq(Object[] a1, Object[] a2) {
assertArrayEquals(a1, a2);
}
protected void eq(Object o1, Object o2) {
assertEquals(o1, o2);
}
protected void ne(Object expected, Object actual) {
no($.eq(expected, actual));
}
protected void ceq(Object o1, Object o2) {
assertEquals(S.string(o1), S.string(o2));
}
protected void yes(Boolean expr, String msg, Object... args) {
assertTrue(S.fmt(msg, args), expr);
}
protected void yes(Boolean expr) {
assertTrue(expr);
}
protected void no(Boolean expr, String msg, Object... args) {
assertFalse(S.fmt(msg, args), expr);
}
protected void no(Boolean expr) {
assertFalse(expr);
}
protected void fail(String msg, Object... args) {
assertFalse(S.fmt(msg, args), true);
}
protected static void run(Class<? extends TestBase> 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(TestBase.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 AppJobManager mockJobManager;
protected SingletonRegistry mockSingletonRegistry;
protected App mockApp;
protected EventBus mockEventBus;
protected H.Request mockReq;
protected H.Response 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(AppJobManager.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();
return $.newInstance((Class)args[0]);
}
});
mockReq = mock(H.Request.class);
mockResp = mock(H.Response.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());
}
}