Source Code Cross Referenced for TestBasicSessionValidator.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: TestSimpleSessionValidator.java 3308 2006-06-15 18:54:14Z gbevin $
008:         */
009:        package com.uwyn.rife.authentication.sessionvalidators;
010:
011:        import com.uwyn.rife.authentication.SessionAttributes;
012:        import com.uwyn.rife.authentication.credentialsmanagers.MemoryUsers;
013:        import com.uwyn.rife.authentication.credentialsmanagers.RoleUserAttributes;
014:        import com.uwyn.rife.authentication.exceptions.CredentialsManagerException;
015:        import com.uwyn.rife.authentication.exceptions.SessionManagerException;
016:        import com.uwyn.rife.authentication.exceptions.SessionValidatorException;
017:        import com.uwyn.rife.authentication.sessionmanagers.MemorySessions;
018:        import com.uwyn.rife.authentication.sessionmanagers.SimpleSessionManagerFactory;
019:        import com.uwyn.rife.authentication.sessionvalidators.BasicSessionValidator;
020:        import com.uwyn.rife.ioc.HierarchicalProperties;
021:        import com.uwyn.rife.tools.ExceptionUtils;
022:        import junit.framework.TestCase;
023:
024:        public class TestBasicSessionValidator extends TestCase {
025:            private HierarchicalProperties mProperties = null;
026:
027:            public TestBasicSessionValidator(String name) {
028:                super (name);
029:                mProperties = new HierarchicalProperties();
030:                mProperties.put(
031:                        SimpleSessionManagerFactory.PROPERTYNAME_MANAGER_CLASS,
032:                        MemorySessions.class.getName());
033:            }
034:
035:            public void testInstantiation() {
036:                AbstractSessionValidator validator = null;
037:
038:                validator = new BasicSessionValidator();
039:
040:                assertNotNull(validator);
041:            }
042:
043:            public void testValidSessionId() {
044:                BasicSessionValidator validator = new BasicSessionValidator();
045:
046:                assertTrue(validator.isAccessAuthorized(1));
047:            }
048:
049:            public void testSessionValidity() {
050:                BasicSessionValidator validator = new BasicSessionValidator();
051:                MemorySessions sessions = (MemorySessions) new SimpleSessionManagerFactory()
052:                        .getManager(mProperties);
053:                sessions.setSessionDuration(120000);
054:                validator.setSessionManager(sessions);
055:
056:                int user_id = 9478;
057:                String host_ip = "98.232.12.456";
058:
059:                String auth_id = null;
060:                try {
061:                    auth_id = sessions.startSession(user_id, host_ip, false);
062:                    assertTrue(validator.isAccessAuthorized(validator
063:                            .validateSession(auth_id, host_ip,
064:                                    new DummyAttributes())));
065:                    sessions.setRestrictHostIp(true);
066:                    assertEquals(AbstractSessionValidator.SESSION_INVALID,
067:                            validator.validateSession(auth_id, "1.1.1.1",
068:                                    new DummyAttributes()));
069:                    sessions.setRestrictHostIp(false);
070:                    assertEquals(AbstractSessionValidator.SESSION_VALID,
071:                            validator.validateSession(auth_id, "1.1.1.1",
072:                                    new DummyAttributes()));
073:                    assertEquals(AbstractSessionValidator.SESSION_INVALID,
074:                            validator.validateSession("not_valid", host_ip,
075:                                    new DummyAttributes()));
076:
077:                    sessions.setSessionDuration(0);
078:
079:                    Thread.sleep(2);
080:                    assertEquals(AbstractSessionValidator.SESSION_INVALID,
081:                            validator.validateSession(auth_id, host_ip,
082:                                    new DummyAttributes()));
083:                } catch (InterruptedException e) {
084:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
085:                } catch (SessionManagerException e) {
086:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
087:                } catch (SessionValidatorException e) {
088:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
089:                }
090:            }
091:
092:            public void testSessionValidityRole() {
093:                BasicSessionValidator validator = new BasicSessionValidator();
094:                MemoryUsers users = new MemoryUsers();
095:                MemorySessions sessions = (MemorySessions) new SimpleSessionManagerFactory()
096:                        .getManager(mProperties);
097:                sessions.setSessionDuration(120000);
098:                validator.setSessionManager(sessions);
099:                validator.setCredentialsManager(users);
100:
101:                String host_ip = "98.232.12.456";
102:
103:                String auth_id1 = null;
104:                String auth_id2 = null;
105:                String auth_id3 = null;
106:                try {
107:                    users.addRole("admin");
108:                    users.addRole("maint");
109:
110:                    users.addUser("login1", new RoleUserAttributes(1,
111:                            "thepassword", new String[] { "admin", "maint" }));
112:                    users.addUser("login2", new RoleUserAttributes(2,
113:                            "thepassword", new String[] { "maint" }));
114:                    users.addUser("login3", new RoleUserAttributes(3,
115:                            "thepassword"));
116:
117:                    auth_id1 = sessions.startSession(1, host_ip, false);
118:                    auth_id2 = sessions.startSession(2, host_ip, false);
119:                    auth_id3 = sessions.startSession(3, host_ip, false);
120:
121:                    assertTrue(validator.isAccessAuthorized(validator
122:                            .validateSession(auth_id1, host_ip,
123:                                    new DummyAttributes())));
124:                    assertTrue(validator.isAccessAuthorized(validator
125:                            .validateSession(auth_id1, host_ip,
126:                                    new RoleAdminAttributes())));
127:                    assertTrue(validator.isAccessAuthorized(validator
128:                            .validateSession(auth_id1, host_ip,
129:                                    new RoleMaintAttributes())));
130:
131:                    assertTrue(validator.isAccessAuthorized(validator
132:                            .validateSession(auth_id2, host_ip,
133:                                    new DummyAttributes())));
134:                    assertEquals(AbstractSessionValidator.SESSION_INVALID,
135:                            validator.validateSession(auth_id2, host_ip,
136:                                    new RoleAdminAttributes()));
137:                    assertTrue(validator.isAccessAuthorized(validator
138:                            .validateSession(auth_id2, host_ip,
139:                                    new RoleMaintAttributes())));
140:
141:                    assertTrue(validator.isAccessAuthorized(validator
142:                            .validateSession(auth_id3, host_ip,
143:                                    new DummyAttributes())));
144:                    assertEquals(AbstractSessionValidator.SESSION_INVALID,
145:                            validator.validateSession(auth_id3, host_ip,
146:                                    new RoleAdminAttributes()));
147:                    assertEquals(AbstractSessionValidator.SESSION_INVALID,
148:                            validator.validateSession(auth_id3, host_ip,
149:                                    new RoleMaintAttributes()));
150:                } catch (SessionManagerException e) {
151:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
152:                } catch (CredentialsManagerException e) {
153:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
154:                } catch (SessionValidatorException e) {
155:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
156:                }
157:            }
158:
159:            class DummyAttributes implements  SessionAttributes {
160:                public boolean hasAttribute(String key) {
161:                    return false;
162:                }
163:
164:                public String getAttribute(String key) {
165:                    return null;
166:                }
167:            }
168:
169:            class RoleMaintAttributes implements  SessionAttributes {
170:                public boolean hasAttribute(String key) {
171:                    if (key.equals("role")) {
172:                        return true;
173:                    }
174:
175:                    return false;
176:                }
177:
178:                public String getAttribute(String key) {
179:                    if (key.equals("role")) {
180:                        return "maint";
181:                    }
182:
183:                    return null;
184:                }
185:            }
186:
187:            class RoleAdminAttributes implements  SessionAttributes {
188:                public boolean hasAttribute(String key) {
189:                    if (key.equals("role")) {
190:                        return true;
191:                    }
192:
193:                    return false;
194:                }
195:
196:                public String getAttribute(String key) {
197:                    if (key.equals("role")) {
198:                        return "admin";
199:                    }
200:
201:                    return null;
202:                }
203:            }
204:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.