01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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://jakarta.apache.org/commons'>Jakarta 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),
40: new PersonBean(name));
41:
42: StringWriter out = new StringWriter();
43: BeanWriter writer = new BeanWriter(out);
44: writer.write(hybrid);
45:
46: boolean isAlienBeforePerson = false;
47: PropertyDescriptor[] propertyDescriptors = Introspector
48: .getBeanInfo(HybridBean.class).getPropertyDescriptors();
49:
50: for (int i = 0; i < propertyDescriptors.length; i++) {
51: String methodName = propertyDescriptors[i].getName();
52: if ("alien".equals(methodName)) {
53: isAlienBeforePerson = true;
54: break;
55: } else if ("person".equals(methodName)) {
56: isAlienBeforePerson = false;
57: break;
58: }
59: }
60: String expected = "<?xml version='1.0'?><HybridBean id='1'>"
61: + "<person id='2'><name id='3'><moniker>Me</moniker></name></person>"
62: + "<alien id='4'><name idref='3'/></alien>"
63: + "</HybridBean>";
64:
65: if (isAlienBeforePerson) {
66: expected = "<?xml version='1.0'?><HybridBean id='1'>"
67: + "<alien id='2'><name id='3'><moniker>Me</moniker></name></alien>"
68: + "<person id='4'><name idref='3'/></person>"
69: + "</HybridBean>";
70: }
71:
72: xmlAssertIsomorphic(parseString(expected), parseString(out),
73: true);
74: }
75:
76: }
|