Search in sources :

Example 1 with BeanClassLoaderAware

use of org.springframework.beans.factory.BeanClassLoaderAware in project spring-framework by spring-projects.

the class HttpInvokerTests method doTestHttpInvokerProxyFactoryBeanAndServiceExporter.

private void doTestHttpInvokerProxyFactoryBeanAndServiceExporter(boolean explicitClassLoader) throws Throwable {
    TestBean target = new TestBean("myname", 99);
    final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
    exporter.setServiceInterface(ITestBean.class);
    exporter.setService(target);
    exporter.afterPropertiesSet();
    HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
    pfb.setServiceInterface(ITestBean.class);
    pfb.setServiceUrl("http://myurl");
    pfb.setHttpInvokerRequestExecutor(new AbstractHttpInvokerRequestExecutor() {

        @Override
        protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config, ByteArrayOutputStream baos) throws Exception {
            assertEquals("http://myurl", config.getServiceUrl());
            MockHttpServletRequest request = new MockHttpServletRequest();
            MockHttpServletResponse response = new MockHttpServletResponse();
            request.setContent(baos.toByteArray());
            exporter.handleRequest(request, response);
            return readRemoteInvocationResult(new ByteArrayInputStream(response.getContentAsByteArray()), config.getCodebaseUrl());
        }
    });
    if (explicitClassLoader) {
        ((BeanClassLoaderAware) pfb.getHttpInvokerRequestExecutor()).setBeanClassLoader(getClass().getClassLoader());
    }
    pfb.afterPropertiesSet();
    ITestBean proxy = (ITestBean) pfb.getObject();
    assertEquals("myname", proxy.getName());
    assertEquals(99, proxy.getAge());
    proxy.setAge(50);
    assertEquals(50, proxy.getAge());
    proxy.setStringArray(new String[] { "str1", "str2" });
    assertTrue(Arrays.equals(new String[] { "str1", "str2" }, proxy.getStringArray()));
    proxy.setSomeIntegerArray(new Integer[] { 1, 2, 3 });
    assertTrue(Arrays.equals(new Integer[] { 1, 2, 3 }, proxy.getSomeIntegerArray()));
    proxy.setNestedIntegerArray(new Integer[][] { { 1, 2, 3 }, { 4, 5, 6 } });
    Integer[][] integerArray = proxy.getNestedIntegerArray();
    assertTrue(Arrays.equals(new Integer[] { 1, 2, 3 }, integerArray[0]));
    assertTrue(Arrays.equals(new Integer[] { 4, 5, 6 }, integerArray[1]));
    proxy.setSomeIntArray(new int[] { 1, 2, 3 });
    assertTrue(Arrays.equals(new int[] { 1, 2, 3 }, proxy.getSomeIntArray()));
    proxy.setNestedIntArray(new int[][] { { 1, 2, 3 }, { 4, 5, 6 } });
    int[][] intArray = proxy.getNestedIntArray();
    assertTrue(Arrays.equals(new int[] { 1, 2, 3 }, intArray[0]));
    assertTrue(Arrays.equals(new int[] { 4, 5, 6 }, intArray[1]));
    try {
        proxy.exceptional(new IllegalStateException());
        fail("Should have thrown IllegalStateException");
    } catch (IllegalStateException ex) {
    // expected
    }
    try {
        proxy.exceptional(new IllegalAccessException());
        fail("Should have thrown IllegalAccessException");
    } catch (IllegalAccessException ex) {
    // expected
    }
}
Also used : RemoteInvocationResult(org.springframework.remoting.support.RemoteInvocationResult) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletException(javax.servlet.ServletException) RemoteAccessException(org.springframework.remoting.RemoteAccessException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ITestBean(org.springframework.tests.sample.beans.ITestBean) BeanClassLoaderAware(org.springframework.beans.factory.BeanClassLoaderAware) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ByteArrayInputStream(java.io.ByteArrayInputStream) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ServletException (javax.servlet.ServletException)1 BeanClassLoaderAware (org.springframework.beans.factory.BeanClassLoaderAware)1 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)1 RemoteAccessException (org.springframework.remoting.RemoteAccessException)1 RemoteInvocationResult (org.springframework.remoting.support.RemoteInvocationResult)1 ITestBean (org.springframework.tests.sample.beans.ITestBean)1 TestBean (org.springframework.tests.sample.beans.TestBean)1