Exemplo n.º 1
0
  @Test(
      description =
          "A simple test to check session replication, doesn't carefully check if a bean ids are correct")
  public void testSimpleSessionReplication() throws Exception {

    TestContainer container1 = bootstrapContainer(1, Collections.singletonList(Foo.class));
    BeanManagerImpl beanManager1 = getBeanManager(container1);
    Bean<?> fooBean1 = beanManager1.resolve(beanManager1.getBeans(Foo.class));

    TestContainer container2 = bootstrapContainer(2, Collections.singletonList(Foo.class));
    BeanManagerImpl beanManager2 = getBeanManager(container2);
    Bean<?> fooBean2 = beanManager2.resolve(beanManager2.getBeans(Foo.class));

    use(1);
    // Set a value into Foo1
    Foo foo1 =
        (Foo)
            beanManager1.getReference(
                fooBean1, Foo.class, beanManager1.createCreationalContext(fooBean1));
    foo1.setName("container 1");

    replicateSession(1, container1, 2, container2);

    use(2);
    Foo foo2 =
        (Foo)
            beanManager2.getReference(
                fooBean2, Foo.class, beanManager2.createCreationalContext(fooBean2));
    assert foo2.getName().equals("container 1");
    use(2);
    container2.stopContainer();
    use(1);
    container1.stopContainer();
  }
  @Test
  public void test() {

    // Bootstrap container 1
    SwitchableSingletonProvider.use(1);

    TestContainer container1 = new TestContainer(Foo.class);
    container1.startContainer();
    container1.ensureRequestActive();

    BeanManagerImpl beanManager1 = getBeanManager(container1);
    Bean<?> fooBean1 = beanManager1.resolve(beanManager1.getBeans(Foo.class));
    Foo foo1 =
        (Foo)
            beanManager1.getReference(
                fooBean1, Foo.class, beanManager1.createCreationalContext(fooBean1));
    foo1.setName("container 1");

    // Bootstrap container 2
    SwitchableSingletonProvider.use(2);

    TestContainer container2 = new TestContainer(Foo.class);
    container2.startContainer();
    container2.ensureRequestActive();

    BeanManagerImpl beanManager2 = getBeanManager(container2);
    Bean<?> fooBean2 = beanManager2.resolve(beanManager2.getBeans(Foo.class));
    Foo foo2 =
        (Foo)
            beanManager2.getReference(
                fooBean2, Foo.class, beanManager2.createCreationalContext(fooBean2));
    foo2.setName("container 2");

    // Switch to container 1 and check value
    SwitchableSingletonProvider.use(1);
    foo1 =
        (Foo)
            beanManager1.getReference(
                fooBean1, Foo.class, beanManager1.createCreationalContext(fooBean1));
    assert foo1.getName().equals("container 1");

    // Switch to container 2 and check value
    SwitchableSingletonProvider.use(2);
    foo2 =
        (Foo)
            beanManager2.getReference(
                fooBean2, Foo.class, beanManager2.createCreationalContext(fooBean2));
    assert foo2.getName().equals("container 2");
    SwitchableSingletonProvider.use(1);
    container1.stopContainer();
    SwitchableSingletonProvider.use(2);
    container2.stopContainer();
    SingletonProvider.reset();
  }
Exemplo n.º 3
0
  @Test
  public void testMultipleDependentObjectsSessionReplication() throws Exception {
    Collection<Class<?>> classes = Arrays.<Class<?>>asList(Stable.class, Horse.class, Fodder.class);
    TestContainer container1 = bootstrapContainer(1, classes);
    BeanManagerImpl beanManager1 = getBeanManager(container1);
    Bean<?> stableBean1 = beanManager1.resolve(beanManager1.getBeans(Stable.class));

    TestContainer container2 = bootstrapContainer(2, classes);
    BeanManagerImpl beanManager2 = getBeanManager(container2);
    Bean<?> stableBean2 = beanManager2.resolve(beanManager2.getBeans(Stable.class));

    use(1);
    // Set a value into Foo1
    Stable stable1 =
        (Stable)
            beanManager1.getReference(
                stableBean1, Stable.class, beanManager1.createCreationalContext(stableBean1));
    stable1.getFodder().setAmount(10);
    stable1.getHorse().setName("George");

    replicateSession(1, container1, 2, container2);

    use(2);

    Stable stable2 =
        (Stable)
            beanManager2.getReference(
                stableBean2, Stable.class, beanManager2.createCreationalContext(stableBean2));
    assert stable2.getFodder().getAmount() == stable1.getFodder().getAmount();
    assert stable2.getHorse().getName() == null;

    use(1);
    assert stable1.getFodder().getAmount() == 10;
    assert stable1.getHorse().getName().equals("George");

    use(2);

    stable2.getFodder().setAmount(11);

    replicateSession(2, container2, 1, container1);

    use(1);

    int i = stable1.getFodder().getAmount();

    assert stable1.getFodder().getAmount() == 11;
    use(1);
    container1.stopContainer();
    use(2);
    container2.stopContainer();
  }
 public T createFor(InterceptorReference<Object> interceptorReference) {
   if (interceptorReference.getInterceptor() instanceof ClassMetadata<?>) {
     try {
       // this is not a managed instance - assume no-argument constructor exists
       Class<T> clazz = cast(interceptorReference.getClassMetadata().getJavaClass());
       Constructor<T> constructor = SecureReflections.getDeclaredConstructor(clazz);
       T interceptorInstance = SecureReflections.ensureAccessible(constructor).newInstance();
       // inject
       AnnotatedType<T> type = manager.createAnnotatedType(clazz);
       InjectionTarget<T> target = manager.createInjectionTarget(type);
       target.inject(interceptorInstance, creationalContext);
       return interceptorInstance;
     } catch (Exception e) {
       throw new DeploymentException(e);
     }
   }
   if (interceptorReference.getInterceptor() instanceof SerializableContextual) {
     try {
       // this is not a managed instance - assume no-argument constructor exists
       SerializableContextual<Interceptor<T>, T> serializableContextual =
           cast(interceptorReference.getInterceptor());
       return Reflections.<T>cast(
           manager.getReference(serializableContextual.get(), creationalContext, false));
     } catch (Exception e) {
       throw new DeploymentException(e);
     }
   }
   throw new IllegalStateException();
 }
Exemplo n.º 5
0
  @Test(expectedExceptions = IllegalStateException.class)
  public void testVariableBeanDeploymentStructure() throws Exception {
    Collection<Class<?>> classes1 =
        Arrays.<Class<?>>asList(Stable.class, Horse.class, Fodder.class);
    Collection<Class<?>> classes2 =
        Arrays.<Class<?>>asList(Stable.class, Horse.class, Fodder.class, Foo.class);
    TestContainer container1 = bootstrapContainer(1, classes1);
    BeanManagerImpl beanManager1 = getBeanManager(container1);
    Bean<?> stableBean1 = beanManager1.resolve(beanManager1.getBeans(Stable.class));
    TestContainer container2 = bootstrapContainer(2, classes2);

    use(1);
    // Set a value into Foo1
    Stable stable1 =
        (Stable)
            beanManager1.getReference(
                stableBean1, Stable.class, beanManager1.createCreationalContext(stableBean1));
    stable1.getFodder().setAmount(10);
    stable1.getHorse().setName("George");

    try {
      replicateSession(1, container1, 2, container2);
    } finally {
      use(1);
      container1.stopContainer();
      use(2);
      container2.stopContainer();
    }
  }