74a7ef7e44659b4f5dead4ea7e8203fd0325b106
[utils] / support / spring / src / test / java / org / wamblee / test / spring / HibernateUtils.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16
17 package org.wamblee.test.spring;
18
19 import java.io.File;
20 import java.io.FileFilter;
21 import java.io.IOException;
22 import java.util.Map;
23 import java.util.Properties;
24 import java.util.TreeMap;
25
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;
30
31 /**
32  * Hibernate utilities.
33  *
34  * @author Erik Brakkee
35  */
36 public final class HibernateUtils {
37
38     private static final String DATABASE_PROPS = "test.database.properties";
39
40     /**
41      * Disabled.
42      *
43      */
44     private HibernateUtils() { 
45         // Empty
46     }
47     
48     /**
49      * @param aDir
50      * @return
51      */
52     public static Configuration getConfiguration(File aDir) throws IOException {
53         Configuration conf = new Configuration();
54         File[] files = aDir.listFiles((FileFilter) (new AwkFilenameFilter(
55                 ".*\\.hbm\\.xml")));
56         for (File f : files) {
57             System.out.println("Mapping file: " + f);
58             conf.addFile(f);
59         }
60
61         Map<String, String> dbProps = getHibernateProperties();
62
63         for (Map.Entry<String, String> entry : dbProps.entrySet()) {
64             System.out.println("Property: " + entry.getKey() + "="
65                     + entry.getValue());
66             conf.setProperty(entry.getKey(), entry.getValue());
67         }
68
69         return conf;
70     }
71
72     private static Map<String, String> getHibernateProperties()
73             throws IOException {
74
75         System.out.println("Reading properties file: " + DATABASE_PROPS);
76         InputResource lPropFile = new ClassPathResource(DATABASE_PROPS);
77         Properties props = new Properties();
78         props.load(lPropFile.getInputStream());
79
80         Map<String, String> result = new TreeMap<String, String>();
81         result.put("hibernate.connection.driver_class", props
82                 .getProperty("database.driver"));
83         result.put("hibernate.connection.url", props
84                 .getProperty("database.url"));
85         result.put("hibernate.connection.username", props
86                 .getProperty("database.username"));
87         result.put("hibernate.connection.password", props
88                 .getProperty("database.password"));
89
90         return result;
91     }
92
93 }