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