1 /*
2 * $Header: /home/cvs/jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/TestBeanWriter.java,v 1.13 2002/05/17 15:24:10 jstrachan Exp $
3 * $Revision: 1.13 $
4 * $Date: 2002/05/17 15:24:10 $
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: TestBeanWriter.java,v 1.13 2002/05/17 15:24:10 jstrachan Exp $
61 */
62 package org.apache.commons.betwixt;
63
64 import java.io.ByteArrayOutputStream;
65 import java.io.PrintStream;
66
67 import junit.framework.Test;
68 import junit.framework.TestSuite;
69 import junit.textui.TestRunner;
70
71 import org.apache.commons.betwixt.io.BeanWriter;
72 import org.apache.commons.betwixt.io.CyclicReferenceException;
73 import org.apache.commons.logging.impl.SimpleLog;
74
75
76 /*** Test harness for the BeanWriter
77 *
78 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
79 * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
80 * @version $Revision: 1.13 $
81 */
82 public class TestBeanWriter extends AbstractTestCase {
83
84 public static void main( String[] args ) {
85 TestRunner.run( suite() );
86 }
87
88 public static Test suite() {
89 return new TestSuite(TestBeanWriter.class);
90 }
91
92 public TestBeanWriter(String testName) {
93 super(testName);
94 }
95
96 public void testBeanWriter() throws Exception {
97 Object bean = createBean();
98
99 System.out.println( "Now trying pretty print" );
100
101 BeanWriter writer = new BeanWriter();
102 writer.enablePrettyPrint();
103 writer.write( bean );
104 }
105
106
107 public void testLooping() throws Exception {
108 BeanWriter writer = new BeanWriter();
109
110 // logging for debugging jsut this method
111 // SimpleLog log = new SimpleLog("[testLooping:BeanWriter]");
112 // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
113 // writer.setLog(log);
114
115 // log = new SimpleLog("[testLooping:AbstractBeanWriter]");
116 // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
117 // writer.setAbstractBeanWriterLog(log);
118
119 // log = new SimpleLog("[testLooping]");
120 // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
121
122 writer.enablePrettyPrint();
123 writer.write( LoopBean.createNoLoopExampleBean() );
124 writer.write( LoopBean.createLoopExampleBean() );
125
126 // test not writing IDs
127 writer.setWriteIDs(false);
128
129 // log.info("Writing LoopBean.createNoLoopExampleBean...");
130
131 writer.write( LoopBean.createNoLoopExampleBean() );
132
133 // log.info("Writing LoopBean.createIdOnlyLoopExampleBean...");
134
135 writer.write( LoopBean.createIdOnlyLoopExampleBean() );
136
137 try {
138 // log.info("Writing LoopBean.createLoopExampleBean...");
139 writer.write( LoopBean.createLoopExampleBean() );
140 fail("CyclicReferenceException not thrown!");
141
142 } catch (CyclicReferenceException e) {
143 // everything's fine
144 }
145 }
146
147 public void testEscaping() throws Exception {
148 //XXX find a way to automatically verify test
149 ByteArrayOutputStream out = new ByteArrayOutputStream();
150 BeanWriter writer = new BeanWriter(out);
151 writer.enablePrettyPrint();
152 XMLIntrospector introspector = new XMLIntrospector();
153 introspector.setAttributesForPrimitives(true);
154 writer.setXMLIntrospector(introspector);
155 writer.write(new LoopBean("Escape<LessThan"));
156 writer.write(new LoopBean("Escape>GreaterThan"));
157 writer.write(new LoopBean("Escape&hersand"));
158 writer.write(new LoopBean("Escape'apostrophe"));
159 writer.write(new LoopBean("Escape\"Quote"));
160
161 CustomerBean bean = new CustomerBean();
162 bean.setEmails( new String[] {
163 "Escape<LessThan",
164 "Escape>GreaterThan",
165 "Escape&hersand",
166 "Escape'apostrophe",
167 "Escape\"Quote"} );
168
169 // The attribute value escaping needs test too..
170 bean.setName("Escape<LessThan");
171 AddressBean address = new AddressBean();
172 address.setCode("Escape>GreaterThan");
173 address.setCountry("Escape&hersand");
174 address.setCity("Escape'apostrophe");
175 address.setStreet("Escape\"Quote");
176 bean.setAddress(address);
177
178 writer.write(bean);
179 out.flush();
180 String result = out.toString();
181
182 System.out.println( "Created..." );
183 System.out.println( result );
184
185 // check for the elemant content..
186 assertTrue(result.indexOf("<email>Escape<LessThan</email>") > -1 );
187 assertTrue(result.indexOf("<email>Escape>GreaterThan</email>") > -1);
188 assertTrue(result.indexOf("<email>Escape&hersand</email>") != -1);
189 assertTrue(result.indexOf("<email>Escape'apostrophe</email>") != -1);
190 assertTrue(result.indexOf("<email>Escape\"Quote</email>") != -1);
191 // check for the attributes..
192 assertTrue(result.indexOf("name=\"Escape<LessThan\"") > -1 );
193 assertTrue(result.indexOf("code=\"Escape>GreaterThan\"") > -1);
194 assertTrue(result.indexOf("country=\"Escape&hersand\"") != -1);
195 assertTrue(result.indexOf("city=\"Escape'apostrophe\"") != -1);
196 assertTrue(result.indexOf("street=\"Escape"Quote\"") != -1);
197 }
198 /***
199 * Testing valid endofline characters.
200 * It tests if there is a warning on System.err
201 */
202 public void testValidEndOfLine() throws Exception {
203 BeanWriter writer = new BeanWriter();
204
205 // store the system err
206 PrintStream errStream = System.err;
207 ByteArrayOutputStream warning = new ByteArrayOutputStream();
208 System.setErr(new PrintStream(warning));
209
210 // force logging to go to System.err
211 writer.setLog( new SimpleLog( "test.betwixt" ) );
212
213
214 writer.setEndOfLine("X");
215 warning.flush();
216 assertTrue(warning.toString().startsWith("[WARN]"));
217 warning.reset();
218 writer.setEndOfLine("\tX");
219 warning.flush();
220 assertTrue(warning.toString().startsWith("[WARN]"));
221 warning.reset();
222 // now test a valid value..
223 writer.setEndOfLine(" ");
224 warning.flush();
225 assertTrue(warning.toString().equals(""));
226 warning.reset();
227 // set the System.err back again..
228 System.setErr(errStream);
229 }
230 }
231
This page was automatically generated by Maven