Package jfun.yan

Examples of jfun.yan.Container


    // 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


        throw e;
      }
    }
    public void testShouldBeAbleToHandleMutualDependenciesWithCachingProxy()
    throws IntrospectionException{
        Container yan = new SingletonProxyContainer();
          /*new DefaultContainer(new CachingComponentAdapterFactory(
                new SetterInjectionComponentAdapterFactory()));*/

        yan.registerComponent(Components.bean(Yin.class)
            .withArgument(1, Components.value("bad ying"))
        );
        yan.registerComponent(Components.bean(Yang.class)
            .withProperty("nonexistent", Components.value("bad yang"))
            );

        IYin yin = (IYin) yan.getInstanceOfType(IYin.class);
        IYang yang = (IYang) yan.getInstanceOfType(IYang.class);

        assertSame(yin, yang.getYin());
        assertSame(yang, yin.getYang());
    }
View Full Code Here

    assertEquals(MyCache.class, icache3.getClass());
    assertEquals("someone", icache3.getName());
    assertEquals(0, icache3.getCapacity());     
  }
  public void testCustomizationsOnProductComponentShouldOverrideFactoryParameter(){
    final Container yan = createYanContainer();
    final Component mycache =
      Components.ctor(MyCache.class,
          new Class[]{String.class, int.class})
      .withArgument(1, Components.value(100));
   
    final Component factory =
      mycache.factory(ICacheFactory.class);
    yan.registerComponent("target", factory);
    final ICacheFactory ifactory =
      (ICacheFactory)yan.getInstance("target");
    final tests.jfun.yan.testmodel.ICache icache = ifactory.createCache("tom", 10);
    assertEquals(MyCache.class, icache.getClass());
    assertEquals("tom", icache.getName());
    assertEquals(100, icache.getCapacity());
    try{
      yan.verify();
      fail("should have failed with IrresolveableArgumentException");
    }
    catch(IrresolveableArgumentException e){
      //e.printResolutionTrace();
      assertTraceSize(e, 4);
      assertEntry(e, 2, "tests.jfun.yan.testmodel.ICacheFactory");
      assertEquals(0, e.getOrdinalPosition());
      assertEquals(String.class, e.getParameterType());
    }
    yan.registerValue("someone");
    yan.verify();
    yan.registerValue(new Integer(0));
    yan.verify();
   
    final tests.jfun.yan.testmodel.ICache icache2 = ifactory.createCache("jack", 11);
    assertEquals(MyCache.class, icache2.getClass());
    assertEquals("jack", icache2.getName());
    assertEquals(100, icache2.getCapacity());
View Full Code Here

    assertEquals("someone", icache3.getName());
    assertEquals(100, icache3.getCapacity());     
  }
  public void testLocalCustomizationOverridesFactoryParametersRedirectedToPropeties()
  throws Exception{
    final Container yan = createYanContainer();
    final Component mycache =
      Components.bean(MyCache.class)
      //.withProperty("name", Components.value("should not see me"))
      .withProperty("name",
          Components.useProperty(MyCache.class, "capacity", int.class)
          .map(new jfun.yan.Map(){
            public Object map(Object obj){
              return obj.toString();
            }
          }).cast(String.class)
      )
      .withProperty("name", Components.value("should not see me"))
      ;
   
    final Component factory =
      Components.fromArguments(mycache, new String[]{"name","capacity"})
      .factory(ICacheFactory.class);
    yan.registerComponent("target", factory);
    final ICacheFactory ifactory =
      (ICacheFactory)yan.getInstance("target");
    final tests.jfun.yan.testmodel.ICache icache = ifactory.createCache("tom", 10);
    assertEquals(MyCache.class, icache.getClass());
    assertEquals("10", icache.getName());
    assertEquals(10, icache.getCapacity());
    try{
      yan.verify();
      fail("should have failed with IrresolveableArgumentException");
    }
    catch(IrresolveableArgumentException e){
     //e.printResolutionTrace();
      assertTraceSize(e, 7);
      assertEntry(e, 5, "tests.jfun.yan.testmodel.ICacheFactory");
      final int pos = e.getOrdinalPosition();
      if(pos==0)
        assertEquals(String.class, e.getParameterType());
      else
        assertEquals(int.class, e.getParameterType());
    }
    yan.registerValue("someone");
    try{
      yan.verify();
      fail("should have failed with IrresolveableArgumentException");
    }
    catch(IrresolveableArgumentException e){
      assertTraceSize(e, 7);
      assertEntry(e, 5, "tests.jfun.yan.testmodel.ICacheFactory");
      final int pos = e.getOrdinalPosition();
      if(pos==0)
        assertEquals(String.class, e.getParameterType());
      else
        assertEquals(int.class, e.getParameterType());
    }
    yan.registerValue(new Integer(0));
    yan.verify();
   
    final tests.jfun.yan.testmodel.ICache icache2 = ifactory.createCache("jack", 11);
    assertEquals(MyCache.class, icache2.getClass());
    assertEquals("11", icache2.getName());
    assertEquals(11, icache2.getCapacity());
View Full Code Here

    assertEquals("0", icache3.getName());
    assertEquals(0, icache3.getCapacity());     
  }
  public void testFactoryWithParametersRedirectedToPropeties()
  throws Exception{
    final Container yan = createYanContainer();
    final Component mycache =
      Components.bean(MyCache.class);
   
    final Component factory =
      Components.fromArguments(mycache, new String[]{"name","capacity"})
      .factory(ICacheFactory.class);
    yan.registerComponent("target", factory);
    final ICacheFactory ifactory =
      (ICacheFactory)yan.getInstance("target");
    final tests.jfun.yan.testmodel.ICache icache = ifactory.createCache("tom", 10);
    assertEquals(MyCache.class, icache.getClass());
    assertEquals("tom", icache.getName());
    assertEquals(10, icache.getCapacity());
    try{
      yan.verify();
      fail("should have failed with IrresolveableArgumentException");
    }
    catch(IrresolveableArgumentException e){
     //e.printResolutionTrace();
      assertTraceSize(e, 5);
      assertEntry(e, 3, "tests.jfun.yan.testmodel.ICacheFactory");
      final int pos = e.getOrdinalPosition();
      if(pos==0)
        assertEquals(String.class, e.getParameterType());
      else
        assertEquals(int.class, e.getParameterType());
    }
    yan.registerValue("someone");
    try{
      yan.verify();
      fail("should have failed with IrresolveableArgumentException");
    }
    catch(IrresolveableArgumentException e){
      assertTraceSize(e, 5);
      assertEntry(e, 3, "tests.jfun.yan.testmodel.ICacheFactory");
      final int pos = e.getOrdinalPosition();
      if(pos==0)
        assertEquals(String.class, e.getParameterType());
      else
        assertEquals(int.class, e.getParameterType());
    }
    yan.registerValue(new Integer(0));
    yan.verify();
   
    final tests.jfun.yan.testmodel.ICache icache2 = ifactory.createCache("jack", 11);
    assertEquals(MyCache.class, icache2.getClass());
    assertEquals("jack", icache2.getName());
    assertEquals(11, icache2.getCapacity());
View Full Code Here

    assertEquals("someone", icache3.getName());
    assertEquals(0, icache3.getCapacity());     
  }
  public void testFactoryWithParametersRedirectedToPropertiesThenRedirectedBackToArguments()
  throws Exception{
    final Container yan = createYanContainer();
    final Component mycache =
      Components.ctor(MyCache.class, new Class[]{String.class, int.class})
      .fromProperties(new String[]{"name","capacity"});
   
    final Component factory =
      mycache.fromArguments(new String[]{"name","capacity"})
      .factory(ICacheFactory.class);
    yan.registerComponent("target", factory);
    final ICacheFactory ifactory =
      (ICacheFactory)yan.getInstance("target");
    final tests.jfun.yan.testmodel.ICache icache = ifactory.createCache("tom", 10);
    assertEquals(MyCache.class, icache.getClass());
    assertEquals("tom", icache.getName());
    assertEquals(10, icache.getCapacity());
    try{
      yan.verify();
      fail("should have failed with IrresolveableArgumentException");
    }
    catch(IrresolveableArgumentException e){
      //e.printResolutionTrace();
      assertTraceSize(e, 5);
      assertEntry(e, 3, "tests.jfun.yan.testmodel.ICacheFactory");
      final int pos = e.getOrdinalPosition();
      if(pos==0)
        assertEquals(String.class, e.getParameterType());
      else
        assertEquals(int.class, e.getParameterType());
    }
    yan.registerValue("someone");
    try{
      yan.verify();
      fail("should have failed with IrresolveableArgumentException");
    }
    catch(IrresolveableArgumentException e){
      assertTraceSize(e, 5);
      assertEntry(e, 3, "tests.jfun.yan.testmodel.ICacheFactory");
      final int pos = e.getOrdinalPosition();
      if(pos==0)
        assertEquals(String.class, e.getParameterType());
      else
        assertEquals(int.class, e.getParameterType());
    }
    yan.registerValue(new Integer(0));
    yan.verify();
   
    final tests.jfun.yan.testmodel.ICache icache2 = ifactory.createCache("jack", 11);
    assertEquals(MyCache.class, icache2.getClass());
    assertEquals("jack", icache2.getName());
    assertEquals(11, icache2.getCapacity());
View Full Code Here

    assertEquals("someone", icache3.getName());
    assertEquals(0, icache3.getCapacity());     
  }
  public void testFactoryWithBadParameterSequence()
  throws Exception{
    final Container yan = createYanContainer();
    final Component mycache =
      Components.ctor(MyCache.class,
          new Class[]{String.class, int.class});
   
    final Component factory =
      mycache.factory(ICacheFactory2.class);
    yan.registerComponent("target", factory);
    try{
      yan.verify();
      fail("should have failed with ParameterTypeMismatchException");
    }
    catch(ParameterTypeMismatchException e){
      assertEquals(0, e.getOrdinalPosition());
      assertEquals(String.class, e.getExpectedType());
View Full Code Here

      assertEquals(String.class, e.getExpectedType());
      assertEquals(int.class, e.getActualType());
    }
  }
  public void testFactoryWithBadReturnType(){
    final Container yan = createYanContainer();
    final Component mycache =
      Components.ctor(MyCache.class,
          new Class[]{String.class, int.class});
   
    final Component factory =
      mycache.factory(tests.jfun.yan.testmodel.IMonitoredCacheFactory.class);
    yan.registerComponent("target", factory);
    try{
      yan.verify();
      fail("should have failed with ReturnTypeMismatchException");
    }
    catch(ReturnTypeMismatchException e){
      //e.printResolutionTrace();
      assertEquals("tests.jfun.yan.testmodel.MyCache cannot match return type of <public abstract tests.jfun.yan.testmodel.IMonitoredCache tests.jfun.yan.testmodel.IMonitoredCacheFactory.createCache(java.lang.String,int)>",
View Full Code Here

      assertEquals(tests.jfun.yan.testmodel.IMonitoredCache.class, e.getExpectedType());
      assertEquals(MyCache.class, e.getActualType());
    }
  }
  public void testSealedComponent(){
    final Container yan = createYanContainer();
    final Component c1 = Components.ctor(java.util.ArrayList.class, new Class[]{int.class});
    yan.registerComponent("list", c1.seal());
    yan.registerValue(new Integer(10));
    try{
      yan.getInstance("list");
      fail ("should have failed with IrresolveableArgumentException");
    }
    catch(IrresolveableArgumentException e){}
    yan.registerComponent("list", c1.seal().withArgument(0, Components.value(5)));
    yan.getInstance("list");
    //fail ("should have failed with IrresolveableArgumentException");

    yan.registerComponent("list", c1);
    yan.registerComponent("target", Components.useKey("list").seal()
        .withArgument(0, Components.value(12)));
    //try{
      yan.getInstance("target");
      //fail("should have failed with IrresolveableArgumentException");
    //}
    //catch(IrresolveableArgumentException e){}
    //assertEquals(10, result.size());
    yan.registerComponent("list", c1.withArgument(0,
        Components.useArgument(Functions.ctor(
            ArrayList.class, new Class[]{int.class}), 0, int.class)));
    yan.registerComponent("target", Components.useKey("list").seal()
        .withArgument(0, Components.value(12)));
    //try{
      yan.getInstance("target");
      //fail("should have failed with IrresolveableArgumentException");
    //}
    //catch(IrresolveableArgumentException e){}
    yan.registerComponent("list", c1.withArgument(0, Components.useType(int.class)));
    yan.registerComponent("target", Components.useKey("list").seal()
        .withArgument(0, Components.value(12)));
    final java.util.ArrayList result = (ArrayList)yan.getInstance("target");
    final Component array = Components.ctor(String[].class);
   
   
    yan.registerComponent("array", array.withArgument(0, Components.useType(int.class)));
    yan.registerComponent("target", Components.useKey("array").seal()
        .withArgument(0, Components.value(12)));
    final String[] arr = (String[])yan.getInstance("target");
    assertEquals(10, arr.length);
    final Component array2 = Components.ctor(int[].class, new Class[]{int.class});
    yan.registerComponent("array", array2.withArgument(0, Components.useType(int.class)));
    yan.registerComponent("target", Components.useKey("array").seal()
        .withArgument(0, Components.value(12)));
    final int[] arr2 = (int[])yan.getInstance("target");
    assertEquals(10, arr2.length);
    try{
      Components.ctor(int[].class, null);
    }
    catch(IllegalArgumentException e){
View Full Code Here

    assertTrue(mon.isInvoking());
    assertTrue(mon.isInvoked());
    assertFalse(mon.isInvocationFailed());
  }
  private void testMonitor(BookmarkMonitor mon, ComponentMonitor m){
    final Container yan = new MonitoringContainer(m);
    yan.registerValue(arg0);
    yan.registerValue(arg1);
    yan.registerConstructor("target", MonitoredObject.class);
    try{
      yan.getInstance("target");
      fail("should have failed");
    }
    catch(ComponentInstantiationException e){     
    }
    assertTrue(mon.isConstructing());
    assertFalse(mon.isConstructed());
    assertTrue(mon.isConstructionFailed());
    assertFalse(mon.isInvoking());
    yan.registerComponent("target",
        new Monitors(m).static_method(MonitoredObject.class, "instance")
        .withArgument(2, Components.value(arg2)));
    yan.getInstance("target");
    assertTrue(mon.isInvoking());
    assertTrue(mon.isInvoked());
    assertFalse(mon.isInvocationFailed());
  }
View Full Code Here

TOP

Related Classes of jfun.yan.Container

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.