1 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 ; 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.Map ; 28 import java.util.Set ; 29 30 public class DistributableBeanFactoryMixinTest extends MockObjectTestCase { 31 private String 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 nonDistributables = new HashSet (); 40 41 private HashMap singletonCache; 42 43 private Object localBean; 44 private Object 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 (); 58 59 74 } 75 76 public void testGetBeanClassName() { 77 AbstractBeanDefinition rootDefinition = new RootBeanDefinition(); 78 Map beanMap = new HashMap (); 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 eventClass = "com.tcspring.events.ChildEvent"; 94 String 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 eventClass = "com.tcspring.events.*Event"; 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 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 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 324 336 350 362 } 364
| Popular Tags
|