47
47
* @author huzijie
48
48
* @version ComponentManagerImplTests.java, v 0.1 2023年04月10日 3:37 PM huzijie Exp $
49
49
*/
50
- @ ExtendWith ({ MockitoExtension .class , OutputCaptureExtension .class })
50
+ @ ExtendWith ({MockitoExtension .class , OutputCaptureExtension .class })
51
51
public class ComponentManagerImplTests {
52
52
53
53
static {
54
54
LogOutPutUtils .openOutPutForLoggers (ComponentManagerImpl .class );
55
55
}
56
56
57
57
@ Mock
58
- private ClientFactoryInternal clientFactoryInternal ;
58
+ private ClientFactoryInternal clientFactoryInternal ;
59
59
60
60
@ Mock
61
- private DemoComponent componentInfoA ;
61
+ private DemoComponent componentInfoA ;
62
62
63
63
@ Mock
64
- private DemoComponent componentInfoB ;
64
+ private DemoComponent componentInfoB ;
65
65
66
- private ComponentManagerImpl componentManager ;
66
+ private ComponentManagerImpl componentManager ;
67
67
68
68
private final ClientFactoryInternal clientFactory = new ClientFactoryImpl ();
69
69
70
70
@ BeforeEach
71
71
public void setUp () {
72
72
componentManager = new ComponentManagerImpl (clientFactoryInternal , this .getClass ()
73
- .getClassLoader ());
73
+ .getClassLoader ());
74
74
}
75
75
76
76
@ Test
@@ -84,11 +84,11 @@ public void registerAndGetComponents() {
84
84
assertThat (componentManager .getComponents ()).contains (componentInfoA , componentInfoB );
85
85
assertThat (componentManager .getComponentTypes ()).containsExactly (componentInfoA .getType ());
86
86
assertThat (componentManager .getComponentInfosByType (componentInfoA .getType ()))
87
- .containsExactly (componentInfoA , componentInfoB );
87
+ .containsExactly (componentInfoA , componentInfoB );
88
88
assertThat (componentManager .getComponentInfo (componentInfoA .getName ())).isEqualTo (
89
- componentInfoA );
89
+ componentInfoA );
90
90
assertThat (componentManager .getComponentInfosByApplicationContext (null )).containsExactly (
91
- componentInfoA , componentInfoB );
91
+ componentInfoA , componentInfoB );
92
92
}
93
93
94
94
@ Test
@@ -97,12 +97,12 @@ public void unRegister() {
97
97
componentManager .register (componentInfoA );
98
98
assertThat (componentManager .getComponents ()).contains (componentInfoA );
99
99
assertThat (componentManager .getComponentInfosByType (componentInfoA .getType ()))
100
- .containsExactly (componentInfoA );
100
+ .containsExactly (componentInfoA );
101
101
102
102
componentManager .unregister (componentInfoA );
103
103
assertThat (componentManager .getComponents ()).doesNotContain (componentInfoA );
104
104
assertThat (componentManager .getComponentInfosByType (componentInfoA .getType ()))
105
- .doesNotContain (componentInfoA );
105
+ .doesNotContain (componentInfoA );
106
106
}
107
107
108
108
@ Test
@@ -138,31 +138,38 @@ public void registerException(CapturedOutput capturedOutput) {
138
138
componentInfoA = new DemoComponent ("A" );
139
139
componentInfoA .setRegisterException (true );
140
140
141
- assertThat (componentManager .registerAndGet (componentInfoA )).isNull ();
142
- assertThat (capturedOutput .getOut ()).contains ("01-03003" );
143
- assertThat (capturedOutput .getOut ()).contains (componentInfoA .getName ().toString ());
141
+ try {
142
+ assertThat (componentManager .registerAndGet (componentInfoA )).isNull ();
143
+ } catch (ServiceRuntimeException e ) {
144
+ assertThat (capturedOutput .getOut ()).contains ("01-03003" );
145
+ assertThat (capturedOutput .getOut ()).contains (componentInfoA .getName ().toString ());
146
+ }
144
147
}
145
148
146
149
@ Test
147
150
public void resolveException (CapturedOutput capturedOutput ) {
148
151
componentInfoA = new DemoComponent ("A" );
149
152
componentInfoA .setResolveException (true );
150
153
151
- assertThat (componentManager .registerAndGet (componentInfoA )).isEqualTo (componentInfoA );
152
- assertThat (componentInfoA .isHealthy ().isHealthy ()).isFalse ();
153
- assertThat (capturedOutput .getOut ()).contains ("01-03004" );
154
- assertThat (capturedOutput .getOut ()).contains (componentInfoA .getName ().toString ());
154
+ try {
155
+ assertThat (componentManager .registerAndGet (componentInfoA )).isEqualTo (componentInfoA );
156
+
157
+ } catch (ServiceRuntimeException e ) {
158
+ assertThat (componentInfoA .isHealthy ().isHealthy ()).isFalse ();
159
+ assertThat (capturedOutput .getOut ()).contains ("01-03004" );
160
+ assertThat (capturedOutput .getOut ()).contains (componentInfoA .getName ().toString ());
161
+ }
155
162
}
156
163
157
164
@ Test
158
165
public void normalShutdown () {
159
166
ComponentManager componentManager = initComponentManager (false , false );
160
167
ComponentInfo demoComponentInfo = componentManager
161
- .getComponentInfosByType (DEMO_COMPONENT_TYPE ).stream ().findFirst ().get ();
168
+ .getComponentInfosByType (DEMO_COMPONENT_TYPE ).stream ().findFirst ().get ();
162
169
ComponentInfo springComponentInfo = componentManager
163
- .getComponentInfosByType (SPRING_COMPONENT_TYPE ).stream ().findFirst ().get ();
170
+ .getComponentInfosByType (SPRING_COMPONENT_TYPE ).stream ().findFirst ().get ();
164
171
GenericApplicationContext applicationContext = (GenericApplicationContext ) springComponentInfo
165
- .getImplementation ().getTarget ();
172
+ .getImplementation ().getTarget ();
166
173
167
174
assertThat (componentManager .size ()).isEqualTo (2 );
168
175
assertThat (demoComponentInfo .isActivated ()).isTrue ();
@@ -179,11 +186,11 @@ public void normalShutdown() {
179
186
public void skipAllComponentShutdown () {
180
187
ComponentManager componentManager = initComponentManager (true , false );
181
188
ComponentInfo demoComponentInfo = componentManager
182
- .getComponentInfosByType (DEMO_COMPONENT_TYPE ).stream ().findFirst ().get ();
189
+ .getComponentInfosByType (DEMO_COMPONENT_TYPE ).stream ().findFirst ().get ();
183
190
ComponentInfo springComponentInfo = componentManager
184
- .getComponentInfosByType (SPRING_COMPONENT_TYPE ).stream ().findFirst ().get ();
191
+ .getComponentInfosByType (SPRING_COMPONENT_TYPE ).stream ().findFirst ().get ();
185
192
GenericApplicationContext applicationContext = (GenericApplicationContext ) springComponentInfo
186
- .getImplementation ().getTarget ();
193
+ .getImplementation ().getTarget ();
187
194
188
195
assertThat (componentManager .size ()).isEqualTo (2 );
189
196
assertThat (demoComponentInfo .isActivated ()).isTrue ();
@@ -200,11 +207,11 @@ public void skipAllComponentShutdown() {
200
207
public void skipCommonComponentShutdown () {
201
208
ComponentManager componentManager = initComponentManager (false , true );
202
209
ComponentInfo demoComponentInfo = componentManager
203
- .getComponentInfosByType (DEMO_COMPONENT_TYPE ).stream ().findFirst ().get ();
210
+ .getComponentInfosByType (DEMO_COMPONENT_TYPE ).stream ().findFirst ().get ();
204
211
ComponentInfo springComponentInfo = componentManager
205
- .getComponentInfosByType (SPRING_COMPONENT_TYPE ).stream ().findFirst ().get ();
212
+ .getComponentInfosByType (SPRING_COMPONENT_TYPE ).stream ().findFirst ().get ();
206
213
GenericApplicationContext applicationContext = (GenericApplicationContext ) springComponentInfo
207
- .getImplementation ().getTarget ();
214
+ .getImplementation ().getTarget ();
208
215
209
216
assertThat (componentManager .size ()).isEqualTo (2 );
210
217
assertThat (demoComponentInfo .isActivated ()).isTrue ();
@@ -219,7 +226,7 @@ public void skipCommonComponentShutdown() {
219
226
220
227
private ComponentManager initComponentManager (boolean skipAll , boolean skipComponent ) {
221
228
StandardSofaRuntimeManager sofaRuntimeManager = new StandardSofaRuntimeManager ("testApp" ,
222
- this .getClass ().getClassLoader (), clientFactory );
229
+ this .getClass ().getClassLoader (), clientFactory );
223
230
ComponentManager componentManager = sofaRuntimeManager .getComponentManager ();
224
231
SofaRuntimeContext sofaRuntimeContext = sofaRuntimeManager .getSofaRuntimeContext ();
225
232
sofaRuntimeContext .getProperties ().setSkipAllComponentShutdown (skipAll );
@@ -230,9 +237,9 @@ private ComponentManager initComponentManager(boolean skipAll, boolean skipCompo
230
237
231
238
GenericApplicationContext applicationContext = new GenericApplicationContext ();
232
239
ComponentName springComponentName = ComponentNameFactory .createComponentName (
233
- SPRING_COMPONENT_TYPE , "testModule" );
240
+ SPRING_COMPONENT_TYPE , "testModule" );
234
241
ComponentInfo springComponentInfo = new SpringContextComponent (springComponentName ,
235
- new SpringContextImplementation (applicationContext ), sofaRuntimeContext );
242
+ new SpringContextImplementation (applicationContext ), sofaRuntimeContext );
236
243
applicationContext .refresh ();
237
244
componentManager .register (springComponentInfo );
238
245
0 commit comments