KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > regis > loader > RegistryConfigLoaderTest


1 package org.sapia.regis.loader;
2
3 import java.io.File JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.Map JavaDoc;
8 import java.util.Properties JavaDoc;
9
10 import junit.framework.TestCase;
11
12 import org.sapia.regis.Node;
13 import org.sapia.regis.Path;
14 import org.sapia.regis.Property;
15 import org.sapia.regis.RWNode;
16 import org.sapia.regis.RWSession;
17 import org.sapia.regis.Registry;
18 import org.sapia.regis.local.LocalRegistryFactory;
19
20 public class RegistryConfigLoaderTest extends TestCase {
21   
22   RegistryConfigLoader loader;
23   Registry reg;
24
25   public RegistryConfigLoaderTest(String JavaDoc arg0) {
26     super(arg0);
27   }
28
29   protected void setUp() throws Exception JavaDoc {
30     LocalRegistryFactory factory = new LocalRegistryFactory();
31     reg = factory.connect(new Properties JavaDoc());
32   }
33   
34   public void testCreate() throws Exception JavaDoc{
35     RWSession session = (RWSession)reg.open();
36     session.begin();
37     RWNode node = (RWNode)reg.getRoot();
38     RegistryConfigLoader loader = new RegistryConfigLoader(node);
39     loader.load(new File JavaDoc("etc/configCreateExample.xml"));
40     session.commit();
41     
42     Node db000 = node.getChild(Path.parse("databases/000"));
43     assertEquals("jsmith", db000.getProperty("username").asString());
44     assertEquals("foo123", db000.getProperty("password").asString());
45     assertEquals("jdbc://saturn:5432/", db000.getProperty("url").asString());
46     
47     Node db001 = node.getChild(Path.parse("databases/001"));
48     assertEquals("stiger", db001.getProperty("username").asString());
49     assertEquals("bar123", db001.getProperty("password").asString());
50     assertEquals("jdbc://pluto:5432/", db001.getProperty("url").asString());
51     
52     Node db002 = node.getChild(Path.parse("databases/002"));
53     assertEquals("stiger", db002.getProperty("username").asString());
54     assertEquals("bar123", db002.getProperty("password").asString());
55     assertEquals("jdbc://mercury:5432/", db002.getProperty("url").asString());
56     
57     Node account1 = node.getChild(Path.parse("users/backoffice/account1"));
58     assertEquals("cbrown", account1.getProperty("username").asString());
59     assertEquals("Charlie", account1.getProperty("firstName").asString());
60     assertEquals("Brown", account1.getProperty("lastName").asString());
61     assertEquals("lupus9890!", account1.getProperty("password").asString());
62     
63     Node account2 = node.getChild(Path.parse("users/backoffice/account2"));
64     assertEquals("dmenace", account2.getProperty("username").asString());
65     assertEquals("Dennis", account2.getProperty("firstName").asString());
66     assertEquals("Menace", account2.getProperty("lastName").asString());
67     assertEquals("canis$2677", account2.getProperty("password").asString());
68   }
69   
70   public void testUpdate() throws Exception JavaDoc{
71     RWSession session = (RWSession)reg.open();
72     session.begin();
73     RWNode node = (RWNode)reg.getRoot();
74     RegistryConfigLoader loader = new RegistryConfigLoader(node);
75     loader.load(new File JavaDoc("etc/configCreateExample.xml"));
76     loader = new RegistryConfigLoader(node);
77     loader.load(new File JavaDoc("etc/configUpdateExample.xml"));
78     session.commit();
79     
80     Node db000 = node.getChild(Path.parse("databases/000"));
81     assertTrue(db000 == null);
82     
83     Node db001 = node.getChild(Path.parse("databases/001"));
84     assertEquals("mrspock", db001.getProperty("username").asString());
85     assertEquals("bar123", db001.getProperty("password").asString());
86     assertEquals("jdbc://pluto:5432/", db001.getProperty("url").asString());
87     
88     Node account1 = node.getChild(Path.parse("users/backoffice/account1"));
89     assertEquals("cbrown", account1.getProperty("username").asString());
90     assertEquals("Charlie", account1.getProperty("firstName").asString());
91     assertEquals("Brown", account1.getProperty("lastName").asString());
92     assertEquals("lupus9890!", account1.getProperty("password").asString());
93     
94     Node address1 = node.getChild(Path.parse("users/backoffice/account1/address"));
95     assertEquals("1234 Sesame", address1.getProperty("street").asString());
96     assertEquals("New York", address1.getProperty("city").asString());
97     assertEquals("NY", address1.getProperty("state").asString());
98     assertEquals("US", address1.getProperty("country").asString());
99     
100     Node account2 = node.getChild(Path.parse("users/backoffice/account2"));
101     assertEquals("dmenace", account2.getProperty("username").asString());
102     assertEquals("Dennis", account2.getProperty("firstName").asString());
103     assertEquals("Menace", account2.getProperty("lastName").asString());
104     assertEquals("canis$2677", account2.getProperty("password").asString());
105   }
106   
107   public void testLink() throws Exception JavaDoc{
108     RWSession session = (RWSession)reg.open();
109     session.begin();
110     RWNode node = (RWNode)reg.getRoot();
111     RegistryConfigLoader loader = new RegistryConfigLoader(node);
112     loader.load(new File JavaDoc("etc/configLinkExample.xml"));
113     session.commit();
114     Node props = node.getChild(Path.parse("properties"));
115     assertEquals("value1", props.getProperty("prop1").asString());
116     assertEquals("value2", props.getProperty("prop2").asString());
117     assertEquals("value3", props.getProperty("prop3").asString());
118     assertEquals("globalValue", props.getProperty("globalProp").asString());
119   }
120   
121   public void testInclude() throws Exception JavaDoc{
122     RWSession session = (RWSession)reg.open();
123     session.begin();
124     RWNode node = (RWNode)reg.getRoot();
125     RegistryConfigLoader loader = new RegistryConfigLoader(node);
126     loader.load(new File JavaDoc("etc/configCreateExample.xml"));
127     session.commit();
128     
129     Node child = node.getChild(Path.parse("users/support"));
130     Collection JavaDoc children = child.getChildren();
131     assertEquals(3, children.size());
132     assertInclude("account1", children);
133     assertInclude("account2", children);
134     assertInclude("account3", children);
135   }
136   
137   public void testWhenConfig() throws Exception JavaDoc{
138     Map JavaDoc params = new HashMap JavaDoc();
139     params.put("env", "prod");
140     params.put("continent", "north-america-01");
141     RWSession session = (RWSession)reg.open();
142     session.begin();
143     RWNode node = (RWNode)reg.getRoot();
144     RegistryConfigLoader loader = new RegistryConfigLoader(node);
145     loader.load(new File JavaDoc("etc/configConditionExample.xml"), params);
146     session.commit();
147     
148     Node child = node.getChild(Path.parse("databases/000"));
149     assertEquals("jdbc://jupiter:5432/", child.getProperty("url").asString());
150     
151     child = node.getChild(Path.parse("connection"));
152     assertEquals("http://www.google.com", child.getProperty("url").asString());
153   }
154   
155   public void testOtherwiseConfig() throws Exception JavaDoc{
156     Map JavaDoc params = new HashMap JavaDoc();
157     params.put("env", "qa");
158     RWSession session = (RWSession)reg.open();
159     session.begin();
160     RWNode node = (RWNode)reg.getRoot();
161     RegistryConfigLoader loader = new RegistryConfigLoader(node);
162     loader.load(new File JavaDoc("etc/configConditionExample.xml"), params);
163     session.commit();
164     
165     Node child = node.getChild(Path.parse("databases/000"));
166     assertEquals("jdbc://saturn:5432/", child.getProperty("url").asString());
167   }
168   
169   public void testDefaultParamRefConfig() throws Exception JavaDoc{
170     Map JavaDoc params = new HashMap JavaDoc();
171     params.put("env", "dev");
172     RWSession session = (RWSession)reg.open();
173     session.begin();
174     RWNode node = (RWNode)reg.getRoot();
175     RegistryConfigLoader loader = new RegistryConfigLoader(node);
176     loader.load(new File JavaDoc("etc/configConditionExample.xml"), params);
177     session.commit();
178     
179     Node child = node.getChild(Path.parse("databases/000"));
180     assertEquals("jdbc://pluto:5432/", child.getProperty("url").asString());
181   }
182   
183   public void testParamRefConfig() throws Exception JavaDoc{
184     Map JavaDoc params = new HashMap JavaDoc();
185     params.put("env", "local");
186     params.put("local.db.url", "jdbc://mercury:5432/");
187     
188     RWSession session = (RWSession)reg.open();
189     session.begin();
190     RWNode node = (RWNode)reg.getRoot();
191     RegistryConfigLoader loader = new RegistryConfigLoader(node);
192     loader.load(new File JavaDoc("etc/configConditionExample.xml"), params);
193     session.commit();
194     
195     Node child = node.getChild(Path.parse("databases/000"));
196     assertEquals("jdbc://mercury:5432/", child.getProperty("url").asString());
197   }
198   
199   public void testLinkIncludePath() throws Exception JavaDoc{
200     RWSession session = (RWSession)reg.open();
201     session.begin();
202     RWNode node = (RWNode)reg.getRoot();
203     RegistryConfigLoader loader = new RegistryConfigLoader(node);
204     loader.load(new File JavaDoc("etc/multifile1.xml"));
205     loader.load(new File JavaDoc("etc/multifile2.xml"));
206     session.commit();
207     Node child = node.getChild(Path.parse("databaseInclude/000"));
208     assertEquals("000", child.getName());
209     child = node.getChild(Path.parse("databaseInclude/001"));
210     assertEquals("001", child.getName());
211     
212     child = node.getChild(Path.parse("userLink"));
213     assertEquals("jsmith", child.getProperty("username").asString());
214
215     child = node.getChild(Path.parse("userInclude/account1"));
216     assertEquals("account1", child.getName());
217
218     session.close();
219   }
220   
221   public void testCustomPropertyTag() throws Exception JavaDoc{
222     RWSession session = (RWSession)reg.open();
223     session.begin();
224     RWNode node = (RWNode)reg.getRoot();
225     RegistryConfigLoader loader = new RegistryConfigLoader(node);
226     loader.load(new File JavaDoc("etc/customPropertyTag.regis.xml"));
227     session.commit();
228     
229     HashMap JavaDoc map = new HashMap JavaDoc();
230     map.put("username", "jsmith");
231     map.put("password", "foo123");
232     map.put("url", "jdbc://saturn:5432/");
233     
234     Node child1 = node.getChild(Path.parse("databases/000"));
235     assertNode("000", map, child1);
236     
237     Node child2 = node.getChild(Path.parse("databases/000-custom"));
238     assertNode("000-custom", map, child2);
239
240     session.close();
241   }
242   
243   void assertInclude(String JavaDoc name, Collection JavaDoc children){
244     Iterator JavaDoc itr = children.iterator();
245     while(itr.hasNext()){
246       Node child = (Node)itr.next();
247       if(child.getName().equals(name)){
248         return;
249       }
250     }
251     fail("Could not find child for: " + name);
252   }
253   
254   protected static void assertNode(String JavaDoc eNodeName, Map JavaDoc eProperties, Node actual) {
255     assertNotNull("The actula node to assert should not be null", actual);
256     assertEquals("The node name is invalid", eNodeName, actual.getName());
257     assertEquals("The number of properties of the node is invalid", eProperties.size(), actual.getProperties().size());
258     for (Iterator JavaDoc it = eProperties.keySet().iterator(); it.hasNext(); ) {
259       String JavaDoc eKey = (String JavaDoc) it.next();
260       Object JavaDoc eValue = eProperties.get(eKey);
261       Property actualProp = actual.getProperty(eKey);
262       assertNotNull("No value found for the key '" + eKey + "' in the actual node", actualProp);
263       assertEquals("The value of the property '" + eKey + "' is invalid", eValue, actualProp.asString());
264     }
265   }
266
267 }
268
Popular Tags