KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > service > impl > TestAutowiring


1 // Copyright 2007 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

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 JavaDoc;
22 import java.util.List JavaDoc;
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 /**
32  * Tests related to {@link Autowiring} and {@link AutowiringStrategy}.
33  *
34  * @author Achim Huegen
35  */

36 public class TestAutowiring extends HiveMindTestCase
37 {
38     /**
39      * Ensures that all configured strategies are called in the configured order
40      * until one signals successful wiring.
41      */

42     public void testStrategies()
43     {
44         // Configure an instance of AutowiringImpl with three strategies
45
MockControl strategy1Control = newControl(AutowiringStrategy.class);
46         AutowiringStrategy strategy1 = (AutowiringStrategy) strategy1Control.getMock();
47         
48         // This strategy is expected to be called and wire successfully
49
AutowiringStrategy strategy2 = new AutowiringByTypeStrategy();
50
51         // This strategy is not expected to be called since the previous one is successful
52
MockControl strategy3Control = newControl(AutowiringStrategy.class);
53         AutowiringStrategy strategy3 = (AutowiringStrategy) strategy3Control.getMock();
54         
55         List JavaDoc strategyContributions = new ArrayList JavaDoc();
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         // Training
72
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 JavaDoc[] {"stringHolder"});
85
86         verifyControls();
87
88         assertNotNull(target.getStringHolder());
89     }
90     
91     /**
92      * Tests the wiring by specifying an explicit strategy
93      */

94     public void testAutowireSingleStrategy()
95     {
96         // Configure an instance of AutowiringImpl with two strategies
97
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 JavaDoc strategyContributions = new ArrayList JavaDoc();
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         // Training
117
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     /**
130      * Checks that it is not tried to wire primitives and strings
131      */

132     public void testSkipPrimitives()
133     {
134         MockControl strategy1Control = newControl(AutowiringStrategy.class);
135         AutowiringStrategy strategy1 = (AutowiringStrategy) strategy1Control.getMock();
136         
137         List JavaDoc strategyContributions = new ArrayList JavaDoc();
138         AutowiringStrategyContribution contrib1 = new AutowiringStrategyContribution(strategy1,
139                 "strategy1", null, null);
140         strategyContributions.add(contrib1);
141
142         Object JavaDoc 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     /**
157      * Checks that it is not tried to wire properties which are assigned already
158      */

159     public void testSkipNotNull()
160     {
161         MockControl strategy1Control = newControl(AutowiringStrategy.class);
162         AutowiringStrategy strategy1 = (AutowiringStrategy) strategy1Control.getMock();
163         
164         List JavaDoc strategyContributions = new ArrayList JavaDoc();
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