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