001: /*
002: jGuard is a security framework based on top of jaas (java authentication and authorization security).
003: it is written for web applications, to resolve simply, access control problems.
004: version $Name: v080beta1 $
005: http://sourceforge.net/projects/jguard/
006:
007: Copyright (C) 2004 Charles GAY
008:
009: This library is free software; you can redistribute it and/or
010: modify it under the terms of the GNU Lesser General Public
011: License as published by the Free Software Foundation; either
012: version 2.1 of the License, or (at your option) any later version.
013:
014: This library is distributed in the hope that it will be useful,
015: but WITHOUT ANY WARRANTY; without even the implied warranty of
016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: Lesser General Public License for more details.
018:
019: You should have received a copy of the GNU Lesser General Public
020: License along with this library; if not, write to the Free Software
021: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022:
023:
024: jGuard project home page:
025: http://sourceforge.net/projects/jguard/
026:
027: */
028: package net.sf.jguard.ext.database;
029:
030: import java.io.File;
031: import java.io.FileInputStream;
032: import java.io.FileNotFoundException;
033: import java.io.IOException;
034: import java.net.URI;
035: import java.net.URISyntaxException;
036: import java.net.URL;
037: import java.util.HashMap;
038: import java.util.Map;
039: import java.util.Properties;
040:
041: import junit.framework.TestCase;
042: import net.sf.jguard.core.CoreConstants;
043: import net.sf.jguard.core.util.FileUtils;
044: import net.sf.jguard.ext.SecurityConstants;
045:
046: public abstract class DatabaseTestCase extends TestCase {
047: private static Properties properties = null;
048: private static Map options = null;
049: private static ConnectionFactory connectionFactory = null;
050: private static String propertiesURIPath = null;
051: protected String database = null;
052:
053: public void setUp() {
054:
055: try {
056: super .setUp();
057: } catch (Exception e) {
058: System.out.println(e.getMessage());
059: }
060: database = System.getProperty("database");
061: System.out.println("'database' system property=" + database);
062: Properties databaseProperties = null;
063: if (database == null || database.equals("")) {
064: database = "hsqldb";
065: System.setProperty("database", "hsqldb");
066: }
067: options = new HashMap();
068: databaseProperties = new Properties();
069: String databasePropertiesLocation = null;
070: if (database.equals("JNDI")) {
071: options.put("JNDI", "java:/comp/env.jguard");
072: databasePropertiesLocation = File.separator + "database"
073: + File.separator + "hsqldb.properties";
074: options
075: .put(SecurityConstants.INITIAL_CONTEXT_FACTORY,
076: "net.sf.jguard.ext.database.MockInitialContextFactory");
077:
078: } else {
079: databasePropertiesLocation = "/database/" + database
080: + ".properties";
081: }
082:
083: loadProperties(databaseProperties, databasePropertiesLocation);
084: options.put(CoreConstants.APPLICATION_NAME, "jGuardExample");
085: options.put(SecurityConstants.DATABASE_DRIVER,
086: databaseProperties
087: .get(SecurityConstants.DATABASE_DRIVER));
088: options.put(SecurityConstants.DATABASE_DRIVER_URL,
089: databaseProperties
090: .get(SecurityConstants.DATABASE_DRIVER_URL));
091: options.put(SecurityConstants.DATABASE_DRIVER_LOGIN,
092: databaseProperties
093: .get(SecurityConstants.DATABASE_DRIVER_LOGIN));
094: options
095: .put(
096: SecurityConstants.DATABASE_DRIVER_PASSWORD,
097: databaseProperties
098: .get(SecurityConstants.DATABASE_DRIVER_PASSWORD));
099:
100: setConnectionFactory(options);
101: }
102:
103: private void loadProperties(Properties properties,
104: String propertiesLocation) {
105: try {
106: URL url = getClass().getResource(propertiesLocation);
107: if (url == null) {
108: throw new IllegalArgumentException(propertiesLocation
109: + " not found ");
110: }
111: URI uri = null;
112: try {
113: uri = new URI(url.toString());
114: propertiesURIPath = uri.toString();
115: } catch (URISyntaxException e) {
116: TestCase.fail(e.getMessage());
117: e.printStackTrace();
118: }
119: File f = FileUtils.getFile(uri);
120: properties.load(new FileInputStream(f));
121: } catch (FileNotFoundException e) {
122: TestCase.fail(" propertiesLocation is not found ");
123: e.printStackTrace();
124: } catch (IOException e) {
125: TestCase.fail(" ioexception ");
126: e.printStackTrace();
127: }
128: }
129:
130: public static void setConnectionFactory(Map options) {
131: connectionFactory = new ConnectionFactory(options);
132: }
133:
134: protected void setProperties(String propertiesLocation) {
135: properties = new Properties();
136: loadProperties(properties, propertiesLocation);
137: }
138:
139: public static Properties getProperties() {
140: return properties;
141: }
142:
143: public void setProperties(Properties props) {
144: properties = props;
145: }
146:
147: public static ConnectionFactory getConnectionFactory() {
148: if (connectionFactory == null) {
149: setConnectionFactory(options);
150: }
151: return connectionFactory;
152: }
153:
154: public static void setConnectionFactory(
155: ConnectionFactory connectionFactory) {
156: DatabaseTestCase.connectionFactory = connectionFactory;
157: }
158:
159: public static Map getOptions() {
160: return options;
161: }
162:
163: public static void setOptions(Map options) {
164: DatabaseTestCase.options = options;
165: }
166:
167: public static String getPropertiesURIPath() {
168: return propertiesURIPath;
169: }
170:
171: public static void setPropertiesURIPath(String propertiesURL) {
172: DatabaseTestCase.propertiesURIPath = propertiesURL;
173: }
174:
175: }
|