Package org.springframework.tests.sample.beans

Examples of org.springframework.tests.sample.beans.Person


    pf.addAdvice(new SerializableNopInterceptor());
    // Try various advice types
    pf.addAdvice(new CountingBeforeAdvice());
    pf.addAdvice(new CountingAfterReturningAdvice());
    pf.addAdvice(cta);
    Person p = (Person) createAopProxy(pf).getProxy();

    p.echo(null);
    assertEquals(0, cta.getCalls());
    try {
      p.echo(new IOException());
    }
    catch (IOException ex) {

    }
    assertEquals(1, cta.getCalls());

    // Will throw exception if it fails
    Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(p);
    assertNotSame(p, p2);
    assertEquals(p.getName(), p2.getName());
    assertEquals(p.getAge(), p2.getAge());
    assertTrue("Deserialized object is an AOP proxy", AopUtils.isAopProxy(p2));

    Advised a1 = (Advised) p;
    Advised a2 = (Advised) p2;
    // Check we can manipulate state of p2
    assertEquals(a1.getAdvisors().length, a2.getAdvisors().length);

    // This should work as SerializablePerson is equal
    assertEquals("Proxies should be equal, even after one was serialized", p, p2);
    assertEquals("Proxies should be equal, even after one was serialized", p2, p);

    // Check we can add a new advisor to the target
    NopInterceptor ni = new NopInterceptor();
    p2.getAge();
    assertEquals(0, ni.getCount());
    a2.addAdvice(ni);
    p2.getAge();
    assertEquals(1, ni.getCount());

    cta = (CountingThrowsAdvice) a2.getAdvisors()[3].getAdvice();
    p2.echo(null);
    assertEquals(1, cta.getCalls());
    try {
      p2.echo(new IOException());
    }
    catch (IOException ex) {

    }
    assertEquals(2, cta.getCalls());
View Full Code Here


  }


  @Test
  public void testProxySerializableWithoutConfigMixin() throws Exception {
    Person pooled = (Person) beanFactory.getBean("pooledPerson");

    //System.out.println(((Advised) pooled).toProxyConfigString());
    assertTrue(((Advised) pooled).getTargetSource() instanceof CommonsPoolTargetSource);

    //((Advised) pooled).setTargetSource(new SingletonTargetSource(new SerializablePerson()));
    Person serialized = (Person) SerializationTestUtils.serializeAndDeserialize(pooled);
    assertTrue(((Advised) serialized).getTargetSource() instanceof SingletonTargetSource);
    serialized.setAge(25);
    assertEquals(25, serialized.getAge());
  }
View Full Code Here

  @Test
  public void testSerializableSingletonProxy() throws Exception {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS));
    Person p = (Person) bf.getBean("serializableSingleton");
    assertSame("Should be a Singleton", p, bf.getBean("serializableSingleton"));
    Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(p);
    assertEquals(p, p2);
    assertNotSame(p, p2);
    assertEquals("serializableSingleton", p2.getName());

    // Add unserializable advice
    Advice nop = new NopInterceptor();
    ((Advised) p).addAdvice(nop);
    // Check it still works
    assertEquals(p2.getName(), p2.getName());
    assertFalse("Not serializable because an interceptor isn't serializable", SerializationTestUtils.isSerializable(p));

    // Remove offending interceptor...
    assertTrue(((Advised) p).removeAdvice(nop));
    assertTrue("Serializable again because offending interceptor was removed", SerializationTestUtils.isSerializable(p));
View Full Code Here

  @Test
  public void testSerializablePrototypeProxy() throws Exception {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS));
    Person p = (Person) bf.getBean("serializablePrototype");
    assertNotSame("Should not be a Singleton", p, bf.getBean("serializablePrototype"));
    Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(p);
    assertEquals(p, p2);
    assertNotSame(p, p2);
    assertEquals("serializablePrototype", p2.getName());
  }
View Full Code Here

  @Test
  public void testSerializableSingletonProxyFactoryBean() throws Exception {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS));
    Person p = (Person) bf.getBean("serializableSingleton");
    ProxyFactoryBean pfb = (ProxyFactoryBean) bf.getBean("&serializableSingleton");
    ProxyFactoryBean pfb2 = (ProxyFactoryBean) SerializationTestUtils.serializeAndDeserialize(pfb);
    Person p2 = (Person) pfb2.getObject();
    assertEquals(p, p2);
    assertNotSame(p, p2);
    assertEquals("serializableSingleton", p2.getName());
  }
View Full Code Here

  @Test
  public void testProxyNotSerializableBecauseOfAdvice() throws Exception {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS));
    Person p = (Person) bf.getBean("interceptorNotSerializableSingleton");
    assertFalse("Not serializable because an interceptor isn't serializable", SerializationTestUtils.isSerializable(p));
  }
View Full Code Here

    TimeStamped ts = new SerializableTimeStamped(time);

    factory.addAdvisor(new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts)));
    factory.addAdvice(new SerializableNopInterceptor());

    Person p = (Person) factory.getProxy();

    assertEquals(name, p.getName());
    assertEquals(time, ((TimeStamped) p).getTimeStamp());

    Person p1 = (Person) SerializationTestUtils.serializeAndDeserialize(p);
    assertEquals(name, p1.getName());
    assertEquals(time, ((TimeStamped) p1).getTimeStamp());
  }
View Full Code Here

    HotSwappableTargetSource hts = new HotSwappableTargetSource(sp1);
    ProxyFactory pf = new ProxyFactory();
    pf.addInterface(Person.class);
    pf.setTargetSource(hts);
    pf.addAdvisor(new DefaultPointcutAdvisor(new SerializableNopInterceptor()));
    Person p = (Person) pf.getProxy();

    assertEquals(sp1.getName(), p.getName());
    hts.swap(sp2);
    assertEquals(sp2.getName(), p.getName());

    p = (Person) SerializationTestUtils.serializeAndDeserialize(p);
    // We need to get a reference to the client-side targetsource
    hts = (HotSwappableTargetSource) ((Advised) p).getTargetSource();
    assertEquals(sp2.getName(), p.getName());
    hts.swap(sp1);
    assertEquals(sp1.getName(), p.getName());

  }
View Full Code Here

  @Test
  public void testSerializable() throws Throwable {
    testSets();
    // Count is now 2
    Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(proxied);
    NopInterceptor nop2 = (NopInterceptor) ((Advised) p2).getAdvisors()[0].getAdvice();
    p2.getName();
    assertEquals(2, nop2.getCount());
    p2.echo(null);
    assertEquals(3, nop2.getCount());
  }
View Full Code Here

  @Test
  public void testSerialization() throws Throwable {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT);
    // This is a CGLIB proxy, so we can proxy it to the target class
    Person p = (Person) bf.getBean("serializableSettersAdvised");
    // Interceptor behind regexp advisor
    NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
    assertEquals(0, nop.getCount());

    int newAge = 12;
    // Not advised
    assertEquals(0, p.getAge());
    assertEquals(0, nop.getCount());

    // This is proxied
    p.setAge(newAge);
    assertEquals(1, nop.getCount());
    p.setAge(newAge);
    assertEquals(newAge, p.getAge());
    // Only setter fired
    assertEquals(2, nop.getCount());

    // Serialize and continue...
    p = (Person) SerializationTestUtils.serializeAndDeserialize(p);
    assertEquals(newAge, p.getAge());
    // Remembers count, but we need to get a new reference to nop...
    nop = (SerializableNopInterceptor) ((Advised) p).getAdvisors()[0].getAdvice();
    assertEquals(2, nop.getCount());
    assertEquals("serializableSettersAdvised", p.getName());
    p.setAge(newAge + 1);
    assertEquals(3, nop.getCount());
    assertEquals(newAge + 1, p.getAge());
  }
View Full Code Here

TOP

Related Classes of org.springframework.tests.sample.beans.Person

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.