1   /*
2    * Copyright 2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  package org.apache.commons.betwixt;
17  
18  
19  import java.beans.IntrospectionException;
20  import java.io.FileReader;
21  import java.io.IOException;
22  import java.io.StringReader;
23  import java.io.StringWriter;
24  import java.util.Date;
25  
26  import org.apache.commons.betwixt.io.BeanReader;
27  import org.apache.commons.betwixt.io.BeanWriter;
28  import org.xml.sax.InputSource;
29  import org.xml.sax.SAXException;
30  
31  /***
32   * @author Brian Pugh
33   */
34  public class TestMultiMapping extends AbstractTestCase {
35  
36      public TestMultiMapping(String testName) {
37          super(testName);
38  
39      }
40      
41  	public void testRoundTripWithSingleMappingFile() throws IOException, SAXException, IntrospectionException {
42  		    AddressBean addressBean = new AddressBean();
43  		    addressBean.setCity("New York");
44  		    addressBean.setCode("92342");
45  		    addressBean.setCountry("USA");
46  		    addressBean.setStreet("12312 Here");
47  		    PartyBean partyBean = new PartyBean();
48  		    partyBean.setDateOfParty(new Date());
49  		    partyBean.setExcuse("too late");
50  		    partyBean.setFromHour(22);
51  		    partyBean.setVenue(addressBean);
52  		
53  		    InputSource source 
54  		    		= new InputSource(
55  		    		        new FileReader(getTestFile("src/test/org/apache/commons/betwixt/mapping.xml")));
56  		    
57  		    StringWriter outputWriter = new StringWriter();
58  		    outputWriter.write("<?xml version='1.0' ?>\n");
59  		    BeanWriter beanWriter = new BeanWriter(outputWriter);
60  		    beanWriter.enablePrettyPrint();
61  		    beanWriter.setWriteEmptyElements(true);
62  		    beanWriter.getXMLIntrospector().register(source);
63  		    beanWriter.write(partyBean);
64  		    String expectedOut = "<?xml version='1.0' ?>\n" +
65  		                     "  <party id=\"1\">\n" +
66  		                     "    <the-excuse>too late</the-excuse>\n" +
67  		                     "    <location id=\"2\">\n" +
68  		                     "      <street>12312 Here</street>\n" +
69  		                     "      <city>New York</city>\n" +
70  		                     "      <code>92342</code>\n" +
71  		                     "      <country>USA</country>\n" +
72  		                     "    </location>\n" +
73  		                     "    <time>22</time>\n" +
74  		                     "  </party>\n";
75  		    assertEquals(expectedOut, outputWriter.toString());
76  		    
77  		    BeanReader beanReader = new BeanReader();
78  		    beanReader.registerMultiMapping(
79  		            new InputSource(
80  		                    new FileReader(getTestFile("src/test/org/apache/commons/betwixt/mapping.xml"))));
81  		    StringReader xmlReader = new StringReader(outputWriter.toString());
82  		    //Parse the xml
83  		    PartyBean result = (PartyBean)beanReader.parse(xmlReader);
84  		    assertEquals(partyBean.getExcuse(), result.getExcuse());
85  		    assertEquals(partyBean.getFromHour(), result.getFromHour());
86  		    AddressBean addressResult = result.getVenue();
87  		    assertEquals(addressBean.getCity(), addressResult.getCity());
88  		    assertEquals(addressBean.getCode(), addressResult.getCode());
89  		    assertEquals(addressBean.getCountry(), addressResult.getCountry());
90  		    assertEquals(addressBean.getStreet(), addressResult.getStreet());
91  	
92  	  }
93  
94  }
95