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


001:        package com.ecyrd.jspwiki.auth.authorize;
002:
003:        import java.io.File;
004:        import java.security.Principal;
005:        import java.sql.Connection;
006:        import java.sql.SQLException;
007:        import java.util.Properties;
008:
009:        import javax.naming.Context;
010:        import javax.naming.InitialContext;
011:        import javax.sql.DataSource;
012:
013:        import junit.framework.TestCase;
014:
015:        import com.ecyrd.jspwiki.*;
016:        import com.ecyrd.jspwiki.auth.NoSuchPrincipalException;
017:        import com.ecyrd.jspwiki.auth.WikiPrincipal;
018:        import com.ecyrd.jspwiki.auth.WikiSecurityException;
019:
020:        /**
021:         * @author Andrew Jaquith
022:         */
023:        public class JDBCGroupDatabaseTest extends TestCase {
024:            private Connection m_conn = null;
025:
026:            private JDBCGroupDatabase m_db = null;
027:
028:            private String m_wiki;
029:
030:            /**
031:             * @see junit.framework.TestCase#setUp()
032:             */
033:            protected void setUp() throws Exception {
034:                super .setUp();
035:
036:                Properties props = new Properties();
037:                props.load(TestEngine.findTestProperties());
038:                WikiEngine engine = new TestEngine(props);
039:                m_wiki = engine.getApplicationName();
040:
041:                // Set up the mock JNDI initial context
042:                TestJNDIContext.initialize();
043:                Context initCtx = new InitialContext();
044:                initCtx.bind("java:comp/env", new TestJNDIContext());
045:                Context ctx = (Context) initCtx.lookup("java:comp/env");
046:                DataSource ds = new TestJDBCDataSource(new File(
047:                        "build.properties"));
048:                ctx.bind(JDBCGroupDatabase.DEFAULT_GROUPDB_DATASOURCE, ds);
049:
050:                // Get the JDBC connection and init tables
051:
052:                try {
053:                    m_conn = ds.getConnection();
054:                } catch (SQLException e) {
055:                    System.err
056:                            .println("Looks like your database could not be connected to - "
057:                                    + "please make sure that you have started your database "
058:                                    + "(e.g. by running ant hsql-start)");
059:
060:                    throw (SQLException) e.fillInStackTrace();
061:                }
062:
063:                // Initialize the user database
064:                m_db = new JDBCGroupDatabase();
065:                m_db.initialize(engine, new Properties());
066:            }
067:
068:            public void tearDown() throws Exception {
069:                if (m_conn != null) {
070:                    m_conn.close();
071:                }
072:            }
073:
074:            public void testDelete() throws WikiException {
075:                // First, count the number of groups in the db now.
076:                int oldUserCount = m_db.groups().length;
077:
078:                // Create a new group with random name
079:                String name = "TestGroup"
080:                        + String.valueOf(System.currentTimeMillis());
081:                Group group = new Group(name, m_wiki);
082:                Principal al = new WikiPrincipal("Al");
083:                Principal bob = new WikiPrincipal("Bob");
084:                group.add(al);
085:                group.add(bob);
086:                m_db.save(group, new WikiPrincipal("Tester"));
087:
088:                // Make sure the profile saved successfully
089:                group = backendGroup(name);
090:                assertEquals(name, group.getName());
091:                assertEquals(oldUserCount + 1, m_db.groups().length);
092:
093:                // Now delete the profile; should be back to old count
094:                m_db.delete(group);
095:                assertEquals(oldUserCount, m_db.groups().length);
096:            }
097:
098:            public void testGroups() throws WikiSecurityException {
099:                // Test file has 4 groups in it: TV, Literature, Art, and Admin
100:                Group[] groups = m_db.groups();
101:                assertEquals(4, groups.length);
102:
103:                Group group;
104:
105:                // Group TV has 3 members
106:                group = backendGroup("TV");
107:                assertEquals("TV", group.getName());
108:                assertEquals(3, group.members().length);
109:
110:                // Group Literature has 2 members
111:                group = backendGroup("Literature");
112:                assertEquals("Literature", group.getName());
113:                assertEquals(2, group.members().length);
114:
115:                // Group Art has no members
116:                group = backendGroup("Art");
117:                assertEquals("Art", group.getName());
118:                assertEquals(0, group.members().length);
119:
120:                // Group Admin has 1 member (Administrator)
121:                group = backendGroup("Admin");
122:                assertEquals("Admin", group.getName());
123:                assertEquals(1, group.members().length);
124:                assertEquals("Administrator", group.members()[0].getName());
125:
126:                // Group Archaeology doesn't exist
127:                try {
128:                    group = backendGroup("Archaeology");
129:                    // We should never get here
130:                    assertTrue(false);
131:                } catch (NoSuchPrincipalException e) {
132:                    assertTrue(true);
133:                }
134:            }
135:
136:            public void testSave() throws Exception {
137:                // Create a new group with random name
138:                String name = "TestGroup"
139:                        + String.valueOf(System.currentTimeMillis());
140:                Group group = new Group(name, m_wiki);
141:                Principal al = new WikiPrincipal("Al");
142:                Principal bob = new WikiPrincipal("Bob");
143:                Principal cookie = new WikiPrincipal("Cookie");
144:                group.add(al);
145:                group.add(bob);
146:                group.add(cookie);
147:                m_db.save(group, new WikiPrincipal("Tester"));
148:
149:                // Make sure the profile saved successfully
150:                group = backendGroup(name);
151:                assertEquals(name, group.getName());
152:                assertEquals(3, group.members().length);
153:                assertTrue(group.isMember(new WikiPrincipal("Al")));
154:                assertTrue(group.isMember(new WikiPrincipal("Bob")));
155:                assertTrue(group.isMember(new WikiPrincipal("Cookie")));
156:
157:                // The back-end should have timestamped the create/modify fields
158:                assertNotNull(group.getCreator());
159:                assertEquals("Tester", group.getCreator());
160:                assertNotNull(group.getCreated());
161:                assertNotNull(group.getModifier());
162:                assertEquals("Tester", group.getModifier());
163:                assertNotNull(group.getLastModified());
164:                assertNotSame(group.getCreated(), group.getLastModified());
165:
166:                // Remove the group
167:                m_db.delete(group);
168:            }
169:
170:            public void testResave() throws Exception {
171:                // Create a new group with random name & 3 members
172:                String name = "TestGroup"
173:                        + String.valueOf(System.currentTimeMillis());
174:                Group group = new Group(name, m_wiki);
175:                Principal al = new WikiPrincipal("Al");
176:                Principal bob = new WikiPrincipal("Bob");
177:                Principal cookie = new WikiPrincipal("Cookie");
178:                group.add(al);
179:                group.add(bob);
180:                group.add(cookie);
181:                m_db.save(group, new WikiPrincipal("Tester"));
182:
183:                // Make sure the profile saved successfully
184:                group = backendGroup(name);
185:                assertEquals(name, group.getName());
186:
187:                // Modify the members by adding the group; re-add Al while we're at it
188:                Principal dave = new WikiPrincipal("Dave");
189:                group.add(al);
190:                group.add(dave);
191:                m_db.save(group, new WikiPrincipal("SecondTester"));
192:
193:                // We should see 4 members and new timestamp info
194:                Principal[] members = group.members();
195:                assertEquals(4, members.length);
196:                assertNotNull(group.getCreator());
197:                assertEquals("Tester", group.getCreator());
198:                assertNotNull(group.getCreated());
199:                assertNotNull(group.getModifier());
200:                assertEquals("SecondTester", group.getModifier());
201:                assertNotNull(group.getLastModified());
202:
203:                // Check the back-end; We should see the same thing
204:                group = backendGroup(name);
205:                members = group.members();
206:                assertEquals(4, members.length);
207:                assertNotNull(group.getCreator());
208:                assertEquals("Tester", group.getCreator());
209:                assertNotNull(group.getCreated());
210:                assertNotNull(group.getModifier());
211:                assertEquals("SecondTester", group.getModifier());
212:                assertNotNull(group.getLastModified());
213:
214:                // Remove the group
215:                m_db.delete(group);
216:            }
217:
218:            private Group backendGroup(String name)
219:                    throws WikiSecurityException {
220:                Group[] groups = m_db.groups();
221:                for (int i = 0; i < groups.length; i++) {
222:                    Group group = groups[i];
223:                    if (group.getName().equals(name)) {
224:                        return group;
225:                    }
226:                }
227:                throw new NoSuchPrincipalException("No group named " + name);
228:            }
229:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.