Package tests.jfun.yan

Source Code of tests.jfun.yan.BaseNonSingletonContainerTestCase$Avenue

/*
* Created on Mar 21, 2005
*
* Author Ben Yu
* ZBS
*/
package tests.jfun.yan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;

import tests.jfun.yan.tck.BaseContainerTestCase;
import tests.jfun.yan.tck.BaseContainerTestCase.Person;
import tests.jfun.yan.testmodel.ICacheFactory;
import tests.jfun.yan.testmodel.ICacheFactory2;
import tests.jfun.yan.testmodel.MyCache;

import jfun.yan.Component;
import jfun.yan.Components;
import jfun.yan.Creator;
import jfun.yan.CyclicDependencyException;
import jfun.yan.Functions;
import jfun.yan.IrresolveableArgumentException;
import jfun.yan.Map2;
import jfun.yan.Monad;
import jfun.yan.ParameterTypeMismatchException;
import jfun.yan.ReturnTypeMismatchException;
import jfun.yan.Container;
import jfun.yan.TypeMismatchException;
import jfun.yan.util.Predicate;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
* Zephyr Business Solution
*
* @author Ben Yu
*
*/
public class BaseNonSingletonContainerTestCase extends tests.jfun.yan.tck.BaseContainerTestCase {
  public static void main(String[] args){
    tests.jfun.yan.Utils.runTest(suite());
  }
  private static TestSuite suite(){
    return new TestSuite(BaseNonSingletonContainerTestCase.class);
  }

  public static class Avenue{
    private final int id;
    private final String name;
    private static int seed = 0;
    Avenue(int id, String name){
      this.id = id;
      this.name = name;
    }
    public static Avenue instance(String name){
      return new Avenue(++seed, name);
    }
   
    public int getId() {
      return id;
    }
    public String getName() {
      return name;
    }
  }
  public void testHashmapComponent(){
    final Container yan = createYanContainer();
    final Component newAvenue = Components.static_method(Avenue.class, "instance");
    yan.registerComponent("target", Components.hashmap(
        new String[]{"first", "second", "third"},
        new Creator[]{newAvenue.withArgument(0, Components.value("Randolph")),
            newAvenue.withArgument(0, Components.value("Lincoln")),
            newAvenue.withArgument(0, Components.value("Fullerton"))}
        )
    );
    assertEquals(java.util.LinkedHashMap.class, yan.verifyType(HashMap.class));
    final HashMap hmap = (HashMap)yan.getInstanceOfType(java.util.Map.class);
    verifyLinkedHashMap((LinkedHashMap)hmap);
    final Component bad_hash = Components.hashmap(
        new String[]{"a","b"},
        new Creator[]{
            Components.useKey("a").withState("hello"),
            Components.useState(new Predicate(){
              public boolean isObject(Object obj){
                return false;
              }
            })
        }
    );
    yan.registerComponent("bad_hash", bad_hash);
    try{
      yan.getInstance("bad_hash");
      fail("should have failed with UnresolvedComponentException");
    }
    catch(jfun.yan.UnresolvedComponentException e){
      assertTraceSize(e,3);
      assertEntry(e, 0, "useKey <a>");
      assertEntry(e, 1, bad_hash);
      assertEntry(e, 2, "getInstance <bad_hash>");
    }
  }
  public void testHashMapComponentWithLessKey(){
    final Container yan = createYanContainer();
    final Component newAvenue = Components.static_method(Avenue.class, "instance");
    try{
      yan.registerComponent("target", Components.hashmap(
          new String[]{"first", "second"},
          new Creator[]{newAvenue.withArgument(0, Components.value("Randolph")),
              newAvenue.withArgument(0, Components.value("Lincoln")),
              newAvenue.withArgument(0, Components.value("Fullerton"))}
          )
      );
      fail("should have failed");
    }
    catch(IllegalArgumentException e){
      assertEquals("keys.length==2, vals.length==3", e.getMessage());
    }
  }
  public void testHashMapComponentWithDuplicateKey(){
    //in the new design, we do not check key dups.
    //wrappers such as Nuts check duplicates.
    final Container yan = createYanContainer();
    final Component newAvenue = Components.static_method(Avenue.class, "instance");
    //try{
      yan.registerComponent("target", Components.hashmap(
          new String[]{"first", "second", "second"},
          new Creator[]{newAvenue.withArgument(0, Components.value("Randolph")),
              newAvenue.withArgument(0, Components.value("Lincoln")),
              newAvenue.withArgument(0, Components.value("Fullerton"))}
          )
      );
      //fail("should have failed");
    //}
    //catch(IllegalArgumentException e){
    //  assertEquals("duplicate key: second", e.getMessage());
    //}
  }
  public void testCreateLinkedHashMapUsingMap(){
    final Container yan = createYanContainer();
    final Component newAvenue = Components.useKey("factory");
    final Component factory = Components.static_method(Avenue.class, "instance")
    .incomplete();
    final String[] keys = new String[]{"first", "second", "third"};
    final Component vals = Components.array(new Component[]{newAvenue.withArgument(0, Components.value("Randolph")),
        newAvenue.withArgument(0, Components.value("Lincoln")),
        newAvenue.withArgument(0, Components.value("Fullerton"))});
    final Component cc = vals.map(new jfun.yan.Map(){
      public Object map(Object obj){
        final Object[] vs = (Object[])obj;
        final LinkedHashMap lhm = new LinkedHashMap(vs.length);
        for(int i=0; i<vs.length; i++){
          lhm.put(keys[i], vs[i]);
        }
        return lhm;
      }
    }).cast(LinkedHashMap.class);
    yan.registerComponent(cc);
    yan.registerComponent("factory", factory);
    yan.verify();
    final LinkedHashMap lhm = (LinkedHashMap)yan.getInstance(LinkedHashMap.class);
    verifyLinkedHashMap(lhm);

  }
  public void testCreateLinkedHashMapUsingMap2(){
    final Container yan = createYanContainer();
    final Component newAvenue = Components.useKey("factory");
    final Component factory = Components.static_method(Avenue.class, "instance")
    .incomplete();
    final Component keys = Components.value(new String[]{"first", "second", "third"});
    final Component vals = Components.list(new Creator[]{newAvenue.withArgument(0, Components.value("Randolph")),
        newAvenue.withArgument(0, Components.value("Lincoln")),
        newAvenue.withArgument(0, Components.value("Fullerton"))});
    final Component cc = Monad.map(keys, vals, new Map2(){
      public Object map(Object o1, Object o2){
        final Object[] ks = (Object[])o1;
        final java.util.List vs = (java.util.List)o2;
        final LinkedHashMap lhm = new LinkedHashMap(ks.length);
        for(int i=0;i<vs.size();i++){
          lhm.put(ks[i], vs.get(i));
        }
        return lhm;
      }
    }).cast(LinkedHashMap.class);
    yan.registerComponent(cc);
    yan.registerComponent("factory", factory);
    yan.verify();
    final LinkedHashMap lhm = (LinkedHashMap)yan.getInstance(LinkedHashMap.class);
    verifyLinkedHashMap(lhm);
  }
  public void testCreateLinkedHashMapUsingBind(){
    final Container yan = createYanContainer();
    final Component newAvenue = Components.useKey("factory");
    final Component factory = Components.static_method(Avenue.class, "instance")
    .incomplete();
    //final Component keys = Components.value(new String[]{"first", "second", "third"});
    final Component vals = Components.list(new Creator[]{newAvenue.withArgument(0, Components.value("Randolph")),
        newAvenue.withArgument(0, Components.value("Lincoln")),
        newAvenue.withArgument(0, Components.value("Fullerton"))});
    final Component cc = Monad.bind(vals, new jfun.yan.Binder(){
      public Creator bind(Object obj){
        final Object[] ks = new String[]{"first", "second", "third"};
        final java.util.List vs = (java.util.List)obj;
        final LinkedHashMap lhm = new LinkedHashMap();
        for(int i=0; i<ks.length; i++){
          lhm.put(ks[i], vs.get(i));
        }
        return Components.value(lhm);
      }
    }).cast(LinkedHashMap.class);
    yan.registerComponent(cc);
    yan.registerComponent("factory", factory);
    yan.verify();
    final LinkedHashMap lhm = (LinkedHashMap)yan.getInstance(LinkedHashMap.class);
    verifyLinkedHashMap(lhm);
  }
  private void verifyLinkedHashMap(final LinkedHashMap lhm){
    assertEquals(3, lhm.size());
    java.util.Iterator it = lhm.keySet().iterator();
    String k = (String)it.next();
    assertEquals("first", k);
    Avenue a = (Avenue)lhm.get(k);
    int from = a.getId();
    assertEquals("Randolph", a.getName());
    k = (String)it.next();
    assertEquals("second", k);
    a = (Avenue)lhm.get(k);
    assertEquals(from+1, a.getId());
    assertEquals("Lincoln", a.getName());
    k = (String)it.next();
    assertEquals("third", k);
    a = (Avenue)lhm.get(k);
    assertEquals(from+2, a.getId());
    assertEquals("Fullerton", a.getName());
  }
  public void testArrayComponent(){
    final Container yan = createYanContainer();
    yan.registerComponent("target",
        Components.array(
        new Component[]{Components.value("hello"), Components.value("world")}));
    assertEquals(String[].class, yan.verifyComponent(
        yan.getComponent("target")));
    String[] arr = (String[])yan.getInstance("target");
    assertEquals(2, arr.length);
    assertEquals("hello", arr[0]);
    assertEquals("world", arr[1]);
  }
  public void testArrayComponentWithDynamicBoundComponent(){
    final Container yan = createYanContainer();
    yan.registerComponent("target", Components.array(
        new Component[]{Components.value("hello"), Components.useKey("you")}));
    yan.registerValue("you", "world");
    assertEquals(Object[].class, yan.verifyComponent(yan.getComponent("target")));
    Object[] arr = (Object[])yan.getInstanceOfType(Object[].class);
    assertEquals(2, arr.length);
    assertEquals("hello", arr[0]);
    assertEquals("world", arr[1]);
  }
  public void testArrayComponentWithComponentTypeSpecified(){
    final Container yan = createYanContainer();
    yan.registerComponent(
        Components.array(
        new Component[]{Components.value("hello"), Components.useKey("you")}
        , CharSequence.class));
    yan.registerValue("you", "world");
    assertEquals(CharSequence[].class, yan.verifyComponent(yan.getComponent(CharSequence[].class)));
    CharSequence[] arr = (CharSequence[])yan.getInstanceOfType(CharSequence[].class);
    assertFalse(arr instanceof String[]);
    assertEquals(2, arr.length);
    assertEquals("hello", arr[0]);
    assertEquals("world", arr[1]);
  }
  public void testArrayComponentWithRaggedType(){
    final Container yan = createYanContainer();
    yan.registerComponent("target", Components.array(
        new Component[]{Components.value("hello"),
            Components.useKey("age")}));
    yan.registerValue("age", new Integer(20));
    assertEquals(Object[].class, yan.verifyComponent(yan.getComponent("target")));
    Object[] arr = (Object[])yan.getInstanceOfType(Object[].class);
   
    assertEquals(2, arr.length);
    assertEquals("hello", arr[0]);
    assertEquals(new Integer(20), arr[1]);
  }
  public void testBadArrayComponentWithRaggedType(){
    final Container yan = createYanContainer();
    yan.registerValue("age", new Integer(20));
    yan.registerComponent("target", Components.array(
        new Component[]{Components.value("hello"),
            Components.useKey("age")}, CharSequence.class));
   
    try{
      yan.verify();
      fail("should have failed with illegal argument");
    }
    catch(TypeMismatchException e){
      assertEquals("type mismatch: the #1 element is of java.lang.Integer, while java.lang.CharSequence is expected.", e.getMessage());
    }
  }
  public void testListComponent(){
    final Container yan = createYanContainer();
    yan.registerComponent(Components.list(new Creator[]{
        Components.value("hello"), Components.value("world")}
    ));
    assertEquals(java.util.ArrayList.class,
        yan.verifyComponent(yan.getComponentOfType(java.util.List.class)));
    final java.util.List list = (java.util.List)yan.getInstanceOfType(java.util.ArrayList.class);
    assertEquals(2, list.size());
    assertEquals("hello", list.get(0));
    assertEquals("world", list.get(1));
  }
  public void testCyclicListComponent(){
    final Container yan = createYanContainer();
    yan.registerComponent("target", Components.list(new Creator[]{
        Components.value("hello"), Components.useKey("target")}
    ));
    try{
      yan.verifyKey("target");
      fail("should have failed with cyclic dependency");
    }
    catch(CyclicDependencyException e){
      assertEquals(4, e.getResolutionTrace().size());
      assertEntry(e, 0, "list [hello,useKey <target>]");
      assertEntry(e, 1, "useKey <target>");
      assertEntry(e, 2, "list [hello,useKey <target>]");
      assertEntry(e, 3, "verifyKey <target>");
    }
  }  
  public void testCyclicArrayComponent(){
    final Container yan = createYanContainer();
    yan.registerComponent("target", Components.array(new Component[]{
        Components.value("hello"), Components.useKey("target")}
    ));
    try{
      yan.verifyKey("target");
      fail("should have failed with cyclic dependency");
    }
    catch(CyclicDependencyException e){
      assertTraceSize(e, 4);
      assertEntry(e, 0, "array [hello,useKey <target>]:java.lang.Object[]");
      assertEntry(e, 1, "useKey <target>");
      assertEntry(e, 2, "array [hello,useKey <target>]:java.lang.Object[]");
      assertEntry(e, 3, "verifyKey <target>");
    }
  }
  public void testUseAll(){
    final Container yan = createYanContainer();
    yan.registerComponent("target", Components.useAll(String.class));
    assertEquals(0, ((java.util.List)yan.getInstanceOfType(java.util.List.class)).size());
    yan.registerValue("abc");
    yan.registerValue("yet another", "cde");
    java.util.List strs = (java.util.List)yan.getInstance("target");
    assertEquals(2, strs.size());
    assertEquals("abc", strs.get(0));
    assertEquals("cde", strs.get(1));
    yan.registerComponent("another", Components.useKey("yet another").subsume(String.class));
    strs = (java.util.List)yan.getInstance("target");
    assertEquals(3, strs.size());
    assertEquals("abc", strs.get(0));
    assertEquals("cde", strs.get(1));
    assertEquals("cde", strs.get(2));
   
  }
  public void testUseAllInSameContainer(){
    final Container yan = createYanContainer();
    yan.registerComponent("target", Components.useAll(yan, String.class));
    assertEquals(0, ((java.util.List)yan.getInstanceOfType(java.util.List.class)).size());
    yan.registerValue("abc");
    yan.registerValue("yet another", "cde");
    java.util.List strs = (java.util.List)yan.getInstance("target");
    assertEquals(2, strs.size());
    assertEquals("abc", strs.get(0));
    assertEquals("cde", strs.get(1));
    yan.registerComponent("another", Components.useKey("yet another").subsume(String.class));
    strs = (java.util.ArrayList)yan.getInstance("target");
    assertEquals(3, strs.size());
    assertEquals("abc", strs.get(0));
    assertEquals("cde", strs.get(1));
    assertEquals("cde", strs.get(2));
   
  }
  public void testUseAllInDifferentContainer(){
    final Container yan1 = createYanContainer();
    final Container yan = createYanContainer();
    yan1.registerComponent("target", Components.useAll(yan, String.class));
    assertEquals(0, ((java.util.List)yan1.getInstanceOfType(java.util.List.class)).size());
    yan.registerValue("abc");
    yan.registerValue("yet another", "cde");
    java.util.List strs = (java.util.List)yan1.getInstance("target");
    assertEquals(2, strs.size());
    assertEquals("abc", strs.get(0));
    assertEquals("cde", strs.get(1));
    //have to specify the container because otherwise "yet another" will be looked up from yan1.
    yan.registerComponent("another", Components.useKey(yan, "yet another").subsume(String.class));
    strs = (java.util.List)yan1.getInstance("target");
    assertEquals(3, strs.size());
    assertEquals("abc", strs.get(0));
    assertEquals("cde", strs.get(1));
    assertEquals("cde", strs.get(2));
   
  }
  public void testUseState(){
    final Container yan = createYanContainer();
    yan.registerComponent("allnul", Components.useState(new Predicate(){
      public boolean isObject(Object obj){
        return obj==null;
      }
      public String toString(){return "isnull";}
    }).withState("!do not recurse on me"));
    yan.registerComponent("startswithhello", Components.useState(new Predicate(){
      public boolean isObject(Object obj){
        if(obj instanceof String){
          final String s = (String)obj;
          return s.startsWith("hello");
        }
        else return false;
      }
      public String toString(){return "startsWithHello";}
    }));
    yan.registerValue("foo");
    yan.registerComponent("bar", Components.value(Class.class)
        .withState("hello world"));
    yan.registerValue("baz", new Integer(10));
    yan.registerComponent("joo", Components.value(int.class).withState("hello me"));
    try{
      final java.util.List nulls = (java.util.List)yan.getInstance("allnul");
      final java.util.List hellos = (java.util.List)yan.getInstance("startswithhello");
      assertEquals(3, nulls.size());
      assertEquals(2, hellos.size());
      final java.util.List the_hellos = (java.util.List)nulls.get(0);
      assertEquals(hellos, the_hellos);
      assertEquals("foo", nulls.get(1));
      assertEquals(new Integer(10), nulls.get(2));
      assertEquals(Class.class, hellos.get(0));
      assertEquals(int.class, hellos.get(1));
    }
    catch(jfun.yan.YanException e){
      e.printResolutionTrace(System.err);
      throw e;
    }
  }
  public String signup(Person p1, Person p2){
    assertSame(p1, p2);
    return p1.getName();
  }
  public void testSharedArgument()
  throws Exception{
    final Container yan = createYanContainer();
    final Component signup = Components.method(this, "signup");
    yan.registerComponent("target",
        Monad.bind(Components.useType(Person.class),
          new jfun.yan.Binder(){
            public Creator bind(Object v){
              final Component c = Components.value(v);
              return signup.withArguments(
                  new Creator[]{c,c}
              );
            }
          }
        ).cast(signup.getType())
    );
    yan.registerComponent(Components.method(this, "newPerson").bean(new String[]{"name"}));
    yan.registerValue(new Integer(10));
    final String name = "Jack Nicolson";
    yan.registerValue(name);
    assertEquals(name, yan.getInstance("target"));
  }
  public void testFactory(){
    final Container yan = createYanContainer();
    final Component mycache =
      Components.ctor(MyCache.class,
          new Class[]{String.class, int.class});
   
    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(10, icache.getCapacity());
    try{
      yan.verify();
      fail("should have failed with IrresolveableArgumentException");
    }
    catch(IrresolveableArgumentException e){
      assertTraceSize(e, 3);
      assertEntry(e, 1, "tests.jfun.yan.testmodel.ICacheFactory");
      assertEquals(0, e.getOrdinalPosition());
      assertEquals(String.class, e.getParameterType());
    }
    yan.registerValue("someone");
    try{
      yan.verify();
      fail("should have failed with IrresolveableArgumentException");
    }
    catch(IrresolveableArgumentException e){
      assertTraceSize(e, 3);
      assertEntry(e, 1, "tests.jfun.yan.testmodel.ICacheFactory");
      assertEquals(1, e.getOrdinalPosition());
      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());
   
    final tests.jfun.yan.testmodel.ICache icache3 = ifactory.createCache();
    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());
   
    final tests.jfun.yan.testmodel.ICache icache3 = ifactory.createCache();
    assertEquals(MyCache.class, icache3.getClass());
    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());
   
    final tests.jfun.yan.testmodel.ICache icache3 = ifactory.createCache();
    assertEquals(MyCache.class, icache3.getClass());
    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());
   
    final tests.jfun.yan.testmodel.ICache icache3 = ifactory.createCache();
    assertEquals(MyCache.class, icache3.getClass());
    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());
   
    final tests.jfun.yan.testmodel.ICache icache3 = ifactory.createCache();
    assertEquals(MyCache.class, icache3.getClass());
    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());
      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)>",
          e.getMessage());
      assertEntry(e, 0, "tests.jfun.yan.testmodel.IMonitoredCacheFactory");
      //assertEquals(0, e.getOrdinalPosition());
      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){
      assertEquals("constructor not found for: int[]()", e.getMessage());
    }
    try{
      Components.ctor(int[].class, new Class[0]);
    }
    catch(IllegalArgumentException e){
      assertEquals("constructor not found for: int[]()", e.getMessage());
    }
    try{
      Components.ctor(int[].class, new Class[]{int.class, int.class});
    }
    catch(IllegalArgumentException e){
      assertEquals("constructor not found for: int[](int,int)", e.getMessage());
   
  }
}
TOP

Related Classes of tests.jfun.yan.BaseNonSingletonContainerTestCase$Avenue

TOP
Copyright © 2018 www.massapi.com. 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.