001: package net.sourceforge.jaxor.example.pico;
002:
003: import net.sourceforge.jaxor.JaxorContextImpl;
004: import net.sourceforge.jaxor.MetaRow;
005: import net.sourceforge.jaxor.impl.MapperRegistryImpl;
006: import net.sourceforge.jaxor.api.*;
007: import net.sourceforge.jaxor.db.SingleConnectionTransaction;
008: import net.sourceforge.jaxor.example.domain.AddressEntity;
009: import net.sourceforge.jaxor.example.domain.AddressFinderBase;
010: import net.sourceforge.jaxor.example.domain.AddressMetaRow;
011: import net.sourceforge.jaxor.example.domain.Logger;
012: import net.sourceforge.jaxor.example.tests.TableTestCase;
013: import net.sourceforge.jaxor.impl.InstanceCacheImpl;
014: import net.sourceforge.jaxor.impl.UnitOfWorkImpl;
015: import net.sourceforge.jaxor.util.MethodCache;
016: import org.picocontainer.MutablePicoContainer;
017: import org.picocontainer.PicoContainer;
018: import org.picocontainer.defaults.DefaultPicoContainer;
019:
020: import java.sql.Connection;
021:
022: /**
023: * Created By: Mike
024: * Date: Jan 15, 2004
025: * Time: 7:19:49 PM
026: *
027: * Last Checkin: $Author: mrettig $
028: * Date: $Date: 2004/02/03 01:50:15 $
029: * Revision: $Revision: 1.12 $
030: */
031: public class PicoTest extends TableTestCase {
032:
033: protected MetaRow getRow() {
034: return new AddressMetaRow();
035: }
036:
037: public void testConfiguration() {
038: MutablePicoContainer container = configureContainer();
039: PicoContainer pico = container;
040:
041: PicoInstanceFactory fact = (PicoInstanceFactory) pico
042: .getComponentInstance(PicoInstanceFactory.class);
043: assertTrue(fact.getContainer() == pico);
044: JaxorContext jaxorContext = (JaxorContext) pico
045: .getComponentInstance(JaxorContext.class);
046:
047: AddressFinderBase finder = (AddressFinderBase) jaxorContext
048: .getFinder(AddressFinderBase.class);
049: assertTrue(finder == (AddressFinderBase) jaxorContext
050: .getFinder(AddressFinderBase.class));
051: AddressEntity address = finder.newInstance(new Long(123));
052: assertNotNull(
053: "Logger should be passed to the entity through the instance factory",
054: address.getLog());
055: assertTrue(pico.getComponentInstance(Logger.class) == address
056: .getLog());
057: address.setCity("Chicago");
058: address.setStreet("899 Madison");
059: address.setZipCode("60661");
060: address.setState("IL");
061:
062: //this will stop the container, committing the object to the db and closes the connection.
063: container.stop();
064:
065: //We'll simulate another session by creating a new container.
066: MutablePicoContainer otherContainer2 = configureContainer();
067: PicoContainer picoContainer2 = otherContainer2;
068: JaxorContext jaxorContext2 = (JaxorContext) picoContainer2
069: .getComponentInstance(JaxorContext.class);
070: AddressFinderBase finder2 = (AddressFinderBase) jaxorContext2
071: .getFinder(AddressFinderBase.class);
072: AddressEntity addressEntity = finder2
073: .selectByAddressId(new Long(123));
074: assertEquals(new Long(123), addressEntity.getAddressId());
075: assertEquals("mrettig", address.getInsertUser());
076:
077: otherContainer2.stop();
078: }
079:
080: private MutablePicoContainer configureContainer() {
081: MutablePicoContainer pico = new DefaultPicoContainer();
082:
083: pico.registerComponentImplementation(JaxorContainer.class);
084: pico.registerComponentImplementation(JaxorContext.class,
085: JaxorContextImpl.class);
086: pico.registerComponentImplementation(UnitOfWork.class,
087: UnitOfWorkImpl.class);
088: pico.registerComponentImplementation(InstanceCache.class,
089: InstanceCacheImpl.class);
090: pico.registerComponentImplementation(QueryCache.class,
091: MethodCache.class);
092: pico.registerComponentImplementation(JaxorTransaction.class,
093: SingleConnectionTransaction.class);
094: pico.registerComponentImplementation(MapperRegistry.class,
095: MapperRegistryImpl.class);
096:
097: //the instance factory uses pico to configure and create new entities and finders.
098: pico.registerComponentImplementation(PicoInstanceFactory.class);
099:
100: pico.registerComponentImplementation(Logger.class);
101:
102: //The testcase sets up the connection, so we'll reuse it here.
103: //The prep for the testcase creates the table using this connection.
104: Connection conn = getConnection();
105: pico.registerComponentInstance(conn);
106: //pico.registerComponentInstance(pico);
107: JaxorContext context = (JaxorContext) pico
108: .getComponentInstance(JaxorContext.class);
109: //this is the context user. We use this for setting the auditing fields (insert/update user) in the db.
110: //pico has support for setting bean properties, but we'll just set the property here for simplicity.
111: context.setUser("mrettig");
112: pico.start();
113: return pico;
114: }
115: }
|