/*****************************************************************************
* Copyright (C) PicoContainer Organization. All rights reserved. *
* ------------------------------------------------------------------------- *
* The software in this package is published under the terms of the BSD *
* style license a copy of which has been included with this distribution in *
* the LICENSE.txt file. *
* *
* Original code by *
*****************************************************************************/
package tests.jfun.yan;
import jfun.yan.Component;
import jfun.yan.Container;
import jfun.yan.containers.*;
import tests.jfun.yan.tck.BaseComponentTestCase;
import tests.jfun.yan.testmodel.PersonBean;
import tests.jfun.yan.testmodel.PurseBean;
import java.beans.IntrospectionException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import jfun.yan.*;
import jfun.yan.containers.DefaultContainer;
import jfun.yan.factory.GlobalScope;
import jfun.yan.factory.ThreadLocalScope;
import junit.framework.TestSuite;
public class SetterInjectionComponentAdapterTestCase
extends tests.jfun.yan.tck.BaseComponentTestCase {
public static void main(String[] args){
tests.jfun.yan.Utils.runTest(suite());
}
private static TestSuite suite(){
return new TestSuite(SetterInjectionComponentAdapterTestCase.class);
}
/*
protected ComponentAdapterFactory createDefaultComponentAdapterFactory() {
return new CachingComponentAdapterFactory(new SetterInjectionComponentAdapterFactory());
}
*/
protected Component prepDEF_verifyWithoutDependencyWorks(Container yan){
try{
return Components.ctor(PersonBean.class).bean()
.withProperty("name", Components.value("Pico Container"));
}
catch(java.beans.IntrospectionException e){
throw new IllegalStateException(e.getMessage());
}
/*
return new SetterInjectionComponentAdapter(PersonBean.class, PersonBean.class, new Parameter[]{new ConstantParameter(
"Pico Container")});*/
}
protected Component prepDEF_verifyDoesNotInstantiate(Container yan) {
yan.registerValue("Pico Container");
try{
return Components.ctor(DeadBody.class).bean();
}
catch(java.beans.IntrospectionException e){
throw new IllegalStateException(e.getMessage());
}
/*
yan.registerComponentInstance("Pico Container");
return new SetterInjectionComponentAdapter(DeadBody.class, DeadBody.class, new Parameter[]{ComponentParameter.DEFAULT});
*/
}
protected Component prepDEF_visitable() {
try{
return Components.bean(Components.ctor(PersonBean.class))
.withProperty("name", Components.value("Pico Container"));
}
catch(java.beans.IntrospectionException e){
throw new IllegalStateException(e.getMessage());
}
/*
return new SetterInjectionComponentAdapter(PersonBean.class, PersonBean.class, new Parameter[]{new ConstantParameter(
"Pico Container")});*/
}
protected Component prepSER_isSerializable(Container yan) {
try{
yan.registerValue("Pico Container");
return Components.bean(Components.ctor(PersonBean.class));
}
catch(java.beans.IntrospectionException e){
throw new IllegalStateException(e.getMessage());
}
/*
yan.registerComponentInstance("Pico Container");
return new SetterInjectionComponentAdapter(PersonBean.class, PersonBean.class, new Parameter[]{ComponentParameter.DEFAULT});
*/
}
protected Component prepSER_isXStreamSerializable(Container yan) {
return prepSER_isSerializable(yan);
}
protected void prepDEF_isAbleToTakeParameters(Container yan) {
yan.registerValue("Pico Container");
yan.registerConstructor(PersonBean.class);
try{
yan.registerComponent(PersonBean.class,
Components.ctor(MoneyPurse.class).bean()
.withProperty("money", Components.value(new Double(100)))
);
}
catch(java.beans.IntrospectionException e){
throw new IllegalStateException(e.getMessage());
}
/*
yan.registerComponentInstance("Pico Container");
yan.registerComponentImplementation(PersonBean.class);
return yan.registerComponent(new SetterInjectionComponentAdapter(
PurseBean.class, MoneyPurse.class, new Parameter[]{
ComponentParameter.DEFAULT, new ConstantParameter(new Double(100.0))}));
*/
}
public static class MoneyPurse
extends PurseBean {
double money;
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
protected Component prepVER_verificationFails(Container yan) {
yan.registerValue("Pico Container");
yan.registerConstructor(PersonBean.class);
try{
yan.registerComponent(PersonBean.class,
Components.bean(Components.ctor(MoneyPurse.class))
);
return yan.getComponent(PersonBean.class);
}
catch(java.beans.IntrospectionException e){
throw new IllegalStateException(e.getMessage());
}
/*
yan.registerComponentInstance("Pico Container");
yan.registerComponentImplementation(PersonBean.class);
return yan.registerComponent(new SetterInjectionComponentAdapter(
PurseBean.class, MoneyPurse.class, new Parameter[]{ComponentParameter.DEFAULT}));
*/
}
protected Component prepINS_createsNewInstances(Container yan) {
yan.registerValue("Pico Container");
try{
return Components.bean(Components.ctor(PersonBean.class));
}
catch(java.beans.IntrospectionException e){
throw new IllegalStateException(e.getMessage());
}
/*yan.registerComponentInstance("Pico Container");
return new SetterInjectionComponentAdapter(PersonBean.class, PersonBean.class, new Parameter[]{ComponentParameter.DEFAULT});
*/
}
public static class Ghost
extends PersonBean {
public Ghost() {
throw new VerifyError("test");
}
}
protected Component prepINS_errorIsRethrown(Container yan) {
yan.registerValue("Pico Container");
try{
return Components.bean(Ghost.class);
}
catch(java.beans.IntrospectionException e){
throw new IllegalStateException(e.getMessage());
}
/*
yan.registerComponentInstance("Pico Container");
return new SetterInjectionComponentAdapter(Ghost.class, Ghost.class, new Parameter[]{ComponentParameter.DEFAULT});
*/
}
public static class DeadBody
extends PersonBean {
public DeadBody() throws Exception {
throw new RuntimeException("test");
}
}
protected Component prepINS_runtimeExceptionIsRethrown(Container yan) {
yan.registerValue("Pico Container");
try{
return Components.bean(Components.ctor(DeadBody.class));
}
catch(java.beans.IntrospectionException e){
throw new IllegalStateException(e.getMessage());
}
/*
yan.registerComponentInstance("Pico Container");
return new SetterInjectionComponentAdapter(DeadBody.class, DeadBody.class, new Parameter[]{ComponentParameter.DEFAULT});
*/
}
public static class HidingPersion
extends PersonBean {
public HidingPersion() throws Exception {
throw new Exception("test");
}
}
protected Component prepINS_normalExceptionIsRethrownInsidePicoInvocationTargetInitializationException(
Container yan) {
yan.registerValue("Pico Container");
try{
return Components.bean(Components.ctor(HidingPersion.class));
}
catch(java.beans.IntrospectionException e){
throw new IllegalStateException(e.getMessage());
}/*
yan.registerComponentInstance("Pico Container");
return new SetterInjectionComponentAdapter(
HidingPersion.class, HidingPersion.class, new Parameter[]{ComponentParameter.DEFAULT});
*/
}
protected Component prepRES_dependenciesAreResolved(Container yan) {
yan.registerValue("Pico Container");
yan.registerConstructor(PersonBean.class);
try{
return Components.bean(PurseBean.class);
}
catch(java.beans.IntrospectionException e){
throw new IllegalStateException(e.getMessage());
}
/*
yan.registerComponentInstance("Pico Container");
yan.registerComponentImplementation(PersonBean.class);
return new SetterInjectionComponentAdapter(PurseBean.class, PurseBean.class, new Parameter[]{ComponentParameter.DEFAULT});
*/
}
public static class WealthyPerson
extends PersonBean {
PurseBean purse;
public PurseBean getPurse() {
return purse;
}
public void setPurse(PurseBean purse) {
this.purse = purse;
}
}
protected Component prepRES_failingVerificationWithCyclicDependencyException(Container yan) {
yan.registerValue("Pico Container");
yan.registerConstructor(PersonBean.class, WealthyPerson.class);
try{
yan.registerComponent(PurseBean.class,
Components.bean(Components.ctor(PurseBean.class)));
return yan.getComponent(PurseBean.class);
}
catch(java.beans.IntrospectionException e){
throw new IllegalStateException(e.getMessage());
}/*
yan.registerComponentInstance("Pico Container");
yan.registerComponentImplementation(PersonBean.class, WealthyPerson.class);
return yan.registerComponent(new SetterInjectionComponentAdapter(
PurseBean.class, PurseBean.class, new Parameter[]{ComponentParameter.DEFAULT}));
*/
}
protected void prepRES_failingInstantiationWithCyclicDependencyException(Container yan) {
yan.registerValue("Pico Container");
yan.registerConstructor(PersonBean.class, WealthyPerson.class);
try{
yan.registerComponent(PurseBean.class,
Components.bean(Components.ctor(PurseBean.class)));
}
catch(java.beans.IntrospectionException e){
throw new IllegalStateException(e.getMessage());
}
/*
yan.registerComponentInstance("Pico Container");
yan.registerComponentImplementation(PersonBean.class, WealthyPerson.class);
return yan.registerComponent(new SetterInjectionComponentAdapter(
PurseBean.class, PurseBean.class, new Parameter[]{ComponentParameter.DEFAULT}));
*/
}
public static class A {
private B b;
private String string = "default";
private List list = new java.util.ArrayList();
public void setB(B b) {
this.b = b;
}
public B getB() {
return b;
}
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
public static class B {
}
public void testAllUnsatisfiableDependenciesAreSignalled()
throws IntrospectionException{
Component aAdapter =
Components.bean(A.class);
//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());
}
public void testVerifyAllUnsatisfiableDependenciesAreSignalled()
throws IntrospectionException{
Component aAdapter =
Components.bean(A.class);
//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));
}
}
public static class C {
private B b;
private List l;
private final boolean asBean;
public C() {
asBean = true;
}
public C(B b) {
this.l = new ArrayList();
this.b = b;
asBean = false;
}
public void setB(B b) {
this.b = b;
}
public B getB() {
return b;
}
public void setList(List l) {
this.l = l;
}
public List getList() {
return l;
}
public boolean instantiatedAsBean() {
return asBean;
}
}
public void testHybridBeans()
throws IntrospectionException{
Component bAdapter =Components.bean(B.class);
//new Component("b", B.class, null);
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());
}
public interface IYin{
public void setYang(IYang yang);
public IYang getYang();
}
public interface IYang{
public void setYin(IYin yang);
public IYin getYin();
}
public static class Yin implements IYin {
private IYang yang;
public void setYang(IYang yang) {
this.yang = yang;
}
public IYang getYang() {
return yang;
}
}
public static class Yang implements IYang{
private IYin yin;
public void setYin(IYin yin) {
this.yin = yin;
}
public IYin getYin() {
return yin;
}
}
// 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){
e.printResolutionTrace();
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());
}
public void testShouldBeAbleToHandleMutualDependenciesWithProxy()
throws IntrospectionException{
Container yan = new ProxyContainer();
/*new DefaultContainer(new CachingComponentAdapterFactory(
new SetterInjectionComponentAdapterFactory()));*/
yan.registerComponent(Components.bean(Yin.class, new String[]{"yang"})
.withArgument(1, Components.value("bad ying"))
);
yan.registerComponent(Components.bean(Yang.class)
.withProperty("nonexistent", Components.value("bad yang"))
.singleton(new ThreadLocalScope())
);
yan.verify();
IYin yin = (IYin) yan.getInstanceOfType(IYin.class);
IYang yang = (IYang) yan.getInstanceOfType(IYang.class);
assertNotSame(yin, yang.getYin());
assertNotSame(yang, yin.getYang());
}
public static Integer getNullString(){return null;}
public void testNullBean(){
}
}