KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > outbound > SubjectInterceptorTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.connector.outbound;
19
20 import javax.security.auth.Subject JavaDoc;
21 import javax.resource.ResourceException JavaDoc;
22
23 import org.apache.geronimo.security.ContextManager;
24
25 /**
26  *
27  *
28  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
29  *
30  * */

31 public class SubjectInterceptorTest extends ConnectionInterceptorTestUtils {
32
33     private SubjectInterceptor subjectInterceptor;
34
35     protected void setUp() throws Exception JavaDoc {
36         super.setUp();
37         subjectInterceptor = new SubjectInterceptor(this);
38     }
39
40     protected void tearDown() throws Exception JavaDoc {
41         super.tearDown();
42         subjectInterceptor = null;
43     }
44
45     public void testGetConnection() throws Exception JavaDoc {
46         subject = new Subject JavaDoc();
47         ContextManager.setCallers(subject, subject);
48         ConnectionInfo connectionInfo = makeConnectionInfo();
49         ManagedConnectionInfo managedConnectionInfo = connectionInfo.getManagedConnectionInfo();
50         subjectInterceptor.getConnection(connectionInfo);
51         assertTrue("Expected call to next with same connectionInfo", connectionInfo == obtainedConnectionInfo);
52         assertTrue("Expected the same managedConnectionInfo", managedConnectionInfo == connectionInfo.getManagedConnectionInfo());
53         assertTrue("Expected supplied subject to be inserted", subject == managedConnectionInfo.getSubject());
54     }
55
56     public void testReturnConnection() throws Exception JavaDoc {
57         ConnectionInfo connectionInfo = makeConnectionInfo();
58         subjectInterceptor.returnConnection(connectionInfo, ConnectionReturnAction.RETURN_HANDLE);
59         assertTrue("Expected call to next with same connectionInfo", connectionInfo == returnedConnectionInfo);
60     }
61
62     public void testEnterWithSameSubject() throws Exception JavaDoc {
63         makeSubject("foo");
64         ConnectionInfo connectionInfo = makeConnectionInfo();
65         managedConnection = new TestPlainManagedConnection();
66         subjectInterceptor.getConnection(connectionInfo);
67         //reset our test indicator
68
obtainedConnectionInfo = null;
69         subjectInterceptor.getConnection(connectionInfo);
70         assertTrue("Expected connection asked for", obtainedConnectionInfo == connectionInfo);
71         assertTrue("Expected no connection returned", returnedConnectionInfo == null);
72     }
73
74     public void testEnterWithChangedSubject() throws Exception JavaDoc {
75         makeSubject("foo");
76         ContextManager.setCallers(subject, subject);
77         ConnectionInfo connectionInfo = makeConnectionInfo();
78         managedConnection = new TestPlainManagedConnection();
79         subjectInterceptor.getConnection(connectionInfo);
80         //reset our test indicator
81
obtainedConnectionInfo = null;
82         makeSubject("bar");
83         ContextManager.setCallers(subject, subject);
84         subjectInterceptor.getConnection(connectionInfo);
85         //expect re-association
86
assertTrue("Expected connection asked for", obtainedConnectionInfo != null);
87         //connection is returned by SubjectInterceptor
88
assertTrue("Expected connection returned", returnedConnectionInfo != null);
89     }
90
91     public void testApplicationManagedSecurity() throws Exception JavaDoc {
92         makeSubject("foo");
93         ConnectionInfo connectionInfo = makeConnectionInfo();
94         connectionInfo.setApplicationManagedSecurity(true);
95         ManagedConnectionInfo managedConnectionInfo = connectionInfo.getManagedConnectionInfo();
96         managedConnection = new TestPlainManagedConnection();
97         subjectInterceptor.getConnection(connectionInfo);
98         //expect no subject set on mci
99
assertTrue("Expected call to next with same connectionInfo", connectionInfo == obtainedConnectionInfo);
100         assertTrue("Expected the same managedConnectionInfo", managedConnectionInfo == connectionInfo.getManagedConnectionInfo());
101         assertTrue("Expected no subject to be inserted", null == managedConnectionInfo.getSubject());
102     }
103
104     public void testUnshareablePreventsReAssociation() throws Exception JavaDoc {
105         makeSubject("foo");
106         ContextManager.setCallers(subject, subject);
107         ConnectionInfo connectionInfo = makeConnectionInfo();
108         connectionInfo.setUnshareable(true);
109         managedConnection = new TestPlainManagedConnection();
110         subjectInterceptor.getConnection(connectionInfo);
111         //reset our test indicator
112
obtainedConnectionInfo = null;
113         makeSubject("bar");
114         ContextManager.setCallers(subject, subject);
115         try {
116             subjectInterceptor.getConnection(connectionInfo);
117             fail("Reassociating should fail on an unshareable connection");
118         } catch (ResourceException JavaDoc e) {
119         }
120     }
121
122 }
123
Popular Tags