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  
18  
19  package org.apache.commons.betwixt.schema;
20  
21  import org.apache.commons.betwixt.AbstractTestCase;
22  import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
23  
24  /**
25   * Tests for the SchemaTranscriber.
26   * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
27   * @version $Revision: 561314 $
28   */
29  public class TestSchemaTranscriber extends AbstractTestCase {
30      
31      public TestSchemaTranscriber(String testName) {
32          super(testName);
33      }
34  	
35      public void testEmpty() {}
36      
37      public void testSimplestBeanAttribute() throws Exception {
38          Schema expected = new Schema();
39          
40          GlobalComplexType simplestBeanType = new GlobalComplexType();
41          simplestBeanType.setName("org.apache.commons.betwixt.schema.SimplestBean");
42          simplestBeanType.addAttribute(new Attribute("name", "xsd:string"));
43          
44          GlobalElement root = new GlobalElement("SimplestBean", "org.apache.commons.betwixt.schema.SimplestBean");
45          expected.addComplexType(simplestBeanType);
46          expected.addElement(root);
47          
48          SchemaTranscriber transcriber = new SchemaTranscriber();
49          transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
50          Schema out = transcriber.generate(SimplestBean.class);
51          
52          assertEquals("Simplest bean schema", expected, out);
53      }
54      
55      public void testSimplestBeanElement() throws Exception {
56          Schema expected = new Schema();
57          
58          GlobalComplexType simplestBeanType = new GlobalComplexType();
59          simplestBeanType.setName("org.apache.commons.betwixt.schema.SimplestElementBean");
60          simplestBeanType.addElement(new SimpleLocalElement("name", "xsd:string"));
61          
62          GlobalElement root = new GlobalElement("SimplestBean", "org.apache.commons.betwixt.schema.SimplestElementBean");
63          expected.addComplexType(simplestBeanType);
64          expected.addElement(root);
65          
66          SchemaTranscriber transcriber = new SchemaTranscriber();
67          transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
68          Schema out = transcriber.generate(SimplestElementBean.class);
69          
70          assertEquals("Simplest bean schema", expected, out);
71      }
72      
73  	public void testSimpleBean() throws Exception {
74  		SchemaTranscriber transcriber = new SchemaTranscriber();
75  		Schema out = transcriber.generate(SimpleBean.class);
76  		
77  		Schema expected = new Schema();
78  		GlobalComplexType simpleBeanType = new GlobalComplexType();
79  		simpleBeanType.setName("org.apache.commons.betwixt.schema.SimpleBean");
80  		simpleBeanType.addAttribute(new Attribute("one", "xsd:string"));
81  		simpleBeanType.addAttribute(new Attribute("two", "xsd:string"));
82  		simpleBeanType.addElement(new SimpleLocalElement("three", "xsd:string"));
83  		simpleBeanType.addElement(new SimpleLocalElement("four", "xsd:string"));
84  		expected.addComplexType(simpleBeanType);
85          expected.addElement(new GlobalElement("simple", "org.apache.commons.betwixt.schema.SimpleBean"));
86          
87          assertEquals("Simple bean schema", expected, out);
88          
89  	}
90      
91      public void testOrderLine() throws Exception {
92          SchemaTranscriber transcriber = new SchemaTranscriber();
93          transcriber.getXMLIntrospector().getConfiguration().setAttributeNameMapper(new HyphenatedNameMapper());
94          transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
95          Schema out = transcriber.generate(OrderLineBean.class);
96          
97          Schema expected = new Schema();
98          
99          GlobalComplexType productBeanType = new GlobalComplexType();
100         productBeanType.setName(ProductBean.class.getName());
101         productBeanType.addAttribute(new Attribute("barcode", "xsd:string"));
102         productBeanType.addAttribute(new Attribute("code", "xsd:string"));
103         productBeanType.addAttribute(new Attribute("name", "xsd:string"));
104         productBeanType.addAttribute(new Attribute("display-name", "xsd:string"));
105         expected.addComplexType(productBeanType);
106         
107         GlobalComplexType orderLineType = new GlobalComplexType();       
108         orderLineType.setName(OrderLineBean.class.getName());
109         orderLineType.addAttribute(new Attribute("quantity", "xsd:string"));
110         orderLineType.addElement(new ElementReference("product", productBeanType));
111         expected.addComplexType(orderLineType);
112         expected.addElement(new GlobalElement("OrderLineBean", OrderLineBean.class.getName()));
113         
114         assertEquals("Transcriber schema", expected, out);   
115     }
116     
117     
118     public void testOrder() throws Exception {
119         SchemaTranscriber transcriber = new SchemaTranscriber();
120         transcriber.getXMLIntrospector().getConfiguration().setElementNameMapper(new HyphenatedNameMapper());
121         transcriber.getXMLIntrospector().getConfiguration().setAttributeNameMapper(new HyphenatedNameMapper());
122         transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
123         transcriber.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(false);
124         Schema out = transcriber.generate(OrderBean.class);
125         
126         Schema expected = new Schema();
127         
128         
129         GlobalComplexType customerBeanType = new GlobalComplexType();
130         customerBeanType.setName(CustomerBean.class.getName());
131         customerBeanType.addAttribute(new Attribute("code", "xsd:string"));
132         customerBeanType.addAttribute(new Attribute("name", "xsd:string"));
133         customerBeanType.addAttribute(new Attribute("street", "xsd:string"));
134         customerBeanType.addAttribute(new Attribute("town", "xsd:string"));
135         customerBeanType.addAttribute(new Attribute("country", "xsd:string"));
136         customerBeanType.addAttribute(new Attribute("postcode", "xsd:string"));
137         expected.addComplexType(customerBeanType);
138         
139         GlobalComplexType productBeanType = new GlobalComplexType();
140         productBeanType.setName(ProductBean.class.getName());
141         productBeanType.addAttribute(new Attribute("barcode", "xsd:string"));
142         productBeanType.addAttribute(new Attribute("code", "xsd:string"));
143         productBeanType.addAttribute(new Attribute("name", "xsd:string"));
144         productBeanType.addAttribute(new Attribute("display-name", "xsd:string"));
145         expected.addComplexType(productBeanType);
146         
147         GlobalComplexType orderLineType = new GlobalComplexType();       
148         orderLineType.setName(OrderLineBean.class.getName());
149         orderLineType.addAttribute(new Attribute("quantity", "xsd:string"));
150         orderLineType.addElement(new ElementReference("product", productBeanType));
151         expected.addComplexType(orderLineType);
152         
153         GlobalComplexType orderType = new GlobalComplexType();       
154         orderType.setName(OrderBean.class.getName());
155         orderType.addAttribute(new Attribute("code", "xsd:string"));
156         orderType.addElement(new ElementReference("customer", customerBeanType));
157         orderType.addElement(new ElementReference("line", orderLineType));
158         expected.addComplexType(orderType);
159         expected.addElement(new GlobalElement("order-bean", OrderBean.class.getName()));
160         
161         assertEquals("Transcriber schema", expected, out);   
162     }
163 
164 }