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 java.util.Iterator;
021:
022: import javax.xml.namespace.QName;
023:
024: import org.w3c.dom.Document;
025:
026: import org.apache.cxf.aegis.AbstractAegisTest;
027: import org.apache.cxf.aegis.type.Type;
028: import org.apache.cxf.aegis.type.TypeMapping;
029: import org.apache.cxf.aegis.type.XMLTypeCreator;
030: import org.apache.cxf.aegis.type.basic.BeanType;
031: import org.apache.cxf.aegis.type.basic.StringType;
032: import org.apache.cxf.aegis.util.XmlConstants;
033: import org.apache.cxf.endpoint.Server;
034: import org.apache.cxf.service.Service;
035: import org.junit.Before;
036: import org.junit.Test;
037:
038: public class AnnotatedTypeTest extends AbstractAegisTest {
039: private TypeMapping tm;
040: private Service service;
041:
042: @Before
043: public void setUp() throws Exception {
044: super .setUp();
045:
046: Server s = createService(AnnotatedService.class, null);
047: service = s.getEndpoint().getService();
048:
049: tm = (TypeMapping) service.get(TypeMapping.class.getName());
050: }
051:
052: @Test
053: public void testTM() {
054: assertTrue(tm.getTypeCreator() instanceof XMLTypeCreator);
055: }
056:
057: @Test
058: public void testType() {
059: AnnotatedTypeInfo info = new AnnotatedTypeInfo(tm,
060: AnnotatedBean1.class, "urn:foo");
061:
062: Iterator elements = info.getElements();
063: assertTrue(elements.hasNext());
064: QName element = (QName) elements.next();
065: assertTrue(elements.hasNext());
066:
067: Type custom = info.getType(element);
068:
069: if ("bogusProperty".equals(element.getLocalPart())) {
070: assertTrue(custom instanceof StringType);
071: } else if ("elementProperty".equals(element.getLocalPart())) {
072: assertTrue(custom instanceof CustomStringType);
073: } else {
074: fail("Unexpected element name: " + element.getLocalPart());
075: }
076: element = (QName) elements.next();
077: assertFalse(elements.hasNext());
078:
079: custom = info.getType(element);
080:
081: if ("bogusProperty".equals(element.getLocalPart())) {
082: assertTrue(custom instanceof StringType);
083: } else if ("elementProperty".equals(element.getLocalPart())) {
084: assertTrue(custom instanceof CustomStringType);
085: } else {
086: fail("Unexpected element name: " + element.getLocalPart());
087: }
088:
089: Iterator atts = info.getAttributes();
090: assertTrue(atts.hasNext());
091: atts.next();
092: assertFalse(atts.hasNext());
093:
094: assertTrue(info.isExtensibleElements());
095: assertTrue(info.isExtensibleAttributes());
096: }
097:
098: @Test
099: public void testAegisType() {
100: BeanType type = (BeanType) tm.getTypeCreator().createType(
101: AnnotatedBean3.class);
102:
103: assertFalse(type.getTypeInfo().getAttributes().hasNext());
104:
105: Iterator itr = type.getTypeInfo().getElements();
106: assertTrue(itr.hasNext());
107: QName q = (QName) itr.next();
108: assertEquals("attProp", q.getLocalPart());
109: }
110:
111: @Test
112: public void testExtensibilityOff() {
113: BeanType type = (BeanType) tm.getTypeCreator().createType(
114: AnnotatedBean4.class);
115:
116: assertFalse(type.getTypeInfo().isExtensibleElements());
117: assertFalse(type.getTypeInfo().isExtensibleAttributes());
118: }
119:
120: @Test
121: public void testNillableAndMinOccurs() {
122: BeanType type = (BeanType) tm.getTypeCreator().createType(
123: AnnotatedBean4.class);
124: AnnotatedTypeInfo info = (AnnotatedTypeInfo) type.getTypeInfo();
125: Iterator elements = info.getElements();
126: assertTrue(elements.hasNext());
127: // nillable first
128: QName element = (QName) elements.next();
129: if ("minOccursProperty".equals(element.getLocalPart())) {
130: assertEquals(1, info.getMinOccurs(element));
131: } else {
132: assertFalse(info.isNillable(element));
133: }
134:
135: assertTrue(elements.hasNext());
136: // minOccurs = 1 second
137: element = (QName) elements.next();
138: if ("minOccursProperty".equals(element.getLocalPart())) {
139: assertEquals(1, info.getMinOccurs(element));
140: } else {
141: assertFalse(info.isNillable(element));
142: }
143: }
144:
145: @Test
146: public void testWSDL() throws Exception {
147: Document wsdl = getWSDLDocument("AnnotatedService");
148:
149: addNamespace("xsd", XmlConstants.XSD);
150: assertValid(
151: "//xsd:complexType[@name='AnnotatedBean1']/xsd:sequence/xsd:"
152: + "element[@name='elementProperty']", wsdl);
153: assertValid(
154: "//xsd:complexType[@name='AnnotatedBean1']/xsd:attribute"
155: + "[@name='attributeProperty']", wsdl);
156: assertValid(
157: "//xsd:complexType[@name='AnnotatedBean1']/xsd:sequence/xsd:element"
158: + "[@name='bogusProperty']", wsdl);
159:
160: assertValid(
161: "//xsd:complexType[@name='AnnotatedBean2']/xsd:sequence/xsd:element"
162: + "[@name='element'][@type='string']", wsdl);
163: assertValid(
164: "//xsd:complexType[@name='AnnotatedBean2']/xsd:attribute"
165: + "[@name='attribute'][@type='string']", wsdl);
166: }
167:
168: @Test
169: public void testGetSetRequired() throws Exception {
170: BeanType type = new BeanType(new AnnotatedTypeInfo(tm,
171: BadBean.class, "urn:foo"));
172: type.setSchemaType(new QName("urn:foo", "BadBean"));
173:
174: assertFalse(type.getTypeInfo().getElements().hasNext());
175: }
176:
177: public static class BadBean {
178: public void setString(String string) {
179: }
180: }
181: }
|