forked from markmandel/JavaLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicProxyTest.cfc
More file actions
250 lines (167 loc) · 8.31 KB
/
Copy pathDynamicProxyTest.cfc
File metadata and controls
250 lines (167 loc) · 8.31 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<cfcomponent extends="unittests.AbstractTestCase" output="false">
<!------------------------------------------- PUBLIC ------------------------------------------->
<cffunction name="setup" hint="setup function" access="public" returntype="void" output="false">
<cfscript>
var local = {};
super.setup();
clean();
local.libpaths = [];
ArrayAppend(local.libpaths, expandPath("/javaloader/support/cfcdynamicproxy/lib/cfcdynamicproxy.jar"));
local.srcparths = [instance.srcPath & "/dynamicproxy"];
instance.loader = createObject("component", "javaloader.JavaLoader").init(loadPaths=local.libpaths, loadColdFusionClassPath=true, sourceDirectories=local.srcparths);
</cfscript>
</cffunction>
<cffunction name="simpleCFCTest" hint="test to create a CFC dynamic proxy, and call it from within CF" access="public" returntype="void" output="false">
<cfscript>
var local = {};
local.cfcProxyTest = instance.loader.create("ut2.CFCProxyTest").init();
local.bar = createObject("component", "cfc.SimpleBean");
local.proxy = local.cfcProxyTest.getDynamicProxy(local.bar);
assertEquals("Hello!", local.proxy.foo("Hello!"));
assertEquals("I could not find my method: doesNotExist", local.proxy.doesNotExist());
local.catch = false;
try
{
local.proxy.thisMethodReallyDoesNotExist();
}
catch(Object exc)
{
local.catch = true;
}
AssertTrue(local.catch, "Exception should be thrown");
debug(local.proxy);
local.interfaces = local.proxy.getClass().getInterfaces();
for(local.i = 1; local.i lte arraylen(local.interfaces); local.i++)
{
local.class = local.interfaces[local.i];
debug(local.class.toString());
assertEquals(local.class.getName(), "ut2.IFoo");
}
AssertEquals(local.cfcProxyTest.runDynamicProxyFoo(local.bar), "Hello from dyanmic proxy");
</cfscript>
</cffunction>
<cffunction name="simpleCFCPathTest" hint="test to create a CFC dynamic proxy, and call it from within CF" access="public" returntype="void" output="false">
<cfscript>
var local = {};
local.cfcProxyTest = instance.loader.create("ut2.CFCProxyTest").init();
local.path = expandPath("/unittests/dynamicproxy/cfc/SimpleBean.cfc");
local.proxy = local.cfcProxyTest.getDynamicProxy(local.path);
assertEquals("Hello!", local.proxy.foo("Hello!"));
assertEquals("I could not find my method: doesNotExist", local.proxy.doesNotExist());
local.catch = false;
try
{
local.proxy.thisMethodReallyDoesNotExist();
}
catch(Object exc)
{
local.catch = true;
}
AssertTrue(local.catch, "Exception should be thrown");
AssertEquals(local.cfcProxyTest.runDynamicProxyFoo(local.path), "Hello from dyanmic proxy");
</cfscript>
</cffunction>
<cffunction name="requestScopeTest" hint="test if the request scope passes through" access="public" returntype="void" output="false">
<cfscript>
local.bar = createObject("component", "cfc.RequestBean");
local.cfcProxyTest = instance.loader.create("ut2.CFCProxyTest").init();
local.proxy = local.cfcProxyTest.getDynamicProxy(local.bar);
request.foo = "yadda";
AssertEquals("yadda :: Hello", local.proxy.foo("Hello"));
local.path = expandPath("/unittests/dynamicproxy/cfc/RequestBean.cfc");
local.proxy = local.cfcProxyTest.getDynamicProxy(local.path);
request.foo = "yadda";
AssertEquals("yadda :: Hello", local.proxy.foo("Hello"));
</cfscript>
</cffunction>
<cffunction name="applicationScopeTest" hint="test if the app scope passes through" access="public" returntype="void" output="false">
<cfscript>
local.bar = createObject("component", "cfc.ApplicationBean");
local.cfcProxyTest = instance.loader.create("ut2.CFCProxyTest").init();
local.proxy = local.cfcProxyTest.getDynamicProxy(local.bar);
application.foo = "yadda";
AssertEquals("yadda :: Hello", local.proxy.foo("Hello"));
local.path = expandPath("/unittests/dynamicproxy/cfc/ApplicationBean.cfc");
local.proxy = local.cfcProxyTest.getDynamicProxy(local.path);
request.foo = "yadda";
AssertEquals("yadda :: Hello", local.proxy.foo("Hello"));
</cfscript>
</cffunction>
<cffunction name="sessionScopeTest" hint="test if the session scope passes through" access="public" returntype="void" output="false">
<cfscript>
local.bar = createObject("component", "cfc.SessionBean");
local.cfcProxyTest = instance.loader.create("ut2.CFCProxyTest").init();
local.proxy = local.cfcProxyTest.getDynamicProxy(local.bar);
session.foo = "yadda";
AssertEquals("yadda :: Hello", local.proxy.foo("Hello"));
local.path = expandPath("/unittests/dynamicproxy/cfc/SessionBean.cfc");
local.proxy = local.cfcProxyTest.getDynamicProxy(local.path);
session.foo = "yadda";
AssertEquals("yadda :: Hello", local.proxy.foo("Hello"));
</cfscript>
</cffunction>
<cffunction name="errorInCodeTest" hint="test if the session scope passes through" access="public" returntype="void" output="false">
<cfscript>
local.bar = createObject("component", "cfc.SessionBean");
local.cfcProxyTest = instance.loader.create("ut2.CFCProxyTest").init();
local.proxy = local.cfcProxyTest.getDynamicProxy(local.bar);
StructClear(session);
local.check = false;
try
{
AssertEquals("yadda :: Hello", local.proxy.foo("Hello"));
}
catch(Expression exc)
{
AssertEquals("Element FOO is undefined in SESSION.", exc.message);
local.check = true;
}
AssertTrue(local.check, "Exception should have fired");
</cfscript>
</cffunction>
<cffunction name="testDynamicProxyInAnotherThread" hint="testing a dynamic proxy executing in another thread" access="public" returntype="void" output="false">
<cfscript>
var local = {};
local.CFCDynamicProxy = instance.loader.create("com.compoundtheory.coldfusion.cfc.CFCDynamicProxy");
local.runnable = createObject("component", "cfc.Runnable").init();
local.interfaces = ["java.lang.Runnable"];
local.proxy = local.CFCDynamicProxy.createInstance(local.runnable, local.interfaces);
assertEquals("Foo", local.runnable.getValue());
local.thread = createObject("java", "java.lang.Thread").init(local.proxy);
local.thread.start();
local.thread.join();
assertEquals("Bar", local.runnable.getValue());
</cfscript>
</cffunction>
<cffunction name="executorServiceTest" hint="test the executor service with the Dynamic proxy" access="public" returntype="any" output="false">
<cfscript>
var local = {};
local.completionQueue = createObject("java", "java.util.concurrent.LinkedBlockingQueue").init(1000000);
local.executor = createObject("java", "java.util.concurrent.Executors").newFixedThreadPool(4);
local.completionService = createObject("java", "java.util.concurrent.ExecutorCompletionService").init(local.executor, local.completionQueue);
local.timeUnit = createObject("java", "java.util.concurrent.TimeUnit");
local.CFCDynamicProxy = instance.loader.create("com.compoundtheory.coldfusion.cfc.CFCDynamicProxy");
//guard... ensure the object works correctly when called directly
local.sanity = createObject("component", "cfc.ThingieDoer").init(1, "normal old thingiedoer");
local.result = local.sanity.call();
assertTrue( local.result.success );
assertFalse( structKeyExists(local.result, "error") );
//now move on to the real test
local.interfaces = ["java.util.concurrent.Callable"];
local.thingieDoer = createObject("component", "cfc.ThingieDoer").init(1 , "name 1" );
local.thingieProxy = local.CFCDynamicProxy.createInstance(local.thingieDoer, local.interfaces);
//submit the proxy. This should cause the completionService to run it immediately;
local.completionService.submit( local.thingieProxy ); //this doesn't error, but the call() method never gets run
//ensure what we've submitted gets run, then shut down the service
local.executor.awaitTermination( javacast("int",1), local.timeUnit.SECONDS );
//get the "future" object, which is the result of completionService running the proxy's call() method
local.futureResult = local.completionService.poll();
//boom
local.callResult = local.futureResult.get();
assertTrue( local.callResult.success );
assertFalse( structKeyExists( local.callResult, "error" ) );
</cfscript>
</cffunction>
<!------------------------------------------- PACKAGE ------------------------------------------->
<!------------------------------------------- PRIVATE ------------------------------------------->
</cfcomponent>