Skip to content

Package: EcoreReferenceServiceCustomizationProviders_ITest

EcoreReferenceServiceCustomizationProviders_ITest

nameinstructionbranchcomplexitylinemethod
EcoreReferenceServiceCustomizationProviders_ITest()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
attachOppositeReference()
M: 0 C: 60
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
createFixture()
M: 0 C: 22
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
createPackage(String)
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
createResource(ResourceSet, EPackage)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
createTestPackage()
M: 0 C: 67
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 19
100%
M: 0 C: 1
100%
destroyFixture()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
everything()
M: 0 C: 29
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
findControl(Composite, Class)
M: 52 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
finishWizard(Shell)
M: 36 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
openInNewContext()
M: 0 C: 64
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
selectExistingAnnotationReference()
M: 0 C: 66
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 17
100%
M: 0 C: 1
100%
selectExistingAttributeType()
M: 0 C: 41
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
selectExistingEOpposite_refHasType()
M: 0 C: 78
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 18
100%
M: 0 C: 1
100%
selectExistingEOpposite_refUntyped()
M: 0 C: 74
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 17
100%
M: 0 C: 1
100%
selectExistingSupertype()
M: 0 C: 60
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
selectFirstElement(Shell)
M: 63 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 21 C: 0
0%
M: 1 C: 0
0%
selectFirstElementInDialog(boolean[])
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
setEOppositeReference()
M: 0 C: 64
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 12
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2018 Christian W. Damus and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Christian W. Damus - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.spi.editor;
15:
16: import static java.util.Collections.singleton;
17: import static org.hamcrest.CoreMatchers.everyItem;
18: import static org.hamcrest.CoreMatchers.hasItem;
19: import static org.hamcrest.CoreMatchers.is;
20: import static org.hamcrest.CoreMatchers.not;
21: import static org.hamcrest.CoreMatchers.notNullValue;
22: import static org.hamcrest.MatcherAssert.assertThat;
23:
24: import java.lang.annotation.ElementType;
25: import java.lang.annotation.Retention;
26: import java.lang.annotation.RetentionPolicy;
27: import java.lang.annotation.Target;
28: import java.lang.reflect.Method;
29: import java.util.Arrays;
30: import java.util.Collection;
31: import java.util.HashSet;
32: import java.util.Iterator;
33: import java.util.Set;
34:
35: import org.eclipse.emf.common.command.BasicCommandStack;
36: import org.eclipse.emf.common.util.URI;
37: import org.eclipse.emf.ecore.EAnnotation;
38: import org.eclipse.emf.ecore.EAttribute;
39: import org.eclipse.emf.ecore.EClass;
40: import org.eclipse.emf.ecore.EClassifier;
41: import org.eclipse.emf.ecore.EDataType;
42: import org.eclipse.emf.ecore.ENamedElement;
43: import org.eclipse.emf.ecore.EObject;
44: import org.eclipse.emf.ecore.EPackage;
45: import org.eclipse.emf.ecore.EReference;
46: import org.eclipse.emf.ecore.EStructuralFeature;
47: import org.eclipse.emf.ecore.EcoreFactory;
48: import org.eclipse.emf.ecore.EcorePackage;
49: import org.eclipse.emf.ecore.change.ChangeDescription;
50: import org.eclipse.emf.ecore.change.ChangeFactory;
51: import org.eclipse.emf.ecore.resource.Resource;
52: import org.eclipse.emf.ecore.resource.ResourceSet;
53: import org.eclipse.emf.ecp.ui.view.swt.reference.AttachmentStrategy;
54: import org.eclipse.emf.ecp.ui.view.swt.reference.EObjectSelectionStrategy;
55: import org.eclipse.emf.ecp.ui.view.swt.reference.OpenInNewContextStrategy;
56: import org.eclipse.emf.ecp.ui.view.swt.reference.ReferenceStrategy;
57: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
58: import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
59: import org.eclipse.emfforms.bazaar.Bazaar;
60: import org.eclipse.emfforms.bazaar.BazaarContext;
61: import org.eclipse.emfforms.bazaar.Vendor;
62: import org.eclipse.emfforms.internal.editor.ecore.referenceservices.EcoreAttachmentStrategyProvider;
63: import org.eclipse.emfforms.internal.editor.ecore.referenceservices.EcoreEObjectSelectionStrategyProvider;
64: import org.eclipse.emfforms.internal.editor.ecore.referenceservices.EcoreOpenInNewContextStrategyProvider;
65: import org.eclipse.emfforms.internal.editor.ecore.referenceservices.EcoreReferenceStrategyProvider;
66: import org.eclipse.jface.dialogs.Dialog;
67: import org.eclipse.jface.dialogs.IDialogConstants;
68: import org.eclipse.swt.SWT;
69: import org.eclipse.swt.widgets.Composite;
70: import org.eclipse.swt.widgets.Control;
71: import org.eclipse.swt.widgets.Display;
72: import org.eclipse.swt.widgets.Event;
73: import org.eclipse.swt.widgets.Item;
74: import org.eclipse.swt.widgets.Shell;
75: import org.eclipse.swt.widgets.Table;
76: import org.eclipse.swt.widgets.Tree;
77: import org.eclipse.swt.widgets.Widget;
78: import org.hamcrest.CoreMatchers;
79: import org.junit.After;
80: import org.junit.Assume;
81: import org.junit.Before;
82: import org.junit.Rule;
83: import org.junit.Test;
84: import org.junit.rules.TestRule;
85: import org.junit.runner.Description;
86: import org.junit.runners.model.Statement;
87: import org.osgi.framework.InvalidSyntaxException;
88:
89: /**
90: * Integration test cases for the Ecore Editor's reference service strategy provider classes.
91: */
92: @SuppressWarnings({ "nls", "restriction" })
93: public class EcoreReferenceServiceCustomizationProviders_ITest {
94:
95:         @Rule
96:         public final BazaarRule bazaar = new BazaarRule();
97:
98:         private EPackage testPackage;
99:
100:         /**
101:          * Initializes me.
102:          */
103:         public EcoreReferenceServiceCustomizationProviders_ITest() {
104:                 super();
105:         }
106:
107:         @ProviderType(EcoreReferenceStrategyProvider.class)
108:         @Test
109:         public void setEOppositeReference() throws InvalidSyntaxException {
110:                 final EClass foo = (EClass) testPackage.getEClassifier("Foo");
111:                 final EClass abstrakt = (EClass) testPackage.getEClassifier("Abstract");
112:                 final EReference ref = (EReference) foo.getEStructuralFeature("ref");
113:                 final EReference opposite = EcoreFactory.eINSTANCE.createEReference();
114:                 abstrakt.getEStructuralFeatures().add(opposite);
115:
116:                 final ReferenceStrategy strategy = bazaar.getStrategy(foo, EcorePackage.Literals.EREFERENCE__EOPPOSITE);
117:
118:                 strategy.addElementsToReference(ref, EcorePackage.Literals.EREFERENCE__EOPPOSITE, singleton(opposite));
119:
120:                 assertThat("Reference's opposite not set", ref.getEOpposite(), is(opposite));
121:                 assertThat("Opposite's opposite not set", opposite.getEOpposite(), is(ref));
122:                 assertThat("Opposite's type not set", opposite.getEReferenceType(), is(foo));
123:                 assertThat("Reference's type not set", ref.getEReferenceType(), is(abstrakt));
124:         }
125:
126:         @ProviderType(EcoreAttachmentStrategyProvider.class)
127:         @Test
128:         public void attachOppositeReference() {
129:                 final EClass foo = (EClass) testPackage.getEClassifier("Foo");
130:                 final EClass abstrakt = (EClass) testPackage.getEClassifier("Abstract");
131:                 final EReference ref = (EReference) foo.getEStructuralFeature("ref");
132:                 ref.setEType(abstrakt);
133:                 final EReference opposite = EcoreFactory.eINSTANCE.createEReference();
134:
135:                 final AttachmentStrategy strategy = bazaar.getStrategy(foo, EcorePackage.Literals.EREFERENCE__EOPPOSITE);
136:
137:                 strategy.addElementToModel(ref, EcorePackage.Literals.EREFERENCE__EOPPOSITE, opposite);
138:
139:                 assertThat("Opposite not added to its owner", opposite.getEContainingClass(), is(abstrakt));
140:                 assertThat("Opposite's opposite not set", opposite.getEOpposite(), is(ref));
141:                 assertThat("Opposite's type not set", opposite.getEReferenceType(), is(foo));
142:                 assertThat("Opposite's name not set", opposite.getName(), notNullValue());
143:         }
144:
145:         @ProviderType(EcoreEObjectSelectionStrategyProvider.class)
146:         @Test
147:         public void selectExistingEOpposite_refUntyped() {
148:                 final EClass foo = (EClass) testPackage.getEClassifier("Foo");
149:                 final EClass abstrakt = (EClass) testPackage.getEClassifier("Abstract");
150:                 final EReference ref = (EReference) foo.getEStructuralFeature("ref");
151:                 final EReference other = EcoreFactory.eINSTANCE.createEReference();
152:                 other.setName("other");
153:                 other.setEType(foo);
154:                 foo.getEStructuralFeatures().add(other);
155:                 final EReference opposite = EcoreFactory.eINSTANCE.createEReference();
156:                 abstrakt.getEStructuralFeatures().add(opposite);
157:
158:                 final EObjectSelectionStrategy strategy = bazaar.getStrategy(foo, EcorePackage.Literals.EREFERENCE__EOPPOSITE);
159:
160:                 Collection<EObject> selection = everything();
161:                 selection = strategy.collectExistingObjects(ref, EcorePackage.Literals.EREFERENCE__EOPPOSITE, selection);
162:
163:                 assertThat(selection, everyItem(CoreMatchers.<EObject> instanceOf(EReference.class)));
164:                 assertThat(selection, hasItem(opposite));
165:                 assertThat(selection, not(hasItem(ref)));
166:                 assertThat(selection, hasItem(other)); // Could be an opposite, in theory
167:         }
168:
169:         @ProviderType(EcoreEObjectSelectionStrategyProvider.class)
170:         @Test
171:         public void selectExistingEOpposite_refHasType() {
172:                 final EClass foo = (EClass) testPackage.getEClassifier("Foo");
173:                 final EClass abstrakt = (EClass) testPackage.getEClassifier("Abstract");
174:                 final EReference ref = (EReference) foo.getEStructuralFeature("ref");
175:                 final EReference other = EcoreFactory.eINSTANCE.createEReference();
176:                 other.setName("other");
177:                 other.setEType(foo);
178:                 foo.getEStructuralFeatures().add(other);
179:                 ref.setEType(abstrakt); // This determines the type that may contain opposites
180:                 final EReference opposite = EcoreFactory.eINSTANCE.createEReference();
181:                 abstrakt.getEStructuralFeatures().add(opposite);
182:
183:                 final EObjectSelectionStrategy strategy = bazaar.getStrategy(foo, EcorePackage.Literals.EREFERENCE__EOPPOSITE);
184:
185:                 Collection<EObject> selection = everything();
186:                 selection = strategy.collectExistingObjects(ref, EcorePackage.Literals.EREFERENCE__EOPPOSITE, selection);
187:
188:                 assertThat(selection, everyItem(CoreMatchers.<EObject> instanceOf(EReference.class)));
189:                 assertThat(selection, hasItem(opposite));
190:                 assertThat(selection, not(hasItem(ref)));
191:                 assertThat(selection, not(hasItem(other))); // Not in the ref's type
192:         }
193:
194:         @ProviderType(EcoreEObjectSelectionStrategyProvider.class)
195:         @Test
196:         public void selectExistingSupertype() {
197:                 final EClass foo = (EClass) testPackage.getEClassifier("Foo");
198:                 final EClass abstrakt = (EClass) testPackage.getEClassifier("Abstract");
199:                 final EClass subclass = EcoreFactory.eINSTANCE.createEClass();
200:                 subclass.setName("Subtype");
201:                 subclass.getESuperTypes().add(foo);
202:                 testPackage.getEClassifiers().add(subclass);
203:
204:                 final EObjectSelectionStrategy strategy = bazaar.getStrategy(foo, EcorePackage.Literals.ECLASS__ESUPER_TYPES);
205:
206:                 Collection<EObject> selection = everything();
207:                 selection = strategy.collectExistingObjects(foo, EcorePackage.Literals.ECLASS__ESUPER_TYPES, selection);
208:
209:                 assertThat(selection, everyItem(CoreMatchers.<EObject> instanceOf(EClass.class)));
210:                 assertThat(selection, hasItem(abstrakt));
211:                 assertThat(selection, not(hasItem(subclass))); // Would cause cycle
212:         }
213:
214:         @ProviderType(EcoreEObjectSelectionStrategyProvider.class)
215:         @Test
216:         public void selectExistingAttributeType() {
217:                 final EClass foo = (EClass) testPackage.getEClassifier("Foo");
218:                 final EClassifier intType = testPackage.getEClassifier("int");
219:                 final EStructuralFeature attr = foo.getEStructuralFeature("attr");
220:
221:                 final EObjectSelectionStrategy strategy = bazaar.getStrategy(attr, EcorePackage.Literals.ETYPED_ELEMENT__ETYPE);
222:
223:                 Collection<EObject> selection = everything();
224:                 selection = strategy.collectExistingObjects(attr, EcorePackage.Literals.ETYPED_ELEMENT__ETYPE, selection);
225:
226:                 assertThat(selection, everyItem(CoreMatchers.<EObject> instanceOf(EDataType.class)));
227:                 assertThat(selection, hasItem(intType));
228:         }
229:
230:         @ProviderType(EcoreEObjectSelectionStrategyProvider.class)
231:         @Test
232:         public void selectExistingAnnotationReference() {
233:                 final EClass foo = (EClass) testPackage.getEClassifier("Foo");
234:                 final EClassifier intType = testPackage.getEClassifier("int");
235:                 final EAnnotation annotation = EcoreFactory.eINSTANCE.createEAnnotation();
236:                 annotation.setSource("test");
237:                 foo.getEAnnotations().add(annotation);
238:                 final EObjectSelectionStrategy strategy = bazaar.getStrategy(annotation,
239:                         EcorePackage.Literals.EANNOTATION__REFERENCES);
240:
241:                 final ChangeDescription change = ChangeFactory.eINSTANCE.createChangeDescription();
242:                 foo.eResource().getContents().add(change);
243:
244:                 Collection<EObject> selection = everything();
245:                 Assume.assumeThat(selection, hasItem(change));
246:                 selection = strategy.collectExistingObjects(annotation, EcorePackage.Literals.EANNOTATION__REFERENCES,
247:                         selection);
248:
249:                 assertThat(selection, everyItem(CoreMatchers.<EObject> instanceOf(ENamedElement.class)));
250:                 assertThat(selection, hasItem(intType));
251:                 assertThat(selection, not(hasItem(change)));
252:         }
253:
254:         @ProviderType(EcoreOpenInNewContextStrategyProvider.class)
255:         @Test
256:         public void openInNewContext() {
257:                 final EClassifier foo = testPackage.getEClassifier("Foo");
258:                 final OpenInNewContextStrategy strategy = bazaar.getStrategy(foo, foo.eContainmentFeature());
259:                 assertThat(strategy, notNullValue());
260:
261:                 final Set<Shell> expectedShells = new HashSet<Shell>(Arrays.asList(Display.getCurrent().getShells()));
262:
263:                 // If a dialog appears close it
264:                 final boolean[] doIt = { true };
265:                 final boolean[] sawDialog = { false };
266:                 Display.getDefault().asyncExec(new Runnable() {
267:
268:                         @Override
269:                         public void run() {
270:                                 if (!doIt[0]) {
271:                                         return;
272:                                 }
273:
274:                                 for (final Shell next : Display.getCurrent().getShells()) {
275:                                         if (!expectedShells.contains(next)) {
276:                                                 sawDialog[0] = true;
277:                                                 next.close();
278:                                         }
279:                                 }
280:                         }
281:                 });
282:
283:                 strategy.openInNewContext(foo.eContainer(), foo.eContainmentFeature(), foo);
284:
285:                 // If the dialog didn't appear, don't try anything after the test suite has moved on
286:                 doIt[0] = false;
287:
288:                 assertThat("Open strategy presented a dialog", sawDialog[0], is(false));
289:         }
290:
291:         //
292:         // Test framework
293:         //
294:
295:         @Before
296:         public void createFixture() {
297:                 final AdapterFactoryEditingDomain domain = new AdapterFactoryEditingDomain(
298:                         new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE),
299:                         new BasicCommandStack());
300:
301:                 testPackage = createTestPackage();
302:                 createResource(domain.getResourceSet(), testPackage);
303:         }
304:
305:         @After
306:         public void destroyFixture() {
307:                 testPackage = null;
308:         }
309:
310:         EPackage createTestPackage() {
311:                 final EPackage result = createPackage("test");
312:                 final EClass foo = EcoreFactory.eINSTANCE.createEClass();
313:                 foo.setName("Foo");
314:                 result.getEClassifiers().add(foo);
315:                 final EAttribute state = EcoreFactory.eINSTANCE.createEAttribute();
316:                 state.setName("attr");
317:                 foo.getEStructuralFeatures().add(state);
318:                 final EReference ref = EcoreFactory.eINSTANCE.createEReference();
319:                 ref.setName("ref");
320:                 foo.getEStructuralFeatures().add(ref);
321:                 final EClass abstrakt = EcoreFactory.eINSTANCE.createEClass();
322:                 abstrakt.setAbstract(true);
323:                 abstrakt.setName("Abstract");
324:                 result.getEClassifiers().add(abstrakt);
325:                 final EDataType intType = EcoreFactory.eINSTANCE.createEDataType();
326:                 intType.setName("int");
327:                 intType.setInstanceTypeName("int");
328:                 result.getEClassifiers().add(intType);
329:                 return result;
330:         }
331:
332:         void createResource(ResourceSet rset, EPackage ePackage) {
333:                 final Resource resource = rset.createResource(URI.createURI(ePackage.getNsURI()).appendFileExtension("ecore"));
334:                 resource.getContents().add(ePackage);
335:         }
336:
337:         EPackage createPackage(String name) {
338:                 final EPackage result = EcoreFactory.eINSTANCE.createEPackage();
339:                 result.setName(name);
340:                 result.setNsURI(String.format("http://test/%s.ecore", name));
341:                 result.setNsPrefix(name);
342:                 return result;
343:         }
344:
345:         Collection<EObject> everything() {
346:                 final Collection<EObject> result = new HashSet<EObject>();
347:
348:                 final ResourceSet rset = testPackage.eResource().getResourceSet();
349:•                for (final Iterator<?> all = rset.getAllContents(); all.hasNext();) {
350:                         final Object next = all.next();
351:•                        if (next instanceof EObject) {
352:                                 result.add((EObject) next);
353:                         }
354:                 }
355:
356:                 return result;
357:         }
358:
359:         Runnable selectFirstElementInDialog(final boolean[] doIt) {
360:                 return new Runnable() {
361:
362:                         @Override
363:                         public void run() {
364:                                 if (doIt[0]) {
365:                                         Shell shell = Display.getCurrent().getActiveShell();
366:                                         if (shell == null) {
367:                                                 final Shell[] all = Display.getCurrent().getShells();
368:                                                 // Get the last opened
369:                                                 if (all.length > 0) {
370:                                                         shell = all[all.length - 1];
371:                                                 }
372:                                         }
373:                                         if (shell != null) {
374:                                                 selectFirstElement(shell);
375:                                                 finishWizard(shell);
376:                                         }
377:                                 }
378:                         }
379:                 };
380:         }
381:
382:         static void selectFirstElement(Shell shell) {
383:                 Widget notify = null;
384:                 Item selected = null;
385:
386:                 final Table table = findControl(shell, Table.class);
387:•                if (table != null) {
388:                         table.setSelection(0);
389:                         notify = table;
390:                         selected = table.getItem(0);
391:                 } else {
392:                         final Tree tree = findControl(shell, Tree.class);
393:•                        if (tree != null) {
394:                                 tree.setSelection(tree.getItem(0));
395:                                 notify = tree;
396:                                 selected = tree.getItem(0);
397:                         } else {
398:                                 throw new IllegalStateException("no table nor tree to select");
399:                         }
400:                 }
401:
402:                 // Have to fire notification for JFace viewer to pick up
403:                 final Event event = new Event();
404:                 event.type = SWT.Selection;
405:                 event.widget = notify;
406:                 event.item = selected;
407:                 notify.notifyListeners(SWT.Selection, event);
408:         }
409:
410:         static <C extends Control> C findControl(Composite composite, Class<C> type) {
411:                 C result = null;
412:
413:•                if (type.isInstance(composite)) {
414:                         result = type.cast(composite);
415:                 } else {
416:•                        for (final Control child : composite.getChildren()) {
417:•                                if (child instanceof Composite) {
418:                                         result = findControl((Composite) child, type);
419:•                                } else if (type.isInstance(child)) {
420:                                         result = type.cast(child);
421:                                 }
422:
423:•                                if (result != null) {
424:                                         break;
425:                                 }
426:                         }
427:                 }
428:
429:                 return result;
430:         }
431:
432:         static void finishWizard(Shell shell) {
433:                 final Dialog dialog = (Dialog) shell.getData();
434:
435:                 try {
436:                         final Method buttonPressed = Dialog.class.getDeclaredMethod("buttonPressed", int.class);
437:                         buttonPressed.setAccessible(true);
438:                         buttonPressed.invoke(dialog, IDialogConstants.FINISH_ID);
439:                         // BEGIN COMPLEX CODE
440:                 } catch (final Exception e) {
441:                         // END COMPLEX CODE
442:                         throw new RuntimeException(e);
443:                 }
444:         }
445:
446:         /**
447:          * Annotates a test method with the strategy provider class to inject as
448:          * a vendor in the {@linkplain BazaarRule bazaar fixture}.
449:          */
450:         @Target(ElementType.METHOD)
451:         @Retention(RetentionPolicy.RUNTIME)
452:         @interface ProviderType {
453:                 Class<? extends Vendor<?>> value();
454:         }
455:
456:         /**
457:          * A test rule that creates a bazaar with the single vendor specified by the
458:          * test's {@link ProviderType} annotation.
459:          */
460:         static class BazaarRule implements TestRule {
461:                 private Bazaar<?> bazaar;
462:
463:                 @Override
464:                 public Statement apply(final Statement base, final Description description) {
465:                         return new Statement() {
466:
467:                                 @Override
468:                                 public void evaluate() throws Throwable {
469:                                         starting(description);
470:
471:                                         try {
472:                                                 base.evaluate();
473:                                         } finally {
474:                                                 finished(description);
475:                                         }
476:                                 }
477:                         };
478:                 }
479:
480:                 protected void starting(Description description) throws InstantiationException, IllegalAccessException {
481:                         final Class<? extends Vendor<?>> strategyType = description
482:                                 .getAnnotation(ProviderType.class).value();
483:
484:                         final Vendor<?> provider = strategyType.newInstance();
485:
486:                         final Bazaar.Builder<?> builder = Bazaar.Builder.with(singleton(provider));
487:                         bazaar = builder.build();
488:                 }
489:
490:                 protected void finished(Description description) {
491:                         bazaar = null;
492:                 }
493:
494:                 <S> S getStrategy(EObject owner, EReference reference) {
495:                         return getStrategy(owner, reference, null, null);
496:                 }
497:
498:                 <S> S getStrategy(EObject owner, EReference reference, Object value) {
499:                         return getStrategy(owner, reference, value == null ? null : value.getClass().getName(), value);
500:                 }
501:
502:                 @SuppressWarnings("unchecked")
503:                 <S> S getStrategy(EObject owner, EReference reference, String key,
504:                         Object value) {
505:                         final BazaarContext.Builder context = BazaarContext.Builder.empty();
506:                         context.put(EObject.class, owner).put(EReference.class, reference);
507:                         if (key != null) {
508:                                 context.put(key, value);
509:                         }
510:
511:                         return (S) bazaar.createProduct(context.build());
512:                 }
513:         }
514: }