KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > PropertiesTest


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
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 package scriptella;
17
18 import scriptella.configuration.ConfigurationFactory;
19 import scriptella.core.ConnectionManager;
20 import scriptella.core.SqlTestHelper;
21 import scriptella.execution.EtlContext;
22 import scriptella.execution.EtlExecutor;
23 import scriptella.execution.EtlExecutorException;
24 import scriptella.interactive.ProgressIndicator;
25 import scriptella.spi.ConnectionParameters;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30
31 /**
32  * TODO: Add documentation
33  *
34  * @author Fyodor Kupolov
35  * @version 1.0
36  */

37 public class PropertiesTest extends AbstractTestCase {
38     private EtlContext ctx; //execution context
39
private ConnectionParameters params;
40
41     public void test() throws EtlExecutorException {
42         EtlExecutor se = prepareExecutor(null);
43         se.execute();
44
45         assertEquals("jdbc:hsqldb:mem:propertiestest", params.getUrl());
46         assertEquals("sa", params.getUser());
47         assertEquals("", params.getPassword());
48
49         //check substituted properties in a context
50
assertEquals("1", ctx.getParameter("a"));
51         assertEquals("bar", ctx.getParameter("foo"));
52         assertEquals("1", ctx.getParameter("var"));
53         assertEquals("1|1|1|1|1|1", ctx.getParameter("b"));
54         assertEquals("jdbc:hsqldb:mem", ctx.getParameter("url.prefix"));
55         assertEquals("propertiestest", ctx.getParameter("dbname"));
56         assertEquals("org.hsqldb.jdbcDriver", ctx.getParameter("driver"));
57         assertEquals("org.hsqldb.jdbcDriver", ctx.getParameter("driver"));
58         assertEquals("jdbc:hsqldb:mem:propertiestest", ctx.getParameter("url"));
59         assertEquals("sa", ctx.getParameter("user"));
60         assertEquals("", ctx.getParameter("password"));
61         Map JavaDoc<String JavaDoc,String JavaDoc> extra = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
62         extra.put("var", "2");
63         se = prepareExecutor(extra);
64         se.execute();
65         assertEquals("2", ctx.getParameter("var"));
66         assertEquals("2|2|2|2|2|2", ctx.getParameter("b"));
67     }
68
69     private EtlExecutor prepareExecutor(Map JavaDoc<String JavaDoc,String JavaDoc> props) {
70         ConfigurationFactory cf = new ConfigurationFactory();
71         cf.setResourceURL(getClass().getResource(getClass().getSimpleName()+".xml"));
72         cf.setExternalProperties(props);
73         return new EtlExecutor(cf.createConfiguration()) {
74             //overrides prepare method to get ctx and params for connection
75
@Override JavaDoc
76             protected EtlContext prepare(final ProgressIndicator indicator) {
77                 ctx = super.prepare(indicator);
78                 Map JavaDoc<String JavaDoc, ConnectionManager> connections = SqlTestHelper.getConnections(ctx.getSession());
79                 ConnectionManager con = connections.entrySet().iterator().next().getValue();
80                 params = SqlTestHelper.getConnectionParameters(con);
81                 return ctx;
82             }
83         };
84
85     }
86
87 }
88
Popular Tags