1 /*
2 * File: HierarchicalDBConfigMixinTest.java
3 * Date: Nov 20, 2003
4 */
5 package com.rhi.architecture.batch;
6
7 import java.sql.Connection;
8 import java.sql.DriverManager;
9 import java.sql.SQLException;
10 import java.sql.Statement;
11 import java.util.Properties;
12
13 import com.rhi.architecture.db.DBUtil;
14 import com.rhi.architecture.resource.InitializationException;
15
16 import junit.framework.Test;
17 import junit.framework.TestCase;
18 import junit.framework.TestSuite;
19 import junit.textui.TestRunner;
20
21 /***
22 * @author <a href="mailto:josh.tolle@rhi.com">Josh Tolle</a>
23 * @copyright 2003, Robert Half International. All rights reserved.
24 *
25 * @version 1.0
26 */
27 public class HierarchicalDBConfigMixinTest extends TestCase {
28 private static final String DB_DRIVER = "org.hsqldb.jdbcDriver";
29 private static final String DB_PASS = "";
30 private static final String DB_USER = "sa";
31 private static final String DB_URL = "jdbc:hsqldb:testdb";
32 private static final String ENV = "qa";
33 private static final String SQL_QUERY = "select name, value from rh_ia_int_prog_config where application = ? and environment = ?";
34 private static final String MISSING_PROP = "missing";
35 private Properties baseProps = new Properties();
36 private Properties oneTierProps = new Properties();
37 private Properties twoTierProps = new Properties();
38 private Properties threeTierProps1 = new Properties();
39 private Properties threeTierProps2 = new Properties();
40
41 static {
42 try {
43 Class.forName(DB_DRIVER);
44 }
45 catch (ClassNotFoundException e) {
46 System.err.println("ClassNotFoundException: " + e);
47 e.printStackTrace(System.err);
48 }
49 }
50
51 public HierarchicalDBConfigMixinTest() {
52 super();
53 }
54
55 public HierarchicalDBConfigMixinTest(String arg0) {
56 super(arg0);
57 }
58 protected void setUp() throws Exception {
59 super.setUp();
60 String firstApp = "first";
61 String secondApp = "second";
62 String thirdApp = "third";
63 String overFirstApp = "overFirst";
64 String propEnv1 = "common";
65 String propEnv2 = "dev";
66 String propEnv3 = "qa";
67 String propName1 = "prop1";
68 String propName2 = "prop2";
69 String propName3 = "prop3";
70 String propName4 = "prop4";
71 String propName5 = "prop5";
72 String propName6 = "prop6";
73 String propName7 = "prop7";
74 String propName8 = "prop8";
75 String propName9 = "prop9";
76 // Create local database for use by the ConfigMixin
77 Connection conn = null;
78 Statement stmt = null;
79 try {
80 conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
81 stmt = conn.createStatement();
82 int result = stmt.executeUpdate("create table rh_ia_int_prog_config " +
83 "( application VARCHAR, environment VARCHAR, name VARCHAR, value VARCHAR )");
84 if (result < 0) {
85 throw new Exception("error creating config table");
86 }
87 result = stmt.executeUpdate("insert into rh_ia_int_prog_config " +
88 "( application, environment, name, value ) " +
89 "values ( '" + firstApp + "', '" + propEnv1 + "', '" + propName1 + "', '" + firstApp + "');");
90 if (result != 1) {
91 throw new Exception("error inserting into config table");
92 }
93 result = stmt.executeUpdate("insert into rh_ia_int_prog_config " +
94 "( application, environment, name, value ) " +
95 "values ( '" + firstApp + "', '" + propEnv2 + "', '" + propName2 + "', '" + firstApp + "');");
96 if (result != 1) {
97 throw new Exception("error inserting into config table");
98 }
99 result = stmt.executeUpdate("insert into rh_ia_int_prog_config " +
100 "( application, environment, name, value ) " +
101 "values ( '" + firstApp + "', '" + propEnv1 + "', '" + propName3 + "', '" + firstApp + "');");
102 if (result != 1) {
103 throw new Exception("error inserting into config table");
104 }
105 result = stmt.executeUpdate("insert into rh_ia_int_prog_config " +
106 "( application, environment, name, value ) " +
107 "values ( '" + firstApp + "', '" + propEnv2 + "', '" + propName3 + "', '" + overFirstApp + "');");
108 if (result != 1) {
109 throw new Exception("error inserting into config table");
110 }
111 result = stmt.executeUpdate("insert into rh_ia_int_prog_config " +
112 "( application, environment, name, value ) " +
113 "values ( '" + secondApp + "', '" + propEnv1 + "', '" + propName4 + "', '" + secondApp + "');");
114 if (result != 1) {
115 throw new Exception("error inserting into config table");
116 }
117 result = stmt.executeUpdate("insert into rh_ia_int_prog_config " +
118 "( application, environment, name, value ) " +
119 "values ( '" + secondApp + "', '" + propEnv2 + "', '" + propName5 + "', '" + secondApp + "');");
120 if (result != 1) {
121 throw new Exception("error inserting into config table");
122 }
123 result = stmt.executeUpdate("insert into rh_ia_int_prog_config " +
124 "( application, environment, name, value ) " +
125 "values ( '" + secondApp + "', '" + propEnv1 + "', '" + propName6 + "', '" + secondApp + "');");
126 if (result != 1) {
127 throw new Exception("error inserting into config table");
128 }
129 result = stmt.executeUpdate("insert into rh_ia_int_prog_config " +
130 "( application, environment, name, value ) " +
131 "values ( '" + secondApp + "', '" + propEnv2 + "', '" + propName7 + "', '" + secondApp + "');");
132 if (result != 1) {
133 throw new Exception("error inserting into config table");
134 }
135 result = stmt.executeUpdate("insert into rh_ia_int_prog_config " +
136 "( application, environment, name, value ) " +
137 "values ( '" + secondApp + "', '" + propEnv2 + "', '" + propName1 + "', '" + secondApp + "');");
138 if (result != 1) {
139 throw new Exception("error inserting into config table");
140 }
141 result = stmt.executeUpdate("insert into rh_ia_int_prog_config " +
142 "( application, environment, name, value ) " +
143 "values ( '" + thirdApp + "', '" + propEnv1 + "', '" + propName8 + "', '" + thirdApp + "');");
144 if (result != 1) {
145 throw new Exception("error inserting into config table");
146 }
147 result = stmt.executeUpdate("insert into rh_ia_int_prog_config " +
148 "( application, environment, name, value ) " +
149 "values ( '" + thirdApp + "', '" + propEnv2 + "', '" + propName9 + "', '" + thirdApp + "');");
150 if (result != 1) {
151 throw new Exception("error inserting into config table");
152 }
153 result = stmt.executeUpdate("insert into rh_ia_int_prog_config " +
154 "( application, environment, name, value ) " +
155 "values ( '" + thirdApp + "', '" + propEnv1 + "', '" + propName7 + "', '" + thirdApp + "');");
156 if (result != 1) {
157 throw new Exception("error inserting into config table");
158 }
159 result = stmt.executeUpdate("insert into rh_ia_int_prog_config " +
160 "( application, environment, name, value ) " +
161 "values ( '" + thirdApp + "', '" + propEnv2 + "', '" + propName2 + "', '" + thirdApp + "');");
162 if (result != 1) {
163 throw new Exception("error inserting into config table");
164 }
165 result = stmt.executeUpdate("insert into rh_ia_int_prog_config " +
166 "( application, environment, name, value ) " +
167 "values ( '" + thirdApp + "', '" + propEnv3 + "', '" + propName1 + "', '" + thirdApp + "');");
168 if (result != 1) {
169 throw new Exception("error inserting into config table");
170 }
171 result = stmt.executeUpdate("insert into rh_ia_int_prog_config " +
172 "( application, environment, name, value ) " +
173 "values ( '" + secondApp + "', '" + propEnv1 + "', '" + HierarchicalDBConfigMixin.PARENT_PROPERTY_NAME + "', '" + firstApp + "');");
174 if (result != 1) {
175 throw new Exception("error inserting into config table");
176 }
177 result = stmt.executeUpdate("insert into rh_ia_int_prog_config " +
178 "( application, environment, name, value ) " +
179 "values ( '" + thirdApp + "', '" + propEnv1 + "', '" + HierarchicalDBConfigMixin.PARENT_PROPERTY_NAME + "', '" + secondApp + "');");
180 if (result != 1) {
181 throw new Exception("error inserting into config table");
182 }
183 result = stmt.executeUpdate("insert into rh_ia_int_prog_config " +
184 "( application, environment, name, value ) " +
185 "values ( '" + thirdApp + "', '" + propEnv3 + "', '" + HierarchicalDBConfigMixin.PARENT_PROPERTY_NAME + "', '" + firstApp + "');");
186 if (result != 1) {
187 throw new Exception("error inserting into config table");
188 }
189 }
190 finally {
191 DBUtil.close(stmt);
192 DBUtil.close(conn);
193 }
194
195 // Populate properties object to hand to ConfigMixin
196 baseProps.setProperty(HierarchicalDBConfigMixin.DB_DRIVER_PROPERTY_NAME, DB_DRIVER);
197 baseProps.setProperty(HierarchicalDBConfigMixin.DB_PASSWORD_PROPERTY_NAME, DB_PASS);
198 baseProps.setProperty(HierarchicalDBConfigMixin.DB_USER_PROPERTY_NAME, DB_USER);
199 baseProps.setProperty(HierarchicalDBConfigMixin.DB_URL_PROPERTY_NAME, DB_URL);
200 baseProps.setProperty(HierarchicalDBConfigMixin.SQL_QUERY_PROPERTY_NAME, SQL_QUERY);
201
202 // Configure the first tier testing properties
203 oneTierProps.putAll(baseProps);
204 oneTierProps.setProperty(propName1, firstApp);
205 oneTierProps.setProperty(propName2, firstApp);
206 oneTierProps.setProperty(propName3, overFirstApp);
207 oneTierProps.setProperty("HierarchicalDBConfigMixin.val0", firstApp);
208 oneTierProps.setProperty("HierarchicalDBConfigMixin.val1", propEnv2);
209
210 // Configure the second tier testing properties
211 twoTierProps.putAll(oneTierProps);
212 twoTierProps.setProperty(propName4, secondApp);
213 twoTierProps.setProperty(propName5, secondApp);
214 twoTierProps.setProperty(propName6, secondApp);
215 twoTierProps.setProperty(propName7, secondApp);
216 twoTierProps.setProperty(propName1, secondApp);
217 twoTierProps.setProperty(HierarchicalDBConfigMixin.PARENT_PROPERTY_NAME, firstApp);
218 twoTierProps.setProperty("HierarchicalDBConfigMixin.val0", secondApp);
219
220 // Configure the third tier testing properties with the previously used environment
221 threeTierProps1.putAll(twoTierProps);
222 threeTierProps1.setProperty(propName8, thirdApp);
223 threeTierProps1.setProperty(propName9, thirdApp);
224 threeTierProps1.setProperty(propName2, thirdApp);
225 threeTierProps1.setProperty(propName7, thirdApp);
226 threeTierProps1.setProperty(HierarchicalDBConfigMixin.PARENT_PROPERTY_NAME, secondApp);
227 threeTierProps1.setProperty("HierarchicalDBConfigMixin.val0", thirdApp);
228
229 // Configure the third tier testing properties with the uncommon environment
230 threeTierProps2.putAll(baseProps);
231 threeTierProps2.setProperty(propName1, thirdApp);
232 threeTierProps2.setProperty(propName3, firstApp);
233 threeTierProps2.setProperty(propName8, thirdApp);
234 threeTierProps2.setProperty(propName7, thirdApp);
235 threeTierProps2.setProperty(HierarchicalDBConfigMixin.PARENT_PROPERTY_NAME, firstApp);
236 threeTierProps2.setProperty("HierarchicalDBConfigMixin.val0", thirdApp);
237 threeTierProps2.setProperty("HierarchicalDBConfigMixin.val1", propEnv3);
238 }
239 protected void tearDown() throws Exception {
240 super.tearDown();
241 Connection conn = null;
242 Statement stmt = null;
243 try {
244 conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
245 stmt = conn.createStatement();
246 stmt.executeUpdate("drop table rh_ia_int_prog_config ");
247 }
248 catch (SQLException e) {
249 System.err.println("SQLException: " +e);
250 e.printStackTrace(System.err);
251 }
252 finally {
253 DBUtil.close(stmt);
254 DBUtil.close(conn);
255 }
256 }
257 public static Test suite() {
258 return new TestSuite(HierarchicalDBConfigMixinTest.class);
259 }
260 public static void main(String[] args) {
261 TestRunner.run(suite());
262 }
263 public void testOneTierProperties() {
264 ConfigMixin mixin = new HierarchicalDBConfigMixin();
265 Properties testProps = new Properties();
266 testProps.putAll(baseProps);
267 testProps.setProperty("HierarchicalDBConfigMixin.val0", oneTierProps.getProperty("HierarchicalDBConfigMixin.val0"));
268 testProps.setProperty("HierarchicalDBConfigMixin.val1", oneTierProps.getProperty("HierarchicalDBConfigMixin.val1"));
269 try {
270 mixin.loadConfiguration(testProps);
271 } catch (InitializationException e) {
272 System.err.println("error loading properties from the database");
273 e.printStackTrace();
274 }
275 if (!testProps.equals(oneTierProps)) {
276 System.err.println("results form HierarchicalDBConfigMixin seem wrong");
277 System.err.println("mixin properties size: " + testProps.size());
278 System.err.println("from mixin: ");
279 testProps.list(System.err);
280 System.out.println("template properties size: " + oneTierProps.size());
281 System.out.println("from template: ");
282 oneTierProps.list(System.out);
283 fail("invalid properties from HierarchicalDBConfigMixin");
284 }
285 else {
286 System.out.println("properties loaded from db successfully");
287 }
288 }
289 public void testTwoTierProperties() {
290 ConfigMixin mixin = new HierarchicalDBConfigMixin();
291 Properties testProps = new Properties();
292 testProps.putAll(baseProps);
293 testProps.setProperty("HierarchicalDBConfigMixin.val0", twoTierProps.getProperty("HierarchicalDBConfigMixin.val0"));
294 testProps.setProperty("HierarchicalDBConfigMixin.val1", twoTierProps.getProperty("HierarchicalDBConfigMixin.val1"));
295 try {
296 mixin.loadConfiguration(testProps);
297 } catch (InitializationException e) {
298 System.err.println("error loading properties from the database");
299 e.printStackTrace();
300 }
301 if (!testProps.equals(twoTierProps)) {
302 System.err.println("results form HierarchicalDBConfigMixin seem wrong");
303 System.err.println("mixin properties size: " + testProps.size());
304 System.err.println("from mixin: ");
305 testProps.list(System.err);
306 System.out.println("template properties size: " + twoTierProps.size());
307 System.out.println("from template: ");
308 twoTierProps.list(System.out);
309 fail("invalid properties from HierarchicalDBConfigMixin");
310 }
311 else {
312 System.out.println("properties loaded from db successfully");
313 }
314 }
315 public void testThreeTierProperties1() {
316 ConfigMixin mixin = new HierarchicalDBConfigMixin();
317 Properties testProps = new Properties();
318 testProps.putAll(baseProps);
319 testProps.setProperty("HierarchicalDBConfigMixin.val0", threeTierProps1.getProperty("HierarchicalDBConfigMixin.val0"));
320 testProps.setProperty("HierarchicalDBConfigMixin.val1", threeTierProps1.getProperty("HierarchicalDBConfigMixin.val1"));
321 try {
322 mixin.loadConfiguration(testProps);
323 } catch (InitializationException e) {
324 System.err.println("error loading properties from the database");
325 e.printStackTrace();
326 }
327 if (!testProps.equals(threeTierProps1)) {
328 System.err.println("results form HierarchicalDBConfigMixin seem wrong");
329 System.err.println("mixin properties size: " + testProps.size());
330 System.err.println("from mixin: ");
331 testProps.list(System.err);
332 System.out.println("template properties size: " + threeTierProps1.size());
333 System.out.println("from template: ");
334 threeTierProps1.list(System.out);
335 fail("invalid properties from HierarchicalDBConfigMixin");
336 }
337 else {
338 System.out.println("properties loaded from db successfully");
339 }
340 }
341 public void testThreeTierProperties2() {
342 ConfigMixin mixin = new HierarchicalDBConfigMixin();
343 Properties testProps = new Properties();
344 testProps.putAll(baseProps);
345 testProps.setProperty("HierarchicalDBConfigMixin.val0", threeTierProps2.getProperty("HierarchicalDBConfigMixin.val0"));
346 testProps.setProperty("HierarchicalDBConfigMixin.val1", threeTierProps2.getProperty("HierarchicalDBConfigMixin.val1"));
347 try {
348 mixin.loadConfiguration(testProps);
349 } catch (InitializationException e) {
350 System.err.println("error loading properties from the database");
351 e.printStackTrace();
352 }
353 if (!testProps.equals(threeTierProps2)) {
354 System.err.println("results form HierarchicalDBConfigMixin seem wrong");
355 System.err.println("mixin properties size: " + testProps.size());
356 System.err.println("from mixin: ");
357 testProps.list(System.err);
358 System.out.println("template properties size: " + threeTierProps2.size());
359 System.out.println("from template: ");
360 threeTierProps2.list(System.out);
361 fail("invalid properties from HierarchicalDBConfigMixin");
362 }
363 else {
364 System.out.println("properties loaded from db successfully");
365 }
366 }
367 }
This page was automatically generated by Maven