Package jfun.yan.containers

Examples of jfun.yan.containers.DefaultContainer


        picoContainer.filo(new MemberCallback(Foo.class, "dispose"));
        assertEquals(true, foo.disposed);
    }
*/
    public void testComponentInstancesFromParentsAreDirectlyAccessible2() {
        final Container a = new DefaultContainer();
        final Container b = new DefaultContainer().inherit(a);
        final Container c = new DefaultContainer().inherit(b);

        Object ao = new Object();
        Object bo = new Object();
        Object co = new Object();

        a.registerValue("a", ao);
        b.registerValue("b", bo);
        c.registerValue("c", co);

        assertEquals(1, a.getInstances().size());
        assertEquals(2, b.getInstances().size());
        assertEquals(3, c.getInstances().size());
    }
View Full Code Here




    public void testAmbiguousDependencies(){

        DefaultContainer yan = new DefaultContainer();

        // Register two Touchables that Fred will be confused about
        yan.registerConstructor(SimpleTouchable.class);
        yan.registerConstructor(DerivedTouchable.class);

        // Register a confused DependsOnTouchable
        yan.registerConstructor(DependsOnTouchable.class);
        assertEquals(DependsOnTouchable.class, yan.getComponentType(DependsOnTouchable.class));
        assertNull(yan.getComponentType(Touchable.class));
        try {
            yan.getInstance(DependsOnTouchable.class);
            fail("DependsOnTouchable should have been confused about the two Touchables");
        } catch (jfun.yan.AmbiguousComponentResolutionException e) {
          assertEquals(2, e.getResolutionTrace().size());
          assertParameter(e, 0, DependsOnTouchable.class, 0);
          assertEquals(Touchable.class, e.getComponentKey());
View Full Code Here

          assertEquals(""+cc, ""+cc1);
        }
      };
    }
    protected Container getContainerImpl(){
      return new DefaultContainer();
    }
View Full Code Here

      yan2.registerValue("target arrived!");
      assertEquals("target arrived!", yan.getInstance("target"));
    }
    public void testLifeycles()
    throws Throwable{
      final Container yan = new DefaultContainer();
      final Component real_engine =
        Components.useKey("real engine").singleton(new ThreadLocalScope());
      final DefaultLifecycleDescriptor desc = new DefaultLifecycleDescriptor();
      desc.setCloser(new Phase(desc.getCloser().getPhaseKey(), ExceptionHandlers.suppresser()));
      desc.setStopper(new Phase(desc.getStopper().getPhaseKey(), ExceptionHandlers.suppresser()));
      final DefaultLifecycleManager lm = new DefaultLifecycleManager(desc);
      final DefaultLifecycleManager.DefaultLifecycle lc = lm.newLifecycle()
      .starter("start")
      .stopper("stop")
      .initializer("initialize")
      .disposer("destroy");
      final Component engine_lc = lc.manage(real_engine);
      yan.registerComponent(CarEngine.class, engine_lc);
      /*
      yan.registerComponent(CarEngine.class, LifecycleDescriptor.instance(
          Components.useKey("real engine").singleton(new ThreadLocalScope()),
          ExceptionHandlers.suppresser())
          .starter("start")
          .stopper("stop")
          .initializer("initialize")
          .disposer("destroy")
          .get());
      */
      yan.registerConstructor("real engine", CarEngine.class);
      final Component sttfrc = Components.ctor(StatefulResource.class)
      .withArgument(0, Components.useType(CarEngine.class));
      yan.registerComponent(lc.manage(sttfrc));
      /*
      yan.registerComponent(LifecycleDescriptor.instance(
          Components.ctor(StatefulResource.class)
          .withArgument(0, Components.useType(CarEngine.class))
          , ExceptionHandlers.suppresser())
          .initializer("initialize")
          .disposer("destroy")
          .starter("start")
          .stopper("stop")
          .get());
      */
      final Component car_engine = yan.getComponentOfType(CarEngine.class);
      assertNull(car_engine.getType());
     
      final StatefulResource resource = (StatefulResource)yan.getInstanceOfType(StatefulResource.class);
      final CarEngine engine = (CarEngine)yan.getInstanceOfType(CarEngine.class);
      assertEquals(0, engine.getStarted());
      assertEquals(0, engine.getStopped());
      assertEquals(0, engine.getInit());
      assertEquals(0, engine.getDisposed());

View Full Code Here

        //new SetterInjectionComponentAdapter("a", A.class, null);
        Component bAdapter =
          Components.bean(B.class).singleton();
          //new SetterInjectionComponentAdapter("b", B.class, null);

        Container yan = new DefaultContainer();
        yan.verify();
        yan.registerComponent("b", bAdapter);
        yan.registerComponent("a", aAdapter);
        try {
            //aAdapter.getComponentInstance(yan);
          yan.getInstance("a");
        } catch (UnsatisfiedComponentException e) {
          //System.out.println(e.getMessage());
          assertEquals("The property list of type java.util.List for component <a> is not resolveable",
              e.getMessage());
            //assertTrue(e.getUnsatisfiableDependencies().contains(List.class));
            //assertTrue(e.getUnsatisfiableDependencies().contains(String.class));
        }
        yan.registerComponent("a", aAdapter.optionalProperty("string")
            .ignoreProperty("list"));
        final B b = (B) yan.getInstance("b");
        A a = (A) yan.getInstance("a");
        assertEquals("default", a.getString());
        assertEquals(new java.util.ArrayList(), a.getList());
        assertSame(b, a.getB());
        yan.registerValue("hello");
        a = (A) yan.getInstance("a");
        assertEquals("hello", a.getString());
        assertEquals(new java.util.ArrayList(), a.getList());
        assertSame(b, a.getB());
        final java.util.List list1 = new java.util.LinkedList();
        list1.add("abc");
        yan.registerValue(list1);
        a = (A) yan.getInstance("a");
        assertEquals("hello", a.getString());
        assertEquals(new java.util.ArrayList(), a.getList());
        assertSame(b, a.getB());
    }
View Full Code Here

        //new SetterInjectionComponentAdapter("a", A.class, null);
        Component bAdapter =
          Components.bean(B.class);
          //new SetterInjectionComponentAdapter("b", B.class, null);

        Container yan = new DefaultContainer();
        yan.verify();
        yan.registerComponent("b", bAdapter);
        yan.registerComponent("a", aAdapter);
        try {
            //aAdapter.getComponentInstance(yan);
          yan.verify();
        } catch (UnsatisfiedComponentException e) {
          //System.out.println(e.getMessage());
            //assertTrue(e.getUnsatisfiableDependencies().contains(List.class));
            //assertTrue(e.getUnsatisfiableDependencies().contains(String.class));
        }
View Full Code Here

        Component cAdapter =Components.bean(C.class);
          //new Component("c", C.class, null);
        //Component cNullAdapter = Components.bean(C.class);
          //new Component("c0", C.class, null);

        Container yan = new DefaultContainer();
        yan.registerComponent("b", bAdapter);
        yan.registerComponent("c", cAdapter);
        yan.registerComponent("c0", cAdapter);
        yan.registerConstructor(ArrayList.class, new Class[0]);

        C c = (C) yan.getInstance("c");
          //(C) cAdapter.getComponentInstance(yan);
        assertTrue(c.instantiatedAsBean());
        C c0 = (C) yan.getInstance("c0");
          //(C) cNullAdapter.getComponentInstance(yan);
        assertTrue(c0.instantiatedAsBean());
    }
View Full Code Here

    // TODO PICO-188
    // http://jira.codehaus.org/browse/PICO-188
    public void testShouldBeAbleToHandleMutualDependenciesWithSetterInjection()
    throws IntrospectionException{
      try{
        Container yan = new DefaultContainer();
          /*new DefaultContainer(new CachingComponentAdapterFactory(
                new SetterInjectionComponentAdapterFactory()));*/
        //call cache() twice to enlarge testing coverage.
        final java.util.Map props = new HashMap();
        props.put("yin", Components.useType(IYin.class));
        props.put("yang", Components.useType(Yang.class));
        yan.registerComponent(Components.bean(Yin.class).singleton().singleton()
            .withArgument(1, Components.value("bad ying"))
            .withProperties(new String[]{"yin","yang"},
                new Creator[]{Components.useType(IYin.class),
                Components.useType(Yang.class)}).guard());
        yan.registerComponent(Components.bean(Yang.class).singleton(new GlobalScope())
            .withProperties(props)
            .withProperty("nonexistent", Components.value("bad yang"))
            .singleton(new ThreadLocalScope()));

        IYin yin = (IYin) yan.getInstanceOfType(IYin.class);
        Yang yang = (Yang) yan.getInstance(Yang.class);

        assertSame(yin, yang.getYin());
        assertSame(yang, yin.getYang());
      }
      catch(YanException e){
View Full Code Here

        new VerifyingVisitor().traverse(pico);
    }*/

    public void testUsageOfADifferentComponentAdapterFactory() {
        // Jira bug 212
        Container parent = new DefaultContainer();
        Container pico = new ProxyContainer().inherit(parent);
        pico.registerConstructor(List.class, ArrayList.class, null);
        List list1 = (List) pico.getInstanceOfType(List.class);
        List list2 = (List) pico.getInstanceOfType(List.class);
        pico.verify();
View Full Code Here

    assertEquals("qian", cb.getName());
    assertEquals(2, cb.getBeans().length);
    assertEquals(true, ((tests.jfun.models.MyBean)cb.getBean()).is1());
  }
  public void test1(){
    final DefaultContainer cc = new DefaultContainer();
    final tests.jfun.models.MyBean[] arr = new tests.jfun.models.MyBean[2];
    cc.registerComponent(CompositeBean.class,
        Components.ctor(CompositeBean.class,
            new Class[]{Named.class, tests.jfun.models.MyBean[].class, String.class})
        .withArgument(1, Components.value(arr))
        .singleton());
       
       
    /*cc.registerConstructor(Named.class, MyBean.class,
        new Class[]{int[].class, short.class, String.class, boolean.class,
        java.util.Map.class});*/
    cc.registerComponent(Named.class,
        Components.static_method(MyBean.class, "instance")
        .withArgument(1,  Components.value(new Short((short)33)))
        .withArgument(2, Components.value("jack"))
        .singleton());
    cc.registerValue(int[].class, new int[]{1,2,3});
    /*
    cc.registerParameter(Named.class, 1,
        ComponentCreators.value(new Short((short)33)));*/
    //cc.registerParameter(Named.class, 2, ComponentCreators.value("jack"));
    cc.registerValue(boolean.class, Boolean.valueOf(true));
    final HashMap hmap = new HashMap();
    hmap.put("firstname", "Jack");
    hmap.put("lastname", "Nicolson");
    cc.registerValue(java.util.Map.class, hmap);
   
    //cc.registerParameter(CompositeBean.class, 1, ComponentCreators.value(arr));
    cc.registerValue(String.class, "qian");
    verify(cc);
  }
View Full Code Here

TOP

Related Classes of jfun.yan.containers.DefaultContainer

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.