Search in sources :

Example 56 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class CauchoRemotingTests method hessianProxyFactoryBeanWithAuthenticationAndAccessError.

@Test
public void hessianProxyFactoryBeanWithAuthenticationAndAccessError() throws Exception {
    HessianProxyFactoryBean factory = new HessianProxyFactoryBean();
    factory.setServiceInterface(ITestBean.class);
    factory.setServiceUrl("http://localhosta/testbean");
    factory.setUsername("test");
    factory.setPassword("bean");
    factory.setOverloadEnabled(true);
    factory.afterPropertiesSet();
    assertTrue("Correct singleton value", factory.isSingleton());
    assertTrue(factory.getObject() instanceof ITestBean);
    ITestBean bean = (ITestBean) factory.getObject();
    exception.expect(RemoteAccessException.class);
    bean.setName("test");
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) Test(org.junit.Test)

Example 57 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class CauchoRemotingTests method hessianProxyFactoryBeanWithCustomProxyFactory.

@Test
public void hessianProxyFactoryBeanWithCustomProxyFactory() throws Exception {
    TestHessianProxyFactory proxyFactory = new TestHessianProxyFactory();
    HessianProxyFactoryBean factory = new HessianProxyFactoryBean();
    factory.setServiceInterface(ITestBean.class);
    factory.setServiceUrl("http://localhosta/testbean");
    factory.setProxyFactory(proxyFactory);
    factory.setUsername("test");
    factory.setPassword("bean");
    factory.setOverloadEnabled(true);
    factory.afterPropertiesSet();
    assertTrue("Correct singleton value", factory.isSingleton());
    assertTrue(factory.getObject() instanceof ITestBean);
    ITestBean bean = (ITestBean) factory.getObject();
    assertEquals(proxyFactory.user, "test");
    assertEquals(proxyFactory.password, "bean");
    assertTrue(proxyFactory.overloadEnabled);
    exception.expect(RemoteAccessException.class);
    bean.setName("test");
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) Test(org.junit.Test)

Example 58 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class CauchoRemotingTests method simpleHessianServiceExporter.

@Test
public void simpleHessianServiceExporter() throws IOException {
    final int port = SocketUtils.findAvailableTcpPort();
    TestBean tb = new TestBean("tb");
    SimpleHessianServiceExporter exporter = new SimpleHessianServiceExporter();
    exporter.setService(tb);
    exporter.setServiceInterface(ITestBean.class);
    exporter.setDebug(true);
    exporter.prepare();
    HttpServer server = HttpServer.create(new InetSocketAddress(port), -1);
    server.createContext("/hessian", exporter);
    server.start();
    try {
        HessianClientInterceptor client = new HessianClientInterceptor();
        client.setServiceUrl("http://localhost:" + port + "/hessian");
        client.setServiceInterface(ITestBean.class);
        //client.setHessian2(true);
        client.prepare();
        ITestBean proxy = ProxyFactory.getProxy(ITestBean.class, client);
        assertEquals("tb", proxy.getName());
        proxy.setName("test");
        assertEquals("test", proxy.getName());
    } finally {
        server.stop(Integer.MAX_VALUE);
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) InetSocketAddress(java.net.InetSocketAddress) HttpServer(com.sun.net.httpserver.HttpServer) Test(org.junit.Test)

Example 59 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class HttpInvokerTests method httpInvokerProxyFactoryBeanAndServiceExporterWithWrappedInvocations.

@Test
public void httpInvokerProxyFactoryBeanAndServiceExporterWithWrappedInvocations() throws Throwable {
    TestBean target = new TestBean("myname", 99);
    final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter() {

        @Override
        protected RemoteInvocation doReadRemoteInvocation(ObjectInputStream ois) throws IOException, ClassNotFoundException {
            Object obj = ois.readObject();
            if (!(obj instanceof TestRemoteInvocationWrapper)) {
                throw new IOException("Deserialized object needs to be assignable to type [" + TestRemoteInvocationWrapper.class.getName() + "]: " + obj);
            }
            return ((TestRemoteInvocationWrapper) obj).remoteInvocation;
        }

        @Override
        protected void doWriteRemoteInvocationResult(RemoteInvocationResult result, ObjectOutputStream oos) throws IOException {
            oos.writeObject(new TestRemoteInvocationResultWrapper(result));
        }
    };
    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());
        }

        @Override
        protected void doWriteRemoteInvocation(RemoteInvocation invocation, ObjectOutputStream oos) throws IOException {
            oos.writeObject(new TestRemoteInvocationWrapper(invocation));
        }

        @Override
        protected RemoteInvocationResult doReadRemoteInvocationResult(ObjectInputStream ois) throws IOException, ClassNotFoundException {
            Object obj = ois.readObject();
            if (!(obj instanceof TestRemoteInvocationResultWrapper)) {
                throw new IOException("Deserialized object needs to be assignable to type [" + TestRemoteInvocationResultWrapper.class.getName() + "]: " + obj);
            }
            return ((TestRemoteInvocationResultWrapper) obj).remoteInvocationResult;
        }
    });
    pfb.afterPropertiesSet();
    ITestBean proxy = (ITestBean) pfb.getObject();
    assertEquals("myname", proxy.getName());
    assertEquals(99, proxy.getAge());
    proxy.setAge(50);
    assertEquals(50, proxy.getAge());
    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 : RemoteInvocation(org.springframework.remoting.support.RemoteInvocation) RemoteInvocationResult(org.springframework.remoting.support.RemoteInvocationResult) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) 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) 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) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 60 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class HttpInvokerTests method httpInvokerProxyFactoryBeanAndServiceExporterWithCustomInvocationObject.

@Test
public void httpInvokerProxyFactoryBeanAndServiceExporterWithCustomInvocationObject() throws Exception {
    TestBean target = new TestBean("myname", 99);
    final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
    exporter.setServiceInterface(ITestBean.class);
    exporter.setService(target);
    exporter.setRemoteInvocationExecutor(new DefaultRemoteInvocationExecutor() {

        @Override
        public Object invoke(RemoteInvocation invocation, Object targetObject) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
            assertTrue(invocation instanceof TestRemoteInvocation);
            assertNull(invocation.getAttributes());
            assertNull(invocation.getAttribute("myKey"));
            return super.invoke(invocation, targetObject);
        }
    });
    exporter.afterPropertiesSet();
    HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
    pfb.setServiceInterface(ITestBean.class);
    pfb.setServiceUrl("http://myurl");
    pfb.setRemoteInvocationFactory(new RemoteInvocationFactory() {

        @Override
        public RemoteInvocation createRemoteInvocation(MethodInvocation methodInvocation) {
            RemoteInvocation invocation = new TestRemoteInvocation(methodInvocation);
            assertNull(invocation.getAttributes());
            assertNull(invocation.getAttribute("myKey"));
            return invocation;
        }
    });
    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());
        }
    });
    pfb.afterPropertiesSet();
    ITestBean proxy = (ITestBean) pfb.getObject();
    assertEquals("myname", proxy.getName());
    assertEquals(99, proxy.getAge());
}
Also used : RemoteInvocation(org.springframework.remoting.support.RemoteInvocation) DefaultRemoteInvocationExecutor(org.springframework.remoting.support.DefaultRemoteInvocationExecutor) RemoteInvocationResult(org.springframework.remoting.support.RemoteInvocationResult) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MethodInvocation(org.aopalliance.intercept.MethodInvocation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InvocationTargetException(java.lang.reflect.InvocationTargetException) 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) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ByteArrayInputStream(java.io.ByteArrayInputStream) RemoteInvocationFactory(org.springframework.remoting.support.RemoteInvocationFactory) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

ITestBean (org.springframework.tests.sample.beans.ITestBean)221 Test (org.junit.Test)205 TestBean (org.springframework.tests.sample.beans.TestBean)127 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)37 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)29 SerializableNopInterceptor (org.springframework.tests.aop.interceptor.SerializableNopInterceptor)24 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)21 IOException (java.io.IOException)15 Advisor (org.springframework.aop.Advisor)15 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)15 Method (java.lang.reflect.Method)14 ProxyFactory (org.springframework.aop.framework.ProxyFactory)14 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)14 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)13 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)13 MethodInvocation (org.aopalliance.intercept.MethodInvocation)12 Advised (org.springframework.aop.framework.Advised)12 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)11 LockedException (test.mixin.LockedException)11 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)10