initial version of support project with build support.
[utils] / test / org / wamblee / test / 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;
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 public class HibernateUtils {
35     
36     private static final String DATABASE_PROPS = "test.database.properties";
37
38     /**
39      * @param dir
40      * @return
41      */
42     public static Configuration getConfiguration(File dir) throws IOException {
43         Configuration conf = new Configuration();
44         File[] files = dir.listFiles((FileFilter)(new AwkFilenameFilter(".*\\.hbm\\.xml")));
45         for (File f: files) { 
46             System.out.println("Mapping file: " + f);
47             conf.addFile(f);
48         }
49       
50         Map<String,String> dbProps = getHibernateProperties(); 
51         
52         for (Map.Entry<String,String> entry : dbProps.entrySet()) {
53             System.out.println("Property: " + entry.getKey() + "=" + entry.getValue());
54             conf.setProperty(entry.getKey(), entry.getValue());
55         }
56     
57         return conf;
58     }
59     
60     private static Map<String,String> getHibernateProperties() throws IOException {
61         
62         System.out.println( "Reading properties file: " +  DATABASE_PROPS);
63         InputResource lPropFile = new ClassPathResource( DATABASE_PROPS );
64         Properties props = new Properties(); 
65         props.load( lPropFile.getInputStream( ) );
66         
67         Map<String,String> result = new TreeMap<String,String>(); 
68         result.put( "hibernate.connection.driver_class", props.getProperty( "database.driver" ) );
69         result.put( "hibernate.connection.url", props.getProperty( "database.url" ) );
70         result.put( "hibernate.connection.username", props.getProperty( "database.username" ) );
71         result.put( "hibernate.connection.password", props.getProperty( "database.password" ) );
72         
73         return result;     
74     }
75
76 }