1   /*
2    * Copyright 2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  
17  
18  package org.apache.commons.betwixt.schema;
19  
20  import org.apache.commons.betwixt.AbstractTestCase;
21  import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
22  
23  /***
24   * Tests for the SchemaTranscriber.
25   * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
26   * @version $Revision: 155402 $
27   */
28  public class TestSchemaTranscriber extends AbstractTestCase {
29      
30      public TestSchemaTranscriber(String testName) {
31          super(testName);
32      }
33  	
34      public void testEmpty() {}
35      
36      public void testSimplestBeanAttribute() throws Exception {
37          Schema expected = new Schema();
38          
39          GlobalComplexType simplestBeanType = new GlobalComplexType();
40          simplestBeanType.setName("org.apache.commons.betwixt.schema.SimplestBean");
41          simplestBeanType.addAttribute(new Attribute("name", "xsd:string"));
42          
43          GlobalElement root = new GlobalElement("SimplestBean", "org.apache.commons.betwixt.schema.SimplestBean");
44          expected.addComplexType(simplestBeanType);
45          expected.addElement(root);
46          
47          SchemaTranscriber transcriber = new SchemaTranscriber();
48          transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
49          Schema out = transcriber.generate(SimplestBean.class);
50          
51          assertEquals("Simplest bean schema", expected, out);
52      }
53      
54      public void testSimplestBeanElement() throws Exception {
55          Schema expected = new Schema();
56          
57          GlobalComplexType simplestBeanType = new GlobalComplexType();
58          simplestBeanType.setName("org.apache.commons.betwixt.schema.SimplestElementBean");
59          simplestBeanType.addElement(new SimpleLocalElement("name", "xsd:string"));
60          
61          GlobalElement root = new GlobalElement("SimplestBean", "org.apache.commons.betwixt.schema.SimplestElementBean");
62          expected.addComplexType(simplestBeanType);
63          expected.addElement(root);
64          
65          SchemaTranscriber transcriber = new SchemaTranscriber();
66          transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
67          Schema out = transcriber.generate(SimplestElementBean.class);
68          
69          assertEquals("Simplest bean schema", expected, out);
70      }
71      
72  	public void testSimpleBean() throws Exception {
73  		SchemaTranscriber transcriber = new SchemaTranscriber();
74  		Schema out = transcriber.generate(SimpleBean.class);
75  		
76  		Schema expected = new Schema();
77  		GlobalComplexType simpleBeanType = new GlobalComplexType();
78  		simpleBeanType.setName("org.apache.commons.betwixt.schema.SimpleBean");
79  		simpleBeanType.addAttribute(new Attribute("one", "xsd:string"));
80  		simpleBeanType.addAttribute(new Attribute("two", "xsd:string"));
81  		simpleBeanType.addElement(new SimpleLocalElement("three", "xsd:string"));
82  		simpleBeanType.addElement(new SimpleLocalElement("four", "xsd:string"));
83  		expected.addComplexType(simpleBeanType);
84          expected.addElement(new GlobalElement("simple", "org.apache.commons.betwixt.schema.SimpleBean"));
85          
86          assertEquals("Simple bean schema", expected, out);
87          
88  	}
89      
90      public void testOrderLine() throws Exception {
91          SchemaTranscriber transcriber = new SchemaTranscriber();
92          transcriber.getXMLIntrospector().getConfiguration().setAttributeNameMapper(new HyphenatedNameMapper());
93          transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
94          Schema out = transcriber.generate(OrderLineBean.class);
95          
96          Schema expected = new Schema();
97          
98          GlobalComplexType productBeanType = new GlobalComplexType();
99          productBeanType.setName(ProductBean.class.getName());
100         productBeanType.addAttribute(new Attribute("barcode", "xsd:string"));
101         productBeanType.addAttribute(new Attribute("code", "xsd:string"));
102         productBeanType.addAttribute(new Attribute("name", "xsd:string"));
103         productBeanType.addAttribute(new Attribute("display-name", "xsd:string"));
104         expected.addComplexType(productBeanType);
105         
106         GlobalComplexType orderLineType = new GlobalComplexType();       
107         orderLineType.setName(OrderLineBean.class.getName());
108         orderLineType.addAttribute(new Attribute("quantity", "xsd:string"));
109         orderLineType.addElement(new ElementReference("product", productBeanType));
110         expected.addComplexType(orderLineType);
111         expected.addElement(new GlobalElement("OrderLineBean", OrderLineBean.class.getName()));
112         
113         assertEquals("Transcriber schema", expected, out);   
114     }
115     
116     
117     public void testOrder() throws Exception {
118         SchemaTranscriber transcriber = new SchemaTranscriber();
119         transcriber.getXMLIntrospector().getConfiguration().setElementNameMapper(new HyphenatedNameMapper());
120         transcriber.getXMLIntrospector().getConfiguration().setAttributeNameMapper(new HyphenatedNameMapper());
121         transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
122         transcriber.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(false);
123         Schema out = transcriber.generate(OrderBean.class);
124         
125         Schema expected = new Schema();
126         
127         
128         GlobalComplexType customerBeanType = new GlobalComplexType();
129         customerBeanType.setName(CustomerBean.class.getName());
130         customerBeanType.addAttribute(new Attribute("code", "xsd:string"));
131         customerBeanType.addAttribute(new Attribute("name", "xsd:string"));
132         customerBeanType.addAttribute(new Attribute("street", "xsd:string"));
133         customerBeanType.addAttribute(new Attribute("town", "xsd:string"));
134         customerBeanType.addAttribute(new Attribute("country", "xsd:string"));
135         customerBeanType.addAttribute(new Attribute("postcode", "xsd:string"));
136         expected.addComplexType(customerBeanType);
137         
138         GlobalComplexType productBeanType = new GlobalComplexType();
139         productBeanType.setName(ProductBean.class.getName());
140         productBeanType.addAttribute(new Attribute("barcode", "xsd:string"));
141         productBeanType.addAttribute(new Attribute("code", "xsd:string"));
142         productBeanType.addAttribute(new Attribute("name", "xsd:string"));
143         productBeanType.addAttribute(new Attribute("display-name", "xsd:string"));
144         expected.addComplexType(productBeanType);
145         
146         GlobalComplexType orderLineType = new GlobalComplexType();       
147         orderLineType.setName(OrderLineBean.class.getName());
148         orderLineType.addAttribute(new Attribute("quantity", "xsd:string"));
149         orderLineType.addElement(new ElementReference("product", productBeanType));
150         expected.addComplexType(orderLineType);
151         
152         GlobalComplexType orderType = new GlobalComplexType();       
153         orderType.setName(OrderBean.class.getName());
154         orderType.addAttribute(new Attribute("code", "xsd:string"));
155         orderType.addElement(new ElementReference("customer", customerBeanType));
156         orderType.addElement(new ElementReference("line", orderLineType));
157         expected.addComplexType(orderType);
158         expected.addElement(new GlobalElement("order-bean", OrderBean.class.getName()));
159         
160         assertEquals("Transcriber schema", expected, out);   
161     }
162 
163 }