1 /* ====================================================================
2 * License:
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * 3. The end-user documentation included with the redistribution,
17 * if any, must include the following acknowledgment:
18 * "This product includes software developed by
19 * Robert Half International (http://www.rhi.com/)."
20 * Alternately, this acknowledgment may appear in the software itself,
21 * if and wherever such third-party acknowledgments normally appear.
22 *
23 * 4. The names "Parc", "RHI", and "Robert Half International" must
24 * not be used to endorse or promote products derived from this
25 * software without prior written permission. For written
26 * permission, please contact pete.mckinstry@rhi.com.
27 *
28 * 5. Products derived from this software may not be called "PARC",
29 * nor may "PARC" appear in their name, without prior written
30 * permission of Robert Half International.
31 *
32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
36 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
39 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
41 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
42 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
44 * ====================================================================
45 *
46 */
47 package com.rhi.architecture.parc;
48
49 import com.rhi.architecture.logging.DefaultLogger;
50 import com.rhi.architecture.logging.Logger;
51 import com.rhi.architecture.resource.ResourceContext;
52
53 import java.io.IOException;
54 import java.io.InputStream;
55 import java.util.Properties;
56
57 import junit.framework.TestCase;
58
59
60 /***
61 * PARCTest - base class for anything testing parc components.
62 * provides the basics required to get components working in
63 * a pseudo-normal sandbox.
64 *
65 * @author Pete McKinstry
66 * @version 1.0
67 */
68 public abstract class PARCTest extends TestCase {
69
70 /***
71 * logger
72 */
73 public static Logger log = new DefaultLogger();
74
75 private ResourceContext ctx = null;
76
77 private boolean loadDriver = true;
78
79 /***
80 * constructor
81 * @param name
82 */
83 public PARCTest( String name ) {
84 super( name );
85 }
86 /***
87 * @return Properties
88 */
89 public Properties getProperties() {
90 Properties p = new Properties();
91
92 // load properties
93 String fullClassName = PARCTest.class.getName();
94 int idx = fullClassName.lastIndexOf(".");
95 StringBuffer tmp = new StringBuffer();
96 tmp.append(
97 fullClassName.substring(idx+1, fullClassName.length()) );
98 tmp.append( ".properties" );
99 String propertyFile = tmp.toString();
100 System.err.println("property file = " + propertyFile );
101 InputStream str = Thread.currentThread().getContextClassLoader().
102 getResourceAsStream(propertyFile);
103 if (str!=null) {
104 try {
105 p.load(str);
106 }
107 catch (IOException e) {
108 throw new RuntimeException(
109 "Unable to load " + propertyFile + ".properties.");
110 }
111 }
112
113 // specific bean configuration can be loaded here...
114 p = doGetProperties( p );
115
116 if (loadDriver()) {
117 // Since we aren't using the bootstrap mixin class, we
118 // need to force the initialization of the db driver.
119 String driver = p.getProperty("driver");
120 if (driver !=null) {
121 try {
122 Class driverClass = Class.forName(driver);
123 log.debug("driver = " + driverClass.getName());
124 }
125 catch (ClassNotFoundException e) {
126 log.error("Unable to load db driver = " + driver );
127 }
128 }
129 }
130 // debug info
131 //p.list(System.out);
132
133 return p;
134 }
135
136 /***
137 * Allow subclasses to add extra params. Default impl provided.
138 * @param p
139 * @return Properties
140 */
141 public Properties doGetProperties( Properties p ) {
142 String fullClassName = this.getClass().getName();
143 int idx = fullClassName.lastIndexOf(".");
144 StringBuffer tmp = new StringBuffer();
145 tmp.append(
146 fullClassName.substring(idx+1, fullClassName.length()) );
147 tmp.append( ".properties" );
148 String propertyFile = tmp.toString();
149 System.err.println("property file = " + propertyFile );
150 InputStream str = Thread.currentThread().getContextClassLoader().
151 getResourceAsStream(propertyFile);
152 if (str!=null) {
153 try {
154 p.load(str);
155 }
156 catch (IOException e) {
157 throw new RuntimeException(
158 "Unable to load " + propertyFile + ".properties.");
159 }
160 }
161 return p;
162 }
163
164
165 /***
166 * setup
167 * @throws Exception
168 */
169 protected void setUp() throws Exception {
170 super.setUp();
171 try {
172 ctx = new ResourceContext();
173 Properties p = getProperties();
174 p.list(System.err);
175 log.debug("calling ResourceContext.init()");
176 ctx.init( p );
177 }
178 catch (Exception e) {
179 log.debug(e.toString());
180 e.printStackTrace();
181 throw new java.lang.RuntimeException(
182 "ResourceContext failed to initialize. " +
183 " e = " + e.toString() );
184 }
185 }
186
187 /***
188 * tear down
189 * @throws Exception
190 */
191 protected void tearDown() throws Exception {
192 super.tearDown();
193 ctx.cleanup();
194 }
195
196 /***
197 * Returns the loadDriver.
198 * @return boolean
199 */
200 public boolean loadDriver() {
201 return loadDriver;
202 }
203
204 /***
205 * Sets the loadDriver.
206 * @param loadDriver The loadDriver to set
207 */
208 public void setLoadDriver(boolean loadDriver) {
209 this.loadDriver = loadDriver;
210 }
211
212 }
This page was automatically generated by Maven