Source Code Cross Referenced for ClientSecurityTest.java in  » J2EE » openejb3 » org » apache » openejb » client » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » openejb3 » org.apache.openejb.client 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         *
003:         * Licensed to the Apache Software Foundation (ASF) under one or more
004:         * contributor license agreements.  See the NOTICE file distributed with
005:         * this work for additional information regarding copyright ownership.
006:         * The ASF licenses this file to You under the Apache License, Version 2.0
007:         * (the "License"); you may not use this file except in compliance with
008:         * the License.  You may obtain a copy of the License at
009:         *
010:         *     http://www.apache.org/licenses/LICENSE-2.0
011:         *
012:         *  Unless required by applicable law or agreed to in writing, software
013:         *  distributed under the License is distributed on an "AS IS" BASIS,
014:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015:         *  See the License for the specific language governing permissions and
016:         *  limitations under the License.
017:         */package org.apache.openejb.client;
018:
019:        import junit.framework.TestCase;
020:
021:        import javax.security.auth.login.FailedLoginException;
022:        import java.util.concurrent.CountDownLatch;
023:        import java.util.concurrent.TimeUnit;
024:
025:        public class ClientSecurityTest extends TestCase {
026:            protected void setUp() throws Exception {
027:                super .setUp();
028:                LoginTestUtil.initialize();
029:                ClientSecurity.logout();
030:                ClientSecurity.setIdentityResolver(null);
031:                System.getProperties().remove(
032:                        ClientSecurity.IDENTITY_RESOLVER_STRATEGY);
033:            }
034:
035:            public void testDefaultStrategy() {
036:                IdentityResolver identityResolver = ClientSecurity
037:                        .getIdentityResolver();
038:                assertNotNull("identityResolver is null", identityResolver);
039:                assertTrue(
040:                        "identityResolver should be an instance of JaasIdentityResolver",
041:                        identityResolver instanceof  JaasIdentityResolver);
042:            }
043:
044:            public void testSimpleStrategy() {
045:                System.setProperty(ClientSecurity.IDENTITY_RESOLVER_STRATEGY,
046:                        "simple");
047:                IdentityResolver identityResolver = ClientSecurity
048:                        .getIdentityResolver();
049:                assertNotNull("identityResolver is null", identityResolver);
050:                assertTrue(
051:                        "identityResolver should be an instance of ClientSecurity.SimpleIdentityResolver",
052:                        identityResolver instanceof  ClientSecurity.SimpleIdentityResolver);
053:            }
054:
055:            public void testJaasStrategy() {
056:                System.setProperty(ClientSecurity.IDENTITY_RESOLVER_STRATEGY,
057:                        "jaas");
058:                IdentityResolver identityResolver = ClientSecurity
059:                        .getIdentityResolver();
060:                assertNotNull("identityResolver is null", identityResolver);
061:                assertTrue(
062:                        "identityResolver should be an instance of JaasIdentityResolver",
063:                        identityResolver instanceof  JaasIdentityResolver);
064:            }
065:
066:            public void testLogin() throws FailedLoginException {
067:                // setup the server response
068:                LoginTestUtil.setAuthGranted();
069:
070:                // attempt a login
071:                ClientSecurity.login("jonathan", "secret");
072:
073:                // Verify stored server request
074:                assertTrue(
075:                        "serverRequest should be an instance of AuthenticationRequest",
076:                        LoginTestUtil.serverRequest instanceof  AuthenticationRequest);
077:                AuthenticationRequest authenticationRequest = (AuthenticationRequest) LoginTestUtil.serverRequest;
078:                assertEquals("jonathan", authenticationRequest.getUsername());
079:                assertEquals("secret", authenticationRequest.getCredentials());
080:
081:                // verify client identity
082:                assertEquals("SecretIdentity", ClientSecurity.getIdentity());
083:
084:                // verify we are using the simple client identity strategy
085:                assertTrue(
086:                        "ClientSecurity.getIdentityResolver() should be an instance of ClientSecurity.SimpleIdentityResolver",
087:                        ClientSecurity.getIdentityResolver() instanceof  ClientSecurity.SimpleIdentityResolver);
088:
089:                // logout
090:                ClientSecurity.logout();
091:
092:                // verify we are logged out
093:                assertNull("ClientSecurity.getIdentity() is not null",
094:                        ClientSecurity.getIdentity());
095:            }
096:
097:            private static Throwable threadException;
098:
099:            public void testThreadLogin() throws Exception {
100:                // setup the server response
101:                LoginTestUtil.setAuthGranted();
102:
103:                // Perform a thread scoped login using a new thread
104:                final CountDownLatch loginLatch = new CountDownLatch(1);
105:                final CountDownLatch loginVerifiedLatch = new CountDownLatch(1);
106:                Thread loginThread = new Thread() {
107:                    public void run() {
108:                        try {
109:                            // attempt a login
110:                            ClientSecurity.login("jonathan", "secret", true);
111:
112:                            // Verify stored server request
113:                            assertTrue(
114:                                    "serverRequest should be an instance of AuthenticationRequest",
115:                                    LoginTestUtil.serverRequest instanceof  AuthenticationRequest);
116:                            AuthenticationRequest authenticationRequest = (AuthenticationRequest) LoginTestUtil.serverRequest;
117:                            assertEquals("jonathan", authenticationRequest
118:                                    .getUsername());
119:                            assertEquals("secret", authenticationRequest
120:                                    .getCredentials());
121:
122:                            // verify client identity
123:                            assertEquals("SecretIdentity", ClientSecurity
124:                                    .getIdentity());
125:
126:                            // verify we are using the simple client identity strategy
127:                            assertTrue(
128:                                    "ClientSecurity.getIdentityResolver() should be an instance of ClientSecurity.SimpleIdentityResolver",
129:                                    ClientSecurity.getIdentityResolver() instanceof  ClientSecurity.SimpleIdentityResolver);
130:
131:                            // notify outer thread that we are logged in
132:                            loginLatch.countDown();
133:
134:                            // wait for outer thread to verify that it is not also logged in
135:                            loginVerifiedLatch.await(5, TimeUnit.SECONDS);
136:
137:                            // logout
138:                            ClientSecurity.logout();
139:
140:                            // verify we are logged out
141:                            assertNull(
142:                                    "ClientSecurity.getIdentity() is not null",
143:                                    ClientSecurity.getIdentity());
144:                        } catch (Throwable e) {
145:                            threadException = e;
146:                        }
147:
148:                    }
149:                };
150:                loginThread.start();
151:
152:                // wait for login thread to login
153:                loginLatch.await(5, TimeUnit.SECONDS);
154:
155:                // verify we are not logged in
156:                assertNull("ClientSecurity.getIdentity() is not null",
157:                        ClientSecurity.getIdentity());
158:
159:                // notify the login thread that we are done with out verifications
160:                loginVerifiedLatch.countDown();
161:
162:                // wait for login thread to finish
163:                loginThread.join(5000);
164:
165:                if (threadException != null) {
166:                    if (threadException instanceof  Exception)
167:                        throw (Exception) threadException;
168:                    if (threadException instanceof  Error)
169:                        throw (Error) threadException;
170:                    throw new Error("login thread threw an exception",
171:                            threadException);
172:                }
173:            }
174:
175:            public void testAuthDenied() throws Exception {
176:                LoginTestUtil.setAuthDenied();
177:
178:                try {
179:                    ClientSecurity.login("nobody", "secret");
180:                    fail("Should have thrown a FailedLoginException");
181:                } catch (FailedLoginException doNothing) {
182:                }
183:
184:                // verify we are not logged in
185:                assertNull("ClientSecurity.getIdentity() is not null",
186:                        ClientSecurity.getIdentity());
187:            }
188:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.