1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.context.rmi;
17
18 import junit.framework.TestCase;
19
20 import org.acegisecurity.Authentication;
21 import org.acegisecurity.TargetObject;
22
23 import org.acegisecurity.context.SecurityContextHolder;
24
25 import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
26
27 import org.acegisecurity.util.SimpleMethodInvocation;
28
29 import org.aopalliance.intercept.MethodInvocation;
30
31 import java.lang.reflect.Method;
32
33
34
35
36
37
38
39
40 public class ContextPropagatingRemoteInvocationTests extends TestCase {
41
42
43 public ContextPropagatingRemoteInvocationTests() {
44 super();
45 }
46
47 public ContextPropagatingRemoteInvocationTests(String arg0) {
48 super(arg0);
49 }
50
51
52
53 private ContextPropagatingRemoteInvocation getRemoteInvocation()
54 throws Exception {
55 Class clazz = TargetObject.class;
56 Method method = clazz.getMethod("makeLowerCase", new Class[] {String.class});
57 MethodInvocation mi = new SimpleMethodInvocation(method, new Object[] {"SOME_STRING"});
58
59 ContextPropagatingRemoteInvocationFactory factory = new ContextPropagatingRemoteInvocationFactory();
60
61 return (ContextPropagatingRemoteInvocation) factory.createRemoteInvocation(mi);
62 }
63
64 public static void main(String[] args) {
65 junit.textui.TestRunner.run(ContextPropagatingRemoteInvocationTests.class);
66 }
67
68 public void testContextIsResetEvenIfExceptionOccurs()
69 throws Exception {
70
71 Authentication clientSideAuthentication = new UsernamePasswordAuthenticationToken("marissa", "koala");
72 SecurityContextHolder.getContext().setAuthentication(clientSideAuthentication);
73
74 ContextPropagatingRemoteInvocation remoteInvocation = getRemoteInvocation();
75
76 try {
77
78 remoteInvocation.setArguments(new Object[] {});
79 remoteInvocation.invoke(TargetObject.class.newInstance());
80 fail("Expected IllegalArgumentException");
81 } catch (IllegalArgumentException e) {
82
83 }
84
85 assertNull("Authentication must be null ", SecurityContextHolder.getContext().getAuthentication());
86 }
87
88 public void testNormalOperation() throws Exception {
89
90 Authentication clientSideAuthentication = new UsernamePasswordAuthenticationToken("marissa", "koala");
91 SecurityContextHolder.getContext().setAuthentication(clientSideAuthentication);
92
93 ContextPropagatingRemoteInvocation remoteInvocation = getRemoteInvocation();
94
95
96
97
98 SecurityContextHolder.clearContext();
99
100
101
102 assertEquals("some_string org.acegisecurity.providers.UsernamePasswordAuthenticationToken false",
103 remoteInvocation.invoke(new TargetObject()));
104 }
105
106 public void testNullContextHolderDoesNotCauseInvocationProblems()
107 throws Exception {
108 SecurityContextHolder.getContext().setAuthentication(null);
109
110 ContextPropagatingRemoteInvocation remoteInvocation = getRemoteInvocation();
111 SecurityContextHolder.getContext().setAuthentication(null);
112
113 assertEquals("some_string Authentication empty", remoteInvocation.invoke(new TargetObject()));
114 }
115 }