2 * Copyright 2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.wamblee.test;
20 import java.io.FileFilter;
21 import java.io.IOException;
23 import java.util.Properties;
24 import java.util.TreeMap;
26 import org.apache.oro.io.AwkFilenameFilter;
27 import org.hibernate.cfg.Configuration;
28 import org.wamblee.io.ClassPathResource;
29 import org.wamblee.io.InputResource;
32 * Hibernate utilities.
34 public final class HibernateUtils {
36 private static final String DATABASE_PROPS = "test.database.properties";
42 private HibernateUtils() {
50 public static Configuration getConfiguration(File aDir) throws IOException {
51 Configuration conf = new Configuration();
52 File[] files = aDir.listFiles((FileFilter) (new AwkFilenameFilter(
54 for (File f : files) {
55 System.out.println("Mapping file: " + f);
59 Map<String, String> dbProps = getHibernateProperties();
61 for (Map.Entry<String, String> entry : dbProps.entrySet()) {
62 System.out.println("Property: " + entry.getKey() + "="
64 conf.setProperty(entry.getKey(), entry.getValue());
70 private static Map<String, String> getHibernateProperties()
73 System.out.println("Reading properties file: " + DATABASE_PROPS);
74 InputResource lPropFile = new ClassPathResource(DATABASE_PROPS);
75 Properties props = new Properties();
76 props.load(lPropFile.getInputStream());
78 Map<String, String> result = new TreeMap<String, String>();
79 result.put("hibernate.connection.driver_class", props
80 .getProperty("database.driver"));
81 result.put("hibernate.connection.url", props
82 .getProperty("database.url"));
83 result.put("hibernate.connection.username", props
84 .getProperty("database.username"));
85 result.put("hibernate.connection.password", props
86 .getProperty("database.password"));