Rev 41 |
Rev 57 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
package org.mentacontainer.impl;
import junit.framework.Assert;
import org.junit.Test;
import org.mentacontainer.Container;
public class MentaContainerTest
{
@Test
public void testSimpleGet
() {
Container c =
new MentaContainer
();
c.
ioc("myStr",
String.
class);
Assert.
assertEquals("", c.
get("myStr"));
String s1 = c.
get("myStr");
String s2 = c.
get("myStr");
Assert.
assertTrue(s1
!= s2
);
Assert.
assertTrue(s1.
equals(s2
));
}
@Test
public void testConstructorInit
() {
Container c =
new MentaContainer
();
c.
ioc("myStr",
String.
class).
addInitValue("hello");
Assert.
assertEquals("hello", c.
get("myStr"));
String s1 = c.
get("myStr");
String s2 = c.
get("myStr");
Assert.
assertTrue(s1
!= s2
);
Assert.
assertTrue(s1.
equals(s2
));
c.
ioc("anotherStr",
String.
class).
addInitValue("hi");
String s3 = c.
get("anotherStr");
Assert.
assertTrue(s1
!= s3
);
Assert.
assertFalse(s1.
equals(s3
));
}
@Test
public void testSingleton
() {
Container c =
new MentaContainer
();
c.
ioc("myStr",
String.
class,
true).
addInitValue("hello");
Assert.
assertEquals("hello", c.
get("myStr"));
String s1 = c.
get("myStr");
String s2 = c.
get("myStr");
Assert.
assertTrue(s1 == s2
);
Assert.
assertTrue(s1.
equals(s2
));
}
public static interface MyDAO
{
public Object getSomething
();
}
public static class SomeDependency
{
private String name
;
public SomeDependency
() { }
public void setName
(String name
) {
this.
name = name
;
}
public String getName
() {
return name
;
}
}
public static class Connection {
private final String name
;
private SomeDependency dep
;
public Connection(String name
) {
this.
name = name
;
}
public String toString
() {
return getClass
().
getName() +
": " + name
;
}
public String getName
() { return name
; }
public void setMyDep
(SomeDependency dep
) { this.
dep = dep
; }
public SomeDependency getMyDep
() { return dep
; }
}
public static class JdbcMyDAO
implements MyDAO
{
private Connection conn
;
public void setConn
(Connection conn
) { this.
conn = conn
; }
public Connection getConn
() { return conn
; }
public Object getSomething
() {
// use the connection to do something...
Assert.
assertNotNull(conn
); // it cannot be null!
// also test if the connection also received the myDep dependency...
Assert.
assertNotNull(conn.
getMyDep());
return conn.
toString();
}
}
private Container getConfiguredContainer
() {
Container c =
new MentaContainer
();
c.
ioc("myDAO", JdbcMyDAO.
class);
c.
ioc("aDependency", SomeDependency.
class,
true /* singleton */).
addProperty("name",
"A super dependency!");
c.
ioc("connection",
Connection.
class).
addInitValue("A super JDBC connection!");
c.
autowire("conn" /* the property name that will receive the dependency */,
Connection.
class /* the type of the property that will receive the dependency */,
"connection" /* the source inside the container */);
c.
autowire("myDep" /* notice this is the setter of the Connection class */,
SomeDependency.
class /* the type - it could be an interface for better decoupling */,
"aDependency" /* again this is the name of the bean/component inside the container */);
return c
;
}
@Test
public void testAutoWiring
() {
Container c = getConfiguredContainer
();
MyDAO myDAO = c.
get("myDAO");
// check implementation...
Assert.
assertEquals(JdbcMyDAO.
class, myDAO.
getClass());
// check dependency...
Connection conn =
((JdbcMyDAO
) myDAO
).
getConn();
Assert.
assertNotNull(conn
);
Assert.
assertEquals("A super JDBC connection!", conn.
getName());
// check dependency of dependency...
Assert.
assertEquals(c.
get("aDependency"), conn.
getMyDep());
Assert.
assertTrue(c.
get("aDependency") == conn.
getMyDep()); // singleton!
// check DAO can do its job...
Assert.
assertNotNull(myDAO.
getSomething());
}
public static class SomeAction
{
private MyDAO myDAO =
null;
public void setMyDAO
(MyDAO myDAO
) {
this.
myDAO = myDAO
;
}
public MyDAO getMyDAO
() { return myDAO
; }
}
public void testPopulate
() {
Container c = getConfiguredContainer
();
SomeAction a =
new SomeAction
();
c.
populate(a
); // great... properties of SomeAction were populated by container...
// let's check if myDAO was injected...
Assert.
assertNotNull(a.
getMyDAO());
// also check if myDAO was corrected wired...
Connection conn =
((JdbcMyDAO
) a.
getMyDAO()).
getConn();
Assert.
assertNotNull(conn
);
// check if conn was also wired...
Assert.
assertNotNull(conn.
getMyDep());
}
}