1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  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;
18  
19  
20  import java.beans.IntrospectionException;
21  import java.io.FileReader;
22  import java.io.IOException;
23  import java.io.StringReader;
24  import java.io.StringWriter;
25  import java.util.Date;
26  
27  import org.apache.commons.betwixt.io.BeanReader;
28  import org.apache.commons.betwixt.io.BeanWriter;
29  import org.xml.sax.InputSource;
30  import org.xml.sax.SAXException;
31  
32  /**
33   * @author Brian Pugh
34   */
35  public class TestMultiMapping extends AbstractTestCase {
36  
37      public TestMultiMapping(String testName) {
38          super(testName);
39  
40      }
41      
42  	public void testRoundTripWithSingleMappingFile() throws IOException, SAXException, IntrospectionException {
43  		    AddressBean addressBean = new AddressBean();
44  		    addressBean.setCity("New York");
45  		    addressBean.setCode("92342");
46  		    addressBean.setCountry("USA");
47  		    addressBean.setStreet("12312 Here");
48  		    PartyBean partyBean = new PartyBean();
49  		    partyBean.setDateOfParty(new Date());
50  		    partyBean.setExcuse("too late");
51  		    partyBean.setFromHour(22);
52  		    partyBean.setVenue(addressBean);
53  		
54  		    InputSource source 
55  		    		= new InputSource(
56  		    		        new FileReader(getTestFile("src/test/org/apache/commons/betwixt/mapping.xml")));
57  		    
58  		    StringWriter outputWriter = new StringWriter();
59  		    outputWriter.write("<?xml version='1.0' ?>\n");
60  		    BeanWriter beanWriter = new BeanWriter(outputWriter);
61              beanWriter.setEndOfLine("\n");
62              beanWriter.enablePrettyPrint();
63  		    beanWriter.setWriteEmptyElements(true);
64  		    beanWriter.getXMLIntrospector().register(source);
65              beanWriter.setEndOfLine("\n"); //force to ensure matches on expected
66  		    beanWriter.write(partyBean);
67  		    String expectedOut = "<?xml version='1.0' ?>\n" +
68  		                     "  <party id=\"1\">\n" +
69  		                     "    <the-excuse>too late</the-excuse>\n" +
70  		                     "    <location id=\"2\">\n" +
71  		                     "      <street>12312 Here</street>\n" +
72  		                     "      <city>New York</city>\n" +
73  		                     "      <code>92342</code>\n" +
74  		                     "      <country>USA</country>\n" +
75  		                     "    </location>\n" +
76  		                     "    <time>22</time>\n" +
77  		                     "  </party>\n";
78  		    assertEquals(expectedOut, outputWriter.toString());
79  		    
80  		    BeanReader beanReader = new BeanReader();
81  		    beanReader.registerMultiMapping(
82  		            new InputSource(
83  		                    new FileReader(getTestFile("src/test/org/apache/commons/betwixt/mapping.xml"))));
84  		    StringReader xmlReader = new StringReader(outputWriter.toString());
85  		    //Parse the xml
86  		    PartyBean result = (PartyBean)beanReader.parse(xmlReader);
87  		    assertEquals(partyBean.getExcuse(), result.getExcuse());
88  		    assertEquals(partyBean.getFromHour(), result.getFromHour());
89  		    AddressBean addressResult = result.getVenue();
90  		    assertEquals(addressBean.getCity(), addressResult.getCity());
91  		    assertEquals(addressBean.getCode(), addressResult.getCode());
92  		    assertEquals(addressBean.getCountry(), addressResult.getCountry());
93  		    assertEquals(addressBean.getStreet(), addressResult.getStreet());
94  	
95  	  }
96  
97  }
98