Clover Coverage Report - Impex Parent 1.0.21-SNAPSHOT (Aggregated)
Coverage timestamp: Tue Feb 8 2011 11:33:53 EST
../../../../img/srcFileCovDistChart0.png 0% of files have more coverage
46   108   18   7.67
14   90   0.39   6
6     3  
1    
 
  JdbcConfigurer       Line # 14 46 0% 18 66 0% 0.0
 
No Tests
 
1    package org.apache.torque.util;
2   
3    import static org.apache.commons.lang.StringUtils.isBlank;
4    import static org.apache.commons.lang.StringUtils.isEmpty;
5   
6    import java.util.Map;
7   
8    import org.apache.commons.beanutils.BeanUtils;
9    import org.kuali.core.db.torque.PropertyHandlingException;
10    import org.kuali.db.jdbc.DatabaseType;
11    import org.kuali.db.jdbc.JDBCConfiguration;
12    import org.kuali.db.jdbc.JDBCUtils;
13   
 
14    public class JdbcConfigurer {
15    JDBCUtils jdbcUtils = new JDBCUtils();
16   
 
17  0 toggle @SuppressWarnings("unchecked")
18    protected boolean hasProperty(String name, Object bean) throws PropertyHandlingException {
19  0 try {
20  0 Map<String, Object> description = BeanUtils.describe(bean);
21  0 return description.containsKey(name);
22    } catch (Exception e) {
23  0 throw new PropertyHandlingException(e);
24    }
25   
26    }
27   
 
28  0 toggle @SuppressWarnings("unchecked")
29    protected <T> T getProperty(String name, Object bean) {
30  0 try {
31  0 if (hasProperty(name, bean)) {
32  0 return (T) BeanUtils.getProperty(bean, name);
33    } else {
34  0 return null;
35    }
36    } catch (Exception e) {
37  0 throw new RuntimeException(e);
38    }
39    }
40   
 
41  0 toggle protected void setProperty(Object bean, String name, Object value) throws PropertyHandlingException {
42  0 try {
43  0 BeanUtils.copyProperty(bean, name, value);
44    } catch (Exception e) {
45  0 throw new PropertyHandlingException(e);
46    }
47    }
48   
 
49  0 toggle public void updateConfiguration(Object bean) throws PropertyHandlingException {
50  0 String url = getProperty("url", bean);
51  0 if (isEmpty(url)) {
52    // If the url is empty, there is nothing to do
53  0 return;
54    }
55   
56  0 JDBCConfiguration config = jdbcUtils.getDatabaseConfiguration(url);
57  0 if (config.equals(JDBCConfiguration.UNKNOWN_CONFIG)) {
58  0 return;
59    }
60   
61  0 String driver = getProperty("driver", bean);
62  0 if (isBlank(driver)) {
63  0 setProperty(bean, "driver", config.getDriver());
64    }
65   
66  0 String targetDatabase = getProperty("targetDatabase", bean);
67  0 if (isBlank(targetDatabase)) {
68  0 setProperty(bean, "targetDatabase", config.getType().toString().toLowerCase());
69    }
70    }
71   
 
72  0 toggle public void validateConfiguration(Object bean) {
73  0 String driver = getProperty("driver", bean);
74  0 if (isBlank(driver)) {
75  0 throw new IllegalArgumentException("No database driver. Specify one in the plugin configuration.");
76    }
77   
78  0 String url = getProperty("url", bean);
79  0 if (isBlank(url)) {
80  0 throw new IllegalArgumentException(getEmptyURLErrorMessage());
81    }
82   
83  0 String targetDatabase = getProperty("targetDatabase", bean);
84  0 try {
85  0 DatabaseType.valueOf(targetDatabase.toUpperCase());
86    } catch (IllegalArgumentException e) {
87  0 throw new IllegalArgumentException("Database type of '" + targetDatabase + "' is invalid. Valid values: " + org.springframework.util.StringUtils.arrayToCommaDelimitedString(DatabaseType.values()));
88    }
89   
90  0 try {
91  0 Class.forName(driver);
92    } catch (ClassNotFoundException e) {
93  0 throw new IllegalArgumentException("Can't load driver class " + driver + ". Be sure to include it as a plugin dependency.");
94    }
95    }
96   
 
97  0 toggle protected String getEmptyURLErrorMessage() {
98  0 StringBuffer sb = new StringBuffer();
99  0 sb.append("\n\n");
100  0 sb.append("No url was supplied.\n");
101  0 sb.append("You can specify a url in the plugin configuration or provide it as a system property.\n\n");
102  0 sb.append("For example:\n\n");
103  0 sb.append("-Durl=jdbc:oracle:thin:@localhost:1521:XE (oracle)\n");
104  0 sb.append("-Durl=jdbc:mysql://localhost:3306/<database> (mysql)\n");
105  0 sb.append("\n.");
106  0 return sb.toString();
107    }
108    }