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.recursion;
17  
18  import java.beans.Introspector;
19  import java.beans.PropertyDescriptor;
20  import java.io.StringWriter;
21  
22  import org.apache.commons.betwixt.AbstractTestCase;
23  import org.apache.commons.betwixt.io.BeanWriter;
24  
25  /***
26   * @author <a href='http://jakarta.apache.org/commons'>Jakarta Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
27   */
28  public class TestSharedIDGeneration extends AbstractTestCase {
29  
30      public TestSharedIDGeneration(String testName) {
31          super(testName);
32      }
33  
34      public void testSharedChild() throws Exception {
35          
36          NameBean name = new NameBean("Me");
37          
38          HybridBean hybrid = new HybridBean(new AlienBean(name), new PersonBean(name));
39          
40          StringWriter out = new StringWriter();
41          BeanWriter writer = new BeanWriter(out);
42          writer.write(hybrid);
43          
44          boolean isAlienBeforePerson = false;
45          PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(HybridBean.class).getPropertyDescriptors();
46          
47          for(int i=0; i<propertyDescriptors.length;i++) {
48               String methodName = propertyDescriptors[i].getName();
49               if ("alien".equals(methodName)) {
50                   isAlienBeforePerson = true;
51                   break;
52               } else if ("person".equals(methodName)) {
53                   isAlienBeforePerson = false;
54                   break;
55               }
56          }
57          String expected = "<?xml version='1.0'?><HybridBean id='1'>" +
58          		"<person id='2'><name id='3'><moniker>Me</moniker></name></person>" +
59          		"<alien id='4'><name idref='3'/></alien>" +
60          		"</HybridBean>";
61          
62          if (isAlienBeforePerson) {
63              expected = "<?xml version='1.0'?><HybridBean id='1'>" +
64              "<alien id='2'><name id='3'><moniker>Me</moniker></name></alien>" +
65              "<person id='4'><name idref='3'/></person>" +
66              "</HybridBean>";
67          }
68          
69          xmlAssertIsomorphic(parseString(expected), parseString(out), true);
70      }
71      
72  }