1 /* 2 * File: DBConfigMixinTest.java 3 * Date: Nov 12, 2003 4 */ 5 package com.rhi.architecture.batch; 6 7 import com.rhi.architecture.db.*; 8 9 import java.util.*; 10 import java.sql.*; 11 12 import junit.framework.*; 13 import junit.textui.*; 14 15 /*** 16 * DBConfigMixinTest 17 * 18 * @author <a href="mailto:pete_mckinstry@yahoo.com">Pete McKinstry</a> 19 * @copyright 2003, All rights reserved. 20 */ 21 public class DBConfigMixinTest extends TestCase { 22 23 private static final String URL = "jdbc:hsqldb:testdb"; 24 private static final String DRIVER = "org.hsqldb.jdbcDriver"; 25 private static final String USER = "sa"; 26 private static final String PASSWORD = ""; 27 28 private static final String QUERY = "select * from config where " + 29 "app = ? and env = ?"; 30 31 static { 32 try { 33 Class.forName(DRIVER); 34 } 35 catch (ClassNotFoundException e) { 36 System.err.println("ClassNotFoundException: " + e); 37 e.printStackTrace(System.err); 38 } 39 } 40 41 /*** 42 * Constructor for DBConfigMixinTest. 43 * @param name 44 */ 45 public DBConfigMixinTest(String name) { 46 super(name); 47 } 48 49 /* 50 * @see TestCase#setUp() 51 */ 52 protected void setUp() throws Exception { 53 super.setUp(); 54 Connection conn = null; 55 Statement stmt = null; 56 try { 57 conn = DriverManager.getConnection(URL, USER, PASSWORD); 58 stmt = conn.createStatement(); 59 int result = stmt.executeUpdate("create table config " + 60 "( app VARCHAR, env VARCHAR, name VARCHAR, value VARCHAR )"); 61 if (result < 0) { 62 throw new Exception("error creating config table"); 63 } 64 result = stmt.executeUpdate("insert into config " + 65 "( app, env, name, value ) " + 66 "values ( 'testapp', 'qa', 'name', 'value');"); 67 if (result != 1) { 68 throw new Exception("error inserting into config table"); 69 } 70 } 71 finally { 72 DBUtil.close(stmt); 73 DBUtil.close(conn); 74 } 75 } 76 77 /* 78 * @see TestCase#tearDown() 79 */ 80 protected void tearDown() throws Exception { 81 super.tearDown(); 82 Connection conn = null; 83 Statement stmt = null; 84 try { 85 conn = DriverManager.getConnection(URL, USER, PASSWORD); 86 stmt = conn.createStatement(); 87 stmt.executeUpdate("drop table config "); 88 } 89 catch (SQLException e) { 90 System.err.println("SQLException: " +e); 91 e.printStackTrace(System.err); 92 } 93 finally { 94 DBUtil.close(stmt); 95 DBUtil.close(conn); 96 } 97 } 98 99 /*** 100 * unit test of db mixin. 101 * 102 * @throws Exception 103 */ 104 public void testLoadConfiguration() throws Exception { 105 Properties p = new Properties(); 106 p.setProperty(ApplicationConfig.PROG_NAME, "testapp"); 107 p.setProperty(ApplicationConfig.ENVIRONMENT, "qa"); 108 p.setProperty(DBConfigMixin.JDBC_DRIVER, DRIVER); 109 p.setProperty(DBConfigMixin.JDBC_URL, URL); 110 p.setProperty(DBConfigMixin.JDBC_USER, USER); 111 p.setProperty(DBConfigMixin.JDBC_PASSWORD, PASSWORD); 112 p.setProperty(DBConfigMixin.PROPS_QUERY, QUERY); 113 114 Properties template = new Properties(); 115 template.setProperty(ApplicationConfig.PROG_NAME, "testapp"); 116 template.setProperty(ApplicationConfig.ENVIRONMENT, "qa"); 117 template.setProperty(DBConfigMixin.JDBC_DRIVER, DRIVER); 118 template.setProperty(DBConfigMixin.JDBC_URL, URL); 119 template.setProperty(DBConfigMixin.JDBC_USER, USER); 120 template.setProperty(DBConfigMixin.JDBC_PASSWORD, PASSWORD); 121 template.setProperty(DBConfigMixin.PROPS_QUERY, QUERY); 122 template.setProperty("name", "value"); 123 124 ConfigMixin mixin = new DBConfigMixin(); 125 mixin.loadConfiguration(p); 126 if (!p.equals(template)) { 127 System.err.println("results form DBConfigMixin seem wrong"); 128 System.err.println("from mixin: "); 129 p.list(System.err); 130 System.out.println("from template: "); 131 template.list(System.out); 132 fail("invalid properties from DBConfigMixin"); 133 } 134 else { 135 System.out.println("properties loaded from db successfully"); 136 } 137 } 138 139 /*** 140 * return all unit tests as a suite. 141 * @return 142 */ 143 public static Test suite() { 144 return new TestSuite(DBConfigMixinTest.class); 145 } 146 147 /*** 148 * run all unit tests as application. 149 * @param args 150 */ 151 public static void main(String[] args) { 152 TestRunner.run(suite()); 153 } 154 155 }

This page was automatically generated by Maven