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.schema;
18  
19  import java.io.StringWriter;
20  
21  import junit.framework.Test;
22  import junit.framework.TestSuite;
23  import junit.textui.TestRunner;
24  
25  import org.apache.commons.betwixt.AbstractTestCase;
26  import org.apache.commons.betwixt.io.BeanWriter;
27  
28  /**
29   * @author <a href='http://commons.apache.org'>Apache Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
30   */
31  public class TestRecursiveBeanSchemaGeneration extends AbstractTestCase{
32  
33      public TestRecursiveBeanSchemaGeneration(String name) {
34          super(name);        
35      }
36      
37      public static void main( String[] args ) {
38        TestRunner.run( suite() );
39      }
40      
41       /**
42        * A unit test suite for JUnit
43        */
44      public static Test suite() {
45        return new TestSuite(TestRecursiveBeanSchemaGeneration.class);
46      }
47      
48      public void testLoopBeanWithAttributes() throws Exception {
49          SchemaTranscriber transcriber = new SchemaTranscriber();
50          transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
51          Schema schema = transcriber.generate(LoopBean.class);
52          StringWriter out = new StringWriter();
53          out.write("<?xml version='1.0'?>");
54          
55          BeanWriter writer = new BeanWriter(out);
56          writer.setBindingConfiguration(transcriber.createSchemaBindingConfiguration());
57          writer.getXMLIntrospector().setConfiguration(transcriber.createSchemaIntrospectionConfiguration());
58          writer.write(schema);
59          String xsd = out.getBuffer().toString();
60          
61          //The expected schema is manual generated, may not be completely match the betwixt generated
62          String expected ="<?xml version='1.0'?><xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
63          		"<xsd:element name='LoopBean' type='org.apache.commons.betwixt.schema.LoopBean'/>" +
64          		"<xsd:complexType name='org.apache.commons.betwixt.schema.LoopBean'>" +
65          		"<xsd:sequence>" +
66          		"<xsd:element name='friend' type='org.apache.commons.betwixt.schema.LoopBean' minOccurs='0' maxOccurs='1'/>" +
67          		"</xsd:sequence>" +
68          		"<xsd:attribute name='name' type='xsd:string'/>" +
69          		"</xsd:complexType>" +
70          		"</xsd:schema>";
71              
72          xmlAssertIsomorphicContent(parseString(expected), parseString(xsd));
73          
74          LoopBean loopBean = new LoopBean("Harry");
75          loopBean.setFriend(new LoopBean("Sally"));
76          
77          out = new StringWriter();
78          out.write("<?xml version='1.0'?>");
79          writer = new BeanWriter(out);
80          writer.getBindingConfiguration().setMapIDs(false);
81          writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
82          writer.write(loopBean);
83          
84          String xml = out.getBuffer().toString();
85          
86         xmlAssertIsValid(xml, xsd);
87      }
88      
89      public void testCyclicBean() throws Exception {
90        	SchemaTranscriber transcriber = new SchemaTranscriber();
91        	transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
92        	Schema schema  = transcriber.generate(CyclicBean.class);
93    
94        	StringWriter out = new StringWriter();
95        	out.write("<?xml version='1.0'?>");
96        	BeanWriter writer = new BeanWriter(out);
97        	writer.setBindingConfiguration(transcriber.createSchemaBindingConfiguration());
98        	writer.getXMLIntrospector().setConfiguration(transcriber.createSchemaIntrospectionConfiguration());
99  
100       	writer.write(schema);
101              
102       	String xsd = out.getBuffer().toString();
103       
104       	String expected ="<?xml version='1.0'?><xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
105       			"<xsd:element name='CyclicBean' type='org.apache.commons.betwixt.schema.CyclicBean'/>" +
106       			"<xsd:complexType name='org.apache.commons.betwixt.schema.CyclicBean'>" +
107       			"<xsd:sequence>" +
108       			"<xsd:element name='layers' minOccurs='0' maxOccurs='1'>" +
109       			"<xsd:complexType>" +
110       			"<xsd:sequence>" +
111       			"<xsd:element name='layer' type='org.apache.commons.betwixt.schema.CyclicLayer' minOccurs='0' maxOccurs='unbounded'/>" +
112       			"</xsd:sequence>" +
113       			"</xsd:complexType>" +
114       			"</xsd:element>" +
115       			"</xsd:sequence>" +
116       			"<xsd:attribute name='name' type='xsd:string'/>" +
117       			"</xsd:complexType>" +
118       			"<xsd:complexType name='org.apache.commons.betwixt.schema.CyclicLayer'>" +
119       			"<xsd:sequence>" +
120       			"<xsd:element name='columns' minOccurs='0' maxOccurs='1'>" +
121       			"<xsd:complexType>" +
122       			"<xsd:sequence>" +
123       			"<xsd:element name='column' type='org.apache.commons.betwixt.schema.CyclicColumn' minOccurs='0' maxOccurs='unbounded'/>" +
124       			"</xsd:sequence>" +
125       			"</xsd:complexType>" +
126       			"</xsd:element>" +
127       			"</xsd:sequence>" +
128       			"<xsd:attribute name='name' type='xsd:string'/>" +
129       			"</xsd:complexType>" +
130       			"<xsd:complexType name='org.apache.commons.betwixt.schema.CyclicColumn'>" +
131       			"<xsd:sequence>" +
132       			"<xsd:element name='bean' type='org.apache.commons.betwixt.schema.CyclicBean' minOccurs='0' maxOccurs='1'/>" +
133       			"</xsd:sequence>" +
134       			"<xsd:attribute name='name' type='xsd:string'/>" +
135       			"</xsd:complexType>" +
136       			"</xsd:schema>";
137                   
138         xmlAssertIsomorphicContent(parseString(expected), parseString(xsd));
139     }
140 }