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