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;
017:
018: import org.springframework.jdbc.core.JdbcTemplate;
019: import org.springframework.jdbc.datasource.DriverManagerDataSource;
020:
021: import javax.sql.DataSource;
022:
023: /**
024: * Singleton which provides a populated database connection for all JDBC-related unit tests.
025: *
026: * @author Ben Alex
027: * @version $Id: PopulatedDatabase.java 1496 2006-05-23 13:38:33Z benalex $
028: */
029: public class PopulatedDatabase {
030: //~ Static fields/initializers =====================================================================================
031:
032: private static DriverManagerDataSource dataSource = null;
033:
034: //~ Constructors ===================================================================================================
035:
036: private PopulatedDatabase() {
037: }
038:
039: //~ Methods ========================================================================================================
040:
041: public static DataSource getDataSource() {
042: if (dataSource == null) {
043: setupDataSource();
044: }
045:
046: return dataSource;
047: }
048:
049: private static void setupDataSource() {
050: dataSource = new DriverManagerDataSource();
051: dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
052: dataSource.setUrl("jdbc:hsqldb:mem:acegisecuritytest");
053: dataSource.setUsername("sa");
054: dataSource.setPassword("");
055:
056: JdbcTemplate template = new JdbcTemplate(dataSource);
057:
058: template
059: .execute("CREATE TABLE USERS(USERNAME VARCHAR_IGNORECASE(50) NOT NULL PRIMARY KEY,PASSWORD VARCHAR_IGNORECASE(50) NOT NULL,ENABLED BOOLEAN NOT NULL)");
060: template
061: .execute("CREATE TABLE AUTHORITIES(USERNAME VARCHAR_IGNORECASE(50) NOT NULL,AUTHORITY VARCHAR_IGNORECASE(50) NOT NULL,CONSTRAINT FK_AUTHORITIES_USERS FOREIGN KEY(USERNAME) REFERENCES USERS(USERNAME))");
062: template
063: .execute("CREATE UNIQUE INDEX IX_AUTH_USERNAME ON AUTHORITIES(USERNAME,AUTHORITY)");
064: template
065: .execute("CREATE TABLE ACL_OBJECT_IDENTITY(ID BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 0) NOT NULL PRIMARY KEY,OBJECT_IDENTITY VARCHAR_IGNORECASE(250) NOT NULL,PARENT_OBJECT BIGINT,ACL_CLASS VARCHAR_IGNORECASE(250) NOT NULL,CONSTRAINT UNIQUE_OBJECT_IDENTITY UNIQUE(OBJECT_IDENTITY),CONSTRAINT SYS_FK_3 FOREIGN KEY(PARENT_OBJECT) REFERENCES ACL_OBJECT_IDENTITY(ID))");
066: template
067: .execute("CREATE TABLE ACL_PERMISSION(ID BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 0) NOT NULL PRIMARY KEY,ACL_OBJECT_IDENTITY BIGINT NOT NULL,RECIPIENT VARCHAR_IGNORECASE(100) NOT NULL,MASK INTEGER NOT NULL,CONSTRAINT UNIQUE_RECIPIENT UNIQUE(ACL_OBJECT_IDENTITY,RECIPIENT),CONSTRAINT SYS_FK_7 FOREIGN KEY(ACL_OBJECT_IDENTITY) REFERENCES ACL_OBJECT_IDENTITY(ID))");
068: template.execute("SET IGNORECASE TRUE");
069: template
070: .execute("INSERT INTO USERS VALUES('dianne','emu',TRUE)");
071: template
072: .execute("INSERT INTO USERS VALUES('marissa','koala',TRUE)");
073: template
074: .execute("INSERT INTO USERS VALUES('peter','opal',FALSE)");
075: template
076: .execute("INSERT INTO USERS VALUES('scott','wombat',TRUE)");
077: template
078: .execute("INSERT INTO USERS VALUES('cooper','kookaburra',TRUE)");
079: template
080: .execute("INSERT INTO AUTHORITIES VALUES('marissa','ROLE_TELLER')");
081: template
082: .execute("INSERT INTO AUTHORITIES VALUES('marissa','ROLE_SUPERVISOR')");
083: template
084: .execute("INSERT INTO AUTHORITIES VALUES('dianne','ROLE_TELLER')");
085: template
086: .execute("INSERT INTO AUTHORITIES VALUES('scott','ROLE_TELLER')");
087: template
088: .execute("INSERT INTO AUTHORITIES VALUES('peter','ROLE_TELLER')");
089: template
090: .execute("INSERT INTO acl_object_identity VALUES (1, 'org.acegisecurity.acl.DomainObject:1', null, 'org.acegisecurity.acl.basic.SimpleAclEntry');");
091: template
092: .execute("INSERT INTO acl_object_identity VALUES (2, 'org.acegisecurity.acl.DomainObject:2', 1, 'org.acegisecurity.acl.basic.SimpleAclEntry');");
093: template
094: .execute("INSERT INTO acl_object_identity VALUES (3, 'org.acegisecurity.acl.DomainObject:3', 1, 'org.acegisecurity.acl.basic.SimpleAclEntry');");
095: template
096: .execute("INSERT INTO acl_object_identity VALUES (4, 'org.acegisecurity.acl.DomainObject:4', 1, 'org.acegisecurity.acl.basic.SimpleAclEntry');");
097: template
098: .execute("INSERT INTO acl_object_identity VALUES (5, 'org.acegisecurity.acl.DomainObject:5', 3, 'org.acegisecurity.acl.basic.SimpleAclEntry');");
099: template
100: .execute("INSERT INTO acl_object_identity VALUES (6, 'org.acegisecurity.acl.DomainObject:6', 3, 'org.acegisecurity.acl.basic.SimpleAclEntry');");
101:
102: // ----- BEGIN deviation from normal sample data load script -----
103: template
104: .execute("INSERT INTO acl_object_identity VALUES (7, 'org.acegisecurity.acl.DomainObject:7', 3, 'some.invalid.acl.entry.class');");
105:
106: // ----- FINISH deviation from normal sample data load script -----
107: template
108: .execute("INSERT INTO acl_permission VALUES (null, 1, 'ROLE_SUPERVISOR', 1);");
109: template
110: .execute("INSERT INTO acl_permission VALUES (null, 2, 'ROLE_SUPERVISOR', 0);");
111: template
112: .execute("INSERT INTO acl_permission VALUES (null, 2, 'marissa', 2);");
113: template
114: .execute("INSERT INTO acl_permission VALUES (null, 3, 'scott', 14);");
115: template
116: .execute("INSERT INTO acl_permission VALUES (null, 6, 'scott', 1);");
117: }
118: }
|