annotationserviceimpldescriptiontests.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 1,126 行 · 第 1/5 页

JAVA
1,126
字号
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */


package org.apache.axis2.jaxws.description;

import junit.framework.TestCase;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.Parameter;
import org.apache.log4j.BasicConfigurator;
import org.apache.ws.axis2.tests.EchoServiceImplWithSEI;

import javax.jws.Oneway;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.Holder;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;

/**
 * Tests the creation of the Description classes based on a service implementation bean and various
 * combinations of annotations
 */
public class AnnotationServiceImplDescriptionTests extends TestCase {
    static {
        // Note you will probably need to increase the java heap size, for example
        // -Xmx512m.  This can be done by setting maven.junit.jvmargs in project.properties.
        // To change the settings, edit the log4j.property file
        // in the test-resources directory.
        BasicConfigurator.configure();
    }

    /**
     * Create the description classes with a service implementation that contains the @WebService
     * JSR-181 annotation which references an SEI.
     */
    public void testServiceImplWithSEI() {
        // Use the description factory directly; this will be done within the JAX-WS runtime
        ServiceDescription serviceDesc =
                DescriptionFactory.createServiceDescription(EchoServiceImplWithSEI.class);
        assertNotNull(serviceDesc);

        EndpointDescription[] endpointDesc = serviceDesc.getEndpointDescriptions();
        assertNotNull(endpointDesc);
        assertEquals(endpointDesc.length, 1);

        assertNotNull(endpointDesc[0].getAxisService());

        // TODO: How will the JAX-WS dispatcher get the appropriate port (i.e. endpoint)?  Currently assumes [0]
        EndpointInterfaceDescription endpointIntfDesc =
                endpointDesc[0].getEndpointInterfaceDescription();
        assertNotNull(endpointIntfDesc);
        // TODO: (JLB) Remove code
//        assertEquals(EchoPort.class, endpointIntfDesc.getSEIClass());

        OperationDescription[] operations =
                endpointIntfDesc.getOperationForJavaMethod("badMethodName");
        assertNull(operations);
        operations = endpointIntfDesc.getOperationForJavaMethod("");
        assertNull(operations);
        operations = endpointIntfDesc.getOperationForJavaMethod((String)null);
        assertNull(operations);
        operations = endpointIntfDesc.getOperationForJavaMethod("echo");
        assertNotNull(operations);
        assertEquals(operations.length, 1);
        assertEquals(operations[0].getJavaMethodName(), "echo");

        String[] paramTypes = operations[0].getJavaParameters();
        assertNotNull(paramTypes);
        assertEquals(paramTypes.length, 1);
        assertEquals("javax.xml.ws.Holder<java.lang.String>", paramTypes[0]);

        // Test RequestWrapper annotations
        assertEquals(operations[0].getRequestWrapperLocalName(), "Echo");
        assertEquals(operations[0].getRequestWrapperTargetNamespace(),
                     "http://ws.apache.org/axis2/tests");
        assertEquals(operations[0].getRequestWrapperClassName(), "org.apache.ws.axis2.tests.Echo");

        // Test ResponseWrapper annotations
        assertEquals(operations[0].getResponseWrapperLocalName(), "EchoResponse");
        assertEquals(operations[0].getResponseWrapperTargetNamespace(),
                     "http://ws.apache.org/axis2/tests");
        assertEquals(operations[0].getResponseWrapperClassName(),
                     "org.apache.ws.axis2.tests.EchoResponse");

        // Test SOAPBinding default; that annotation is not present in the SEI
        // Note that annotation could occur on the operation or the type
        // (although on this SEI it doesn't occur either place).
        assertEquals(SOAPBinding.Style.DOCUMENT, operations[0].getSoapBindingStyle());
        assertEquals(SOAPBinding.Style.DOCUMENT, endpointIntfDesc.getSoapBindingStyle());

    }

    public void testAxisServiceBackpointer() {
        // Test that the AxisService points back to the EndpointDesc
        // TODO: Temporary: Create an AxisService to pass in using WSDL
        // TODO: Eventually remove AxisService paramater from the factory; AxisService should be created (using annotations/wsdl/wsm etc)

        // Creating the AxisService this way is temporary; it should be created as part of creating the EndpointDescription from the
        // Service Impl.  For now, though, create a service-request-based ServiceDesc using WSDL.  Then specificy that AxisService
        // on the creation of the ServiceDesc from the service impl.  Verify that the AxisService points to the ServiceDesc.

        AxisService axisService = new AxisService();
        ServiceDescription serviceDesc =
                DescriptionFactory.createServiceDescriptionFromServiceImpl(
                        EchoServiceImplWithSEI.class, axisService);
        EndpointDescription endpointDesc = serviceDesc.getEndpointDescriptions()[0];
        assertNotNull(serviceDesc);
        Parameter endpointDescParam =
                axisService.getParameter(EndpointDescription.AXIS_SERVICE_PARAMETER);
        assertNotNull(endpointDescParam);
        assertEquals(endpointDesc, endpointDescParam.getValue());

    }

    public void testOverloadedServiceImplWithSEI() {
        ServiceDescription serviceDesc =
                DescriptionFactory.createServiceDescription(DocLitWrappedImplWithSEI.class);
        assertNotNull(serviceDesc);

        EndpointDescription[] endpointDesc = serviceDesc.getEndpointDescriptions();
        assertNotNull(endpointDesc);
        assertEquals(endpointDesc.length, 1);
        // TODO: Using hardcoded endpointDesc[0] from ServiceDesc
        EndpointInterfaceDescription endpointIntfDesc =
                endpointDesc[0].getEndpointInterfaceDescription();
        assertNotNull(endpointIntfDesc);
        // TODO: (JLB) Remove code
//        assertEquals(endpointIntfDesc.getSEIClass(), DocLitWrappedProxy.class);

        // Test for overloaded methods
        // SEI defines two Java methods with this name
        OperationDescription[] operations =
                endpointIntfDesc.getOperationForJavaMethod("invokeAsync");
        assertNotNull(operations);
        assertEquals(operations.length, 2);
        assertEquals(operations[0].getJavaMethodName(), "invokeAsync");
        assertEquals(operations[1].getJavaMethodName(), "invokeAsync");

        // Check the Java parameters, WebParam names, and WebResult (actually lack thereof) for each of these operations

        // Note regarding WebParam names:
        //In the client Async Call the the WebParam name will not remove JAX-WS AsyncHandler.
        //Proxy invoking the the Async Call will check the input method object and if
        //its of type JAX-WS AsyncHandler then that WebParam will be skipped.
        //This is done because AsyncHandler is NOT part of the contract, and thus it is NOT part of
        // the JAXB object constructed for the method invocation.  The AsyncHandler is part of the 
        // JAX-WS programming model to support an asynchronous callback to receive the response.

        // Note regarding WebResult annotation:
        // The async methods on this SEI do not carry a WebResult annotations.
        boolean twoArgSignatureChecked = false;
        boolean oneArgSignatureChecked = false;
        for (OperationDescription operation : operations) {
            String[] checkParams = operation.getJavaParameters();
            String[] webParamNames = operation.getParamNames();
            if (checkParams.length == 1) {
                // Check the one arguement signature
                if (oneArgSignatureChecked) {
                    fail("One Arg signature occured more than once");
                } else {
                    oneArgSignatureChecked = true;
                    // Check the Java parameter
                    assertEquals(checkParams[0], "java.lang.String");
                    // Check the WebParam Names (see note above) 
                    assertEquals(1, webParamNames.length);
                    assertEquals("invoke_str", webParamNames[0]);
                    // Check the lack of a WebResult annotation and the default values for
                    // SOAPBinding Style=DOCUMENT, Use=LITERAL, ParamStyle=WRAPPED
                    // Note that the SOAPBinding annotation is also not present and thus fully defaulted.
                    assertNull(((OperationDescriptionJava)operation).getAnnoWebResult());
                    assertEquals("return", operation.getResultName());
                    assertEquals("return", operation.getResultPartName());
                    assertEquals("", operation.getResultTargetNamespace());
                    assertFalse(operation.isResultHeader());
                }
            } else if (checkParams.length == 2) {
                // Check the two arguement signature
                if (twoArgSignatureChecked) {
                    fail("Two Arg signature occured more than once");
                } else {
                    twoArgSignatureChecked = true;
                    // Check the Java parameter
                    assertEquals(checkParams[0], "java.lang.String");
                    assertEquals(checkParams[1],
                                 "javax.xml.ws.AsyncHandler<org.test.proxy.doclitwrapped.ReturnType>");
                    // Check the WebParam Names (see note above) 
                    assertEquals(2, webParamNames.length);
                    assertEquals("invoke_str", webParamNames[0]);
                    // Check the lack of a WebResult annotation and the default values for
                    // SOAPBinding Style=DOCUMENT, Use=LITERAL, ParamStyle=WRAPPED
                    // Note that the SOAPBinding annotation is also not present and thus fully defaulted.
                    assertNull(((OperationDescriptionJava)operation).getAnnoWebResult());
                    assertEquals("return", operation.getResultName());
                    assertEquals("return", operation.getResultPartName());
                    assertEquals("", operation.getResultTargetNamespace());
                    assertFalse(operation.isResultHeader());
                }
            } else {
                fail("Wrong number of parameters returned");
            }
        }

        // Test for a method with parameters of primitive types.  Note
        // this method IS overloaded
        operations = endpointIntfDesc.getOperationForJavaMethod("twoWayHolderAsync");
        assertNotNull(operations);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?