1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity;
17  
18  import org.springframework.jdbc.core.JdbcTemplate;
19  import org.springframework.jdbc.datasource.DriverManagerDataSource;
20  
21  import javax.sql.DataSource;
22  
23  
24  /***
25   * Singleton which provides a populated database connection for all
26   * JDBC-related unit tests.
27   *
28   * @author Ben Alex
29   * @version $Id: PopulatedDatabase.java,v 1.4 2005/11/17 00:55:47 benalex Exp $
30   */
31  public class PopulatedDatabase {
32      //~ Static fields/initializers =============================================
33  
34      private static DriverManagerDataSource dataSource = null;
35  
36      //~ Constructors ===========================================================
37  
38      private PopulatedDatabase() {}
39  
40      //~ Methods ================================================================
41  
42      public static DataSource getDataSource() {
43          if (dataSource == null) {
44              setupDataSource();
45          }
46  
47          return dataSource;
48      }
49  
50      private static void setupDataSource() {
51          dataSource = new DriverManagerDataSource();
52          dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
53          dataSource.setUrl("jdbc:hsqldb:mem:acegisecuritytest");
54          dataSource.setUsername("sa");
55          dataSource.setPassword("");
56  
57          JdbcTemplate template = new JdbcTemplate(dataSource);
58  
59          template.execute(
60              "CREATE TABLE USERS(USERNAME VARCHAR_IGNORECASE(50) NOT NULL PRIMARY KEY,PASSWORD VARCHAR_IGNORECASE(50) NOT NULL,ENABLED BOOLEAN NOT NULL)");
61          template.execute(
62              "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))");
63          template.execute(
64              "CREATE UNIQUE INDEX IX_AUTH_USERNAME ON AUTHORITIES(USERNAME,AUTHORITY)");
65          template.execute(
66              "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))");
67          template.execute(
68              "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))");
69          template.execute("SET IGNORECASE TRUE");
70          template.execute("INSERT INTO USERS VALUES('dianne','emu',TRUE)");
71          template.execute("INSERT INTO USERS VALUES('marissa','koala',TRUE)");
72          template.execute("INSERT INTO USERS VALUES('peter','opal',FALSE)");
73          template.execute("INSERT INTO USERS VALUES('scott','wombat',TRUE)");
74          template.execute("INSERT INTO USERS VALUES('cooper','kookaburra',TRUE)");
75          template.execute(
76              "INSERT INTO AUTHORITIES VALUES('marissa','ROLE_TELLER')");
77          template.execute(
78              "INSERT INTO AUTHORITIES VALUES('marissa','ROLE_SUPERVISOR')");
79          template.execute(
80              "INSERT INTO AUTHORITIES VALUES('dianne','ROLE_TELLER')");
81          template.execute(
82              "INSERT INTO AUTHORITIES VALUES('scott','ROLE_TELLER')");
83          template.execute(
84              "INSERT INTO AUTHORITIES VALUES('peter','ROLE_TELLER')");
85          template.execute(
86              "INSERT INTO acl_object_identity VALUES (1, 'org.acegisecurity.acl.DomainObject:1', null, 'org.acegisecurity.acl.basic.SimpleAclEntry');");
87          template.execute(
88              "INSERT INTO acl_object_identity VALUES (2, 'org.acegisecurity.acl.DomainObject:2', 1, 'org.acegisecurity.acl.basic.SimpleAclEntry');");
89          template.execute(
90              "INSERT INTO acl_object_identity VALUES (3, 'org.acegisecurity.acl.DomainObject:3', 1, 'org.acegisecurity.acl.basic.SimpleAclEntry');");
91          template.execute(
92              "INSERT INTO acl_object_identity VALUES (4, 'org.acegisecurity.acl.DomainObject:4', 1, 'org.acegisecurity.acl.basic.SimpleAclEntry');");
93          template.execute(
94              "INSERT INTO acl_object_identity VALUES (5, 'org.acegisecurity.acl.DomainObject:5', 3, 'org.acegisecurity.acl.basic.SimpleAclEntry');");
95          template.execute(
96              "INSERT INTO acl_object_identity VALUES (6, 'org.acegisecurity.acl.DomainObject:6', 3, 'org.acegisecurity.acl.basic.SimpleAclEntry');");
97  
98          // ----- BEGIN deviation from normal sample data load script -----
99          template.execute(
100             "INSERT INTO acl_object_identity VALUES (7, 'org.acegisecurity.acl.DomainObject:7', 3, 'some.invalid.acl.entry.class');");
101 
102         // ----- FINISH deviation from normal sample data load script -----
103         template.execute(
104             "INSERT INTO acl_permission VALUES (null, 1, 'ROLE_SUPERVISOR', 1);");
105         template.execute(
106             "INSERT INTO acl_permission VALUES (null, 2, 'ROLE_SUPERVISOR', 0);");
107         template.execute(
108             "INSERT INTO acl_permission VALUES (null, 2, 'marissa', 2);");
109         template.execute(
110             "INSERT INTO acl_permission VALUES (null, 3, 'scott', 14);");
111         template.execute(
112             "INSERT INTO acl_permission VALUES (null, 6, 'scott', 1);");
113     }
114 }