Source Code Cross Referenced for TestDatabaseSessionValidator.java in  » Web-Framework » rife-1.6.1 » com » uwyn » rife » authentication » sessionvalidators » 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 » Web Framework » rife 1.6.1 » com.uwyn.rife.authentication.sessionvalidators 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com> and
003:         * Steven Grimm <koreth[remove] at midwinter dot com>
004:         * Distributed under the terms of either:
005:         * - the common development and distribution license (CDDL), v1.0; or
006:         * - the GNU Lesser General Public License, v2.1 or later
007:         * $Id: TestDatabaseSessionValidator.java 3643 2007-01-12 15:29:45Z gbevin $
008:         */
009:        package com.uwyn.rife.authentication.sessionvalidators;
010:
011:        import com.uwyn.rife.authentication.SessionAttributes;
012:        import com.uwyn.rife.authentication.SessionValidator;
013:        import com.uwyn.rife.authentication.credentialsmanagers.DatabaseUsers;
014:        import com.uwyn.rife.authentication.credentialsmanagers.DatabaseUsersFactory;
015:        import com.uwyn.rife.authentication.credentialsmanagers.RoleUserAttributes;
016:        import com.uwyn.rife.authentication.exceptions.CredentialsManagerException;
017:        import com.uwyn.rife.authentication.exceptions.SessionManagerException;
018:        import com.uwyn.rife.authentication.exceptions.SessionValidatorException;
019:        import com.uwyn.rife.authentication.sessionmanagers.DatabaseSessions;
020:        import com.uwyn.rife.authentication.sessionmanagers.DatabaseSessionsFactory;
021:        import com.uwyn.rife.database.Datasource;
022:        import com.uwyn.rife.ioc.HierarchicalProperties;
023:        import com.uwyn.rife.tools.ExceptionUtils;
024:        import junit.framework.TestCase;
025:
026:        public class TestDatabaseSessionValidator extends TestCase {
027:            private Datasource mDatasource = null;
028:            private HierarchicalProperties mProperties = null;
029:
030:            public TestDatabaseSessionValidator(Datasource datasource,
031:                    String datasourceName, String name) {
032:                super (name);
033:                mDatasource = datasource;
034:                mProperties = new HierarchicalProperties();
035:                mProperties.put("datasource", mDatasource);
036:            }
037:
038:            public void testInstantiation() {
039:                SessionValidator validator = new DatabaseSessionValidatorFactory()
040:                        .getValidator(mProperties);
041:                assertNotNull(validator);
042:                assertTrue(validator instanceof  DatabaseSessionValidator);
043:            }
044:
045:            public void testValidSessionId() {
046:                SessionValidator validator = new DatabaseSessionValidatorFactory()
047:                        .getValidator(mProperties);
048:
049:                assertTrue(validator.isAccessAuthorized(1));
050:            }
051:
052:            public void testSessionValidity() {
053:                SessionValidator validator = new DatabaseSessionValidatorFactory()
054:                        .getValidator(mProperties);
055:                DatabaseSessions sessions = new DatabaseSessionsFactory()
056:                        .getManager(mProperties);
057:                sessions.setSessionDuration(120000);
058:                validator.setSessionManager(sessions);
059:
060:                int user_id = 9478;
061:                String host_ip = "98.232.12.456";
062:
063:                String auth_id = null;
064:                try {
065:                    sessions.install();
066:
067:                    auth_id = sessions.startSession(user_id, host_ip, false);
068:                    assertTrue(validator.isAccessAuthorized(validator
069:                            .validateSession(auth_id, host_ip,
070:                                    new DummyAttributes())));
071:                    sessions.setRestrictHostIp(true);
072:                    assertEquals(DatabaseSessionValidator.SESSION_INVALID,
073:                            validator.validateSession(auth_id, "1.1.1.1",
074:                                    new DummyAttributes()));
075:                    sessions.setRestrictHostIp(false);
076:                    assertEquals(DatabaseSessionValidator.SESSION_VALID,
077:                            validator.validateSession(auth_id, "1.1.1.1",
078:                                    new DummyAttributes()));
079:                    assertEquals(DatabaseSessionValidator.SESSION_INVALID,
080:                            validator.validateSession("not_valid", host_ip,
081:                                    new DummyAttributes()));
082:
083:                    sessions.setSessionDuration(0);
084:
085:                    Thread.sleep(2);
086:                    assertEquals(DatabaseSessionValidator.SESSION_INVALID,
087:                            validator.validateSession(auth_id, host_ip,
088:                                    new DummyAttributes()));
089:                } catch (InterruptedException e) {
090:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
091:                } catch (SessionManagerException e) {
092:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
093:                } catch (SessionValidatorException e) {
094:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
095:                } finally {
096:                    try {
097:                        sessions.remove();
098:                    } catch (SessionManagerException e) {
099:                        assertTrue(ExceptionUtils.getExceptionStackTrace(e),
100:                                false);
101:                    }
102:                }
103:            }
104:
105:            public void testSessionValidityRole() {
106:                SessionValidator validator = new DatabaseSessionValidatorFactory()
107:                        .getValidator(mProperties);
108:                DatabaseUsers users = DatabaseUsersFactory
109:                        .getInstance(mDatasource);
110:                DatabaseSessions sessions = new DatabaseSessionsFactory()
111:                        .getManager(mProperties);
112:                sessions.setSessionDuration(120000);
113:                validator.setSessionManager(sessions);
114:
115:                String host_ip = "98.232.12.456";
116:
117:                String auth_id1 = null;
118:                String auth_id2 = null;
119:                String auth_id3 = null;
120:                try {
121:                    users.install();
122:                    sessions.install();
123:
124:                    users.addRole("admin");
125:                    users.addRole("maint");
126:
127:                    users.addUser("login1", new RoleUserAttributes(1,
128:                            "thepassword", new String[] { "admin", "maint" }));
129:                    users.addUser("login2", new RoleUserAttributes(2,
130:                            "thepassword", new String[] { "maint" }));
131:                    users.addUser("login3", new RoleUserAttributes(3,
132:                            "thepassword"));
133:
134:                    auth_id1 = sessions.startSession(1, host_ip, false);
135:                    auth_id2 = sessions.startSession(2, host_ip, false);
136:                    auth_id3 = sessions.startSession(3, host_ip, false);
137:
138:                    assertTrue(validator.isAccessAuthorized(validator
139:                            .validateSession(auth_id1, host_ip,
140:                                    new DummyAttributes())));
141:                    assertTrue(validator.isAccessAuthorized(validator
142:                            .validateSession(auth_id1, host_ip,
143:                                    new RoleAdminAttributes())));
144:                    assertTrue(validator.isAccessAuthorized(validator
145:                            .validateSession(auth_id1, host_ip,
146:                                    new RoleMaintAttributes())));
147:
148:                    assertTrue(validator.isAccessAuthorized(validator
149:                            .validateSession(auth_id2, host_ip,
150:                                    new DummyAttributes())));
151:                    assertEquals(AbstractSessionValidator.SESSION_INVALID,
152:                            validator.validateSession(auth_id2, host_ip,
153:                                    new RoleAdminAttributes()));
154:                    assertTrue(validator.isAccessAuthorized(validator
155:                            .validateSession(auth_id2, host_ip,
156:                                    new RoleMaintAttributes())));
157:
158:                    assertTrue(validator.isAccessAuthorized(validator
159:                            .validateSession(auth_id3, host_ip,
160:                                    new DummyAttributes())));
161:                    assertEquals(AbstractSessionValidator.SESSION_INVALID,
162:                            validator.validateSession(auth_id3, host_ip,
163:                                    new RoleAdminAttributes()));
164:                    assertEquals(AbstractSessionValidator.SESSION_INVALID,
165:                            validator.validateSession(auth_id3, host_ip,
166:                                    new RoleMaintAttributes()));
167:                } catch (SessionManagerException e) {
168:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
169:                } catch (CredentialsManagerException e) {
170:                    e.printStackTrace();
171:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
172:                } catch (SessionValidatorException e) {
173:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
174:                } finally {
175:                    try {
176:                        sessions.remove();
177:                    } catch (SessionManagerException e) {
178:                        assertTrue(ExceptionUtils.getExceptionStackTrace(e),
179:                                false);
180:                    }
181:                    try {
182:                        users.remove();
183:                    } catch (CredentialsManagerException e) {
184:                        assertTrue(ExceptionUtils.getExceptionStackTrace(e),
185:                                false);
186:                    }
187:                }
188:            }
189:
190:            class DummyAttributes implements  SessionAttributes {
191:                public boolean hasAttribute(String key) {
192:                    return false;
193:                }
194:
195:                public String getAttribute(String key) {
196:                    return null;
197:                }
198:            }
199:
200:            class RoleMaintAttributes implements  SessionAttributes {
201:                public boolean hasAttribute(String key) {
202:                    if (key.equals("role")) {
203:                        return true;
204:                    }
205:
206:                    return false;
207:                }
208:
209:                public String getAttribute(String key) {
210:                    if (key.equals("role")) {
211:                        return "maint";
212:                    }
213:
214:                    return null;
215:                }
216:            }
217:
218:            class RoleAdminAttributes implements  SessionAttributes {
219:                public boolean hasAttribute(String key) {
220:                    if (key.equals("role")) {
221:                        return true;
222:                    }
223:
224:                    return false;
225:                }
226:
227:                public String getAttribute(String key) {
228:                    if (key.equals("role")) {
229:                        return "admin";
230:                    }
231:
232:                    return null;
233:                }
234:            }
235:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.