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:
018: package org.apache.commons.betwixt.schema;
019:
020: import org.apache.commons.betwixt.AbstractTestCase;
021: import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
022:
023: /**
024: * Tests for the SchemaTranscriber.
025: * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
026: * @version $Revision: 438373 $
027: */
028: public class TestSchemaTranscriber extends AbstractTestCase {
029:
030: public TestSchemaTranscriber(String testName) {
031: super (testName);
032: }
033:
034: public void testEmpty() {
035: }
036:
037: public void testSimplestBeanAttribute() throws Exception {
038: Schema expected = new Schema();
039:
040: GlobalComplexType simplestBeanType = new GlobalComplexType();
041: simplestBeanType
042: .setName("org.apache.commons.betwixt.schema.SimplestBean");
043: simplestBeanType.addAttribute(new Attribute("name",
044: "xsd:string"));
045:
046: GlobalElement root = new GlobalElement("SimplestBean",
047: "org.apache.commons.betwixt.schema.SimplestBean");
048: expected.addComplexType(simplestBeanType);
049: expected.addElement(root);
050:
051: SchemaTranscriber transcriber = new SchemaTranscriber();
052: transcriber.getXMLIntrospector().getConfiguration()
053: .setAttributesForPrimitives(true);
054: Schema out = transcriber.generate(SimplestBean.class);
055:
056: assertEquals("Simplest bean schema", expected, out);
057: }
058:
059: public void testSimplestBeanElement() throws Exception {
060: Schema expected = new Schema();
061:
062: GlobalComplexType simplestBeanType = new GlobalComplexType();
063: simplestBeanType
064: .setName("org.apache.commons.betwixt.schema.SimplestElementBean");
065: simplestBeanType.addElement(new SimpleLocalElement("name",
066: "xsd:string"));
067:
068: GlobalElement root = new GlobalElement("SimplestBean",
069: "org.apache.commons.betwixt.schema.SimplestElementBean");
070: expected.addComplexType(simplestBeanType);
071: expected.addElement(root);
072:
073: SchemaTranscriber transcriber = new SchemaTranscriber();
074: transcriber.getXMLIntrospector().getConfiguration()
075: .setAttributesForPrimitives(false);
076: Schema out = transcriber.generate(SimplestElementBean.class);
077:
078: assertEquals("Simplest bean schema", expected, out);
079: }
080:
081: public void testSimpleBean() throws Exception {
082: SchemaTranscriber transcriber = new SchemaTranscriber();
083: Schema out = transcriber.generate(SimpleBean.class);
084:
085: Schema expected = new Schema();
086: GlobalComplexType simpleBeanType = new GlobalComplexType();
087: simpleBeanType
088: .setName("org.apache.commons.betwixt.schema.SimpleBean");
089: simpleBeanType.addAttribute(new Attribute("one", "xsd:string"));
090: simpleBeanType.addAttribute(new Attribute("two", "xsd:string"));
091: simpleBeanType.addElement(new SimpleLocalElement("three",
092: "xsd:string"));
093: simpleBeanType.addElement(new SimpleLocalElement("four",
094: "xsd:string"));
095: expected.addComplexType(simpleBeanType);
096: expected.addElement(new GlobalElement("simple",
097: "org.apache.commons.betwixt.schema.SimpleBean"));
098:
099: assertEquals("Simple bean schema", expected, out);
100:
101: }
102:
103: public void testOrderLine() throws Exception {
104: SchemaTranscriber transcriber = new SchemaTranscriber();
105: transcriber.getXMLIntrospector().getConfiguration()
106: .setAttributeNameMapper(new HyphenatedNameMapper());
107: transcriber.getXMLIntrospector().getConfiguration()
108: .setAttributesForPrimitives(true);
109: Schema out = transcriber.generate(OrderLineBean.class);
110:
111: Schema expected = new Schema();
112:
113: GlobalComplexType productBeanType = new GlobalComplexType();
114: productBeanType.setName(ProductBean.class.getName());
115: productBeanType.addAttribute(new Attribute("barcode",
116: "xsd:string"));
117: productBeanType
118: .addAttribute(new Attribute("code", "xsd:string"));
119: productBeanType
120: .addAttribute(new Attribute("name", "xsd:string"));
121: productBeanType.addAttribute(new Attribute("display-name",
122: "xsd:string"));
123: expected.addComplexType(productBeanType);
124:
125: GlobalComplexType orderLineType = new GlobalComplexType();
126: orderLineType.setName(OrderLineBean.class.getName());
127: orderLineType.addAttribute(new Attribute("quantity",
128: "xsd:string"));
129: orderLineType.addElement(new ElementReference("product",
130: productBeanType));
131: expected.addComplexType(orderLineType);
132: expected.addElement(new GlobalElement("OrderLineBean",
133: OrderLineBean.class.getName()));
134:
135: assertEquals("Transcriber schema", expected, out);
136: }
137:
138: public void testOrder() throws Exception {
139: SchemaTranscriber transcriber = new SchemaTranscriber();
140: transcriber.getXMLIntrospector().getConfiguration()
141: .setElementNameMapper(new HyphenatedNameMapper());
142: transcriber.getXMLIntrospector().getConfiguration()
143: .setAttributeNameMapper(new HyphenatedNameMapper());
144: transcriber.getXMLIntrospector().getConfiguration()
145: .setAttributesForPrimitives(true);
146: transcriber.getXMLIntrospector().getConfiguration()
147: .setWrapCollectionsInElement(false);
148: Schema out = transcriber.generate(OrderBean.class);
149:
150: Schema expected = new Schema();
151:
152: GlobalComplexType customerBeanType = new GlobalComplexType();
153: customerBeanType.setName(CustomerBean.class.getName());
154: customerBeanType.addAttribute(new Attribute("code",
155: "xsd:string"));
156: customerBeanType.addAttribute(new Attribute("name",
157: "xsd:string"));
158: customerBeanType.addAttribute(new Attribute("street",
159: "xsd:string"));
160: customerBeanType.addAttribute(new Attribute("town",
161: "xsd:string"));
162: customerBeanType.addAttribute(new Attribute("country",
163: "xsd:string"));
164: customerBeanType.addAttribute(new Attribute("postcode",
165: "xsd:string"));
166: expected.addComplexType(customerBeanType);
167:
168: GlobalComplexType productBeanType = new GlobalComplexType();
169: productBeanType.setName(ProductBean.class.getName());
170: productBeanType.addAttribute(new Attribute("barcode",
171: "xsd:string"));
172: productBeanType
173: .addAttribute(new Attribute("code", "xsd:string"));
174: productBeanType
175: .addAttribute(new Attribute("name", "xsd:string"));
176: productBeanType.addAttribute(new Attribute("display-name",
177: "xsd:string"));
178: expected.addComplexType(productBeanType);
179:
180: GlobalComplexType orderLineType = new GlobalComplexType();
181: orderLineType.setName(OrderLineBean.class.getName());
182: orderLineType.addAttribute(new Attribute("quantity",
183: "xsd:string"));
184: orderLineType.addElement(new ElementReference("product",
185: productBeanType));
186: expected.addComplexType(orderLineType);
187:
188: GlobalComplexType orderType = new GlobalComplexType();
189: orderType.setName(OrderBean.class.getName());
190: orderType.addAttribute(new Attribute("code", "xsd:string"));
191: orderType.addElement(new ElementReference("customer",
192: customerBeanType));
193: orderType
194: .addElement(new ElementReference("line", orderLineType));
195: expected.addComplexType(orderType);
196: expected.addElement(new GlobalElement("order-bean",
197: OrderBean.class.getName()));
198:
199: assertEquals("Transcriber schema", expected, out);
200: }
201:
202: }
|