Source Code Cross Referenced for ResinAcegiAuthenticatorTests.java in  » Security » acegi-security » org » acegisecurity » adapters » resin » 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 » Security » acegi security » org.acegisecurity.adapters.resin 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002:         *
003:         * Licensed under the Apache License, Version 2.0 (the "License");
004:         * you may not use this file except in compliance with the License.
005:         * You may obtain a copy of the License at
006:         *
007:         *     http://www.apache.org/licenses/LICENSE-2.0
008:         *
009:         * Unless required by applicable law or agreed to in writing, software
010:         * distributed under the License is distributed on an "AS IS" BASIS,
011:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012:         * See the License for the specific language governing permissions and
013:         * limitations under the License.
014:         */
015:
016:        package org.acegisecurity.adapters.resin;
017:
018:        import junit.framework.TestCase;
019:
020:        import org.acegisecurity.GrantedAuthority;
021:        import org.acegisecurity.GrantedAuthorityImpl;
022:
023:        import org.acegisecurity.adapters.PrincipalAcegiUserToken;
024:
025:        import java.security.Principal;
026:
027:        import javax.servlet.ServletException;
028:
029:        /**
030:         * Tests {@link ResinAcegiAuthenticator}.
031:         *
032:         * @author Ben Alex
033:         * @version $Id: ResinAcegiAuthenticatorTests.java 1496 2006-05-23 13:38:33Z benalex $
034:         */
035:        public class ResinAcegiAuthenticatorTests extends TestCase {
036:            //~ Instance fields ================================================================================================
037:
038:            private final String ADAPTER_KEY = "my_key";
039:
040:            //~ Constructors ===================================================================================================
041:
042:            public ResinAcegiAuthenticatorTests() {
043:                super ();
044:            }
045:
046:            public ResinAcegiAuthenticatorTests(String arg0) {
047:                super (arg0);
048:            }
049:
050:            //~ Methods ========================================================================================================
051:
052:            public static void main(String[] args) {
053:                junit.textui.TestRunner.run(ResinAcegiAuthenticatorTests.class);
054:            }
055:
056:            public final void setUp() throws Exception {
057:                super .setUp();
058:            }
059:
060:            public void testAdapterAbortsIfAppContextDoesNotContainAnAuthenticationBean()
061:                    throws Exception {
062:                ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
063:                adapter
064:                        .setAppContextLocation("org/acegisecurity/adapters/adaptertest-invalid.xml");
065:                adapter.setKey(ADAPTER_KEY);
066:
067:                try {
068:                    adapter.init();
069:                    fail("Should have thrown ServletException");
070:                } catch (ServletException expected) {
071:                    assertEquals(
072:                            "Bean context must contain at least one bean of type AuthenticationManager",
073:                            expected.getMessage());
074:                }
075:            }
076:
077:            public void testAdapterAbortsIfNoAppContextSpecified()
078:                    throws Exception {
079:                ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
080:                adapter.setKey(ADAPTER_KEY);
081:
082:                try {
083:                    adapter.init();
084:                    fail("Should have thrown ServletException");
085:                } catch (ServletException expected) {
086:                    assertEquals("appContextLocation must be defined", expected
087:                            .getMessage());
088:                }
089:
090:                adapter.setAppContextLocation("");
091:
092:                try {
093:                    adapter.init();
094:                    fail("Should have thrown ServletException");
095:                } catch (ServletException expected) {
096:                    assertEquals("appContextLocation must be defined", expected
097:                            .getMessage());
098:                }
099:            }
100:
101:            public void testAdapterAbortsIfNoKeySpecified() throws Exception {
102:                ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
103:                adapter
104:                        .setAppContextLocation("org/acegisecurity/adapters/adaptertest-valid.xml");
105:
106:                try {
107:                    adapter.init();
108:                    fail("Should have thrown ServletException");
109:                } catch (ServletException expected) {
110:                    assertEquals("key must be defined", expected.getMessage());
111:                }
112:
113:                adapter.setKey("");
114:
115:                try {
116:                    adapter.init();
117:                    fail("Should have thrown ServletException");
118:                } catch (ServletException expected) {
119:                    assertEquals("key must be defined", expected.getMessage());
120:                }
121:            }
122:
123:            public void testAdapterAbortsWithIncorrectApplicationContextLocation()
124:                    throws Exception {
125:                ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
126:                adapter.setAppContextLocation("FILE_DOES_NOT_EXIST");
127:                adapter.setKey(ADAPTER_KEY);
128:
129:                try {
130:                    adapter.init();
131:                    fail("Should have thrown ServletException");
132:                } catch (ServletException expected) {
133:                    assertTrue(expected.getMessage()
134:                            .startsWith("Cannot locate"));
135:                }
136:            }
137:
138:            public void testAdapterStartsUpSuccess() throws Exception {
139:                ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
140:                adapter
141:                        .setAppContextLocation("org/acegisecurity/adapters/adaptertest-valid.xml");
142:                adapter.setKey(ADAPTER_KEY);
143:                adapter.init();
144:                assertTrue(true);
145:            }
146:
147:            public void testAuthenticationFailsForIncorrectPassword()
148:                    throws Exception {
149:                ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
150:                adapter
151:                        .setAppContextLocation("org/acegisecurity/adapters/adaptertest-valid.xml");
152:                adapter.setKey(ADAPTER_KEY);
153:                adapter.init();
154:                assertEquals(null, adapter.loginImpl("marissa", "kangaroo"));
155:            }
156:
157:            public void testAuthenticationFailsForIncorrectUserName()
158:                    throws Exception {
159:                ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
160:                adapter
161:                        .setAppContextLocation("org/acegisecurity/adapters/adaptertest-valid.xml");
162:                adapter.setKey(ADAPTER_KEY);
163:                adapter.init();
164:                assertEquals(null, adapter.loginImpl("melissa", "koala"));
165:            }
166:
167:            public void testAuthenticationSuccess() throws Exception {
168:                ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
169:                adapter
170:                        .setAppContextLocation("org/acegisecurity/adapters/adaptertest-valid.xml");
171:                adapter.setKey(ADAPTER_KEY);
172:                adapter.init();
173:
174:                Principal result = adapter.loginImpl("marissa", "koala");
175:
176:                if (!(result instanceof  PrincipalAcegiUserToken)) {
177:                    fail("Should have returned PrincipalAcegiUserToken");
178:                }
179:
180:                PrincipalAcegiUserToken castResult = (PrincipalAcegiUserToken) result;
181:                assertEquals("marissa", castResult.getPrincipal());
182:                assertEquals("koala", castResult.getCredentials());
183:                assertEquals("ROLE_TELLER", castResult.getAuthorities()[0]
184:                        .getAuthority());
185:                assertEquals("ROLE_SUPERVISOR", castResult.getAuthorities()[1]
186:                        .getAuthority());
187:                assertEquals(ADAPTER_KEY.hashCode(), castResult.getKeyHash());
188:            }
189:
190:            public void testAuthenticationSuccessUsingAlternateMethod()
191:                    throws Exception {
192:                ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
193:                adapter
194:                        .setAppContextLocation("org/acegisecurity/adapters/adaptertest-valid.xml");
195:                adapter.setKey(ADAPTER_KEY);
196:                adapter.init();
197:
198:                Principal result = adapter.loginImpl(null, null, null,
199:                        "marissa", "koala");
200:
201:                if (!(result instanceof  PrincipalAcegiUserToken)) {
202:                    fail("Should have returned PrincipalAcegiUserToken");
203:                }
204:
205:                PrincipalAcegiUserToken castResult = (PrincipalAcegiUserToken) result;
206:                assertEquals("marissa", castResult.getPrincipal());
207:                assertEquals("koala", castResult.getCredentials());
208:                assertEquals("ROLE_TELLER", castResult.getAuthorities()[0]
209:                        .getAuthority());
210:                assertEquals("ROLE_SUPERVISOR", castResult.getAuthorities()[1]
211:                        .getAuthority());
212:                assertEquals(ADAPTER_KEY.hashCode(), castResult.getKeyHash());
213:            }
214:
215:            public void testAuthenticationWithNullPasswordHandledGracefully()
216:                    throws Exception {
217:                ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
218:                adapter
219:                        .setAppContextLocation("org/acegisecurity/adapters/adaptertest-valid.xml");
220:                adapter.setKey(ADAPTER_KEY);
221:                adapter.init();
222:                assertEquals(null, adapter.loginImpl("marissa", null));
223:            }
224:
225:            public void testAuthenticationWithNullUserNameHandledGracefully()
226:                    throws Exception {
227:                ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
228:                adapter
229:                        .setAppContextLocation("org/acegisecurity/adapters/adaptertest-valid.xml");
230:                adapter.setKey(ADAPTER_KEY);
231:                adapter.init();
232:                assertEquals(null, adapter.loginImpl(null, "koala"));
233:            }
234:
235:            public void testGetters() throws Exception {
236:                ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
237:                adapter
238:                        .setAppContextLocation("org/acegisecurity/adapters/adaptertest-valid.xml");
239:                adapter.setKey(ADAPTER_KEY);
240:                assertEquals(ADAPTER_KEY, adapter.getKey());
241:                assertEquals(
242:                        "org/acegisecurity/adapters/adaptertest-valid.xml",
243:                        adapter.getAppContextLocation());
244:            }
245:
246:            public void testHasRoleWithANullPrincipalFails() throws Exception {
247:                ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
248:                adapter
249:                        .setAppContextLocation("org/acegisecurity/adapters/adaptertest-valid.xml");
250:                adapter.setKey(ADAPTER_KEY);
251:                adapter.init();
252:                assertTrue(!adapter.isUserInRole(null, null, null, null,
253:                        "ROLE_ONE"));
254:            }
255:
256:            public void testHasRoleWithAPrincipalTheAdapterDidNotCreateFails()
257:                    throws Exception {
258:                ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
259:                adapter
260:                        .setAppContextLocation("org/acegisecurity/adapters/adaptertest-valid.xml");
261:                adapter.setKey(ADAPTER_KEY);
262:                adapter.init();
263:                assertTrue(!adapter.isUserInRole(null, null, null,
264:                        new Principal() {
265:                            public String getName() {
266:                                return "MockPrincipal";
267:                            }
268:                        }, "ROLE_ONE"));
269:            }
270:
271:            public void testHasRoleWithPrincipalAcegiUserToken()
272:                    throws Exception {
273:                PrincipalAcegiUserToken token = new PrincipalAcegiUserToken(
274:                        "KEY", "Test", "Password", new GrantedAuthority[] {
275:                                new GrantedAuthorityImpl("ROLE_ONE"),
276:                                new GrantedAuthorityImpl("ROLE_TWO") }, null);
277:                ResinAcegiAuthenticator adapter = new ResinAcegiAuthenticator();
278:                adapter
279:                        .setAppContextLocation("org/acegisecurity/adapters/adaptertest-valid.xml");
280:                adapter.setKey(ADAPTER_KEY);
281:                adapter.init();
282:                assertTrue(adapter.isUserInRole(null, null, null, token,
283:                        "ROLE_ONE"));
284:                assertTrue(adapter.isUserInRole(null, null, null, token,
285:                        "ROLE_ONE"));
286:                assertTrue(!adapter.isUserInRole(null, null, null, token,
287:                        "ROLE_WE_DO_NOT_HAVE"));
288:            }
289:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.