1
2 /*
3 * Copyright 2001-2004 The Apache Software Foundation.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.betwixt.digester;
18
19 import java.io.FileInputStream;
20 import java.io.InputStream;
21 import java.io.StringWriter;
22
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25 import junit.textui.TestRunner;
26
27 import org.apache.commons.betwixt.AbstractTestCase;
28 import org.apache.commons.betwixt.io.BeanReader;
29 import org.apache.commons.betwixt.io.BeanWriter;
30
31 //import org.apache.commons.logging.Log;
32 //import org.apache.commons.logging.impl.SimpleLog;
33 //import org.apache.commons.betwixt.expression.MethodUpdater;
34 //import org.apache.commons.betwixt.io.BeanCreateRule;
35 //import org.apache.commons.betwixt.io.BeanRuleSet;
36
37
38 /*** Test harness for ID-IDRef reading.
39 *
40 * @author Robert Burrell Donkin
41 * @version $Revision: 1.9 $
42 */
43 public class TestIDRead extends AbstractTestCase {
44
45 public static void main( String[] args ) {
46 TestRunner.run( suite() );
47 }
48
49 public static Test suite() {
50 return new TestSuite(TestIDRead.class);
51 }
52
53 public TestIDRead(String testName) {
54 super(testName);
55 }
56
57 public void testSimpleRead() throws Exception {
58 StringWriter out = new StringWriter();
59 out.write("<?xml version='1.0'?>");
60 BeanWriter writer = new BeanWriter(out);
61 IDBean bean = new IDBean("alpha","one");
62 bean.addChild(new IDBean("beta","two"));
63 bean.addChild(new IDBean("gamma","three"));
64 writer.write(bean);
65
66 String xml = "<IDBean><name>one</name><children><child><name>two</name><children/>"
67 + "<id>beta</id></child><child><name>three</name><children/>"
68 + "<id>gamma</id></child></children><id>alpha</id></IDBean>";
69
70 xmlAssertIsomorphicContent(
71 parseString(xml),
72 parseString(out.getBuffer().toString()),
73 true);
74
75 BeanReader reader = new BeanReader();
76
77 // logging just for this method
78 // SimpleLog log = new SimpleLog("[testSimpleRead:XMLIntrospectorHelper]");
79 // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
80 // XMLIntrospectorHelper.setLog(log);
81
82 // log = new SimpleLog("[testSimpleRead:MethodUpdater]");
83 // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
84 // MethodUpdater.setLog(log);
85
86 // log = new SimpleLog("[testSimpleRead:BeanCreateRule]");
87 // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
88 // BeanCreateRule.setLog(log);
89
90 // log = new SimpleLog("[testSimpleRead:BeanRuleSet]");
91 // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
92 // BeanRuleSet.setLog(log);
93
94 // log = new SimpleLog("[testSimpleRead:IDBean]");
95 // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
96 // IDBean.log = log;
97
98 // log = new SimpleLog("[testSimpleRead:BeanReader]");
99 // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
100 // reader.setLog(log);
101
102 // log = new SimpleLog("[testSimpleRead:XMLIntrospector]");
103 // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
104 // reader.getXMLIntrospector().setLog(log);
105
106 reader.registerBeanClass( IDBean.class );
107
108 InputStream in = new FileInputStream(
109 getTestFile("src/test/org/apache/commons/betwixt/digester/SimpleReadTest.xml") );
110
111 try {
112 // log = new SimpleLog("[testSimpleRead]");
113 // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
114 Object obj = reader.parse( in );
115 // log.debug(obj);
116
117 assertEquals("Read bean type is incorrect", true, (obj instanceof IDBean) );
118 IDBean alpha = (IDBean) obj;
119
120 assertEquals("Wrong list size", 2 , alpha.getChildren().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 }
128 finally {
129 in.close();
130 }
131 }
132
133 public void testIDRead() throws Exception {
134
135 BeanReader reader = new BeanReader();
136
137 // logging just for this method
138 // SimpleLog log = new SimpleLog("[testIDRead:XMLIntrospectorHelper]");
139 // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
140 // XMLIntrospectorHelper.setLog(log);
141 //
142 // log = new SimpleLog("[testIDRead:BeanCreateRule]");
143 // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
144 // BeanCreateRule.setLog(log);
145 //
146 // log = new SimpleLog("[testIDRead:BeanReader]");
147 // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
148 // reader.setLog(log);
149 //
150 // log = new SimpleLog("[testIDRead:XMLIntrospector]");
151 // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
152 // reader.getXMLIntrospector().setLog(log);
153
154 reader.registerBeanClass( IDBean.class );
155
156 InputStream in = new FileInputStream(
157 getTestFile("src/test/org/apache/commons/betwixt/digester/IDTest1.xml") );
158
159 try {
160 Object obj = reader.parse( in );
161
162 assertEquals("Read bean type is incorrect", true, (obj instanceof IDBean) );
163 IDBean alpha = (IDBean) obj;
164
165 assertEquals("Wrong list size (A)", 2 , alpha.getChildren().size());
166
167 IDBean beta = (IDBean) alpha.getChildren().get(0);
168 assertEquals("Wrong name (A)", "beta" , beta.getName());
169
170 IDBean gamma = (IDBean) alpha.getChildren().get(1);
171 assertEquals("Wrong name (B)", "gamma" , gamma.getName());
172 assertEquals("Wrong list size (B)", 2 , gamma.getChildren().size());
173
174 IDBean sonOfGamma = (IDBean) gamma.getChildren().get(1);
175
176 assertEquals("Wrong id (A)", "two" , sonOfGamma.getId());
177 assertEquals("Wrong name (C)", "beta" , sonOfGamma.getName());
178
179 assertEquals("IDREF bean not equal to ID bean", beta, sonOfGamma);
180 }
181 finally {
182 in.close();
183 }
184 }
185 }