001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.aegis.type.java5;
019:
020: import javax.xml.namespace.QName;
021:
022: import org.w3c.dom.Document;
023:
024: import org.apache.cxf.aegis.AbstractAegisTest;
025: import org.apache.cxf.aegis.Context;
026: import org.apache.cxf.aegis.type.Configuration;
027: import org.apache.cxf.aegis.type.CustomTypeMapping;
028: import org.apache.cxf.aegis.type.Type;
029: import org.apache.cxf.aegis.type.java5.CurrencyService.Currency;
030: import org.apache.cxf.aegis.util.XmlConstants;
031: import org.apache.cxf.aegis.xml.jdom.JDOMReader;
032: import org.apache.cxf.aegis.xml.jdom.JDOMWriter;
033: import org.jdom.Element;
034: import org.jdom.output.DOMOutputter;
035: import org.junit.Before;
036: import org.junit.Test;
037:
038: public class EnumTypeTest extends AbstractAegisTest {
039: private CustomTypeMapping tm;
040:
041: private enum smallEnum {
042: VALUE1, VALUE2
043: };
044:
045: @Before
046: public void setUp() throws Exception {
047: super .setUp();
048:
049: tm = new CustomTypeMapping();
050: Java5TypeCreator creator = new Java5TypeCreator();
051: creator.setConfiguration(new Configuration());
052: tm.setTypeCreator(creator);
053: }
054:
055: @Test
056: public void testType() throws Exception {
057: EnumType type = new EnumType();
058: type.setTypeClass(smallEnum.class);
059: type.setSchemaType(new QName("urn:test", "test"));
060:
061: tm.register(type);
062:
063: Element root = new Element("root");
064: JDOMWriter writer = new JDOMWriter(root);
065:
066: type.writeObject(smallEnum.VALUE1, writer, new Context());
067:
068: assertEquals("VALUE1", root.getValue());
069:
070: JDOMReader reader = new JDOMReader(root);
071: Object value = type.readObject(reader, new Context());
072:
073: assertEquals(smallEnum.VALUE1, value);
074: }
075:
076: @Test
077: public void testAutoCreation() throws Exception {
078: Type type = (Type) tm.getTypeCreator().createType(
079: smallEnum.class);
080:
081: assertTrue(type instanceof EnumType);
082: }
083:
084: @Test
085: public void testTypeAttributeOnEnum() throws Exception {
086: Type type = (Type) tm.getTypeCreator().createType(
087: TestEnum.class);
088:
089: assertEquals("urn:xfire:foo", type.getSchemaType()
090: .getNamespaceURI());
091:
092: assertTrue(type instanceof EnumType);
093: }
094:
095: @Test
096: public void testWSDL() throws Exception {
097: EnumType type = new EnumType();
098: type.setTypeClass(smallEnum.class);
099: type.setSchemaType(new QName("urn:test", "test"));
100:
101: Element root = new Element("root");
102: org.jdom.Document wsdl = new org.jdom.Document(root);
103: type.writeSchema(root);
104:
105: Document doc = new DOMOutputter().output(wsdl);
106: addNamespace("xsd", XmlConstants.XSD);
107: assertValid(
108: "//xsd:simpleType[@name='test']/xsd:restriction[@base='xsd:string']",
109: doc);
110: assertValid(
111: "//xsd:restriction[@base='xsd:string']/xsd:enumeration[@value='VALUE1']",
112: doc);
113: assertValid(
114: "//xsd:restriction[@base='xsd:string']/xsd:enumeration[@value='VALUE2']",
115: doc);
116: }
117:
118: @Test
119: public void testCurrencyService() throws Exception {
120: createService(CurrencyService.class, null);
121:
122: Document wsdl = getWSDLDocument("CurrencyService");
123:
124: assertValid(
125: "//xsd:element[@name='inputCurrency'][@minOccurs='0']",
126: wsdl);
127: assertValid(
128: "//xsd:simpleType[@name='Currency']/xsd:restriction[@base='string']",
129: wsdl);
130: assertValid(
131: "//xsd:restriction[@base='string']/xsd:enumeration[@value='USD']",
132: wsdl);
133: assertValid(
134: "//xsd:restriction[@base='string']/xsd:enumeration[@value='EURO']",
135: wsdl);
136: assertValid(
137: "//xsd:restriction[@base='string']/xsd:enumeration[@value='POUNDS']",
138: wsdl);
139: }
140:
141: @Test
142: public void testNillable() throws Exception {
143: Type type = tm.getTypeCreator().createType(EnumBean.class);
144:
145: tm.register(type);
146:
147: Element root = new Element("root");
148: JDOMWriter writer = new JDOMWriter(root);
149:
150: type.writeObject(new EnumBean(), writer, new Context());
151:
152: JDOMReader reader = new JDOMReader(root);
153: Object value = type.readObject(reader, new Context());
154:
155: assertTrue(value instanceof EnumBean);
156: EnumBean bean = (EnumBean) value;
157: assertNull(bean.getCurrency());
158: }
159:
160: public static class EnumBean {
161: private Currency currency;
162:
163: public Currency getCurrency() {
164: return currency;
165: }
166:
167: public void setCurrency(Currency currency) {
168: this .currency = currency;
169: }
170:
171: public Currency[] getSomeCurrencies() {
172: return new Currency[] { Currency.EURO, null };
173: }
174:
175: public void setSomeCurrencies(Currency[] currencies) {
176:
177: }
178: }
179: }
|