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.systest.schema_validation;
019:
020: import java.io.Serializable;
021: import java.net.URL;
022: import java.util.List;
023:
024: import javax.xml.namespace.QName;
025: import javax.xml.ws.WebServiceException;
026:
027: import org.apache.cxf.interceptor.Fault;
028: import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
029: import org.apache.schema_validation.SchemaValidation;
030: import org.apache.schema_validation.SchemaValidationService;
031: import org.apache.schema_validation.types.ComplexStruct;
032: import org.apache.schema_validation.types.OccuringStruct;
033: import org.junit.BeforeClass;
034: import org.junit.Test;
035:
036: public class ValidationClientServerTest extends
037: AbstractBusClientServerTestBase {
038:
039: private final QName serviceName = new QName(
040: "http://apache.org/schema_validation",
041: "SchemaValidationService");
042: private final QName portName = new QName(
043: "http://apache.org/schema_validation", "SoapPort");
044:
045: @BeforeClass
046: public static void startservers() throws Exception {
047: // set up configuration to enable schema validation
048: URL url = ValidationClientServerTest.class
049: .getResource("cxf-config.xml");
050: assertNotNull("cannot find test resource", url);
051: defaultConfigFileName = url.toString();
052:
053: assertTrue("server did not launch correctly",
054: launchServer(ValidationServer.class));
055: }
056:
057: // TODO : Change this test so that we test the combinations of
058: // client and server with schema validation enabled/disabled...
059: // Only tests client side validation enabled/server side disabled.
060: @Test
061: public void testSchemaValidation() throws Exception {
062: System.setProperty("cxf.config.file.url", getClass()
063: .getResource("cxf-config.xml").toString());
064: URL wsdl = getClass().getResource(
065: "/wsdl/schema_validation.wsdl");
066: assertNotNull(wsdl);
067:
068: SchemaValidationService service = new SchemaValidationService(
069: wsdl, serviceName);
070: assertNotNull(service);
071:
072: SchemaValidation validation = service.getPort(portName,
073: SchemaValidation.class);
074:
075: ComplexStruct complexStruct = new ComplexStruct();
076: complexStruct.setElem1("one");
077: // Don't initialize a member of the structure.
078: // Client side validation should throw an exception.
079: // complexStruct.setElem2("two");
080: complexStruct.setElem3(3);
081: try {
082: /*boolean result =*/
083: validation.setComplexStruct(complexStruct);
084: fail("Set ComplexStruct hould have thrown ProtocolException");
085: } catch (WebServiceException e) {
086: assertTrue(e.getCause() instanceof Fault);
087: String expected = "'{\"http://apache.org/schema_validation/types\":elem2}' is expected.";
088: assertTrue(e.getCause().getMessage().indexOf(expected) != -1);
089: }
090:
091: OccuringStruct occuringStruct = new OccuringStruct();
092: // Populate the list in the wrong order.
093: // Client side validation should throw an exception.
094: List<Serializable> floatIntStringList = occuringStruct
095: .getVarFloatAndVarIntAndVarString();
096: floatIntStringList.add(new Integer(42));
097: floatIntStringList.add(new Float(4.2f));
098: floatIntStringList.add("Goofus and Gallant");
099: try {
100: /*boolean result =*/
101: validation.setOccuringStruct(occuringStruct);
102: fail("Set OccuringStruct hould have thrown ProtocolException");
103: } catch (WebServiceException e) {
104: assertTrue(e.getCause() instanceof Fault);
105: String expected = "'{\"http://apache.org/schema_validation/types\":varFloat}' is expected.";
106: assertTrue(e.getCause().getMessage().indexOf(expected) != -1);
107: }
108:
109: try {
110: // The server will attempt to return an invalid ComplexStruct
111: // When validation is disabled on the server side, we'll get the
112: // exception while unmarshalling the invalid response.
113: /*complexStruct =*/
114: validation.getComplexStruct("Hello");
115: fail("Get ComplexStruct should have thrown ProtocolException");
116: } catch (WebServiceException e) {
117: assertTrue(e.getCause() instanceof Fault);
118: String expected = "'{\"http://apache.org/schema_validation/types\":elem2}' is expected.";
119: assertTrue("Found message " + e.getCause().getMessage(), e
120: .getCause().getMessage().indexOf(expected) != -1);
121: }
122:
123: try {
124: // The server will attempt to return an invalid OccuringStruct
125: // When validation is disabled on the server side, we'll get the
126: // exception while unmarshalling the invalid response.
127: /*occuringStruct =*/
128: validation.getOccuringStruct("World");
129: fail("Get OccuringStruct should have thrown ProtocolException");
130: } catch (WebServiceException e) {
131: assertTrue(e.getCause() instanceof Fault);
132: String expected = "'{\"http://apache.org/schema_validation/types\":varFloat}' is expected.";
133: assertTrue(e.getCause().getMessage().indexOf(expected) != -1);
134: }
135: }
136:
137: }
|