MentaContainer

Compare Revisions

Ignore whitespace Rev 25 → Rev 26

/trunk/src/test/java/org/mentacontainer/impl/MentaContainerTest.java
88,4 → 88,71
 
c.autowire("myThread", Runnable.class);
}
public static interface MyDAO {
public Object getSomething();
}
public static class Connection {
private final String name;
public Connection(String name) {
this.name = name;
}
public String toString() {
return getClass().getName() + ": " + name;
}
public String getName() { return name; }
}
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 get something...
return conn.toString(); // it cannot be null!
}
}
@Test
public void testAutoWiring() {
Container c = new MentaContainer();
c.ioc("myDAO", JdbcMyDAO.class);
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 */);
MyDAO 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());
Assert.assertNotNull(myDAO.getSomething());
}
}