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: */
019: package org.apache.axis2.wsdl.codegen.extension;
020:
021: import junit.framework.TestCase;
022: import org.apache.axis2.description.AxisMessage;
023: import org.apache.axis2.description.AxisOperation;
024: import org.apache.axis2.description.AxisService;
025: import org.apache.axis2.description.InOutAxisOperation;
026: import org.apache.axis2.description.Parameter;
027: import org.apache.axis2.wsdl.WSDLConstants;
028: import org.apache.axis2.wsdl.WSDLUtil;
029: import org.apache.axis2.wsdl.codegen.CodeGenerationException;
030: import org.apache.axis2.wsdl.util.Constants;
031: import org.apache.axis2.wsdl.util.MessagePartInformationHolder;
032: import org.apache.ws.commons.schema.XmlSchema;
033: import org.apache.ws.commons.schema.XmlSchemaCollection;
034:
035: import javax.xml.namespace.QName;
036: import javax.xml.transform.stream.StreamSource;
037: import java.io.FileInputStream;
038: import java.io.FileNotFoundException;
039: import java.io.InputStream;
040: import java.util.List;
041:
042: public class SchemaUnwrapperExtensionTest extends TestCase {
043:
044: private AxisMessage axisMessage;
045: private AxisService axisService;
046:
047: private static final String PARAMETER_ONE = "ParameterOne";
048: private static final String PARAMETER_TWO = "ParameterTwo";
049: private static final String PARAMETER_THREE = "ParameterThree";
050: private static final String PARAMETER_FOUR = "ParameterFour";
051: private static final String ADD_OPERATION = "Add";
052:
053: protected void setUp() throws Exception {
054: AxisOperation axisOperation = new InOutAxisOperation(new QName(
055: ADD_OPERATION));
056: axisMessage = new AxisMessage();
057: axisMessage.setName("AddRequest");
058: axisMessage.setElementQName(new QName(
059: "http://ws.apache.org/schemas/axis2", "AddRequest"));
060: axisOperation.addMessage(axisMessage,
061: WSDLConstants.MESSAGE_LABEL_IN_VALUE);
062: axisMessage.setParent(axisOperation);
063:
064: axisService = new AxisService("DummyService");
065: axisService.addOperation(axisOperation);
066: axisOperation.setParent(axisService);
067: }
068:
069: /** This refers to the schema-1.xsd which has an AddRequest element which is of complex type */
070: public void testScenarioOne() {
071: String schemaLocation = "test-resources/schemas/schema-1.xsd";
072:
073: createAndWalkSchema(schemaLocation);
074:
075: assertTrue(axisMessage.getParameter(Constants.UNWRAPPED_KEY)
076: .getValue() == Boolean.TRUE);
077:
078: Parameter parameter = axisMessage
079: .getParameter(Constants.UNWRAPPED_DETAILS);
080: MessagePartInformationHolder messagePartInformationHolder = (MessagePartInformationHolder) parameter
081: .getValue();
082: List partsList = messagePartInformationHolder.getPartsList();
083:
084: assertTrue(partsList.contains(WSDLUtil.getPartQName(
085: ADD_OPERATION, WSDLConstants.INPUT_PART_QNAME_SUFFIX,
086: PARAMETER_ONE)));
087: assertTrue(partsList.contains(WSDLUtil.getPartQName(
088: ADD_OPERATION, WSDLConstants.INPUT_PART_QNAME_SUFFIX,
089: PARAMETER_TWO)));
090: assertTrue(partsList.size() == 2);
091:
092: }
093:
094: /**
095: * This refers to the schema-2.xsd which has an AddRequest element which is of AddRequestType.
096: * AddRequestType is a complex type
097: */
098: public void testScenarioTwo() {
099: String schemaLocation = "test-resources/schemas/schema-2.xsd";
100:
101: createAndWalkSchema(schemaLocation);
102:
103: assertTrue(axisMessage.getParameter(Constants.UNWRAPPED_KEY)
104: .getValue() == Boolean.TRUE);
105:
106: Parameter parameter = axisMessage
107: .getParameter(Constants.UNWRAPPED_DETAILS);
108: MessagePartInformationHolder messagePartInformationHolder = (MessagePartInformationHolder) parameter
109: .getValue();
110: List partsList = messagePartInformationHolder.getPartsList();
111:
112: assertTrue(partsList.contains(WSDLUtil.getPartQName(
113: ADD_OPERATION, WSDLConstants.INPUT_PART_QNAME_SUFFIX,
114: PARAMETER_ONE)));
115: assertTrue(partsList.contains(WSDLUtil.getPartQName(
116: ADD_OPERATION, WSDLConstants.INPUT_PART_QNAME_SUFFIX,
117: PARAMETER_TWO)));
118: assertTrue(partsList.size() == 2);
119: }
120:
121: /**
122: * 1. AddRequest is of AddRequestType 2. AddRequestType extends from AbstractParameterType 3.
123: * AbstractParameterType has primitive types only
124: */
125: public void testScenarioThree() {
126: String schemaLocation = "test-resources/schemas/schema-3.xsd";
127:
128: createAndWalkSchema(schemaLocation);
129:
130: assertTrue(axisMessage.getParameter(Constants.UNWRAPPED_KEY)
131: .getValue() == Boolean.TRUE);
132:
133: Parameter parameter = axisMessage
134: .getParameter(Constants.UNWRAPPED_DETAILS);
135: MessagePartInformationHolder messagePartInformationHolder = (MessagePartInformationHolder) parameter
136: .getValue();
137: List partsList = messagePartInformationHolder.getPartsList();
138:
139: assertTrue(partsList.contains(WSDLUtil.getPartQName(
140: ADD_OPERATION, WSDLConstants.INPUT_PART_QNAME_SUFFIX,
141: PARAMETER_ONE)));
142: assertTrue(partsList.contains(WSDLUtil.getPartQName(
143: ADD_OPERATION, WSDLConstants.INPUT_PART_QNAME_SUFFIX,
144: PARAMETER_TWO)));
145: assertTrue(partsList.size() == 2);
146: }
147:
148: /**
149: * 1. AddRequest is of AddRequestType 2. AddRequestType extends from AbstractParameterType and
150: * it AddRequestType has more stuff defined in a sequence, in addition to the extension. 3.
151: * AbstractParameterType has primitive types only
152: */
153: public void testScenarioFour() {
154: String schemaLocation = "test-resources/schemas/schema-4.xsd";
155:
156: createAndWalkSchema(schemaLocation);
157:
158: assertTrue(axisMessage.getParameter(Constants.UNWRAPPED_KEY)
159: .getValue() == Boolean.TRUE);
160:
161: Parameter parameter = axisMessage
162: .getParameter(Constants.UNWRAPPED_DETAILS);
163: MessagePartInformationHolder messagePartInformationHolder = (MessagePartInformationHolder) parameter
164: .getValue();
165: List partsList = messagePartInformationHolder.getPartsList();
166:
167: assertTrue(partsList.contains(WSDLUtil.getPartQName(
168: ADD_OPERATION, WSDLConstants.INPUT_PART_QNAME_SUFFIX,
169: PARAMETER_ONE)));
170: assertTrue(partsList.contains(WSDLUtil.getPartQName(
171: ADD_OPERATION, WSDLConstants.INPUT_PART_QNAME_SUFFIX,
172: PARAMETER_TWO)));
173: assertTrue(partsList.contains(WSDLUtil.getPartQName(
174: ADD_OPERATION, WSDLConstants.INPUT_PART_QNAME_SUFFIX,
175: PARAMETER_THREE)));
176: assertTrue(partsList.contains(WSDLUtil.getPartQName(
177: ADD_OPERATION, WSDLConstants.INPUT_PART_QNAME_SUFFIX,
178: PARAMETER_FOUR)));
179: assertTrue(partsList.size() == 4);
180: }
181:
182: private void createAndWalkSchema(String schemaLocation) {
183: try {
184: XmlSchema xmlSchema = loadSchema(schemaLocation);
185: axisService.addSchema(xmlSchema);
186: SchemaUnwrapperExtension extension = new SchemaUnwrapperExtension();
187: extension.walkSchema(axisMessage,
188: WSDLConstants.INPUT_PART_QNAME_SUFFIX);
189: } catch (FileNotFoundException e) {
190: fail(schemaLocation + " file can not be found");
191: } catch (CodeGenerationException e) {
192: fail(e.getMessage());
193: }
194: }
195:
196: private XmlSchema loadSchema(String schemaLocation)
197: throws FileNotFoundException {
198: InputStream is = new FileInputStream(schemaLocation);
199: XmlSchemaCollection schemaCol = new XmlSchemaCollection();
200: return schemaCol.read(new StreamSource(is), null);
201: }
202: }
|