KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tcspring > DistributableBeanFactoryMixinTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tcspring;
5
6 import org.jmock.Mock;
7 import org.jmock.MockObjectTestCase;
8 import org.springframework.beans.factory.support.AbstractBeanDefinition;
9 import org.springframework.beans.factory.support.ChildBeanDefinition;
10 import org.springframework.beans.factory.support.RootBeanDefinition;
11
12 import com.tc.object.bytecode.Manager;
13 import com.tc.object.bytecode.hook.DSOContext;
14 import com.tc.object.config.DSOSpringConfigHelper;
15 import com.tcspring.DistributableBeanFactoryMixin.ManagerUtilWrapper;
16 import com.tcspring.beans.SimpleBean;
17 import com.tcspring.beans.SimpleBean1;
18 import com.tcspring.beans.SimpleBean2;
19 import com.tcspring.beans.SimpleParentBean;
20 import com.tcspring.beans.SimplePropertyBean;
21 import com.tcspring.events.BaseEvent;
22 import com.tcspring.events.ChildEvent;
23
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29
30 public class DistributableBeanFactoryMixinTest extends MockObjectTestCase {
31   private String JavaDoc appName = "testApp";
32   private DSOContext dsoContext;
33   private Mock dsoContextMock;
34
35   private DistributableBeanFactoryMixin distributableBeanFactoryMixin;
36   private Mock mockSpringConfigHelper;
37   private DSOSpringConfigHelper springConfigHelper;
38   private Mock mockManagerUtilWrapper;
39   private Set JavaDoc nonDistributables = new HashSet JavaDoc();
40
41   private HashMap JavaDoc singletonCache;
42
43   private Object JavaDoc localBean;
44   private Object JavaDoc distributedBean;
45
46   protected void setUp() {
47     dsoContextMock = mock(DSOContext.class);
48     dsoContext = (DSOContext) dsoContextMock.proxy();
49
50     mockManagerUtilWrapper = new Mock(DistributableBeanFactoryMixin.ManagerUtilWrapper.class);
51     ManagerUtilWrapper managerUtilWrapper = (ManagerUtilWrapper) mockManagerUtilWrapper.proxy();
52     distributableBeanFactoryMixin = new DistributableBeanFactoryMixin(appName, dsoContext, managerUtilWrapper, nonDistributables);
53
54     mockSpringConfigHelper = new Mock(DSOSpringConfigHelper.class);
55     springConfigHelper = (DSOSpringConfigHelper) mockSpringConfigHelper.proxy();
56
57     singletonCache = new HashMap JavaDoc();
58
59     // tcClassMock = new Mock(TCClass.class);
60
// tcClass = (TCClass) tcClassMock.proxy();
61
//
62
// tcSuperClassMock = new Mock(TCClass.class);
63
//
64
// tcObjectMock = new Mock(TCObject.class);
65
//
66
// tcFieldMock = new Mock(TCField.class);
67
// tcField = (TCField) tcFieldMock.proxy();
68
//
69
// tcObject = (TCObject) tcObjectMock.proxy();
70
//
71
// localBean = new TestObject();
72
// distributedBean = new TestObject();
73

74   }
75
76   public void testGetBeanClassName() {
77     AbstractBeanDefinition rootDefinition = new RootBeanDefinition();
78     Map JavaDoc beanMap = new HashMap JavaDoc();
79
80     assertNull(DistributableBeanFactoryMixin.getBeanClassName(rootDefinition, beanMap));
81
82     beanMap.put("parent1", rootDefinition);
83
84     AbstractBeanDefinition childDefinition = new ChildBeanDefinition("parent1");
85     assertNull(DistributableBeanFactoryMixin.getBeanClassName(childDefinition, beanMap));
86
87     rootDefinition.setBeanClassName("class1");
88     assertEquals("class1", DistributableBeanFactoryMixin.getBeanClassName(rootDefinition, beanMap));
89     assertEquals("class1", DistributableBeanFactoryMixin.getBeanClassName(childDefinition, beanMap));
90   }
91
92   public void testAddDistributedEvents() {
93     String JavaDoc eventClass = "com.tcspring.events.ChildEvent";
94     String JavaDoc baseEventClass = "com.tcspring.events.BaseEvent";
95
96     dsoContextMock.expects(once()).method("addInclude")
97         .with(eq(eventClass), ANYTHING, eq("* " + eventClass + ".*(..)"));
98     dsoContextMock.expects(once()).method("addInclude").with(eq(baseEventClass), ANYTHING,
99                                                              eq("* " + baseEventClass + ".*(..)"));
100     distributableBeanFactoryMixin.registerDistributedEvents(Collections.singletonList(eventClass));
101   }
102
103   public void testAddDistributedEventsWithWildcard() {
104     String JavaDoc eventClass = "com.tcspring.events.*Event"; // XXX we don't support this kind of wildcards!
105
distributableBeanFactoryMixin.registerDistributedEvents(Collections.singletonList(eventClass));
106   }
107
108   public void testRegisterBeanDefinitions() {
109     distributableBeanFactoryMixin.addLocation("config/foo.xml");
110
111     dsoContextMock.expects(once()).method("getDSOSpringConfigHelpers").withNoArguments()
112         .will(returnValue(Collections.singletonList(springConfigHelper)));
113     mockSpringConfigHelper.expects(atLeastOnce()).method("isMatchingApplication").with(eq("testApp"))
114         .will(returnValue(true));
115     mockSpringConfigHelper.expects(atLeastOnce()).method("isMatchingConfig").with(eq("config/foo.xml"))
116         .will(returnValue(true));
117     mockSpringConfigHelper.expects(atLeastOnce()).method("getDistributedEvents").withNoArguments()
118         .will(returnValue(Collections.singletonList(ChildEvent.class.getName())));
119     mockSpringConfigHelper.expects(atLeastOnce()).method("getDistributedBeans").withNoArguments()
120         .will(returnValue(Collections.singletonMap("bean", Collections.singleton("transientX"))));
121
122     expectsAddInclude(SimplePropertyBean.class.getName());
123     expectsAddInclude(SimpleBean.class.getName());
124     expectsAddInclude(SimpleParentBean.class.getName());
125     expectsAddInclude(ChildEvent.class.getName());
126     expectsAddInclude(BaseEvent.class.getName());
127     expectsAddInclude(SimpleBean1.class.getName());
128     expectsAddInclude(SimpleBean2.class.getName());
129
130     dsoContextMock.expects(once()).method("addTransient").with(eq(SimplePropertyBean.class.getName()), eq("transientX"));
131
132     mockManagerUtilWrapper.expects(once()).method("beginLock").with(ANYTHING, eq(Manager.LOCK_TYPE_WRITE));
133     mockManagerUtilWrapper.expects(once()).method("lookupOrCreateRoot").with(ANYTHING, ANYTHING)
134         .will(returnValue(singletonCache));
135     mockManagerUtilWrapper.expects(once()).method("commitLock").with(ANYTHING);
136
137     mockSpringConfigHelper.expects(atLeastOnce()).method("getRootName").will(returnValue(null));
138     mockSpringConfigHelper.expects(atLeastOnce()).method("isLocationInfoEnabled").will(returnValue(false));
139     
140     distributableBeanFactoryMixin.registerBeanDefinitions(Collections.singletonMap("bean",
141                                                                   new RootBeanDefinition(SimplePropertyBean.class)));
142   }
143
144   private void expectsAddInclude(String JavaDoc name) {
145     dsoContextMock.expects(once()).method("addInclude").with(eq(name), ANYTHING, eq(("* " + name + ".*(..)")));
146   }
147
148   public void testIsDistibutedBean_yes() {
149     testRegisterBeanDefinitions();
150     mockSpringConfigHelper.expects(once()).method("isDistributedBean").with(eq("bean")).will(returnValue(true));
151     assertTrue(distributableBeanFactoryMixin.isDistributedBean("bean"));
152   }
153
154   public void testIsDistibutedBean_no() {
155     testRegisterBeanDefinitions();
156     mockSpringConfigHelper.expects(once()).method("isDistributedBean").with(eq("someOtherBean")).will(returnValue(false));
157     assertFalse(distributableBeanFactoryMixin.isDistributedBean("someOtherBean"));
158   }
159
160   public void testIsDistributedField_yes() {
161     testRegisterBeanDefinitions();
162     mockSpringConfigHelper.expects(once()).method("isDistributedField").with(eq("bean"), eq("fieldA"))
163         .will(returnValue(true));
164     assertTrue(distributableBeanFactoryMixin.isDistributedField("bean", "fieldA"));
165   }
166
167   public void testIsDistributedField_no() {
168     testRegisterBeanDefinitions();
169     mockSpringConfigHelper.expects(once()).method("isDistributedField").with(eq("bean"), eq("fieldB"))
170         .will(returnValue(false));
171     assertFalse(distributableBeanFactoryMixin.isDistributedField("bean", "fieldB"));
172   }
173
174   public void testIsDistributedEvent_yes() {
175     testRegisterBeanDefinitions();
176     mockSpringConfigHelper.expects(once()).method("isDistributedEvent").with(eq("event1")).will(returnValue(true));
177     assertTrue(distributableBeanFactoryMixin.isDistributedEvent("event1"));
178   }
179
180   public void testIsDistributedEvent_no() {
181     testRegisterBeanDefinitions();
182     mockSpringConfigHelper.expects(once()).method("isDistributedEvent").with(eq("event2")).will(returnValue(false));
183     assertFalse(distributableBeanFactoryMixin.isDistributedEvent("event2"));
184
185   }
186
187   // public void testCopyTransientField_NotPortableNotDistributed() throws IllegalAccessException {
188
// tcClassMock.expects(once()).method("getSuperclass").will(returnValue(null));
189
// tcClassMock.expects(once()).method("getField").with(eq(ClassWithIntField.class.getName() + ".foo"))
190
// .will(returnValue(tcField));
191
// tcFieldMock.expects(once()).method("isPortable").will(returnValue(false));
192
// // beanFactoryMock.expects(once()).method("isDistributedField").with(eq(beanName),
193
// // eq("foo")).will(returnValue(false));
194
//
195
// ClassWithIntField sourceBean = new ClassWithIntField(99);
196
// ClassWithIntField targetBean = new ClassWithIntField(-1);
197
//
198
// identifiable.copyTransientFields(beanName, sourceBean, targetBean, ClassWithIntField.class, tcClass);
199
//
200
// assertEquals(99, targetBean.foo);
201
// }
202
//
203
// public void testCopyTransientField_PortableNotDistributed() throws IllegalAccessException {
204
// tcClassMock.expects(once()).method("getSuperclass").will(returnValue(null));
205
// tcClassMock.expects(once()).method("getField").with(eq(ClassWithIntField.class.getName() + ".foo"))
206
// .will(returnValue(tcField));
207
// tcFieldMock.expects(once()).method("isPortable").will(returnValue(true));
208
// // identifiableMock.expects(once()).method("isDistributedField").with(eq(beanName), eq("foo"))
209
// // .will(returnValue(false));
210
//
211
// ClassWithIntField sourceBean = new ClassWithIntField(99);
212
// ClassWithIntField targetBean = new ClassWithIntField(-1);
213
//
214
// identifiable.copyTransientFields(beanName, sourceBean, targetBean, ClassWithIntField.class, tcClass);
215
//
216
// assertEquals(99, targetBean.foo);
217
// }
218
//
219
// public void testCopyTransientField_dsoTransientDistributed() throws IllegalAccessException {
220
// tcClassMock.expects(once()).method("getSuperclass").will(returnValue(null));
221
// tcClassMock.expects(once()).method("getField").with(eq(ClassWithIntField.class.getName() + ".foo"))
222
// .will(returnValue(null));
223
// // beanFactoryMock.expects(once()).method("isDistributedField").with(eq(beanName),
224
// // eq("foo")).will(returnValue(true));
225
//
226
// ClassWithIntField sourceBean = new ClassWithIntField(98);
227
// ClassWithIntField targetBean = new ClassWithIntField(-1);
228
//
229
// identifiable.copyTransientFields(beanName, sourceBean, targetBean, ClassWithIntField.class, tcClass);
230
//
231
// assertEquals(98, targetBean.foo);
232
// }
233
//
234
// public void testCopyTransientField_dsoTransientNonDistributed() throws IllegalAccessException {
235
// tcClassMock.expects(once()).method("getSuperclass").will(returnValue(null));
236
// tcClassMock.expects(once()).method("getField").with(eq(ClassWithIntField.class.getName() + ".foo"))
237
// .will(returnValue(null));
238
// // beanFactoryMock.expects(once()).method("isDistributedField").with(eq(beanName),
239
// // eq("foo")).will(returnValue(false));
240
//
241
// ClassWithIntField sourceBean = new ClassWithIntField(97);
242
// ClassWithIntField targetBean = new ClassWithIntField(-1);
243
//
244
// identifiable.copyTransientFields(beanName, sourceBean, targetBean, ClassWithIntField.class, tcClass);
245
//
246
// assertEquals(97, targetBean.foo);
247
// }
248
//
249
// public void testCopyTransientField_PortableNonDistributed() throws IllegalAccessException {
250
// tcClassMock.expects(once()).method("getSuperclass").will(returnValue(null));
251
// tcClassMock.expects(once()).method("getField").with(eq(ClassWithIntField.class.getName() + ".foo"))
252
// .will(returnValue(tcField));
253
// tcFieldMock.expects(once()).method("isPortable").will(returnValue(true));
254
//
255
// ClassWithIntField sourceBean = new ClassWithIntField(96);
256
// ClassWithIntField targetBean = new ClassWithIntField(-1);
257
//
258
// identifiable.copyTransientFields(beanName, sourceBean, targetBean, ClassWithIntField.class, tcClass);
259
//
260
// assertEquals(96, targetBean.foo);
261
// }
262
//
263
// public void testCopyTransientField_final() throws IllegalAccessException {
264
// tcClassMock.expects(once()).method("getSuperclass").will(returnValue(null));
265
// identifiable.copyTransientFields(beanName, new Object(), new Object(), ClassWithFinalField.class, tcClass);
266
// }
267
//
268
// public void testCopyTransientField_static() throws IllegalAccessException {
269
// tcClassMock.expects(once()).method("getSuperclass").will(returnValue(null));
270
// identifiable.copyTransientFields(beanName, new Object(), new Object(), ClassWithStaticField.class, tcClass);
271
// }
272
//
273
// public void testCopyTransientField_tcField() throws IllegalAccessException {
274
// tcClassMock.expects(once()).method("getSuperclass").will(returnValue(null));
275
// identifiable.copyTransientFields(beanName, new Object(), new Object(), ClassWithTCField.class, tcClass);
276
// }
277
//
278
// public void testCopyTransientField_distributed() throws IllegalAccessException {
279
// testRegisterBeanDefinitions();
280
//
281
// tcClassMock.expects(once()).method("getSuperclass").will(returnValue(null));
282
// tcClassMock.expects(once()).method("getField").with(eq(ClassWithIntField.class.getName() + ".foo"))
283
// .will(returnValue(tcField));
284
// tcFieldMock.expects(once()).method("isPortable").will(returnValue(true));
285
// mockSpringConfigHelper.expects(once()).method("isDistributedField").with(eq(beanName), eq("foo"))
286
// .will(returnValue(true));
287
//
288
// identifiable.copyTransientFields(beanName, new Object(), new Object(), ClassWithIntField.class, tcClass);
289
// }
290

291   static class ClassWithTCField {
292     static int _tc_foo = 1;
293   }
294
295   class ClassWithFinalField {
296     final int foo = 1;
297   }
298
299   static class ClassWithStaticField {
300     static int foo = 1;
301   }
302
303   class ClassWithIntField {
304     int foo;
305
306     public ClassWithIntField(int foo) {
307       this.foo = foo;
308     }
309   }
310
311   // class TestObject implements Manageable {
312
//
313
// public void __tc_managed(TCObject t) {
314
// throw new RuntimeException();
315
//
316
// }
317
//
318
// public TCObject __tc_managed() {
319
// return tcObject;
320
// }
321
//
322
// }
323

324 // public void testVirtualizeSingletonBeanWhenNotYetShared() {
325
// testRegisterBeanDefinitions();
326
//
327
// // mockSpringConfigHelper.expects(once()).method("isDistributedBean").with(eq("beanName")).will(returnValue(true));
328
//
329
// ComplexBeanId beanId = new ComplexBeanId("beanName");
330
//
331
// distributableBeanFactoryMixin.virtualizeBean(beanId, localBean, container);
332
// assertSame(localBean, result);
333
// assertSame(localBean, distributableBeanFactoryMixin.getBeanContainer(beanId).getBean());
334
// }
335

336 // public void testVirtualizeSingletonBeanWhenAlreadyShared() {
337
// testRegisterBeanDefinitions();
338
//
339
// ComplexBeanId beanId = new ComplexBeanId("beanName");
340
//
341
// singletonCache.put(beanId, new BeanContainer(distributedBean, true));
342
//
343
// // mockSpringConfigHelper.expects(once()).method("isDistributedBean").with(eq("beanName")).will(returnValue(true));
344
//
345
// distributableBeanFactoryMixin.virtualizeBean(beanId, localBean, container);
346
// assertSame(distributedBean, result);
347
// assertSame(distributedBean, distributableBeanFactoryMixin.getBeanContainer(beanId).getBean());
348
// }
349

350 // public void testVirtualizeSingletonBeanWhenNotDistributed() {
351
// testRegisterBeanDefinitions();
352
//
353
// ComplexBeanId beanId = new ComplexBeanId("beanName");
354
//
355
// // mockSpringConfigHelper.expects(once()).method("isDistributedBean").with(eq("beanName")).will(returnValue(false));
356
//
357
// Object result = distributableBeanFactoryMixin.virtualizeBean(beanId, localBean);
358
// assertSame(localBean, result);
359
// assertNull(distributableBeanFactoryMixin.getBeanContainer(beanId));
360
// }
361

362   // TODO - mock the monitor code
363
}
364
Popular Tags