MentaContainer

Compare Revisions

Ignore whitespace Rev 96 → Rev 97

/trunk/src/test/java/org/mentacontainer/DependencyTest.java
File deleted
\ No newline at end of file
/trunk/src/test/java/org/mentacontainer/impl/ClassFactoryTest.java
30,7 → 30,7
@Test
public void testFactory() {
Container container = new MentaContainer();
MentaContainer container = new MentaContainer();
Factory c1 = new ClassFactory(container, User.class).addInitValue("saoj");
63,7 → 63,7
@Test
public void testDependable1() {
Container c = new MentaContainer();
MentaContainer c = new MentaContainer();
c.ioc("myString", String.class).addInitValue("hello");
92,7 → 92,7
@Test
public void testDependable2() {
Container c = new MentaContainer();
MentaContainer c = new MentaContainer();
c.ioc("myString", String.class).addInitValue("hello");
118,7 → 118,7
@Test
public void testPrimitivePropertyValue1() {
Container container = new MentaContainer();
MentaContainer container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj2.class).addPropertyValue("integer", 20);
130,7 → 130,7
@Test
public void testPrimitivePropertyValue2() {
Container container = new MentaContainer();
MentaContainer container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj2.class).addPropertyValue("integer", new Integer(20));
153,7 → 153,7
@Test
public void testPrimitivePropertyValue3() {
Container container = new MentaContainer();
MentaContainer container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj2_1.class).addPropertyValue("integer", 20);
165,7 → 165,7
@Test
public void testPrimitivePropertyValue4() {
Container container = new MentaContainer();
MentaContainer container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj2_1.class).addPropertyValue("integer", new Integer(20));
187,7 → 187,7
@Test
public void testPrimitiveInitValue1() {
Container container = new MentaContainer();
MentaContainer container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj3.class).addInitValue(20);
208,7 → 208,7
@Test
public void testPrimitiveInitValue2() {
Container container = new MentaContainer();
MentaContainer container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj3_1.class).addInitValue(20);
231,7 → 231,7
@Test
public void testPrimitiveInitValue3() {
Container container = new MentaContainer();
MentaContainer container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj3_2.class).addInitValue(20).addInitPrimitive(30L);
259,7 → 259,7
@Test
public void testMixInitValues() {
Container container = new MentaContainer();
MentaContainer container = new MentaContainer();
Factory f = new ClassFactory(container, TestObj4.class).addInitPrimitive(20).addInitValue("hello").addInitValue(20L).addInitPrimitive(20L);
/trunk/src/test/java/org/mentacontainer/example/CoreExamplesTest.java
New file
0,0 → 1,198
package org.mentacontainer.example;
 
import java.util.Calendar;
import java.util.Date;
 
import junit.framework.Assert;
 
import org.junit.Test;
import org.mentacontainer.Container;
import org.mentacontainer.impl.MentaContainer;
 
public class CoreExamplesTest {
@Test
public void testBeanInitialization() {
Container c = new MentaContainer();
c.ioc("myString1", String.class);
String myString1 = c.get("myString1");
Assert.assertEquals("", myString1); // default constructor...
c.ioc("myString2", String.class).addInitValue("saoj");
String myString2 = c.get("myString2"); // using constructor....
Assert.assertEquals("saoj", myString2);
Assert.assertNotSame(c.get("myString1"), c.get("myString1")); // most be different instances...
// test setters...
c.ioc("myDate1", Date.class).addPropertyValue("hours", 15) // setHours(15)
.addPropertyValue("minutes", 10) // setMinutes(10)
.addPropertyValue("seconds", 45); // setSeconds(45)
Date myDate1 = c.get("myDate1");
Assert.assertTrue(myDate1.toString().indexOf("15:10:45") > 0);
// test setter together with constructor...
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 1976);
c.ioc("myDate2", Date.class).addInitValue(cal.getTimeInMillis()).addPropertyValue("seconds", 59);
Date myDate2 = c.get("myDate2");
Assert.assertTrue(myDate2.toString().indexOf(":59") > 0);
Assert.assertTrue(myDate2.toString().indexOf("1976") > 0);
}
@Test
public void testDependencies() {
// constructor dependency...
Container c = new MentaContainer();
c.ioc("username", String.class).addInitValue("saoj");
 
c.ioc("myString", String.class).addInitDependency("username");
String myString = c.get("myString");
Assert.assertEquals("saoj", myString);
// setter dependency...
c.ioc("myBirthdayYear", Integer.class).addInitValue(76);
c.ioc("myBirthday", Date.class).addPropertyDependency("year", "myBirthdayYear").addPropertyValue("month", 0).addPropertyValue("date", 20);
Date myBirthday = c.get("myBirthday");
Assert.assertTrue(myBirthday.toString().indexOf("Jan 20") > 0);
Assert.assertTrue(myBirthday.toString().indexOf("1976") > 0);
// both...
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 1976);
 
c.ioc("timeInMillis", Long.class).addInitValue(cal.getTimeInMillis());
c.ioc("myBirthdayMonth", Integer.class).addInitValue(0);
c.ioc("myBirthday", Date.class).addInitDependency("timeInMillis").addPropertyDependency("month", "myBirthdayMonth").addPropertyValue("date", 20);
myBirthday = c.get("myBirthday");
Assert.assertTrue(myBirthday.toString().indexOf("Jan 20") > 0);
Assert.assertTrue(myBirthday.toString().indexOf("1976") > 0);
}
@Test
public void testAutoWiring() {
// constructor dependency...
Container c = new MentaContainer();
c.ioc("username", String.class).addInitValue("saoj");
 
c.ioc("myString", String.class);
c.autowireByConstructor("username");
String myString = c.get("myString");
Assert.assertEquals("saoj", myString);
// setter dependency...
c = new MentaContainer();
c.ioc("myBirthdayYear", Integer.class).addInitValue(76);
c.ioc("myBirthday", Date.class).addPropertyValue("month", 0).addPropertyValue("date", 20);
c.autowireBySetter("year", "myBirthdayYear");
Date myBirthday = c.get("myBirthday");
Assert.assertTrue(myBirthday.toString().indexOf("Jan 20") > 0);
Assert.assertTrue(myBirthday.toString().indexOf("1976") > 0);
// both...
c = new MentaContainer();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 1976);
 
c.ioc("timeInMillis", Long.class).addInitValue(cal.getTimeInMillis());
c.ioc("myBirthdayMonth", Integer.class).addInitValue(0);
c.ioc("myBirthday", Date.class).addPropertyValue("date", 20);
c.autowireByConstructor("timeInMillis");
c.autowireBySetter("month", "myBirthdayMonth");
myBirthday = c.get("myBirthday");
Assert.assertTrue(myBirthday.toString().indexOf("Jan 20") > 0);
Assert.assertTrue(myBirthday.toString().indexOf("1976") > 0);
// bypass autowireByConstructor by specifying...
c = new MentaContainer();
cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 1976);
 
Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.YEAR, 1977);
c.ioc("timeInMillis", Long.class).addInitValue(cal.getTimeInMillis());
c.ioc("timeInMillis2", Long.class).addInitValue(cal2.getTimeInMillis());
c.ioc("myBirthdayMonth", Integer.class).addInitValue(0);
c.ioc("myBirthday", Date.class).addPropertyValue("date", 20).addInitDependency("timeInMillis2");
c.autowireByConstructor("timeInMillis");
c.autowireBySetter("month", "myBirthdayMonth");
myBirthday = c.get("myBirthday");
Assert.assertTrue(myBirthday.toString().indexOf("Jan 20") > 0);
Assert.assertTrue(myBirthday.toString().indexOf("1977") > 0);
// bypass autowireBySetter by specifying...
// not supported yet...
// force zero arguments constructor...
c = new MentaContainer();
cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 1976);
 
c.ioc("timeInMillis", Long.class).addInitValue(cal.getTimeInMillis());
c.ioc("myBirthdayMonth", Integer.class).addInitValue(0);
c.ioc("myBirthday", Date.class).addPropertyValue("date", 20).useZeroArgumentConstructor();
c.autowireByConstructor("timeInMillis");
c.autowireBySetter("month", "myBirthdayMonth");
myBirthday = c.get("myBirthday");
Assert.assertTrue(myBirthday.toString().indexOf("Jan 20") > 0);
Assert.assertTrue(myBirthday.toString().indexOf("1976") == -1);
}
}