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.tcspring;
005:
006: import org.jmock.Mock;
007: import org.jmock.MockObjectTestCase;
008: import org.springframework.beans.factory.support.AbstractBeanDefinition;
009: import org.springframework.beans.factory.support.ChildBeanDefinition;
010: import org.springframework.beans.factory.support.RootBeanDefinition;
011:
012: import com.tc.object.bytecode.Manager;
013: import com.tc.object.bytecode.hook.DSOContext;
014: import com.tc.object.config.DSOSpringConfigHelper;
015: import com.tcspring.DistributableBeanFactoryMixin.ManagerUtilWrapper;
016: import com.tcspring.beans.SimpleBean;
017: import com.tcspring.beans.SimpleBean1;
018: import com.tcspring.beans.SimpleBean2;
019: import com.tcspring.beans.SimpleParentBean;
020: import com.tcspring.beans.SimplePropertyBean;
021: import com.tcspring.events.BaseEvent;
022: import com.tcspring.events.ChildEvent;
023:
024: import java.util.Collections;
025: import java.util.HashMap;
026: import java.util.HashSet;
027: import java.util.Map;
028: import java.util.Set;
029:
030: public class DistributableBeanFactoryMixinTest extends
031: MockObjectTestCase {
032: private String appName = "testApp";
033: private DSOContext dsoContext;
034: private Mock dsoContextMock;
035:
036: private DistributableBeanFactoryMixin distributableBeanFactoryMixin;
037: private Mock mockSpringConfigHelper;
038: private DSOSpringConfigHelper springConfigHelper;
039: private Mock mockManagerUtilWrapper;
040: private Set nonDistributables = new HashSet();
041:
042: private HashMap singletonCache;
043:
044: private Object localBean;
045: private Object distributedBean;
046:
047: protected void setUp() {
048: dsoContextMock = mock(DSOContext.class);
049: dsoContext = (DSOContext) dsoContextMock.proxy();
050:
051: mockManagerUtilWrapper = new Mock(
052: DistributableBeanFactoryMixin.ManagerUtilWrapper.class);
053: ManagerUtilWrapper managerUtilWrapper = (ManagerUtilWrapper) mockManagerUtilWrapper
054: .proxy();
055: distributableBeanFactoryMixin = new DistributableBeanFactoryMixin(
056: appName, dsoContext, managerUtilWrapper,
057: nonDistributables);
058:
059: mockSpringConfigHelper = new Mock(DSOSpringConfigHelper.class);
060: springConfigHelper = (DSOSpringConfigHelper) mockSpringConfigHelper
061: .proxy();
062:
063: singletonCache = new HashMap();
064:
065: // tcClassMock = new Mock(TCClass.class);
066: // tcClass = (TCClass) tcClassMock.proxy();
067: //
068: // tcSuperClassMock = new Mock(TCClass.class);
069: //
070: // tcObjectMock = new Mock(TCObject.class);
071: //
072: // tcFieldMock = new Mock(TCField.class);
073: // tcField = (TCField) tcFieldMock.proxy();
074: //
075: // tcObject = (TCObject) tcObjectMock.proxy();
076: //
077: // localBean = new TestObject();
078: // distributedBean = new TestObject();
079:
080: }
081:
082: public void testGetBeanClassName() {
083: AbstractBeanDefinition rootDefinition = new RootBeanDefinition();
084: Map beanMap = new HashMap();
085:
086: assertNull(DistributableBeanFactoryMixin.getBeanClassName(
087: rootDefinition, beanMap));
088:
089: beanMap.put("parent1", rootDefinition);
090:
091: AbstractBeanDefinition childDefinition = new ChildBeanDefinition(
092: "parent1");
093: assertNull(DistributableBeanFactoryMixin.getBeanClassName(
094: childDefinition, beanMap));
095:
096: rootDefinition.setBeanClassName("class1");
097: assertEquals("class1", DistributableBeanFactoryMixin
098: .getBeanClassName(rootDefinition, beanMap));
099: assertEquals("class1", DistributableBeanFactoryMixin
100: .getBeanClassName(childDefinition, beanMap));
101: }
102:
103: public void testAddDistributedEvents() {
104: String eventClass = "com.tcspring.events.ChildEvent";
105: String baseEventClass = "com.tcspring.events.BaseEvent";
106:
107: dsoContextMock.expects(once()).method("addInclude") //
108: .with(eq(eventClass), ANYTHING,
109: eq("* " + eventClass + ".*(..)"), ANYTHING);
110: dsoContextMock.expects(once()).method("addInclude") //
111: .with(eq(baseEventClass), ANYTHING,
112: eq("* " + baseEventClass + ".*(..)"), ANYTHING);
113: distributableBeanFactoryMixin
114: .registerDistributedEvents(Collections
115: .singletonList(eventClass));
116: }
117:
118: public void testAddDistributedEventsWithWildcard() {
119: String eventClass = "com.tcspring.events.*Event"; // XXX we don't support this kind of wildcards!
120: distributableBeanFactoryMixin
121: .registerDistributedEvents(Collections
122: .singletonList(eventClass));
123: }
124:
125: public void testRegisterBeanDefinitions() {
126: distributableBeanFactoryMixin.addLocation("config/foo.xml");
127:
128: dsoContextMock.expects(once()).method(
129: "getDSOSpringConfigHelpers").withNoArguments().will(
130: returnValue(Collections
131: .singletonList(springConfigHelper)));
132: mockSpringConfigHelper.expects(atLeastOnce()).method(
133: "isMatchingApplication").with(eq("testApp")).will(
134: returnValue(true));
135: mockSpringConfigHelper.expects(atLeastOnce()).method(
136: "isMatchingConfig").with(eq("config/foo.xml")).will(
137: returnValue(true));
138: mockSpringConfigHelper.expects(atLeastOnce()).method(
139: "getDistributedEvents").withNoArguments().will(
140: returnValue(Collections.singletonList(ChildEvent.class
141: .getName())));
142: mockSpringConfigHelper.expects(atLeastOnce()).method(
143: "getDistributedBeans").withNoArguments().will(
144: returnValue(Collections.singletonMap("bean",
145: Collections.singleton("transientX"))));
146:
147: expectsAddInclude(SimplePropertyBean.class.getName());
148: expectsAddInclude(SimpleBean.class.getName());
149: expectsAddInclude(SimpleParentBean.class.getName());
150: expectsAddInclude(ChildEvent.class.getName());
151: expectsAddInclude(BaseEvent.class.getName());
152: expectsAddInclude(SimpleBean1.class.getName());
153: expectsAddInclude(SimpleBean2.class.getName());
154:
155: dsoContextMock.expects(once()).method("addTransient").with(
156: eq(SimplePropertyBean.class.getName()),
157: eq("transientX"));
158:
159: mockManagerUtilWrapper.expects(once()).method("beginLock")
160: .with(ANYTHING, eq(Manager.LOCK_TYPE_WRITE));
161: mockManagerUtilWrapper.expects(once()).method(
162: "lookupOrCreateRoot").with(ANYTHING, ANYTHING).will(
163: returnValue(singletonCache));
164: mockManagerUtilWrapper.expects(once()).method("commitLock")
165: .with(ANYTHING);
166:
167: mockSpringConfigHelper.expects(atLeastOnce()).method(
168: "getRootName").will(returnValue(null));
169: mockSpringConfigHelper.expects(atLeastOnce()).method(
170: "isLocationInfoEnabled").will(returnValue(false));
171:
172: distributableBeanFactoryMixin
173: .registerBeanDefinitions(Collections.singletonMap(
174: "bean", new RootBeanDefinition(
175: SimplePropertyBean.class)));
176: }
177:
178: private void expectsAddInclude(String name) {
179: dsoContextMock.expects(once()).method("addInclude").with(
180: eq(name), ANYTHING, eq(("* " + name + ".*(..)")),
181: ANYTHING);
182: }
183:
184: public void testIsDistibutedBean_yes() {
185: testRegisterBeanDefinitions();
186: mockSpringConfigHelper.expects(once()).method(
187: "isDistributedBean").with(eq("bean")).will(
188: returnValue(true));
189: assertTrue(distributableBeanFactoryMixin
190: .isDistributedBean("bean"));
191: }
192:
193: public void testIsDistibutedBean_no() {
194: testRegisterBeanDefinitions();
195: mockSpringConfigHelper.expects(once()).method(
196: "isDistributedBean").with(eq("someOtherBean")).will(
197: returnValue(false));
198: assertFalse(distributableBeanFactoryMixin
199: .isDistributedBean("someOtherBean"));
200: }
201:
202: public void testIsDistributedField_yes() {
203: testRegisterBeanDefinitions();
204: mockSpringConfigHelper.expects(once()).method(
205: "isDistributedField").with(eq("bean"), eq("fieldA"))
206: .will(returnValue(true));
207: assertTrue(distributableBeanFactoryMixin.isDistributedField(
208: "bean", "fieldA"));
209: }
210:
211: public void testIsDistributedField_no() {
212: testRegisterBeanDefinitions();
213: mockSpringConfigHelper.expects(once()).method(
214: "isDistributedField").with(eq("bean"), eq("fieldB"))
215: .will(returnValue(false));
216: assertFalse(distributableBeanFactoryMixin.isDistributedField(
217: "bean", "fieldB"));
218: }
219:
220: public void testIsDistributedEvent_yes() {
221: testRegisterBeanDefinitions();
222: mockSpringConfigHelper.expects(once()).method(
223: "isDistributedEvent").with(eq("event1")).will(
224: returnValue(true));
225: assertTrue(distributableBeanFactoryMixin
226: .isDistributedEvent("event1"));
227: }
228:
229: public void testIsDistributedEvent_no() {
230: testRegisterBeanDefinitions();
231: mockSpringConfigHelper.expects(once()).method(
232: "isDistributedEvent").with(eq("event2")).will(
233: returnValue(false));
234: assertFalse(distributableBeanFactoryMixin
235: .isDistributedEvent("event2"));
236:
237: }
238:
239: // public void testCopyTransientField_NotPortableNotDistributed() throws IllegalAccessException {
240: // tcClassMock.expects(once()).method("getSuperclass").will(returnValue(null));
241: // tcClassMock.expects(once()).method("getField").with(eq(ClassWithIntField.class.getName() + ".foo"))
242: // .will(returnValue(tcField));
243: // tcFieldMock.expects(once()).method("isPortable").will(returnValue(false));
244: // // beanFactoryMock.expects(once()).method("isDistributedField").with(eq(beanName),
245: // // eq("foo")).will(returnValue(false));
246: //
247: // ClassWithIntField sourceBean = new ClassWithIntField(99);
248: // ClassWithIntField targetBean = new ClassWithIntField(-1);
249: //
250: // identifiable.copyTransientFields(beanName, sourceBean, targetBean, ClassWithIntField.class, tcClass);
251: //
252: // assertEquals(99, targetBean.foo);
253: // }
254: //
255: // public void testCopyTransientField_PortableNotDistributed() throws IllegalAccessException {
256: // tcClassMock.expects(once()).method("getSuperclass").will(returnValue(null));
257: // tcClassMock.expects(once()).method("getField").with(eq(ClassWithIntField.class.getName() + ".foo"))
258: // .will(returnValue(tcField));
259: // tcFieldMock.expects(once()).method("isPortable").will(returnValue(true));
260: // // identifiableMock.expects(once()).method("isDistributedField").with(eq(beanName), eq("foo"))
261: // // .will(returnValue(false));
262: //
263: // ClassWithIntField sourceBean = new ClassWithIntField(99);
264: // ClassWithIntField targetBean = new ClassWithIntField(-1);
265: //
266: // identifiable.copyTransientFields(beanName, sourceBean, targetBean, ClassWithIntField.class, tcClass);
267: //
268: // assertEquals(99, targetBean.foo);
269: // }
270: //
271: // public void testCopyTransientField_dsoTransientDistributed() throws IllegalAccessException {
272: // tcClassMock.expects(once()).method("getSuperclass").will(returnValue(null));
273: // tcClassMock.expects(once()).method("getField").with(eq(ClassWithIntField.class.getName() + ".foo"))
274: // .will(returnValue(null));
275: // // beanFactoryMock.expects(once()).method("isDistributedField").with(eq(beanName),
276: // // eq("foo")).will(returnValue(true));
277: //
278: // ClassWithIntField sourceBean = new ClassWithIntField(98);
279: // ClassWithIntField targetBean = new ClassWithIntField(-1);
280: //
281: // identifiable.copyTransientFields(beanName, sourceBean, targetBean, ClassWithIntField.class, tcClass);
282: //
283: // assertEquals(98, targetBean.foo);
284: // }
285: //
286: // public void testCopyTransientField_dsoTransientNonDistributed() throws IllegalAccessException {
287: // tcClassMock.expects(once()).method("getSuperclass").will(returnValue(null));
288: // tcClassMock.expects(once()).method("getField").with(eq(ClassWithIntField.class.getName() + ".foo"))
289: // .will(returnValue(null));
290: // // beanFactoryMock.expects(once()).method("isDistributedField").with(eq(beanName),
291: // // eq("foo")).will(returnValue(false));
292: //
293: // ClassWithIntField sourceBean = new ClassWithIntField(97);
294: // ClassWithIntField targetBean = new ClassWithIntField(-1);
295: //
296: // identifiable.copyTransientFields(beanName, sourceBean, targetBean, ClassWithIntField.class, tcClass);
297: //
298: // assertEquals(97, targetBean.foo);
299: // }
300: //
301: // public void testCopyTransientField_PortableNonDistributed() throws IllegalAccessException {
302: // tcClassMock.expects(once()).method("getSuperclass").will(returnValue(null));
303: // tcClassMock.expects(once()).method("getField").with(eq(ClassWithIntField.class.getName() + ".foo"))
304: // .will(returnValue(tcField));
305: // tcFieldMock.expects(once()).method("isPortable").will(returnValue(true));
306: //
307: // ClassWithIntField sourceBean = new ClassWithIntField(96);
308: // ClassWithIntField targetBean = new ClassWithIntField(-1);
309: //
310: // identifiable.copyTransientFields(beanName, sourceBean, targetBean, ClassWithIntField.class, tcClass);
311: //
312: // assertEquals(96, targetBean.foo);
313: // }
314: //
315: // public void testCopyTransientField_final() throws IllegalAccessException {
316: // tcClassMock.expects(once()).method("getSuperclass").will(returnValue(null));
317: // identifiable.copyTransientFields(beanName, new Object(), new Object(), ClassWithFinalField.class, tcClass);
318: // }
319: //
320: // public void testCopyTransientField_static() throws IllegalAccessException {
321: // tcClassMock.expects(once()).method("getSuperclass").will(returnValue(null));
322: // identifiable.copyTransientFields(beanName, new Object(), new Object(), ClassWithStaticField.class, tcClass);
323: // }
324: //
325: // public void testCopyTransientField_tcField() throws IllegalAccessException {
326: // tcClassMock.expects(once()).method("getSuperclass").will(returnValue(null));
327: // identifiable.copyTransientFields(beanName, new Object(), new Object(), ClassWithTCField.class, tcClass);
328: // }
329: //
330: // public void testCopyTransientField_distributed() throws IllegalAccessException {
331: // testRegisterBeanDefinitions();
332: //
333: // tcClassMock.expects(once()).method("getSuperclass").will(returnValue(null));
334: // tcClassMock.expects(once()).method("getField").with(eq(ClassWithIntField.class.getName() + ".foo"))
335: // .will(returnValue(tcField));
336: // tcFieldMock.expects(once()).method("isPortable").will(returnValue(true));
337: // mockSpringConfigHelper.expects(once()).method("isDistributedField").with(eq(beanName), eq("foo"))
338: // .will(returnValue(true));
339: //
340: // identifiable.copyTransientFields(beanName, new Object(), new Object(), ClassWithIntField.class, tcClass);
341: // }
342:
343: static class ClassWithTCField {
344: static int _tc_foo = 1;
345: }
346:
347: class ClassWithFinalField {
348: final int foo = 1;
349: }
350:
351: static class ClassWithStaticField {
352: static int foo = 1;
353: }
354:
355: class ClassWithIntField {
356: int foo;
357:
358: public ClassWithIntField(int foo) {
359: this .foo = foo;
360: }
361: }
362:
363: // class TestObject implements Manageable {
364: //
365: // public void __tc_managed(TCObject t) {
366: // throw new RuntimeException();
367: //
368: // }
369: //
370: // public TCObject __tc_managed() {
371: // return tcObject;
372: // }
373: //
374: // }
375:
376: // public void testVirtualizeSingletonBeanWhenNotYetShared() {
377: // testRegisterBeanDefinitions();
378: //
379: // // mockSpringConfigHelper.expects(once()).method("isDistributedBean").with(eq("beanName")).will(returnValue(true));
380: //
381: // ComplexBeanId beanId = new ComplexBeanId("beanName");
382: //
383: // distributableBeanFactoryMixin.virtualizeBean(beanId, localBean, container);
384: // assertSame(localBean, result);
385: // assertSame(localBean, distributableBeanFactoryMixin.getBeanContainer(beanId).getBean());
386: // }
387:
388: // public void testVirtualizeSingletonBeanWhenAlreadyShared() {
389: // testRegisterBeanDefinitions();
390: //
391: // ComplexBeanId beanId = new ComplexBeanId("beanName");
392: //
393: // singletonCache.put(beanId, new BeanContainer(distributedBean, true));
394: //
395: // // mockSpringConfigHelper.expects(once()).method("isDistributedBean").with(eq("beanName")).will(returnValue(true));
396: //
397: // distributableBeanFactoryMixin.virtualizeBean(beanId, localBean, container);
398: // assertSame(distributedBean, result);
399: // assertSame(distributedBean, distributableBeanFactoryMixin.getBeanContainer(beanId).getBean());
400: // }
401:
402: // public void testVirtualizeSingletonBeanWhenNotDistributed() {
403: // testRegisterBeanDefinitions();
404: //
405: // ComplexBeanId beanId = new ComplexBeanId("beanName");
406: //
407: // // mockSpringConfigHelper.expects(once()).method("isDistributedBean").with(eq("beanName")).will(returnValue(false));
408: //
409: // Object result = distributableBeanFactoryMixin.virtualizeBean(beanId, localBean);
410: // assertSame(localBean, result);
411: // assertNull(distributableBeanFactoryMixin.getBeanContainer(beanId));
412: // }
413:
414: // TODO - mock the monitor code
415: }
|