001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.object;
005:
006: import com.tc.exception.ImplementMe;
007: import com.tc.object.bytecode.TransparentAccess;
008: import com.tc.object.dna.api.DNA;
009: import com.tc.object.dna.api.DNAWriter;
010: import com.tc.object.field.TCField;
011: import com.tc.object.field.TCFieldFactory;
012: import com.tc.object.tx.optimistic.OptimisticTransactionManager;
013: import com.tc.objectserver.core.api.TestDNAWriter;
014:
015: import java.lang.reflect.Constructor;
016: import java.lang.reflect.Field;
017: import java.util.Collection;
018: import java.util.HashMap;
019: import java.util.Map;
020:
021: /**
022: * @author steve
023: */
024: public class TCObjectImplTest extends BaseDSOTestCase {
025:
026: public void tests() throws Exception {
027: TestClientObjectManager objectManager = new TestClientObjectManager();
028: TCClass clazz = new TestTCClass(objectManager);
029: TestObject to1 = new TestObject(null, null);
030: TestObject to2 = new TestObject("TestObject2", null);
031: ObjectID id1 = new ObjectID(1);
032: ObjectID id2 = new ObjectID(2);
033: objectManager.add(id2, new TCObjectPhysical(objectManager
034: .getReferenceQueue(), id2, to2, clazz));
035:
036: TCObjectImpl tcObj = new TCObjectPhysical(objectManager
037: .getReferenceQueue(), id1, to1, clazz);
038: tcObj.resolveReference(TestObject.class.getName() + ".test1");
039: tcObj.resolveReference(TestObject.class.getName() + ".test2");
040: assertTrue(to1.test1 == null);// nothing should happen from that
041: assertTrue(to1.test2 == null);
042:
043: tcObj.setReference(TestObject.class.getName() + ".test2",
044: new ObjectID(2));
045:
046: assertTrue(to1.test1 == null);// nothing should happen from that
047: assertTrue(to1.test2 == null);
048:
049: tcObj.resolveReference(TestObject.class.getName() + ".test2");
050: assertTrue(to1.test1 == null);// nothing should happen from that
051: assertTrue(to1.test2 == to2);
052:
053: tcObj.dehydrateIfNew(new TestDNAWriter());
054: tcObj.clearReferences(100);
055: assertTrue(to1.test2 == null);
056: tcObj.resolveReference(TestObject.class.getName() + ".test2");
057: assertTrue(to1.test2 == to2);
058: }
059:
060: private static class TestObject implements TransparentAccess {
061: public String test1;
062: public TestObject test2;
063:
064: public TCObject managed;
065:
066: public TestObject(String test1, TestObject test2) {
067: this .test1 = test1;
068: this .test2 = test2;
069: }
070:
071: public void __tc_getallfields(Map map) {
072: map.put(getClass().getName() + "." + "test1", test1);
073: map.put(getClass().getName() + "." + "test2", test2);
074: }
075:
076: public void __tc_setfield(String fieldName, Object value) {
077: if (fieldName.equals(TestObject.class.getName() + ".test1")) {
078: test1 = (String) value;
079: }
080: if (fieldName.equals(TestObject.class.getName() + ".test2")) {
081: test2 = (TestObject) value;
082: }
083: }
084:
085: public void __tc_managed(TCObject b) {
086: this .managed = b;
087: }
088:
089: public TCObject __tc_managed() {
090: return managed;
091: }
092:
093: public Object __tc_getmanagedfield(String name) {
094: throw new ImplementMe();
095: }
096:
097: public void __tc_setmanagedfield(String name, Object value) {
098: throw new ImplementMe();
099: }
100:
101: }
102:
103: public class TestTCClass implements TCClass {
104: private TCFieldFactory fieldFactory;
105: private Map fields = new HashMap();
106: private final TestClientObjectManager objectManager;
107:
108: public Field getParentField() {
109: return null;
110: }
111:
112: public String getParentFieldName() {
113: return "className.this$0";
114: }
115:
116: public TestTCClass(TestClientObjectManager objectManager)
117: throws Exception {
118: this .objectManager = objectManager;
119: fieldFactory = new TCFieldFactory(configHelper());
120: Field[] flds = TestObject.class.getDeclaredFields();
121: for (int i = 0; i < flds.length; i++) {
122: fields.put(TestObject.class.getName() + "."
123: + flds[i].getName(), fieldFactory.getInstance(
124: this , flds[i]));
125: }
126: }
127:
128: public TCField[] getPortableFields() {
129: Collection fs = fields.values();
130: return (TCField[]) fs.toArray(new TCField[fs.size()]);
131: }
132:
133: public Constructor getConstructor() throws SecurityException {
134: // TODO Auto-generated method stub
135: return null;
136: }
137:
138: public TCClass getSuperclass() {
139: // TODO Auto-generated method stub
140: return null;
141: }
142:
143: public boolean isPortable() {
144: // TODO Auto-generated method stub
145: return false;
146: }
147:
148: public TCField getDeclaredField(String name) {
149: try {
150: return (TCField) fields.get(name);
151: } catch (Exception e) {
152: throw new RuntimeException(e);
153: }
154: }
155:
156: public TCField getField(String name) {
157: return getDeclaredField(name);
158: }
159:
160: public TCField getField(String classname, String fieldname) {
161: // TODO Auto-generated method stub
162: return null;
163: }
164:
165: public TCField getField(Field field) {
166: // TODO Auto-generated method stub
167: return null;
168: }
169:
170: public boolean isIndexed() {
171: // TODO Auto-generated method stub
172: return false;
173: }
174:
175: public void hydrate(TCObject tcObject, DNA dna, Object pojo,
176: boolean force) {
177: //
178: }
179:
180: public void dehydrate(TCObject tcObject, DNAWriter writer,
181: Object pojo) {
182: //
183: }
184:
185: public String getName() {
186: return TestObject.class.getName();
187: }
188:
189: public Class getComponentType() {
190: return null;
191: }
192:
193: public TraversedReferences getPortableObjects(Object pojo,
194: TraversedReferences addTo) {
195: return addTo;
196: }
197:
198: public String getDefiningLoaderDescription() {
199: throw new ImplementMe();
200: }
201:
202: public boolean isNonStaticInner() {
203: return false;
204: }
205:
206: public boolean isLogical() {
207: return false;
208: }
209:
210: public TCObject createTCObject(ObjectID id, Object peer) {
211: throw new ImplementMe();
212: }
213:
214: public boolean hasOnLoadExecuteScript() {
215: return false;
216: }
217:
218: public String getOnLoadExecuteScript() {
219:
220: return null;
221: }
222:
223: public boolean hasOnLoadMethod() {
224: return false;
225: }
226:
227: public String getOnLoadMethod() {
228: return null;
229: }
230:
231: public boolean isUseNonDefaultConstructor() {
232: return false;
233: }
234:
235: public Object getNewInstanceFromNonDefaultConstructor(DNA dna) {
236: return null;
237: }
238:
239: public Class getPeerClass() {
240: throw new ImplementMe();
241: }
242:
243: public Map connectedCopy(Object source, Object dest,
244: Map visited, OptimisticTransactionManager txManager) {
245: throw new ImplementMe();
246: }
247:
248: public String getFieldNameByOffset(long fieldOffset) {
249: throw new ImplementMe();
250: }
251:
252: public ClientObjectManager getObjectManager() {
253: return objectManager;
254: }
255:
256: public boolean isProxyClass() {
257: return false;
258: }
259:
260: public String getExtendingClassName() {
261: return getName();
262: }
263:
264: public boolean isEnum() {
265: return false;
266: }
267:
268: public boolean isPortableField(long fieldOffset) {
269: throw new ImplementMe();
270: }
271: }
272: }
|