forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoConfigPluginTest.java
More file actions
120 lines (92 loc) · 3.99 KB
/
AutoConfigPluginTest.java
File metadata and controls
120 lines (92 loc) · 3.99 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
package act.conf;
import act.Act;
import act.TestBase;
import act.app.conf.AutoConfig;
import act.app.conf.AutoConfigPlugin;
import act.app.data.StringValueResolverManager;
import act.inject.genie.GenieInjector;
import act.plugin.GenericPluginManager;
import org.hamcrest.Description;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentMatcher;
import org.osgl.$;
import org.osgl.http.H;
import org.osgl.util.C;
import org.osgl.util.Const;
import java.lang.reflect.Field;
import java.util.List;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class AutoConfigPluginTest extends TestBase {
private String intVal = "37";
private String intListVal = "37,23";
private long longVal = 21345315436L;
private boolean boolVal = true;
private String stringVal = $.randomStr();
private H.Method enumVal = H.Method.CONNECT;
private StringValueResolverManager resolverManager;
private GenieInjector injector;
@Before
public void before() throws Exception {
setup();
resolverManager = new StringValueResolverManager(mockApp);
when(mockApp.resolverManager()).thenReturn(resolverManager);
injector = new GenieInjector(mockApp);
when(mockApp.injector()).thenReturn(injector);
GenericPluginManager pluginManager = mock(GenericPluginManager.class);
Field field = Act.class.getDeclaredField("pluginManager");
field.setAccessible(true);
field.set(null, pluginManager);
when(mockAppConfig.getIgnoreCase(argThat(new ContainsIgnoreCase(".int")))).thenReturn(intVal);
when(mockAppConfig.getIgnoreCase(argThat(new ContainsIgnoreCase(".long")))).thenReturn(longVal);
when(mockAppConfig.getIgnoreCase(argThat(new ContainsIgnoreCase(".bool")))).thenReturn(boolVal);
when(mockAppConfig.getIgnoreCase(argThat(new ContainsIgnoreCase(".str")))).thenReturn(stringVal);
when(mockAppConfig.getIgnoreCase(argThat(new ContainsIgnoreCase(".method")))).thenReturn(enumVal);
when(mockAppConfig.getIgnoreCase(argThat(new ContainsIgnoreCase(".list")))).thenReturn(intListVal);
}
@Test
public void testProvisionConfigData() {
AutoConfigPlugin.loadPluginAutoConfig(Foo.class, mockApp);
eq(Integer.parseInt(intVal), Foo.foo_int);
eq(Integer.parseInt(intVal), Foo.foo_int_val.get());
eq(longVal, Foo.foo_long);
eq(longVal, Foo.foo_long_val.get());
eq(boolVal, Foo.foo_bool);
eq(boolVal, Foo.foo_bool_val.get());
eq(stringVal, Foo.foo_str);
eq(stringVal, Foo.FOO_STR_VAL.get());
eq(enumVal, Foo.foo_method);
eq(enumVal, Foo.FOO_METHOD_VAL.get());
ceq(C.listOf(37, 23), Foo.foo_list);
ceq(C.listOf(37, 23), Foo.FOO_LIST.get());
}
@AutoConfig
private static class Foo {
static int foo_int;
static final Const<Integer> foo_int_val = $.constant(0);
static long foo_long;
static final Const<Long> foo_long_val = $.constant(0L);
static boolean foo_bool;
static final Const<Boolean> foo_bool_val = $.constant(false);
static String foo_str;
static final Const<String> FOO_STR_VAL = $.constant("UNIX");
static H.Method foo_method;
static final $.Val<H.Method> FOO_METHOD_VAL = $.val(H.Method.GET);
static List<Integer> foo_list;
static final Const<List<Integer>> FOO_LIST = $.constant(null);
}
private class ContainsIgnoreCase extends ArgumentMatcher<String> {
private final String substring;
ContainsIgnoreCase(String substring) {
this.substring = substring;
}
public boolean matches(Object actual) {
return actual != null && ((String) actual).toUpperCase().contains(substring.toUpperCase());
}
public void describeTo(Description description) {
description.appendText("containsIgnoreCase(\"" + substring + "\")");
}
}
}