1 /*
2 * $Header: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/recursion/TestRecursion.java,v 1.6 2002/12/30 18:16:48 mvdb Exp $
3 * $Revision: 1.6 $
4 * $Date: 2002/12/30 18:16:48 $
5 *
6 * ====================================================================
7 *
8 * The Apache Software License, Version 1.1
9 *
10 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
11 * reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 *
25 * 3. The end-user documentation included with the redistribution, if
26 * any, must include the following acknowlegement:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowlegement may appear in the software itself,
30 * if and wherever such third-party acknowlegements normally appear.
31 *
32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33 * Foundation" must not be used to endorse or promote products derived
34 * from this software without prior written permission. For written
35 * permission, please contact apache@apache.org.
36 *
37 * 5. Products derived from this software may not be called "Apache"
38 * nor may "Apache" appear in their names without prior written
39 * permission of the Apache Group.
40 *
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 * ====================================================================
54 *
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
59 *
60 * $Id: TestRecursion.java,v 1.6 2002/12/30 18:16:48 mvdb Exp $
61 */
62 package org.apache.commons.betwixt.recursion;
63
64 import java.io.StringWriter;
65 import java.io.Writer;
66
67 import junit.framework.Test;
68 import junit.framework.TestSuite;
69
70 import org.apache.commons.betwixt.AbstractTestCase;
71 import org.apache.commons.betwixt.XMLIntrospector;
72 import org.apache.commons.betwixt.io.BeanReader;
73 import org.apache.commons.betwixt.io.BeanWriter;
74 import org.apache.commons.betwixt.io.CyclicReferenceException;
75
76
77 /***
78 * This will test the recursive behaviour of betwixt.
79 *
80 * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
81 * @version $Id: TestRecursion.java,v 1.6 2002/12/30 18:16:48 mvdb Exp $
82 */
83 public class TestRecursion extends AbstractTestCase
84 {
85
86
87
88 public TestRecursion(String testName)
89 {
90 super(testName);
91 }
92
93 public static Test suite()
94 {
95 return new TestSuite(TestRecursion.class);
96 }
97
98 /***
99 * This will test reading a simple recursive xml file
100 *
101 */
102 public void testReadwithCollectionsInElementRoundTrip()
103 throws Exception
104 {
105 XMLIntrospector intro = createXMLIntrospector();
106 //((SimpleLog) intro.getLog()).setLevel(SimpleLog.LOG_LEVEL_TRACE);
107 intro.setWrapCollectionsInElement(true);
108 BeanReader reader = new BeanReader();
109 reader.registerBeanClass(ElementBean.class);
110 reader.setXMLIntrospector(intro);
111 Object object = reader.parse(getTestFileURL("src/test/org/apache/commons/betwixt/recursion/recursion.xml"));
112 StringWriter buffer = new StringWriter();
113 write (object, buffer, true);
114 System.out.println("buffer : "+buffer);
115 }
116 /***
117 * This will test reading a simple recursive xml file
118 */
119 public void testReadWithoutCollectionsInElementRoundTrip()
120 throws Exception
121 {
122 System.out.println("\ntestReadWithoutCollectionsInElement()\n");
123 XMLIntrospector intro = createXMLIntrospector();
124 BeanReader reader = new BeanReader();
125 reader.registerBeanClass(ElementBean.class);
126 reader.setXMLIntrospector(intro);
127 Object object = reader.parse(getTestFileURL("src/test/org/apache/commons/betwixt/recursion/recursion2.xml"));
128 StringWriter buffer = new StringWriter();
129 write (object, buffer, false);
130 System.out.println("buffer : "+buffer);
131 }
132
133 /***
134 * Opens a writer and writes an object model according to the
135 * retrieved bean
136 */
137 private void write(Object bean, Writer out, boolean wrapIt)
138 throws Exception
139 {
140 BeanWriter writer = new BeanWriter(out);
141 writer.setXMLIntrospector(createXMLIntrospector());
142 // specifies weather to use collection elements or not.
143 writer.getXMLIntrospector().setWrapCollectionsInElement(wrapIt);
144 // we don't want to write Id attributes to every element
145 // we just want our opbject model written nothing more..
146 writer.setWriteIDs(false);
147 // the source has 2 spaces indention and \n as line seperator.
148 writer.setIndent(" ");
149 writer.setEndOfLine("\n");
150 writer.write(bean);
151 }
152 /***
153 * Set up the XMLIntroSpector
154 */
155 protected XMLIntrospector createXMLIntrospector() {
156 XMLIntrospector introspector = new XMLIntrospector();
157
158 // set elements for attributes to true
159 introspector.setAttributesForPrimitives(true);
160 introspector.setWrapCollectionsInElement(false);
161
162 return introspector;
163 }
164
165
166 /***
167 */
168 public void testBeanWithIdProperty() throws Exception
169 {
170 IdBean bean = new IdBean("Hello, World");
171 BeanWriter writer = new BeanWriter();
172 writer.getXMLIntrospector().setAttributesForPrimitives(true);
173 writer.setWriteIDs(true);
174 writer.write(bean);
175 }
176
177 /***
178 * Check that a cyclic reference exception is not thrown in this case
179 */
180 public void testCyclicReferenceStack1() throws Exception
181 {
182 Element alpha = new Element("Alpha");
183 Element beta = new Element("Beta");
184 Element gamma = new Element("Gamma");
185 Element epsilon = new Element("Epsilon");
186
187 alpha.addElement(beta);
188 beta.addElement(gamma);
189 gamma.addElement(epsilon);
190 alpha.addElement(epsilon);
191
192 StringWriter stringWriter = new StringWriter();
193 BeanWriter writer = new BeanWriter(stringWriter);
194 writer.setWriteIDs(false);
195 writer.write(alpha);
196 }
197
198 /***
199 * This should throw a cyclic reference
200 */
201 public void testCyclicReferenceStack2() throws Exception
202 {
203 Element alpha = new Element("Alpha");
204 Element beta = new Element("Beta");
205 Element gamma = new Element("Gamma");
206 Element epsilon = new Element("Epsilon");
207
208 alpha.addElement(beta);
209 beta.addElement(gamma);
210 gamma.addElement(epsilon);
211 epsilon.addElement(beta);
212
213 StringWriter stringWriter = new StringWriter();
214 BeanWriter writer = new BeanWriter(stringWriter);
215 writer.setWriteIDs(false);
216
217 //SimpleLog log = new SimpleLog("[testCyclicReferenceStack2:BeanWriter]");
218 //log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
219 //writer.setLog(log);
220
221 //log = new SimpleLog("[testCyclicReferenceStack2:BeanWriter]");
222 //log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
223 //writer.setAbstractBeanWriterLog(log);
224
225 try {
226 writer.write(alpha);
227 fail("Cycle was not detected!");
228
229 } catch (CyclicReferenceException e) {
230 // that's what we expected!
231 }
232 }
233 }
234
This page was automatically generated by Maven