Source Code Cross Referenced for JDBCUserDatabaseTest.java in  » Wiki-Engine » JSPWiki » com » ecyrd » jspwiki » auth » user » 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 » Wiki Engine » JSPWiki » com.ecyrd.jspwiki.auth.user 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.ecyrd.jspwiki.auth.user;
002:
003:        import java.io.File;
004:        import java.security.Principal;
005:        import java.sql.Connection;
006:        import java.sql.SQLException;
007:        import java.sql.Statement;
008:        import java.sql.Timestamp;
009:        import java.util.Properties;
010:
011:        import javax.naming.Context;
012:        import javax.naming.InitialContext;
013:        import javax.sql.DataSource;
014:
015:        import junit.framework.TestCase;
016:
017:        import com.ecyrd.jspwiki.TestJDBCDataSource;
018:        import com.ecyrd.jspwiki.TestJNDIContext;
019:        import com.ecyrd.jspwiki.auth.NoSuchPrincipalException;
020:        import com.ecyrd.jspwiki.auth.WikiSecurityException;
021:
022:        /**
023:         * @author Andrew Jaquith
024:         */
025:        public class JDBCUserDatabaseTest extends TestCase {
026:            private JDBCUserDatabase m_db = null;
027:
028:            private static final String INSERT_JANNE = "INSERT INTO users ("
029:                    + JDBCUserDatabase.DEFAULT_DB_EMAIL
030:                    + ","
031:                    + JDBCUserDatabase.DEFAULT_DB_FULL_NAME
032:                    + ","
033:                    + JDBCUserDatabase.DEFAULT_DB_LOGIN_NAME
034:                    + ","
035:                    + JDBCUserDatabase.DEFAULT_DB_PASSWORD
036:                    + ","
037:                    + JDBCUserDatabase.DEFAULT_DB_WIKI_NAME
038:                    + ","
039:                    + JDBCUserDatabase.DEFAULT_DB_CREATED
040:                    + ") VALUES ("
041:                    + "'janne@ecyrd.com',"
042:                    + "'Janne Jalkanen',"
043:                    + "'janne',"
044:                    + "'{SHA}457b08e825da547c3b77fbc1ff906a1d00a7daee',"
045:                    + "'JanneJalkanen',"
046:                    + "'"
047:                    + new Timestamp(new Timestamp(System.currentTimeMillis())
048:                            .getTime()).toString() + "'" + ");";
049:
050:            private static final String INSERT_USER = "INSERT INTO users ("
051:                    + JDBCUserDatabase.DEFAULT_DB_EMAIL
052:                    + ","
053:                    + JDBCUserDatabase.DEFAULT_DB_LOGIN_NAME
054:                    + ","
055:                    + JDBCUserDatabase.DEFAULT_DB_PASSWORD
056:                    + ","
057:                    + JDBCUserDatabase.DEFAULT_DB_CREATED
058:                    + ") VALUES ("
059:                    + "'user@example.com',"
060:                    + "'user',"
061:                    + "'{SHA}5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8',"
062:                    + "'"
063:                    + new Timestamp(new Timestamp(System.currentTimeMillis())
064:                            .getTime()).toString() + "'" + ");";
065:
066:            /**
067:             * @see junit.framework.TestCase#setUp()
068:             */
069:            protected void setUp() throws Exception {
070:                super .setUp();
071:
072:                // Set up the mock JNDI initial context
073:                TestJNDIContext.initialize();
074:                Context initCtx = new InitialContext();
075:                initCtx.bind("java:comp/env", new TestJNDIContext());
076:                Context ctx = (Context) initCtx.lookup("java:comp/env");
077:                DataSource ds = new TestJDBCDataSource(new File(
078:                        "build.properties"));
079:                ctx.bind(JDBCUserDatabase.DEFAULT_DB_JNDI_NAME, ds);
080:
081:                // Get the JDBC connection and init tables
082:                try {
083:                    Connection conn = ds.getConnection();
084:                    Statement stmt = conn.createStatement();
085:                    String sql;
086:
087:                    sql = "DELETE FROM " + JDBCUserDatabase.DEFAULT_DB_TABLE
088:                            + ";";
089:                    stmt.executeUpdate(sql);
090:
091:                    // Create a new test user 'janne'
092:                    stmt.executeUpdate(INSERT_JANNE);
093:
094:                    // Create a new test user 'user'
095:                    stmt.executeUpdate(INSERT_USER);
096:                    stmt.close();
097:
098:                    conn.close();
099:
100:                    // Initialize the user database
101:                    m_db = new JDBCUserDatabase();
102:                    m_db.initialize(null, new Properties());
103:                } catch (SQLException e) {
104:                    System.err
105:                            .println("Looks like your database could not be connected to - "
106:                                    + "please make sure that you have started your database "
107:                                    + "(e.g. by running ant hsql-start)");
108:
109:                    throw (SQLException) e.fillInStackTrace();
110:                }
111:            }
112:
113:            public void testDeleteByLoginName() throws WikiSecurityException {
114:                // First, count the number of users in the db now.
115:                int oldUserCount = m_db.getWikiNames().length;
116:
117:                // Create a new user with random name
118:                String loginName = "TestUser"
119:                        + String.valueOf(System.currentTimeMillis());
120:                UserProfile profile = new DefaultUserProfile();
121:                profile.setEmail("testuser@testville.com");
122:                profile.setLoginName(loginName);
123:                profile.setFullname("FullName" + loginName);
124:                profile.setPassword("password");
125:                m_db.save(profile);
126:
127:                // Make sure the profile saved successfully
128:                profile = m_db.findByLoginName(loginName);
129:                assertEquals(loginName, profile.getLoginName());
130:                assertEquals(oldUserCount + 1, m_db.getWikiNames().length);
131:
132:                // Now delete the profile; should be back to old count
133:                m_db.deleteByLoginName(loginName);
134:                assertEquals(oldUserCount, m_db.getWikiNames().length);
135:            }
136:
137:            public void testFindByEmail() {
138:                try {
139:                    UserProfile profile = m_db.findByEmail("janne@ecyrd.com");
140:                    assertEquals("janne", profile.getLoginName());
141:                    assertEquals("Janne Jalkanen", profile.getFullname());
142:                    assertEquals("JanneJalkanen", profile.getWikiName());
143:                    assertEquals(
144:                            "{SHA}457b08e825da547c3b77fbc1ff906a1d00a7daee",
145:                            profile.getPassword());
146:                    assertEquals("janne@ecyrd.com", profile.getEmail());
147:                    assertNotNull(profile.getCreated());
148:                    assertNull(profile.getLastModified());
149:                } catch (NoSuchPrincipalException e) {
150:                    assertTrue(false);
151:                }
152:                try {
153:                    m_db.findByEmail("foo@bar.org");
154:                    // We should never get here
155:                    assertTrue(false);
156:                } catch (NoSuchPrincipalException e) {
157:                    assertTrue(true);
158:                }
159:            }
160:
161:            public void testFindByFullName() {
162:                try {
163:                    UserProfile profile = m_db.findByFullName("Janne Jalkanen");
164:                    assertEquals("janne", profile.getLoginName());
165:                    assertEquals("Janne Jalkanen", profile.getFullname());
166:                    assertEquals("JanneJalkanen", profile.getWikiName());
167:                    assertEquals(
168:                            "{SHA}457b08e825da547c3b77fbc1ff906a1d00a7daee",
169:                            profile.getPassword());
170:                    assertEquals("janne@ecyrd.com", profile.getEmail());
171:                    assertNotNull(profile.getCreated());
172:                    assertNull(profile.getLastModified());
173:                } catch (NoSuchPrincipalException e) {
174:                    assertTrue(false);
175:                }
176:                try {
177:                    m_db.findByEmail("foo@bar.org");
178:                    // We should never get here
179:                    assertTrue(false);
180:                } catch (NoSuchPrincipalException e) {
181:                    assertTrue(true);
182:                }
183:            }
184:
185:            public void testFindByWikiName() {
186:                try {
187:                    UserProfile profile = m_db.findByWikiName("JanneJalkanen");
188:                    assertEquals("janne", profile.getLoginName());
189:                    assertEquals("Janne Jalkanen", profile.getFullname());
190:                    assertEquals("JanneJalkanen", profile.getWikiName());
191:                    assertEquals(
192:                            "{SHA}457b08e825da547c3b77fbc1ff906a1d00a7daee",
193:                            profile.getPassword());
194:                    assertEquals("janne@ecyrd.com", profile.getEmail());
195:                    assertNotNull(profile.getCreated());
196:                    assertNull(profile.getLastModified());
197:                } catch (NoSuchPrincipalException e) {
198:                    assertTrue(false);
199:                }
200:                try {
201:                    m_db.findByEmail("foo");
202:                    // We should never get here
203:                    assertTrue(false);
204:                } catch (NoSuchPrincipalException e) {
205:                    assertTrue(true);
206:                }
207:            }
208:
209:            public void testFindByLoginName() {
210:                try {
211:                    UserProfile profile = m_db.findByLoginName("janne");
212:                    assertEquals("janne", profile.getLoginName());
213:                    assertEquals("Janne Jalkanen", profile.getFullname());
214:                    assertEquals("JanneJalkanen", profile.getWikiName());
215:                    assertEquals(
216:                            "{SHA}457b08e825da547c3b77fbc1ff906a1d00a7daee",
217:                            profile.getPassword());
218:                    assertEquals("janne@ecyrd.com", profile.getEmail());
219:                    assertNotNull(profile.getCreated());
220:                    assertNull(profile.getLastModified());
221:                } catch (NoSuchPrincipalException e) {
222:                    assertTrue(false);
223:                }
224:                try {
225:                    m_db.findByEmail("FooBar");
226:                    // We should never get here
227:                    assertTrue(false);
228:                } catch (NoSuchPrincipalException e) {
229:                    assertTrue(true);
230:                }
231:            }
232:
233:            public void testGetWikiName() throws WikiSecurityException {
234:                Principal[] principals = m_db.getWikiNames();
235:                assertEquals(1, principals.length);
236:            }
237:
238:            public void testRename() throws Exception {
239:                // Try renaming a non-existent profile; it should fail
240:                try {
241:                    m_db.rename("nonexistentname", "renameduser");
242:                    fail("Should not have allowed rename...");
243:                } catch (NoSuchPrincipalException e) {
244:                    // Cool; that's what we expect
245:                }
246:
247:                // Create new user & verify it saved ok
248:                UserProfile profile = new DefaultUserProfile();
249:                profile.setEmail("renamed@example.com");
250:                profile.setFullname("Renamed User");
251:                profile.setLoginName("olduser");
252:                profile.setPassword("password");
253:                m_db.save(profile);
254:                profile = m_db.findByLoginName("olduser");
255:                assertNotNull(profile);
256:
257:                // Try renaming to a login name that's already taken; it should fail
258:                try {
259:                    m_db.rename("olduser", "janne");
260:                    fail("Should not have allowed rename...");
261:                } catch (DuplicateUserException e) {
262:                    // Cool; that's what we expect
263:                }
264:
265:                // Now, rename it to an unused name
266:                m_db.rename("olduser", "renameduser");
267:
268:                // The old user shouldn't be found
269:                try {
270:                    profile = m_db.findByLoginName("olduser");
271:                    fail("Old user was found, but it shouldn't have been.");
272:                } catch (NoSuchPrincipalException e) {
273:                    // Cool, it's gone
274:                }
275:
276:                // The new profile should be found, and its properties should match the old ones
277:                profile = m_db.findByLoginName("renameduser");
278:                assertEquals("renamed@example.com", profile.getEmail());
279:                assertEquals("Renamed User", profile.getFullname());
280:                assertEquals("renameduser", profile.getLoginName());
281:                assertEquals("{SHA}5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8",
282:                        profile.getPassword());
283:
284:                // Delete the user
285:                m_db.deleteByLoginName("renameduser");
286:            }
287:
288:            public void testSave() {
289:                try {
290:                    // Overwrite existing user
291:                    UserProfile profile = new DefaultUserProfile();
292:                    profile.setEmail("user@example.com");
293:                    profile.setFullname("Test User");
294:                    profile.setLoginName("user");
295:                    profile.setPassword("password");
296:                    m_db.save(profile);
297:                    profile = m_db.findByEmail("user@example.com");
298:                    assertEquals("user@example.com", profile.getEmail());
299:                    assertEquals("Test User", profile.getFullname());
300:                    assertEquals("user", profile.getLoginName());
301:                    assertEquals(
302:                            "{SHA}5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8",
303:                            profile.getPassword());
304:                    assertEquals("TestUser", profile.getWikiName());
305:                    assertNotNull(profile.getCreated());
306:                    assertNotNull(profile.getLastModified());
307:                    assertNotSame(profile.getCreated(), profile
308:                            .getLastModified());
309:
310:                    // Create new user
311:                    profile = new DefaultUserProfile();
312:                    profile.setEmail("user2@example.com");
313:                    profile.setFullname("Test User 2");
314:                    profile.setLoginName("user2");
315:                    profile.setPassword("password");
316:                    m_db.save(profile);
317:                    profile = m_db.findByEmail("user2@example.com");
318:                    assertEquals("user2@example.com", profile.getEmail());
319:                    assertEquals("Test User 2", profile.getFullname());
320:                    assertEquals("user2", profile.getLoginName());
321:                    assertEquals(
322:                            "{SHA}5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8",
323:                            profile.getPassword());
324:                    assertEquals("TestUser2", profile.getWikiName());
325:                    assertNotNull(profile.getCreated());
326:                    assertNotNull(profile.getLastModified());
327:                    assertEquals(profile.getCreated(), profile
328:                            .getLastModified());
329:                } catch (NoSuchPrincipalException e) {
330:                    assertTrue(false);
331:                } catch (WikiSecurityException e) {
332:                    assertTrue(false);
333:                }
334:            }
335:
336:            public void testValidatePassword() {
337:                assertFalse(m_db.validatePassword("janne", "test"));
338:                assertTrue(m_db.validatePassword("janne", "myP@5sw0rd"));
339:                assertTrue(m_db.validatePassword("user", "password"));
340:            }
341:
342:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.