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 java.math.BigDecimal;
22  import java.math.BigInteger;
23  
24  import junit.framework.TestCase;
25  
26  /**
27   * Tests for <code>DataTypeMapper</code>
28   * both usages and implementations.
29   * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
30   * @version $Revision: 561314 $
31   */
32  public class TestDataTypeMapper extends TestCase {
33  
34      public TestDataTypeMapper(String testName) {
35          super(testName);
36      }
37  
38      public void testDefaultDataTypeMapping() throws Exception {
39          DefaultDataTypeMapper mapper = new DefaultDataTypeMapper();
40          assertEquals("java.lang.String", "xsd:string", mapper.toXMLSchemaDataType(String.class));
41          assertEquals("java.math.BigInteger", "xsd:integer", mapper.toXMLSchemaDataType(BigInteger.class));
42          assertEquals("java.math.BigDecimal", "xsd:decimal", mapper.toXMLSchemaDataType(BigDecimal.class));
43          assertEquals("Integer", "xsd:int", mapper.toXMLSchemaDataType(Integer.TYPE));
44          assertEquals("int", "xsd:int", mapper.toXMLSchemaDataType(Integer.class));
45          assertEquals("Long", "xsd:long", mapper.toXMLSchemaDataType(Long.TYPE));
46          assertEquals("long", "xsd:long", mapper.toXMLSchemaDataType(Long.class));
47          assertEquals("Short", "xsd:short", mapper.toXMLSchemaDataType(Short.TYPE));
48          assertEquals("short", "xsd:short", mapper.toXMLSchemaDataType(Short.class));
49          assertEquals("Float", "xsd:float", mapper.toXMLSchemaDataType(Float.TYPE));
50          assertEquals("float", "xsd:float", mapper.toXMLSchemaDataType(Float.class));
51          assertEquals("Double", "xsd:double", mapper.toXMLSchemaDataType(Double.TYPE));
52          assertEquals("double", "xsd:double", mapper.toXMLSchemaDataType(Double.class));
53          assertEquals("Boolean", "xsd:boolean", mapper.toXMLSchemaDataType(Boolean.TYPE));
54          assertEquals("boolean", "xsd:boolean", mapper.toXMLSchemaDataType(Boolean.class));
55          assertEquals("Byte", "xsd:byte", mapper.toXMLSchemaDataType(Byte.TYPE));
56          assertEquals("byte", "xsd:byte", mapper.toXMLSchemaDataType(byte.class));
57          assertEquals("java.util.Date", "xsd:dateTime", mapper.toXMLSchemaDataType(java.util.Date.class));
58          assertEquals("java.sql.Date", "xsd:date", mapper.toXMLSchemaDataType(java.sql.Date.class));
59          assertEquals("java.sql.Time", "xsd:time", mapper.toXMLSchemaDataType(java.sql.Time.class));
60      }
61      
62      public void testDefaultDataTypeTransciption() throws Exception {
63          Schema expected = new Schema();
64          
65          GlobalComplexType allSimplesBeanType = new GlobalComplexType();
66          allSimplesBeanType.setName("org.apache.commons.betwixt.schema.AllSimplesBean");
67          allSimplesBeanType.addElement(new SimpleLocalElement("string", "xsd:string"));
68          allSimplesBeanType.addElement(new SimpleLocalElement("bigInteger", "xsd:integer"));
69          allSimplesBeanType.addElement(new SimpleLocalElement("primitiveInt", "xsd:int"));
70          allSimplesBeanType.addElement(new SimpleLocalElement("objectInt", "xsd:int"));
71          allSimplesBeanType.addElement(new SimpleLocalElement("primitiveLong", "xsd:long"));
72          allSimplesBeanType.addElement(new SimpleLocalElement("objectLong", "xsd:long"));
73          allSimplesBeanType.addElement(new SimpleLocalElement("primitiveShort", "xsd:short"));
74          allSimplesBeanType.addElement(new SimpleLocalElement("objectShort", "xsd:short"));
75          allSimplesBeanType.addElement(new SimpleLocalElement("bigDecimal", "xsd:decimal"));
76          allSimplesBeanType.addElement(new SimpleLocalElement("primitiveFloat", "xsd:float"));
77          allSimplesBeanType.addElement(new SimpleLocalElement("objectFloat", "xsd:float"));
78          allSimplesBeanType.addElement(new SimpleLocalElement("primitiveDouble", "xsd:double"));
79          allSimplesBeanType.addElement(new SimpleLocalElement("objectDouble", "xsd:double"));
80          allSimplesBeanType.addElement(new SimpleLocalElement("primitiveBoolean", "xsd:boolean"));
81          allSimplesBeanType.addElement(new SimpleLocalElement("objectBoolean", "xsd:boolean"));
82          allSimplesBeanType.addElement(new SimpleLocalElement("primitiveByte", "xsd:byte"));
83          allSimplesBeanType.addElement(new SimpleLocalElement("objectByte", "xsd:byte"));
84          allSimplesBeanType.addElement(new SimpleLocalElement("utilDate", "xsd:dateTime"));
85          allSimplesBeanType.addElement(new SimpleLocalElement("sqlDate", "xsd:date"));
86          allSimplesBeanType.addElement(new SimpleLocalElement("sqlTime", "xsd:time"));
87  
88          GlobalElement root = new GlobalElement("AllSimplesBean", "org.apache.commons.betwixt.schema.AllSimplesBean");
89          expected.addComplexType(allSimplesBeanType);
90          expected.addElement(root);
91          
92          SchemaTranscriber transcriber = new SchemaTranscriber();
93          transcriber.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
94          Schema out = transcriber.generate(AllSimplesBean.class);
95          
96          assertEquals("AllSimplesBean schema", expected, out);        
97      }
98  }