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.beans.Introspector;
021: import java.beans.PropertyDescriptor;
022: import java.lang.reflect.Method;
023: import java.util.Collection;
024: import java.util.Set;
025:
026: import javax.xml.namespace.QName;
027:
028: import org.w3c.dom.Document;
029:
030: import org.apache.cxf.aegis.AbstractAegisTest;
031: import org.apache.cxf.aegis.type.Configuration;
032: import org.apache.cxf.aegis.type.CustomTypeMapping;
033: import org.apache.cxf.aegis.type.Type;
034: import org.apache.cxf.aegis.type.collection.CollectionType;
035: import org.apache.cxf.aegis.type.java5.dto.CollectionDTO;
036: import org.apache.cxf.aegis.type.java5.dto.DTOService;
037: import org.apache.cxf.aegis.type.java5.dto.ObjectDTO;
038: import org.junit.Before;
039: import org.junit.Test;
040:
041: public class CollectionTest extends AbstractAegisTest {
042: private CustomTypeMapping tm;
043: private Java5TypeCreator creator;
044:
045: @Before
046: public void setUp() throws Exception {
047: super .setUp();
048:
049: tm = new CustomTypeMapping();
050: creator = new Java5TypeCreator();
051: creator.setConfiguration(new Configuration());
052: tm.setTypeCreator(creator);
053: }
054:
055: @SuppressWarnings("unchecked")
056: @Test
057: public void testType() throws Exception {
058: Method m = CollectionService.class.getMethod("getStrings",
059: new Class[0]);
060:
061: Type type = creator.createType(m, -1);
062: tm.register(type);
063: assertTrue(type instanceof CollectionType);
064:
065: CollectionType colType = (CollectionType) type;
066: QName componentName = colType.getSchemaType();
067:
068: assertEquals("ArrayOfString", componentName.getLocalPart());
069: assertEquals("ArrayOfString", componentName.getLocalPart());
070:
071: type = colType.getComponentType();
072: assertNotNull(type);
073: assertTrue(type.getTypeClass().isAssignableFrom(String.class));
074: }
075:
076: @SuppressWarnings("unchecked")
077: @Test
078: public void testRecursiveCollections() throws Exception {
079: Method m = CollectionService.class.getMethod(
080: "getStringCollections", new Class[0]);
081:
082: Type type = creator.createType(m, -1);
083: tm.register(type);
084: assertTrue(type instanceof CollectionType);
085:
086: CollectionType colType = (CollectionType) type;
087: QName componentName = colType.getSchemaType();
088:
089: assertEquals("ArrayOfArrayOfString", componentName
090: .getLocalPart());
091:
092: type = colType.getComponentType();
093: assertNotNull(type);
094: assertTrue(type instanceof CollectionType);
095:
096: CollectionType colType2 = (CollectionType) type;
097: componentName = colType2.getSchemaType();
098:
099: assertEquals("ArrayOfString", componentName.getLocalPart());
100:
101: type = colType2.getComponentType();
102: assertTrue(type.getTypeClass().isAssignableFrom(String.class));
103: }
104:
105: @SuppressWarnings("unchecked")
106: @Test
107: public void testPDType() throws Exception {
108: PropertyDescriptor pd = Introspector.getBeanInfo(
109: CollectionDTO.class, Object.class)
110: .getPropertyDescriptors()[0];
111: Type type = creator.createType(pd);
112: tm.register(type);
113: assertTrue(type instanceof CollectionType);
114:
115: CollectionType colType = (CollectionType) type;
116:
117: type = colType.getComponentType();
118: assertNotNull(type);
119: assertTrue(type.getTypeClass().isAssignableFrom(String.class));
120: }
121:
122: @Test
123: public void testCollectionDTO() {
124: tm = new CustomTypeMapping();
125: creator = new Java5TypeCreator();
126: creator.setConfiguration(new Configuration());
127: tm.setTypeCreator(creator);
128:
129: Type dto = creator.createType(CollectionDTO.class);
130: Set deps = dto.getDependencies();
131:
132: Type type = (Type) deps.iterator().next();
133:
134: assertTrue(type instanceof CollectionType);
135:
136: CollectionType colType = (CollectionType) type;
137:
138: deps = dto.getDependencies();
139: assertEquals(1, deps.size());
140:
141: Type comType = colType.getComponentType();
142: assertEquals(String.class, comType.getTypeClass());
143: }
144:
145: @Test
146: public void testObjectDTO() {
147: tm = new CustomTypeMapping();
148: creator = new Java5TypeCreator();
149: creator.setConfiguration(new Configuration());
150: tm.setTypeCreator(creator);
151:
152: Type dto = creator.createType(ObjectDTO.class);
153: Set deps = dto.getDependencies();
154:
155: assertFalse(deps.isEmpty());
156:
157: Type type = (Type) deps.iterator().next();
158:
159: assertTrue(type instanceof CollectionType);
160:
161: CollectionType colType = (CollectionType) type;
162:
163: deps = dto.getDependencies();
164: assertEquals(1, deps.size());
165:
166: Type comType = colType.getComponentType();
167: assertEquals(Object.class, comType.getTypeClass());
168: }
169:
170: @Test
171: public void testCollectionDTOService() throws Exception {
172: createService(DTOService.class, null);
173: invoke("DTOService",
174: "/org/apache/cxf/aegis/type/java5/dto/GetDTO.xml");
175: }
176:
177: @Test
178: public void testCollectionServiceWSDL() throws Exception {
179: createService(CollectionService.class, null);
180:
181: Document wsdl = getWSDLDocument("CollectionService");
182: assertValid(
183: "//xsd:element[@name='return'][@type='ns1:ArrayOfString']",
184: wsdl);
185: }
186:
187: @Test
188: public void testUnannotatedStrings() throws Exception {
189: createService(CollectionService.class, null);
190:
191: Document doc = getWSDLDocument("CollectionService");
192: // printNode(doc);
193: assertValid(
194: "//xsd:complexType[@name='getUnannotatedStringsResponse']"
195: + "/xsd:sequence/xsd:element[@type='ns1:ArrayOfString']",
196: doc);
197: }
198:
199: public class CollectionService {
200: public Collection<String> getStrings() {
201: return null;
202: }
203:
204: public void setLongs(Collection<Long> longs) {
205: }
206:
207: public Collection getUnannotatedStrings() {
208: return null;
209: }
210:
211: public Collection<Collection<String>> getStringCollections() {
212: return null;
213: }
214: }
215: }
|