001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.betwixt.digester;
018:
019: import java.io.FileInputStream;
020: import java.io.InputStream;
021: import java.io.StringWriter;
022:
023: import junit.framework.Test;
024: import junit.framework.TestSuite;
025: import junit.textui.TestRunner;
026:
027: import org.apache.commons.betwixt.AbstractTestCase;
028: import org.apache.commons.betwixt.io.BeanReader;
029: import org.apache.commons.betwixt.io.BeanWriter;
030:
031: //import org.apache.commons.logging.Log;
032: //import org.apache.commons.logging.impl.SimpleLog;
033: //import org.apache.commons.betwixt.expression.MethodUpdater;
034: //import org.apache.commons.betwixt.io.BeanCreateRule;
035: //import org.apache.commons.betwixt.io.BeanRuleSet;
036:
037: /** Test harness for ID-IDRef reading.
038: *
039: * @author Robert Burrell Donkin
040: * @version $Revision: 438373 $
041: */
042: public class TestIDRead extends AbstractTestCase {
043:
044: public static void main(String[] args) {
045: TestRunner.run(suite());
046: }
047:
048: public static Test suite() {
049: return new TestSuite(TestIDRead.class);
050: }
051:
052: public TestIDRead(String testName) {
053: super (testName);
054: }
055:
056: public void testSimpleRead() throws Exception {
057: StringWriter out = new StringWriter();
058: out.write("<?xml version='1.0'?>");
059: BeanWriter writer = new BeanWriter(out);
060: writer.getBindingConfiguration().setMapIDs(false);
061: IDBean bean = new IDBean("alpha", "one");
062: bean.addChild(new IDBean("beta", "two"));
063: bean.addChild(new IDBean("gamma", "three"));
064: writer.write(bean);
065:
066: String xml = "<IDBean><name>one</name><children><child><name>two</name><children/>"
067: + "<id>beta</id></child><child><name>three</name><children/>"
068: + "<id>gamma</id></child></children><id>alpha</id></IDBean>";
069:
070: xmlAssertIsomorphicContent(parseString(xml), parseString(out
071: .getBuffer().toString()), true);
072:
073: BeanReader reader = new BeanReader();
074:
075: // logging just for this method
076: // SimpleLog log = new SimpleLog("[testSimpleRead:XMLIntrospectorHelper]");
077: // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
078: // XMLIntrospectorHelper.setLog(log);
079:
080: // log = new SimpleLog("[testSimpleRead:MethodUpdater]");
081: // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
082: // MethodUpdater.setLog(log);
083:
084: // log = new SimpleLog("[testSimpleRead:BeanCreateRule]");
085: // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
086: // BeanCreateRule.setLog(log);
087:
088: // log = new SimpleLog("[testSimpleRead:BeanRuleSet]");
089: // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
090: // BeanRuleSet.setLog(log);
091:
092: // log = new SimpleLog("[testSimpleRead:IDBean]");
093: // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
094: // IDBean.log = log;
095:
096: // log = new SimpleLog("[testSimpleRead:BeanReader]");
097: // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
098: // reader.setLog(log);
099:
100: // log = new SimpleLog("[testSimpleRead:XMLIntrospector]");
101: // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
102: // reader.getXMLIntrospector().setLog(log);
103:
104: reader.registerBeanClass(IDBean.class);
105:
106: InputStream in = new FileInputStream(
107: getTestFile("src/test/org/apache/commons/betwixt/digester/SimpleReadTest.xml"));
108:
109: try {
110: // log = new SimpleLog("[testSimpleRead]");
111: // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
112: Object obj = reader.parse(in);
113: // log.debug(obj);
114:
115: assertEquals("Read bean type is incorrect", true,
116: (obj instanceof IDBean));
117: IDBean alpha = (IDBean) obj;
118:
119: assertEquals("Wrong list size", 2, alpha.getChildren()
120: .size());
121:
122: IDBean beta = (IDBean) alpha.getChildren().get(0);
123: assertEquals("Wrong name (A)", "beta", beta.getName());
124:
125: IDBean gamma = (IDBean) alpha.getChildren().get(1);
126: assertEquals("Wrong name (B)", "gamma", gamma.getName());
127: } finally {
128: in.close();
129: }
130: }
131:
132: public void testIDRead() throws Exception {
133:
134: BeanReader reader = new BeanReader();
135:
136: // logging just for this method
137: // SimpleLog log = new SimpleLog("[testIDRead:XMLIntrospectorHelper]");
138: // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
139: // XMLIntrospectorHelper.setLog(log);
140: //
141: // log = new SimpleLog("[testIDRead:BeanCreateRule]");
142: // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
143: // BeanCreateRule.setLog(log);
144: //
145: // log = new SimpleLog("[testIDRead:BeanReader]");
146: // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
147: // reader.setLog(log);
148: //
149: // log = new SimpleLog("[testIDRead:XMLIntrospector]");
150: // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
151: // reader.getXMLIntrospector().setLog(log);
152:
153: reader.registerBeanClass(IDBean.class);
154:
155: InputStream in = new FileInputStream(
156: getTestFile("src/test/org/apache/commons/betwixt/digester/IDTest1.xml"));
157:
158: try {
159: Object obj = reader.parse(in);
160:
161: assertEquals("Read bean type is incorrect", true,
162: (obj instanceof IDBean));
163: IDBean alpha = (IDBean) obj;
164:
165: assertEquals("Wrong list size (A)", 2, alpha.getChildren()
166: .size());
167:
168: IDBean beta = (IDBean) alpha.getChildren().get(0);
169: assertEquals("Wrong name (A)", "beta", beta.getName());
170:
171: IDBean gamma = (IDBean) alpha.getChildren().get(1);
172: assertEquals("Wrong name (B)", "gamma", gamma.getName());
173: assertEquals("Wrong list size (B)", 2, gamma.getChildren()
174: .size());
175:
176: IDBean sonOfGamma = (IDBean) gamma.getChildren().get(1);
177:
178: assertEquals("Wrong id (A)", "two", sonOfGamma.getId());
179: assertEquals("Wrong name (C)", "beta", sonOfGamma.getName());
180:
181: assertEquals("IDREF bean not equal to ID bean", beta,
182: sonOfGamma);
183: } finally {
184: in.close();
185: }
186: }
187: }
|