forked from kptran/mct
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TestExternalComponentRegistryImpl.java
428 lines (355 loc) · 20.1 KB
/
TestExternalComponentRegistryImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*******************************************************************************
* Mission Control Technologies, Copyright (c) 2009-2012, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* The MCT platform is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
* MCT includes source code licensed under additional open source licenses. See
* the MCT Open Source Licenses file included with this distribution or the About
* MCT Licenses dialog available at runtime from the MCT Help menu for additional
* information.
*******************************************************************************/
package gov.nasa.arc.mct.registry;
import gov.nasa.arc.mct.components.AbstractComponent;
import gov.nasa.arc.mct.components.Bootstrap;
import gov.nasa.arc.mct.gui.View;
import gov.nasa.arc.mct.platform.spi.PersistenceProvider;
import gov.nasa.arc.mct.platform.spi.Platform;
import gov.nasa.arc.mct.platform.spi.PlatformAccess;
import gov.nasa.arc.mct.policy.ExecutionResult;
import gov.nasa.arc.mct.policy.PolicyContext;
import gov.nasa.arc.mct.registry.ExternalComponentRegistryImpl.ExtendedComponentProvider;
import gov.nasa.arc.mct.services.component.AbstractComponentProvider;
import gov.nasa.arc.mct.services.component.ComponentProvider;
import gov.nasa.arc.mct.services.component.ComponentTypeInfo;
import gov.nasa.arc.mct.services.component.PolicyManager;
import gov.nasa.arc.mct.services.component.TypeInfo;
import gov.nasa.arc.mct.services.component.ViewInfo;
import gov.nasa.arc.mct.services.component.ViewType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class TestExternalComponentRegistryImpl {
private ExternalComponentRegistryImpl registry;
private Platform mockPlatform;
private PolicyManager mockPolicyManager;
private PersistenceProvider mockPersistence;
private AbstractComponent mySandbox = new AbstractComponent() {
};
private ExtendedComponentProvider createProvider(final Collection<ComponentTypeInfo> infos, final Collection<ViewInfo> viewInfos) {
ComponentProvider provider = new TestComponentProvider(infos, viewInfos);
return new ExtendedComponentProvider(provider, "test");
}
@BeforeMethod
public void clearRegistry() throws Exception {
registry = new ExternalComponentRegistryImpl() {
@Override
protected String getDefaultUser() {
return "defaultUser";
}
};
mockPlatform = Mockito.mock(Platform.class);
mockPolicyManager = Mockito.mock(PolicyManager.class);
mockPersistence = Mockito.mock(PersistenceProvider.class);
(new PlatformAccess()).setPlatform(mockPlatform);
Mockito.when(mockPlatform.getPolicyManager()).thenReturn(mockPolicyManager);
Mockito.when(mockPlatform.getPersistenceProvider()).thenReturn(mockPersistence);
Mockito.when(mockPlatform.getMySandbox()).thenReturn(mySandbox);
ExecutionResult er = new ExecutionResult(null, true, null);
Mockito.when(mockPolicyManager.execute(Mockito.anyString(), Mockito.any(PolicyContext.class))).thenReturn(er);
}
@Test
public void testGetComponentType() {
Assert.assertNull(registry.getComponentType("someType"), "components should be empty prior to populating registry");
ComponentTypeInfo info = new ComponentTypeInfo("displayName", "desc", TestBaseComponent.class);
ExtendedComponentProvider provider = createProvider(Collections.singletonList(info), null);
registry.refreshComponents(Collections.singletonList(provider));
Assert.assertEquals(TestBaseComponent.class, registry.getComponentType(TestBaseComponent.class.getName()),"supplied info must be equal");
registry.refreshComponents(Collections.<ExtendedComponentProvider>emptyList());
Assert.assertNull(registry.getComponentType(TestBaseComponent.class.getName()),"removing provider must also remove component types");
}
private static class TestComponentProvider extends AbstractComponentProvider {
private final Collection<ComponentTypeInfo> infos;
private final Collection<ViewInfo> viewInfos;
private TestComponentProvider(Collection<ComponentTypeInfo> infos, Collection<ViewInfo> viewInfos) {
this.infos = infos;
this.viewInfos = viewInfos;
}
@Override
public Collection<ComponentTypeInfo> getComponentTypes() {
return infos;
}
@Override
public Collection<ViewInfo> getViews(String componentTypeId) {
return viewInfos;
}
}
public static class TestBaseComponent extends AbstractComponent {
}
public static class TestingView extends View {
private static final long serialVersionUID = 1L;
public TestingView(AbstractComponent ac, ViewInfo vi) {
super(ac,vi);
}
}
public static class OtherTestingView extends View {
private static final long serialVersionUID = 1L;
public OtherTestingView(AbstractComponent ac, ViewInfo vi) {
super(ac,vi);
}
}
public static class TestingView2 extends View {
private static final long serialVersionUID = 1L;
public TestingView2(AbstractComponent ac, ViewInfo vi) {
super(ac,vi);
}
}
public static class TestingView3 extends View {
private static final long serialVersionUID = 1L;
public TestingView3(AbstractComponent ac, ViewInfo vi) {
super(ac,vi);
}
}
@Test
public void testGetViews() {
Assert.assertTrue(registry.getViewInfos("someType", ViewType.CENTER).isEmpty(), "views should be empty prior to populating registry");
ViewInfo vi1 = new ViewInfo(TestingView.class, "abc", ViewType.OBJECT);
ViewInfo vi3 = new ViewInfo(TestingView2.class, "abc", ViewType.CENTER);
ViewInfo vi2 = new ViewInfo(TestingView3.class, "def", ViewType.CENTER);
ExtendedComponentProvider provider = createProvider(Collections.<ComponentTypeInfo>emptyList(), Arrays.asList(vi1, vi2, vi3));
registry.refreshComponents(Collections.singletonList(provider));
Collection<ViewInfo> infos = registry.getViewInfos("abc", ViewType.CENTER);
Assert.assertEquals(infos.size(),2);
Assert.assertTrue(infos.containsAll(Arrays.asList(vi2,vi3)));
infos = registry.getViewInfos("abc", ViewType.OBJECT);
Assert.assertEquals(infos.size(),1);
Assert.assertTrue(infos.containsAll(Arrays.asList(vi1)));
Assert.assertTrue(registry.getViewInfos("abc", ViewType.LAYOUT).isEmpty());
registry.refreshComponents(Collections.<ExtendedComponentProvider>emptyList());
Assert.assertTrue(registry.getViewInfos("someType", ViewType.CENTER).isEmpty(), "views should be empty prior to populating registry");
}
@Test
public void testDefaultViews() {
ExtendedComponentProvider provider = createProvider(Collections.<ComponentTypeInfo>singleton(new ComponentTypeInfo("displayName", "desc", TestBaseComponent.class)), Collections.<ViewInfo>emptyList());
registry.refreshComponents(Collections.singletonList(provider));
Assert.assertTrue(registry.getViewInfos("someType", ViewType.CENTER).isEmpty(), "views should be empty prior to populating registry");
ExtendedComponentProvider defaultProvider = createProvider(Collections.<ComponentTypeInfo>emptyList(), Collections.<ViewInfo>singleton(new ViewInfo(TestingView.class,"",ViewType.CENTER)));
registry.setDefaultViewProvider(defaultProvider);
Assert.assertEquals(Collections.singletonList(new ViewInfo(TestingView.class,"", ViewType.CENTER)), registry.getViewInfos(TestBaseComponent.class.getName(), ViewType.CENTER), "default test view should be provided");
// TODO add additional tests to verify the multiplicity of the ViewType is considered
}
@Test
public void testNewInstance() throws Exception {
ComponentTypeInfo info = new ComponentTypeInfo("displayName", "desc", TestBaseComponent.class);
ExtendedComponentProvider provider = createProvider(Collections.singletonList(info), null);
registry.refreshComponents(Collections.singletonList(provider));
TestBaseComponent newComponent = registry.newInstance(TestBaseComponent.class,null);
Assert.assertTrue(newComponent.getClass().equals(TestBaseComponent.class));
// now pass a parent which is shared
AbstractComponent parentComponent = new MockComponent() {
@Override
public synchronized List<AbstractComponent> getComponents() {
return Collections.<AbstractComponent> emptyList();
}
};
TestBaseComponent newComponent2 = registry.newInstance(TestBaseComponent.class,parentComponent);
Assert.assertTrue(newComponent2.getClass().equals(TestBaseComponent.class));
}
@Test
public void testNewCollection() {
// Environment setup: platform, collection provider, lock manager, and component registry.
Platform mockPlatform = Mockito.mock(Platform.class);
// Also need a sandbox
AbstractComponent mockSandbox = Mockito.mock(AbstractComponent.class);
Mockito.when(mockPlatform.getMySandbox()).thenReturn(mockSandbox);
MockComponentRegistry registry = new MockComponentRegistry();
// Set the platform SPI
PlatformAccess platformAccess = new PlatformAccess();
platformAccess.setPlatform(mockPlatform);
// Case #1: test returned collection when adding selectedComponents to the new collection is successful
// Setup
TestBaseComponent collection = Mockito.mock(TestBaseComponent.class);
registry.setDefaultCollection(collection);
List<AbstractComponent> selectedComponents = Collections.singletonList(Mockito.mock(AbstractComponent.class));
registry.setExpectedResultForAddComponents(true);
// The test
AbstractComponent newCollection = registry.newCollection(selectedComponents);
Assert.assertSame(newCollection, collection);
// Case #2: test returned collection when adding selectedComponents to the new collection fails
// Setup
registry.clearRegistry();
registry.setDefaultCollection(collection);
registry.setExpectedResultForAddComponents(true);
// The test
newCollection = registry.newCollection(selectedComponents);
Assert.assertNotNull(newCollection);
// Tear down
platformAccess.setPlatform(null);
}
@Test
public void testGetComponentInfos() {
ComponentTypeInfo info = new ComponentTypeInfo("displayName", "desc", TestBaseComponent.class);
ExtendedComponentProvider provider = createProvider(Collections.singletonList(info), null);
registry.refreshComponents(Collections.singletonList(provider));
Assert.assertEquals(Collections.singleton(info), registry.getComponentInfos(),"component infos must be the same");
}
@Test
public void testNullReturnFromComponentProvider() {
ExtendedComponentProvider provider = createProvider(null, null);
registry.refreshComponents(Collections.singletonList(provider));
Assert.assertTrue(registry.getComponentInfos().isEmpty());
}
@Test
public void testMultipleReturnsFromGetInfos() {
ComponentTypeInfo info = new ComponentTypeInfo("displayName", "desc", TestBaseComponent.class);
ViewInfo providerViewInfo = new ViewInfo(TestingView.class,"", ViewType.CENTER);
ViewInfo defaultViewInfo = new ViewInfo(OtherTestingView.class,"", ViewType.CENTER);
ExtendedComponentProvider cp = createProvider(Collections.singleton(info), Collections.<ViewInfo>singleton(providerViewInfo));
registry.refreshComponents(Collections.singletonList(cp));
ExtendedComponentProvider defaultProvider = createProvider(Collections.<ComponentTypeInfo>emptyList(), Collections.singleton(defaultViewInfo));
registry.setDefaultViewProvider(defaultProvider);
List<ViewInfo> expected = Arrays.asList(providerViewInfo,defaultViewInfo);
Collection<ViewInfo> viewInfos = registry.getViewInfos(TestBaseComponent.class.getName(), ViewType.CENTER);
Assert.assertEquals(2, viewInfos.size());
Assert.assertTrue(viewInfos.containsAll(expected));
}
@Test
public void testExceptionWhileInvokingGetInfo() {
ComponentTypeInfo info = new ComponentTypeInfo("displayName", "desc", TestBaseComponent.class);
ExtendedComponentProvider goodProvider = createProvider(Collections.singletonList(info), null);
ComponentProvider exceptionProvider = new TestComponentProvider(null, null) {
@Override
public Collection<ComponentTypeInfo> getComponentTypes() {
throw new ClassCastException();
}
};
ExtendedComponentProvider badProvider = new ExtendedComponentProvider(exceptionProvider, "symName");
registry.refreshComponents(Arrays.asList(badProvider, goodProvider));
Assert.assertEquals(Collections.singleton(info), registry.getComponentInfos(),"component infos must be the same");
}
@Test
public void testGetInstance() {
Assert.assertNotNull(ExternalComponentRegistryImpl.getInstance());
}
@Test (dataProvider="assetTestCases")
public <T> void testGetAsset(int count, int found, T asset, Class<T> clazz) {
// Setup some mock providers to look at for assets
ComponentProvider mockDefaultProvider = Mockito.mock(ComponentProvider.class);
ExtendedComponentProvider mockProviders[] = new ExtendedComponentProvider[count];
TypeInfo<?> mockTypeInfo = Mockito.mock(TypeInfo.class);
for (int i = 0; i < count; i++) {
mockProviders[i] = Mockito.mock(ExtendedComponentProvider.class, "provider" + i);
}
// Find the expected item in provider "found" (with default provider at end)
if (found < count) {
Mockito.when(mockProviders[found].getAsset(mockTypeInfo, clazz)).thenReturn(asset);
} else if (found == count) {
Mockito.when(mockDefaultProvider.getAsset(mockTypeInfo, clazz)).thenReturn(asset);
}
registry.setDefaultViewProvider(mockDefaultProvider);
registry.refreshComponents(Arrays.asList(mockProviders));
// Should find the object, unless found > count
// (in that case, none of the providers was configured to give it,
// so should have returned null)
Object actual = registry.getAsset(mockTypeInfo, asset.getClass());
if (found <= count) { // up to / including default provider
Assert.assertEquals(actual, asset);
} else {
Assert.assertNull(actual);
}
// Should have invoked getAsset on all providers until found
for (int i = 0; i <= found && i < count; i++) {
Mockito.verify(mockProviders[i]).getAsset(mockTypeInfo, clazz);
}
// Should not have invoked getAsset on subsequent providers
for (int i = found + 1; i < count; i++) {
Mockito.verify(mockProviders[i], Mockito.never()).getAsset(mockTypeInfo, clazz);
}
// Should have invoked getAsset on defaultProvider iff not found earlier
if (found >= count) {
Mockito.verify(mockDefaultProvider).getAsset(mockTypeInfo, clazz);
} else {
Mockito.verify(mockDefaultProvider, Mockito.never()).getAsset(mockTypeInfo, clazz);
}
}
@DataProvider
public Object[][] assetTestCases() {
class TestClass {}
List<Object[]> caseList = new ArrayList<Object[]>();
Object[] testAssets = { "a string", new javax.swing.ImageIcon(), new TestClass() };
for (int count = 0; count < 5; count++) {
for (int found = 0; found <= count + 1; found++) {
for (Object asset : testAssets) {
caseList.add(new Object[]{count,found,asset,asset.getClass()});
}
}
}
return caseList.toArray(new Object[caseList.size()][]);
}
@SuppressWarnings("unchecked")
@Test
public void testPluginBootstraps() {
final Bootstrap mockGlobal = Mockito.mock(Bootstrap.class);
final Bootstrap mockNonGlobal = Mockito.mock(Bootstrap.class);
Mockito.when(mockGlobal.isGlobal()).thenReturn(true);
Mockito.when(mockNonGlobal.isGlobal()).thenReturn(false);
class BootstrapComponent extends AbstractComponent {
private boolean global;
public BootstrapComponent(boolean global) {
this.global = global;
}
@Override
protected <T> T handleGetCapability(Class<T> capability) {
if (capability.isAssignableFrom(Bootstrap.class)) {
return capability.cast(global ? mockGlobal : mockNonGlobal);
}
return super.handleGetCapability(capability);
}
}
// First, do a run with no bootstrap components
ExtendedComponentProvider mockProvider = Mockito.mock(ExtendedComponentProvider.class);
Mockito.when(mockProvider.getBootstrapComponents()).thenReturn(Collections.<AbstractComponent>emptyList());
registry.refreshComponents(Arrays.asList(mockProvider));
// Verify that this method was probed
Mockito.verify(mockProvider).getBootstrapComponents();
// Verify that persistence was not invoked
Mockito.verifyNoMoreInteractions(mockPersistence);
// Now, do a run with two bootstraps components (one global, one local)
AbstractComponent global = new BootstrapComponent(true);
AbstractComponent local = new BootstrapComponent(false);
Mockito.when(mockProvider.getBootstrapComponents()).thenReturn(Arrays.asList(global, local));
registry.refreshComponents(Arrays.asList(mockProvider));
// Verify that persistence was invoked, including appropriate tagging
@SuppressWarnings("rawtypes")
ArgumentCaptor<Collection> captor =
ArgumentCaptor.forClass(Collection.class);
Mockito.verify(mockPersistence).persist(captor.capture());
Assert.assertEquals(captor.getValue().size(), 2);
Assert.assertTrue(captor.getValue().contains(global));
Assert.assertTrue(captor.getValue().contains(local));
Mockito.verify(mockPersistence).tagComponents(Mockito.eq("bootstrap:admin"), captor.capture());
Assert.assertEquals(captor.getValue().size(), 1);
Assert.assertTrue(captor.getValue().contains(global));
Mockito.verify(mockPersistence).tagComponents(Mockito.eq("bootstrap:creator"), captor.capture());
Assert.assertEquals(captor.getValue().size(), 1);
Assert.assertTrue(captor.getValue().contains(local));
}
}