001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestXml2Config.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.config;
009:
010: import com.uwyn.rife.config.exceptions.ConfigErrorException;
011: import com.uwyn.rife.resources.ResourceFinderClasspath;
012: import com.uwyn.rife.tools.ExceptionUtils;
013: import java.util.Collection;
014: import java.util.Iterator;
015: import junit.framework.TestCase;
016:
017: public class TestXml2Config extends TestCase {
018: public TestXml2Config(String name) {
019: super (name);
020: }
021:
022: public void testInstantiation() {
023: Xml2Config xml2config = new Xml2Config();
024:
025: assertNotNull(xml2config);
026: }
027:
028: public void testParse() {
029: Config config = null;
030:
031: try {
032: config = new Config("xml/test_xml2config.xml",
033: ResourceFinderClasspath.getInstance());
034: } catch (ConfigErrorException e) {
035: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
036: }
037:
038: assertTrue(config.hasParameter("paramstring"));
039: assertTrue(config.hasParameter("parambool"));
040: assertTrue(config.hasParameter("paramchar"));
041: assertTrue(config.hasParameter("paramint"));
042: assertTrue(config.hasParameter("paramlong"));
043: assertTrue(config.hasParameter("paramfloat"));
044: assertTrue(config.hasParameter("paramdouble"));
045: assertTrue(config.hasParameter("paramproperty"));
046: assertTrue(config.hasParameter("paramfinal"));
047: assertEquals(config.countParameters(), 9);
048:
049: assertEquals(config.getString("paramstring"), "astring");
050: assertEquals(config.getBool("parambool"), true);
051: assertEquals(config.getChar("paramchar"), 'C');
052: assertEquals(config.getInt("paramint"), 5133);
053: assertEquals(config.getLong("paramlong"), 8736478L);
054: assertEquals(config.getFloat("paramfloat"), 545.2546f, 0);
055: assertEquals(config.getDouble("paramdouble"), 7863.3434353d, 0);
056: assertEquals(config.getString("paramproperty"),
057: "begin:property_test_value:end");
058: assertEquals(config.getString("paramfinal"), "initial value");
059:
060: assertFalse(config.isFinalParameter("paramstring"));
061: assertFalse(config.isFinalParameter("parambool"));
062: assertFalse(config.isFinalParameter("paramchar"));
063: assertFalse(config.isFinalParameter("paramint"));
064: assertFalse(config.isFinalParameter("paramlong"));
065: assertFalse(config.isFinalParameter("paramfloat"));
066: assertFalse(config.isFinalParameter("paramdouble"));
067: assertFalse(config.isFinalParameter("paramproperty"));
068: assertTrue(config.isFinalParameter("paramfinal"));
069:
070: assertTrue(config.hasList("list1"));
071: assertTrue(config.hasList("list2"));
072: assertTrue(config.hasList("listfinal"));
073: assertEquals(config.countLists(), 3);
074:
075: assertFalse(config.isFinalList("list1"));
076: assertFalse(config.isFinalList("list2"));
077: assertTrue(config.isFinalList("listfinal"));
078:
079: Collection<String> items = null;
080: Iterator<String> item_it = null;
081:
082: items = config.getStringItems("list1");
083: assertEquals(items.size(), 3);
084: item_it = items.iterator();
085: assertEquals(item_it.hasNext(), true);
086: assertEquals(item_it.next(), "item1");
087: assertEquals(item_it.hasNext(), true);
088: assertEquals(item_it.next(), "item2");
089: assertEquals(item_it.hasNext(), true);
090: assertEquals(item_it.next(), "item3");
091: assertEquals(item_it.hasNext(), false);
092:
093: items = config.getStringItems("list2");
094: assertEquals(items.size(), 3);
095: item_it = items.iterator();
096: assertEquals(item_it.hasNext(), true);
097: assertEquals(item_it.next(), "item4");
098: assertEquals(item_it.hasNext(), true);
099: assertEquals(item_it.next(), "item5");
100: assertEquals(item_it.hasNext(), true);
101: assertEquals(item_it.next(), "start:property_test_value:finish");
102: assertEquals(item_it.hasNext(), false);
103:
104: items = config.getStringItems("listfinal");
105: assertEquals(items.size(), 2);
106: item_it = items.iterator();
107: assertEquals(item_it.hasNext(), true);
108: assertEquals(item_it.next(), "item6");
109: assertEquals(item_it.hasNext(), true);
110: assertEquals(item_it.next(), "item7");
111: assertEquals(item_it.hasNext(), false);
112: }
113:
114: public void testIncluding() {
115: Config config = null;
116:
117: try {
118: config = new Config("xml/test_xml2config_including.xml",
119: ResourceFinderClasspath.getInstance());
120: } catch (ConfigErrorException e) {
121: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
122: }
123:
124: assertTrue(config.hasParameter("includingstring"));
125: assertTrue(config.hasParameter("paramstring"));
126: assertTrue(config.hasParameter("parambool"));
127: assertTrue(config.hasParameter("paramchar"));
128: assertTrue(config.hasParameter("paramint"));
129: assertTrue(config.hasParameter("paramlong"));
130: assertTrue(config.hasParameter("paramfloat"));
131: assertTrue(config.hasParameter("paramdouble"));
132: assertTrue(config.hasParameter("paramincluding"));
133: assertTrue(config.hasParameter("paramproperty"));
134: assertTrue(config.hasParameter("paramfinal"));
135: assertEquals(config.countParameters(), 11);
136:
137: assertEquals(config.getString("includingstring"),
138: "the including value goes to the included file : ");
139: assertEquals(config.getString("paramstring"),
140: "the including value goes to the included file : astring");
141: assertEquals(config.getBool("parambool"), true);
142: assertEquals(config.getChar("paramchar"), 'C');
143: assertEquals(config.getInt("paramint"), 9999);
144: assertEquals(config.getLong("paramlong"), 8736478L);
145: assertEquals(config.getFloat("paramfloat"), 545.2546f, 0);
146: assertEquals(config.getDouble("paramdouble"), 7863.3434353d, 0);
147: assertEquals(config.getString("paramincluding"),
148: "it's including : C");
149: assertEquals(config.getString("paramproperty"),
150: "begin:property_test_value:end");
151: assertEquals(config.getString("paramfinal"), "initial value");
152:
153: assertFalse(config.isFinalParameter("paramstring"));
154: assertFalse(config.isFinalParameter("parambool"));
155: assertFalse(config.isFinalParameter("paramchar"));
156: assertTrue(config.isFinalParameter("paramint"));
157: assertFalse(config.isFinalParameter("paramlong"));
158: assertFalse(config.isFinalParameter("paramfloat"));
159: assertFalse(config.isFinalParameter("paramdouble"));
160: assertFalse(config.isFinalParameter("paramproperty"));
161: assertTrue(config.isFinalParameter("paramfinal"));
162:
163: assertTrue(config.hasList("list1"));
164: assertTrue(config.hasList("list2"));
165: assertTrue(config.hasList("listfinal"));
166: assertEquals(config.countLists(), 3);
167:
168: Collection<String> items = null;
169: Iterator<String> item_it = null;
170:
171: items = config.getStringItems("list1");
172: assertEquals(items.size(), 1);
173: item_it = items.iterator();
174: assertEquals(item_it.hasNext(), true);
175: assertEquals(item_it.next(), "item10");
176: assertEquals(item_it.hasNext(), false);
177:
178: items = config.getStringItems("list2");
179: assertEquals(items.size(), 4);
180: item_it = items.iterator();
181: assertEquals(item_it.hasNext(), true);
182: assertEquals(item_it.next(), "item13");
183: assertEquals(item_it.hasNext(), true);
184: assertEquals(item_it.next(), "item14");
185: assertEquals(item_it.hasNext(), true);
186: assertEquals(item_it.next(), "item15");
187: assertEquals(item_it.hasNext(), true);
188: assertEquals(item_it.next(), "item16");
189: assertEquals(item_it.hasNext(), false);
190:
191: items = config.getStringItems("listfinal");
192: assertEquals(items.size(), 2);
193: item_it = items.iterator();
194: assertEquals(item_it.hasNext(), true);
195: assertEquals(item_it.next(), "item6");
196: assertEquals(item_it.hasNext(), true);
197: assertEquals(item_it.next(), "item7");
198: assertEquals(item_it.hasNext(), false);
199: }
200:
201: public void testSelectedShortClassname() {
202: Config config = null;
203:
204: try {
205: config = new Config("TestSelectorConfig",
206: ResourceFinderClasspath.getInstance());
207: } catch (ConfigErrorException e) {
208: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
209: }
210:
211: assertTrue(config.hasParameter("selectedparamstring"));
212: }
213:
214: public void testSelectedFullClassname() {
215: Config config = null;
216:
217: try {
218: config = new Config(
219: "com.uwyn.rife.selector.TestSelectorConfig",
220: ResourceFinderClasspath.getInstance());
221: } catch (ConfigErrorException e) {
222: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
223: }
224:
225: assertTrue(config.hasParameter("selectedparamstring"));
226: }
227:
228: public void testUnavailableXmlFile() {
229: Config config = null;
230:
231: try {
232: config = new Config("xml/this_file_is_not_there.xml",
233: ResourceFinderClasspath.getInstance());
234: fail();
235: assertNotNull(config);
236: } catch (IllegalArgumentException e) {
237: assertTrue(true);
238: } catch (ConfigErrorException e) {
239: assertTrue(true);
240: }
241: }
242: }
|