1 15 package org.apache.hivemind.service.impl; 16 17 import hivemind.test.services.ServiceAutowireTarget; 18 import hivemind.test.services.StringHolder; 19 import hivemind.test.services.impl.StringHolderImpl; 20 21 import java.util.ArrayList ; 22 import java.util.List ; 23 24 import org.apache.hivemind.impl.DefaultErrorHandler; 25 import org.apache.hivemind.internal.RegistryInfrastructure; 26 import org.apache.hivemind.service.Autowiring; 27 import org.apache.hivemind.service.AutowiringStrategy; 28 import org.apache.hivemind.test.HiveMindTestCase; 29 import org.easymock.MockControl; 30 31 36 public class TestAutowiring extends HiveMindTestCase 37 { 38 42 public void testStrategies() 43 { 44 MockControl strategy1Control = newControl(AutowiringStrategy.class); 46 AutowiringStrategy strategy1 = (AutowiringStrategy) strategy1Control.getMock(); 47 48 AutowiringStrategy strategy2 = new AutowiringByTypeStrategy(); 50 51 MockControl strategy3Control = newControl(AutowiringStrategy.class); 53 AutowiringStrategy strategy3 = (AutowiringStrategy) strategy3Control.getMock(); 54 55 List strategyContributions = new ArrayList (); 56 AutowiringStrategyContribution contrib1 = new AutowiringStrategyContribution(strategy1, 57 "strategy1", null, null); 58 strategyContributions.add(contrib1); 59 AutowiringStrategyContribution contrib2 = new AutowiringStrategyContribution(strategy2, 60 "strategy2", null, null); 61 strategyContributions.add(contrib2); 62 AutowiringStrategyContribution contrib3 = new AutowiringStrategyContribution(strategy3, 63 "strategy3", null, null); 64 strategyContributions.add(contrib3); 65 66 MockControl registryControl = newControl(RegistryInfrastructure.class); 67 RegistryInfrastructure registry = (RegistryInfrastructure) registryControl.getMock(); 68 69 ServiceAutowireTarget target = new ServiceAutowireTarget(); 70 71 strategy1.autowireProperty(registry, target, "stringHolder"); 73 strategy1Control.setReturnValue(false); 74 75 registry.containsService(StringHolder.class, null); 76 registryControl.setReturnValue(true); 77 78 registry.getService(StringHolder.class, null); 79 registryControl.setReturnValue(new StringHolderImpl()); 80 81 replayControls(); 82 83 Autowiring autowiring = new AutowiringImpl(registry, strategyContributions, new DefaultErrorHandler()); 84 autowiring.autowireProperties(target, new String [] {"stringHolder"}); 85 86 verifyControls(); 87 88 assertNotNull(target.getStringHolder()); 89 } 90 91 94 public void testAutowireSingleStrategy() 95 { 96 MockControl strategy1Control = newControl(AutowiringStrategy.class); 98 AutowiringStrategy strategy1 = (AutowiringStrategy) strategy1Control.getMock(); 99 100 MockControl strategy2Control = newControl(AutowiringStrategy.class); 101 AutowiringStrategy strategy2 = (AutowiringStrategy) strategy2Control.getMock(); 102 103 List strategyContributions = new ArrayList (); 104 AutowiringStrategyContribution contrib1 = new AutowiringStrategyContribution(strategy1, 105 "strategy1", null, null); 106 strategyContributions.add(contrib1); 107 AutowiringStrategyContribution contrib2 = new AutowiringStrategyContribution(strategy2, 108 "strategy2", null, null); 109 strategyContributions.add(contrib2); 110 111 ServiceAutowireTarget target = new ServiceAutowireTarget(); 112 113 MockControl registryControl = newControl(RegistryInfrastructure.class); 114 RegistryInfrastructure registry = (RegistryInfrastructure) registryControl.getMock(); 115 116 strategy1.autowireProperty(registry, target, "stringHolder"); 118 strategy1Control.setReturnValue(false); 119 120 replayControls(); 121 122 Autowiring autowiring = new AutowiringImpl(registry, strategyContributions, new DefaultErrorHandler()); 123 autowiring.autowireProperties("strategy1", target); 124 125 verifyControls(); 126 127 } 128 129 132 public void testSkipPrimitives() 133 { 134 MockControl strategy1Control = newControl(AutowiringStrategy.class); 135 AutowiringStrategy strategy1 = (AutowiringStrategy) strategy1Control.getMock(); 136 137 List strategyContributions = new ArrayList (); 138 AutowiringStrategyContribution contrib1 = new AutowiringStrategyContribution(strategy1, 139 "strategy1", null, null); 140 strategyContributions.add(contrib1); 141 142 Object target = new SkippedPropertiesAutowireTarget(); 143 144 MockControl registryControl = newControl(RegistryInfrastructure.class); 145 RegistryInfrastructure registry = (RegistryInfrastructure) registryControl.getMock(); 146 147 replayControls(); 148 149 Autowiring autowiring = new AutowiringImpl(registry, strategyContributions, new DefaultErrorHandler()); 150 autowiring.autowireProperties(target); 151 152 verifyControls(); 153 154 } 155 156 159 public void testSkipNotNull() 160 { 161 MockControl strategy1Control = newControl(AutowiringStrategy.class); 162 AutowiringStrategy strategy1 = (AutowiringStrategy) strategy1Control.getMock(); 163 164 List strategyContributions = new ArrayList (); 165 AutowiringStrategyContribution contrib1 = new AutowiringStrategyContribution(strategy1, 166 "strategy1", null, null); 167 strategyContributions.add(contrib1); 168 169 ServiceAutowireTarget target = new ServiceAutowireTarget(); 170 target.setStringHolder(new StringHolderImpl()); 171 172 MockControl registryControl = newControl(RegistryInfrastructure.class); 173 RegistryInfrastructure registry = (RegistryInfrastructure) registryControl.getMock(); 174 175 replayControls(); 176 177 Autowiring autowiring = new AutowiringImpl(registry, strategyContributions, new DefaultErrorHandler()); 178 autowiring.autowireProperties(target); 179 180 verifyControls(); 181 182 } 183 184 } 185 | Popular Tags |