1 /*------------------------------------------------------------------------------
2 Name: TestInfo.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 ------------------------------------------------------------------------------*/
6
7 package org.xmlBlaster.test.contrib;
8
9 import java.util.HashMap;
10 import java.util.Map;
11 import java.util.Properties;
12 import java.util.logging.Logger;
13
14 import org.custommonkey.xmlunit.XMLTestCase;
15 import org.custommonkey.xmlunit.XMLUnit;
16 import org.xmlBlaster.contrib.ClientPropertiesInfo;
17 import org.xmlBlaster.contrib.GlobalInfo;
18 import org.xmlBlaster.contrib.I_Info;
19 import org.xmlBlaster.contrib.InfoHelper;
20 import org.xmlBlaster.contrib.PropertiesInfo;
21 import org.xmlBlaster.contrib.dbwatcher.Info;
22 import org.xmlBlaster.util.Global;
23 import org.xmlBlaster.util.XmlBlasterException;
24 import org.xmlBlaster.util.plugin.PluginInfo;
25
26
27 /**
28 * TestInfo
29 * @author <a href="mailto:michele@laghi.eu">Michele Laghi</a>
30 */
31 public class TestInfo extends XMLTestCase {
32
33 private class OwnGlobalInfo extends GlobalInfo {
34 public OwnGlobalInfo() {
35 super(new String[] {});
36 }
37
38 protected void doInit(Global global, PluginInfo pluginInfo) throws XmlBlasterException {
39 Map map = InfoHelper.getPropertiesStartingWith("test.one.two.", this, null);
40 String[] keys = (String[])map.keySet().toArray(new String[map.size()]);
41 for (int i=0; i < keys.length; i++) {
42 log.info("KEY " + keys[i] + " : value " + (String)map.get(keys[i]));
43 }
44
45 }
46
47 }
48
49 private final static Logger log = Logger.getLogger(TestInfo.class.getName());
50
51 public TestInfo() {
52 super();
53 XMLUnit.setIgnoreWhitespace(true);
54 }
55
56 /**
57 * Configure database access.
58 * @see TestCase#setUp()
59 */
60 protected void setUp() throws Exception {
61 super.setUp();
62 }
63
64 /*
65 * @see TestCase#tearDown()
66 */
67 protected void tearDown() throws Exception {
68 super.tearDown();
69 }
70
71 public void testRemoveEntry() {
72 I_Info[] infos = {
73 new Info("id"),
74 new PropertiesInfo(new Properties()),
75 new ClientPropertiesInfo(new HashMap()),
76 /* new DbInfo(new HashMap()), */
77 new OwnGlobalInfo()
78 };
79 String[] names = new String[] {"Info", "PropertiesInfo", "ClientPropertiesInfo", /*.*/ "GlobalInfo"};
80
81 for (int i=0; i < infos.length; i++)
82 doTestRemoveEntry(names[i], infos[i]);
83 }
84
85 public void testReplaceKey() {
86 try {
87 Global global = new Global();
88
89 global.getProperty().set("test1", "key1");
90 global.getProperty().set("test2", "key2");
91 global.getProperty().set("test3", "key3");
92
93 global.getProperty().set("test.one.two.${test0}", "test0");
94 global.getProperty().set("test.one.two.${test1}", "test1");
95 global.getProperty().set("test.one.two.${test2}", "test2");
96 global.getProperty().set("test.one.two.${test3}", "test3");
97
98 global.getProperty().set("${test.replace.key}", "testReplaceKey");
99 global.getProperty().set("${test.replace.key1}", "testReplaceKey1");
100 global.getProperty().set("someKey3", "testReplaceKey1");
101 global.getProperty().set("${test.replace.key}", "testReplaceKey");
102 global.getProperty().set("test.replace.key", "someKey");
103 GlobalInfo info = new OwnGlobalInfo();
104 info.init(global, null);
105 String val = info.get("someKey", null);
106 assertNotNull("The value must be set", val);
107 assertEquals("wrong value of replaced key", "testReplaceKey", val);
108
109 val = info.get("${test.replace.key1}", null);
110 assertNotNull("The value must be set", val);
111
112 val = global.getProperty().get("someKey3", (String)null);
113 assertNotNull("The value must be set", val);
114
115
116 Properties props = new Properties();
117 props.put("one", "one");
118 val = props.getProperty("one");
119 assertNotNull("The value must be set", val);
120
121 props.remove("one");
122 val = props.getProperty("one");
123 assertNull("The value must NOT be set", val);
124
125
126 global.getProperty().removeProperty("someKey3");
127 val = global.getProperty().get("someKey3", (String)null);
128 assertNull("The value must NOT be set", val);
129
130 val = info.get("${test.replace.key}", null);
131 assertNotNull("The value must be set", val);
132
133 val = global.getProperty().get("someKey3", (String)null);
134 assertNull("The value must NOT be set", val);
135
136
137 }
138 catch (XmlBlasterException ex) {
139 fail(ex.getMessage());
140 }
141
142 }
143
144 public void doTestRemoveEntry(String name, I_Info info) {
145 log.info("doTestRemoveEntry: Start with '" + name + "'");
146 String obj = "testValue";
147 String key = "test";
148 info.putObject(key, obj);
149
150 Object ret = info.putObject(key, null);
151 assertNotNull(name + " object must not be null since it existed before removing", ret);
152 assertEquals(name + " the object returned must be the one associated initially", obj, (String)ret);
153 ret = info.getObject(key);
154 assertNull(name + " object must be null after deletion", ret);
155 log.info("doTestRemoveEntry: Successfully ended with '" + name + "'");
156 }
157
158 /**
159 * @param args
160 */
161 public static void main(String[] args) {
162 // junit.swingui.TestRunner.run(TestDbBasics.class);
163
164 TestInfo test = new TestInfo();
165 try {
166 test.setUp();
167 test.testRemoveEntry();
168 test.tearDown();
169
170 test.setUp();
171 test.testReplaceKey();
172 test.tearDown();
173
174 }
175 catch (Exception ex) {
176 ex.printStackTrace();
177 fail();
178 }
179 }
180
181 }
syntax highlighted by Code2HTML, v. 0.9.1