KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > ext > database > DatabaseTestCase


1 /*
2 jGuard is a security framework based on top of jaas (java authentication and authorization security).
3 it is written for web applications, to resolve simply, access control problems.
4 version $Name: v080beta1 $
5 http://sourceforge.net/projects/jguard/
6
7 Copyright (C) 2004 Charles GAY
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23
24 jGuard project home page:
25 http://sourceforge.net/projects/jguard/
26
27 */

28 package net.sf.jguard.ext.database;
29
30 import java.io.File JavaDoc;
31 import java.io.FileInputStream JavaDoc;
32 import java.io.FileNotFoundException JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.net.URI JavaDoc;
35 import java.net.URISyntaxException JavaDoc;
36 import java.net.URL JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.Properties JavaDoc;
40
41 import junit.framework.TestCase;
42 import net.sf.jguard.core.CoreConstants;
43 import net.sf.jguard.core.util.FileUtils;
44 import net.sf.jguard.ext.SecurityConstants;
45
46 public abstract class DatabaseTestCase extends TestCase {
47     private static Properties JavaDoc properties = null;
48     private static Map JavaDoc options = null;
49     private static ConnectionFactory connectionFactory = null;
50     private static String JavaDoc propertiesURIPath = null;
51     protected String JavaDoc database = null;
52
53     public void setUp(){
54
55         try {
56             super.setUp();
57         } catch (Exception JavaDoc e) {
58             System.out.println(e.getMessage());
59         }
60         database = System.getProperty("database");
61         System.out.println("'database' system property="+database);
62         Properties JavaDoc databaseProperties = null;
63         if(database==null||database.equals("")){
64             database = "hsqldb";
65             System.setProperty("database", "hsqldb");
66         }
67         options = new HashMap JavaDoc();
68         databaseProperties = new Properties JavaDoc();
69         String JavaDoc databasePropertiesLocation = null;
70         if(database.equals("JNDI")){
71             options.put("JNDI","java:/comp/env.jguard");
72             databasePropertiesLocation = File.separator+"database"+File.separator+"hsqldb.properties";
73             options.put(SecurityConstants.INITIAL_CONTEXT_FACTORY,"net.sf.jguard.ext.database.MockInitialContextFactory");
74
75         }else{
76             databasePropertiesLocation = "/database/"+database+".properties";
77         }
78
79         loadProperties(databaseProperties, databasePropertiesLocation);
80         options.put(CoreConstants.APPLICATION_NAME,"jGuardExample");
81         options.put(SecurityConstants.DATABASE_DRIVER,databaseProperties.get(SecurityConstants.DATABASE_DRIVER));
82         options.put(SecurityConstants.DATABASE_DRIVER_URL,databaseProperties.get(SecurityConstants.DATABASE_DRIVER_URL));
83         options.put(SecurityConstants.DATABASE_DRIVER_LOGIN,databaseProperties.get(SecurityConstants.DATABASE_DRIVER_LOGIN));
84         options.put(SecurityConstants.DATABASE_DRIVER_PASSWORD,databaseProperties.get(SecurityConstants.DATABASE_DRIVER_PASSWORD));
85
86         setConnectionFactory(options);
87     }
88
89     private void loadProperties(Properties JavaDoc properties, String JavaDoc propertiesLocation) {
90         try {
91             URL JavaDoc url = getClass().getResource(propertiesLocation);
92             if(url == null){
93                 throw new IllegalArgumentException JavaDoc(propertiesLocation +" not found ");
94             }
95             URI JavaDoc uri = null;
96             try {
97                 uri = new URI JavaDoc(url.toString());
98                 propertiesURIPath = uri.toString();
99             } catch (URISyntaxException JavaDoc e) {
100                 TestCase.fail(e.getMessage());
101                 e.printStackTrace();
102             }
103             File JavaDoc f = FileUtils.getFile(uri);
104             properties.load(new FileInputStream JavaDoc(f));
105         } catch (FileNotFoundException JavaDoc e) {
106             TestCase.fail(" propertiesLocation is not found ");
107             e.printStackTrace();
108         } catch (IOException JavaDoc e) {
109             TestCase.fail(" ioexception ");
110             e.printStackTrace();
111         }
112     }
113
114     public static void setConnectionFactory(Map JavaDoc options){
115         connectionFactory = new ConnectionFactory(options);
116     }
117
118     protected void setProperties(String JavaDoc propertiesLocation) {
119         properties = new Properties JavaDoc();
120         loadProperties(properties,propertiesLocation);
121     }
122
123
124
125     public static Properties JavaDoc getProperties() {
126         return properties;
127     }
128
129     public void setProperties(Properties JavaDoc props) {
130         properties = props;
131     }
132
133     public static ConnectionFactory getConnectionFactory() {
134         if(connectionFactory==null){
135             setConnectionFactory(options);
136         }
137         return connectionFactory;
138     }
139
140     public static void setConnectionFactory(ConnectionFactory connectionFactory) {
141         DatabaseTestCase.connectionFactory = connectionFactory;
142     }
143
144     public static Map JavaDoc getOptions() {
145         return options;
146     }
147
148     public static void setOptions(Map JavaDoc options) {
149         DatabaseTestCase.options = options;
150     }
151
152     public static String JavaDoc getPropertiesURIPath() {
153         return propertiesURIPath;
154     }
155
156     public static void setPropertiesURIPath(String JavaDoc propertiesURL) {
157         DatabaseTestCase.propertiesURIPath = propertiesURL;
158     }
159
160
161
162 }
163
Popular Tags