001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.betwixt.schema;
018:
019: import java.io.StringWriter;
020:
021: import junit.framework.Test;
022: import junit.framework.TestSuite;
023: import junit.textui.TestRunner;
024:
025: import org.apache.commons.betwixt.AbstractTestCase;
026: import org.apache.commons.betwixt.io.BeanWriter;
027:
028: /**
029: * @author <a href='http://jakarta.apache.org/commons'>Jakarta Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
030: */
031: public class TestRecursiveBeanSchemaGeneration extends AbstractTestCase {
032:
033: public TestRecursiveBeanSchemaGeneration(String name) {
034: super (name);
035: }
036:
037: public static void main(String[] args) {
038: TestRunner.run(suite());
039: }
040:
041: /**
042: * A unit test suite for JUnit
043: */
044: public static Test suite() {
045: return new TestSuite(TestRecursiveBeanSchemaGeneration.class);
046: }
047:
048: public void testLoopBeanWithAttributes() throws Exception {
049: SchemaTranscriber transcriber = new SchemaTranscriber();
050: transcriber.getXMLIntrospector().getConfiguration()
051: .setAttributesForPrimitives(true);
052: Schema schema = transcriber.generate(LoopBean.class);
053: StringWriter out = new StringWriter();
054: out.write("<?xml version='1.0'?>");
055:
056: BeanWriter writer = new BeanWriter(out);
057: writer.setBindingConfiguration(transcriber
058: .createSchemaBindingConfiguration());
059: writer.getXMLIntrospector().setConfiguration(
060: transcriber.createSchemaIntrospectionConfiguration());
061: writer.write(schema);
062: String xsd = out.getBuffer().toString();
063:
064: //The expected schema is manual generated, may not be completely match the betwixt generated
065: String expected = "<?xml version='1.0'?><xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"
066: + "<xsd:element name='LoopBean' type='org.apache.commons.betwixt.schema.LoopBean'/>"
067: + "<xsd:complexType name='org.apache.commons.betwixt.schema.LoopBean'>"
068: + "<xsd:sequence>"
069: + "<xsd:element name='friend' type='org.apache.commons.betwixt.schema.LoopBean' minOccurs='0' maxOccurs='1'/>"
070: + "</xsd:sequence>"
071: + "<xsd:attribute name='name' type='xsd:string'/>"
072: + "</xsd:complexType>" + "</xsd:schema>";
073:
074: xmlAssertIsomorphicContent(parseString(expected),
075: parseString(xsd));
076:
077: LoopBean loopBean = new LoopBean("Harry");
078: loopBean.setFriend(new LoopBean("Sally"));
079:
080: out = new StringWriter();
081: out.write("<?xml version='1.0'?>");
082: writer = new BeanWriter(out);
083: writer.getBindingConfiguration().setMapIDs(false);
084: writer.getXMLIntrospector().getConfiguration()
085: .setAttributesForPrimitives(true);
086: writer.write(loopBean);
087:
088: String xml = out.getBuffer().toString();
089:
090: xmlAssertIsValid(xml, xsd);
091: }
092:
093: public void testCyclicBean() throws Exception {
094: SchemaTranscriber transcriber = new SchemaTranscriber();
095: transcriber.getXMLIntrospector().getConfiguration()
096: .setAttributesForPrimitives(true);
097: Schema schema = transcriber.generate(CyclicBean.class);
098:
099: StringWriter out = new StringWriter();
100: out.write("<?xml version='1.0'?>");
101: BeanWriter writer = new BeanWriter(out);
102: writer.setBindingConfiguration(transcriber
103: .createSchemaBindingConfiguration());
104: writer.getXMLIntrospector().setConfiguration(
105: transcriber.createSchemaIntrospectionConfiguration());
106:
107: writer.write(schema);
108:
109: String xsd = out.getBuffer().toString();
110:
111: String expected = "<?xml version='1.0'?><xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"
112: + "<xsd:element name='CyclicBean' type='org.apache.commons.betwixt.schema.CyclicBean'/>"
113: + "<xsd:complexType name='org.apache.commons.betwixt.schema.CyclicBean'>"
114: + "<xsd:sequence>"
115: + "<xsd:element name='layers' minOccurs='0' maxOccurs='1'>"
116: + "<xsd:complexType>"
117: + "<xsd:sequence>"
118: + "<xsd:element name='layer' type='org.apache.commons.betwixt.schema.CyclicLayer' minOccurs='0' maxOccurs='unbounded'/>"
119: + "</xsd:sequence>"
120: + "</xsd:complexType>"
121: + "</xsd:element>"
122: + "</xsd:sequence>"
123: + "<xsd:attribute name='name' type='xsd:string'/>"
124: + "</xsd:complexType>"
125: + "<xsd:complexType name='org.apache.commons.betwixt.schema.CyclicLayer'>"
126: + "<xsd:sequence>"
127: + "<xsd:element name='columns' minOccurs='0' maxOccurs='1'>"
128: + "<xsd:complexType>"
129: + "<xsd:sequence>"
130: + "<xsd:element name='column' type='org.apache.commons.betwixt.schema.CyclicColumn' minOccurs='0' maxOccurs='unbounded'/>"
131: + "</xsd:sequence>"
132: + "</xsd:complexType>"
133: + "</xsd:element>"
134: + "</xsd:sequence>"
135: + "<xsd:attribute name='name' type='xsd:string'/>"
136: + "</xsd:complexType>"
137: + "<xsd:complexType name='org.apache.commons.betwixt.schema.CyclicColumn'>"
138: + "<xsd:sequence>"
139: + "<xsd:element name='bean' type='org.apache.commons.betwixt.schema.CyclicBean' minOccurs='0' maxOccurs='1'/>"
140: + "</xsd:sequence>"
141: + "<xsd:attribute name='name' type='xsd:string'/>"
142: + "</xsd:complexType>" + "</xsd:schema>";
143:
144: xmlAssertIsomorphicContent(parseString(expected),
145: parseString(xsd));
146: }
147: }
|