001: /*
002: * User: Michael Rettig
003: * Date: Aug 10, 2002
004: * Time: 1:14:59 PM
005: */
006: package net.sourceforge.jaxor.tests;
007:
008: import net.sourceforge.jaxor.EntityNotFoundException;
009: import net.sourceforge.jaxor.LazyLoader;
010: import net.sourceforge.jaxor.LazyProxy;
011: import net.sourceforge.jaxor.example.domain.*;
012: import net.sourceforge.jaxor.example.tests.MultiTableTestCase;
013: import net.sourceforge.jaxor.util.SystemException;
014:
015: import java.util.ArrayList;
016: import java.util.List;
017:
018: public class LazyProxyTest extends MultiTableTestCase {
019:
020: protected List getRows() {
021: List list = new ArrayList();
022: list.add(new AddressMetaRow());
023: list.add(new CustomerMetaRow());
024: return list;
025: }
026:
027: public void testResolutionWithImplClass() {
028: attemptLoad(AddressBase.class, new MyLazyLoader());
029: }
030:
031: public void testResolutionWithEntityClass() {
032: try {
033: attemptLoad(AddressEntity.class, new MyLazyLoader());
034: fail("should not work");
035: } catch (ClassCastException expected) {
036: //Does not work b/c the interfaces are pulled from the interface
037: }
038: }
039:
040: public void testSystemException() {
041: final SystemException exc = new SystemException("failed");
042: MyLazyLoader loader = new MyLazyLoader() {
043: public Object resolve() {
044: throw exc;
045: }
046: };
047:
048: try {
049: attemptLoad(AddressBase.class, loader);
050: } catch (SystemException e) {
051: assertTrue(e == exc);
052: }
053: }
054:
055: public void testNotUsingProxy() {
056: try {
057: AddressFinder.selectByPrimaryKey(new Long(342));
058: fail("should not be found");
059: } catch (EntityNotFoundException exc) {
060: }
061: }
062:
063: public void testUsingProxy() {
064: AddressFinder.selectByPrimaryKey(new Long(342), true);
065: }
066:
067: public void testUsingAProxyWithAnImplObject() {
068: LazyLoader loader = new LazyLoader() {
069: protected Object resolve() {
070: return new AddressImpl() {
071: public String getCity() {
072: return "lazy city";
073: }
074: };
075: }
076: };
077: AddressEntity theAddress = (AddressEntity) LazyProxy.create(
078: loader, AddressImpl.class);
079: assertNotNull(theAddress);
080: assertEquals("lazy city", theAddress.getCity());
081: }
082:
083: public void testWrapper() {
084: try {
085: CustomerFinder.selectByPrimaryKey(new Long(123));
086: fail("Should not be found");
087: } catch (EntityNotFoundException expected) {
088: }
089: }
090:
091: private void attemptLoad(Class clzz, MyLazyLoader loader) {
092: AddressEntity entity = (AddressEntity) LazyProxy.create(loader,
093: clzz);
094: assertEquals(loader.resolvedCount, 0);
095: assertNotNull(entity);
096: assertEquals(new Long(123), entity.getAddressId());
097: assertEquals(loader.resolvedCount, 1);
098: assertEquals(new Long(123), entity.getAddressId());
099: assertEquals(loader.resolvedCount, 1);
100: }
101:
102: private class MyLazyLoader extends LazyLoader {
103: public int resolvedCount = 0;
104:
105: public Object resolve() {
106: resolvedCount++;
107: return ObjectFactory.createAddress(new Long(123));
108: }
109: }
110: }
|