18
18
*/
19
19
package org .apache .struts2 .interceptor ;
20
20
21
+ import com .mockobjects .servlet .MockHttpServletRequest ;
21
22
import com .opensymphony .xwork2 .ActionContext ;
22
23
import com .opensymphony .xwork2 .mock .MockActionInvocation ;
23
24
import org .apache .struts2 .StrutsInternalTestCase ;
26
27
import java .io .ByteArrayOutputStream ;
27
28
import java .io .ObjectInputStream ;
28
29
import java .io .ObjectOutputStream ;
30
+ import java .util .concurrent .Callable ;
31
+ import java .util .concurrent .Semaphore ;
32
+ import java .util .concurrent .TimeUnit ;
29
33
30
34
/**
31
35
* Test case for BackgroundProcessTest.
32
36
*/
33
37
public class BackgroundProcessTest extends StrutsInternalTestCase {
34
38
35
39
public void testSerializeDeserialize () throws Exception {
36
- MockActionInvocation invocation = new MockActionInvocation ();
37
- invocation .setResultCode ("BackgroundProcessTest.testSerializeDeserialize" );
40
+ final NotSerializableException expectedException = new NotSerializableException (new MockHttpServletRequest ());
41
+ final Semaphore lock = new Semaphore (1 );
42
+ lock .acquire ();
43
+ MockActionInvocationWithActionInvoker invocation = new MockActionInvocationWithActionInvoker (new Callable <String >() {
44
+ @ Override
45
+ public String call () throws Exception {
46
+ lock .release ();
47
+ throw expectedException ;
48
+ }
49
+ });
38
50
invocation .setInvocationContext (ActionContext .getContext ());
39
51
40
52
BackgroundProcess bp = new BackgroundProcess ("BackgroundProcessTest.testSerializeDeserialize" , invocation
41
53
, Thread .MIN_PRIORITY );
54
+ if (!lock .tryAcquire (1500L , TimeUnit .MILLISECONDS )) {
55
+ lock .release ();
56
+ fail ("background thread did not release lock on timeout" );
57
+ }
58
+ lock .release ();
42
59
43
- bp .exception = new Exception () ;
60
+ bp .result = "BackgroundProcessTest.testSerializeDeserialize" ;
44
61
bp .done = true ;
62
+ Thread .sleep (1000 );//give a chance to background thread to set exception
63
+ assertEquals (expectedException , bp .exception );
45
64
46
65
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
47
66
ObjectOutputStream oos = new ObjectOutputStream (baos );
48
67
oos .writeObject (bp );
49
68
oos .close ();
50
- assertTrue ("should have serialized data" , baos .size () > 0 );
51
69
byte b [] = baos .toByteArray ();
52
70
baos .close ();
53
71
@@ -62,4 +80,25 @@ public void testSerializeDeserialize() throws Exception {
62
80
assertEquals (bp .result , deserializedBp .result );
63
81
assertEquals (bp .done , deserializedBp .done );
64
82
}
83
+
84
+
85
+ private class MockActionInvocationWithActionInvoker extends MockActionInvocation {
86
+ private Callable <String > actionInvoker ;
87
+
88
+ MockActionInvocationWithActionInvoker (Callable <String > actionInvoker ){
89
+ this .actionInvoker = actionInvoker ;
90
+ }
91
+
92
+ @ Override
93
+ public String invokeActionOnly () throws Exception {
94
+ return actionInvoker .call ();
95
+ }
96
+ }
97
+
98
+ private class NotSerializableException extends Exception {
99
+ private MockHttpServletRequest notSerializableField ;
100
+ NotSerializableException (MockHttpServletRequest notSerializableField ) {
101
+ this .notSerializableField = notSerializableField ;
102
+ }
103
+ }
65
104
}
0 commit comments