Skip to content

Package: Bazaar_Test$3

Bazaar_Test$3

nameinstructionbranchcomplexitylinemethod
make(String)
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
{...}
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2018 EclipseSource Muenchen GmbH 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: * jonas - initial API and implementation
13: * Christian W. Damus - bug 529542
14: ******************************************************************************/
15: package org.eclipse.emfforms.bazaar.internal;
16:
17: import static java.util.Arrays.asList;
18: import static org.hamcrest.CoreMatchers.any;
19: import static org.hamcrest.CoreMatchers.hasItem;
20: import static org.hamcrest.CoreMatchers.is;
21: import static org.hamcrest.CoreMatchers.not;
22: import static org.hamcrest.CoreMatchers.nullValue;
23: import static org.hamcrest.MatcherAssert.assertThat;
24: import static org.junit.Assert.assertFalse;
25: import static org.junit.Assert.assertNotNull;
26: import static org.junit.Assert.assertNull;
27: import static org.junit.Assert.assertSame;
28: import static org.junit.Assert.assertTrue;
29: import static org.junit.Assume.assumeThat;
30: import static org.mockito.Mockito.mock;
31: import static org.mockito.Mockito.times;
32: import static org.mockito.Mockito.verify;
33: import static org.mockito.Mockito.when;
34:
35: import java.util.Collections;
36: import java.util.HashMap;
37: import java.util.List;
38:
39: import org.eclipse.e4.core.contexts.EclipseContextFactory;
40: import org.eclipse.e4.core.contexts.IEclipseContext;
41: import org.eclipse.emfforms.bazaar.Bazaar.PriorityOverlapCallBack;
42: import org.eclipse.emfforms.bazaar.BazaarContext;
43: import org.eclipse.emfforms.bazaar.BazaarContextFunction;
44: import org.eclipse.emfforms.bazaar.Bid;
45: import org.eclipse.emfforms.bazaar.Create;
46: import org.eclipse.emfforms.bazaar.Vendor;
47: import org.junit.Test;
48: import org.junit.runner.RunWith;
49: import org.junit.runners.Parameterized;
50: import org.junit.runners.Parameterized.Parameters;
51:
52: /**
53: * @author jonas
54: *
55: */
56: @RunWith(Parameterized.class)
57: public class Bazaar_Test {
58:
59:         public static final String TESTSTRING = ""; //$NON-NLS-1$
60:         private final BazaarImpl<MyProduct> bazaar;
61:
62:         /**
63:          * Initializes me with the bazaar variant to create.
64:          *
65:          * @param variant the bazaar variant
66:          */
67:         public Bazaar_Test(BazaarVariant variant) {
68:                 super();
69:
70:                 bazaar = variant.createBazaarImpl();
71:         }
72:
73:         @Parameters(name = "{0}")
74:         public static Object[] parameters() {
75:                 return BazaarVariant.values();
76:         }
77:
78:         @Test
79:         public void testEmptyBazaar() {
80:                 final Vendor<? extends MyProduct> bestVendor = bazaar.getBestVendor(EclipseContextFactory.create());
81:
82:                 assertSame(null, bestVendor);
83:         }
84:
85:         @Test
86:         public void testSingleVendorNoParameter() {
87:                 final Vendor<MyProduct> vendor = new VendorPriority1Parameter0();
88:                 bazaar.addVendor(vendor);
89:
90:                 final Vendor<? extends MyProduct> bestVendor = bazaar.getBestVendor(EclipseContextFactory.create());
91:
92:                 assertSame(vendor, bestVendor);
93:         }
94:
95:         @Test
96:         public void testSingleVendorNoParameterWrongBid() {
97:                 final Vendor<MyProduct> vendor = new VendorWrongBidParameter0();
98:                 bazaar.addVendor(vendor);
99:
100:                 final Vendor<? extends MyProduct> bestVendor = bazaar.getBestVendor(EclipseContextFactory.create());
101:
102:                 assertNull(bestVendor);
103:         }
104:
105:         @Test
106:         public void testTwoVendorNoParameter() {
107:                 final Vendor<MyProduct> vendor = new VendorPriority1Parameter0();
108:                 final Vendor<MyProduct> vendor2 = new VendorPriority2Parameter0();
109:                 bazaar.addVendor(vendor);
110:                 bazaar.addVendor(vendor2);
111:
112:                 final Vendor<? extends MyProduct> bestVendor = bazaar.getBestVendor(EclipseContextFactory.create());
113:
114:                 assertSame(vendor2, bestVendor);
115:         }
116:
117:         @Test
118:         public void testTwoVendorNoParameterTwisted() {
119:                 final Vendor<MyProduct> vendor = new VendorPriority1Parameter0();
120:                 final Vendor<MyProduct> vendor2 = new VendorPriority2Parameter0();
121:                 bazaar.addVendor(vendor2);
122:                 bazaar.addVendor(vendor);
123:
124:                 final Vendor<? extends MyProduct> bestVendor = bazaar.getBestVendor(EclipseContextFactory.create());
125:
126:                 assertSame(vendor2, bestVendor);
127:         }
128:
129:         @Test
130:         public void testTwoVendorNoMatchingParameter() {
131:                 final Vendor<MyProduct> vendor = new VendorPriority1Parameter0();
132:                 final Vendor<MyProduct> vendor2 = new VendorPriority2Parameter1();
133:                 bazaar.addVendor(vendor);
134:                 bazaar.addVendor(vendor2);
135:
136:                 final IEclipseContext context = EclipseContextFactory.create();
137:
138:                 final Vendor<? extends MyProduct> bestVendor = bazaar.getBestVendor(context);
139:
140:                 assertSame(vendor, bestVendor);
141:         }
142:
143:         @Test
144:         public void testTwoVendorMatchingParameter() {
145:                 final Vendor<MyProduct> vendor = new VendorPriority1Parameter0();
146:                 final Vendor<MyProduct> vendor2 = new VendorPriority2Parameter1();
147:                 bazaar.addVendor(vendor);
148:                 bazaar.addVendor(vendor2);
149:
150:                 final IEclipseContext context = EclipseContextFactory.create();
151:                 context.set(String.class, TESTSTRING);
152:
153:                 final Vendor<? extends MyProduct> bestVendor = bazaar.getBestVendor(context);
154:
155:                 assertSame(vendor2, bestVendor);
156:         }
157:
158:         @Test
159:         public void testTwoVendorSamePriority() {
160:                 final Vendor<MyProduct> vendor = new VendorPriority1Parameter0();
161:                 final Vendor<MyProduct> vendor2 = new VendorPriority1Parameter0();
162:                 bazaar.addVendor(vendor);
163:                 bazaar.addVendor(vendor2);
164:
165:                 @SuppressWarnings("unchecked")
166:                 final PriorityOverlapCallBack<MyProduct> priorityOverlapCallBackMock = mock(PriorityOverlapCallBack.class);
167:
168:                 bazaar.setPriorityOverlapCallBack(priorityOverlapCallBackMock);
169:
170:                 final IEclipseContext context = EclipseContextFactory.create();
171:                 final Vendor<? extends MyProduct> best = bazaar.getBestVendor(context);
172:
173:                 verify(priorityOverlapCallBackMock, times(1)).priorityOverlap(vendor, vendor2);
174:                 assertThat(best, is((Object) vendor));
175:         }
176:
177:         @Test
178:         public void testTwoVendorSamePriorityNoCallBack() {
179:                 final Vendor<MyProduct> vendor = new VendorPriority1Parameter0();
180:                 final Vendor<MyProduct> vendor2 = new VendorPriority1Parameter0();
181:                 bazaar.addVendor(vendor);
182:                 bazaar.addVendor(vendor2);
183:
184:                 final IEclipseContext context = EclipseContextFactory.create();
185:                 bazaar.getBestVendor(context);
186:         }
187:
188:         @Test
189:         public void testVendorNoMatchingPreCondition() {
190:                 final Vendor<MyProduct> vendor = new VendorWithPrecondition();
191:                 bazaar.addVendor(vendor);
192:                 final IEclipseContext context = EclipseContextFactory.create();
193:                 final Vendor<? extends MyProduct> bestVendor = bazaar.getBestVendor(context);
194:                 assertNull(bestVendor);
195:         }
196:
197:         @Test
198:         public void testVendorMatchingPreCondition() {
199:                 final Vendor<MyProduct> vendor = new VendorWithPrecondition(0);
200:                 bazaar.addVendor(vendor);
201:                 final IEclipseContext context = EclipseContextFactory.create();
202:                 context.set(VendorWithPrecondition.KEY, VendorWithPrecondition.VALUE);
203:                 final Vendor<? extends MyProduct> bestVendor = bazaar.getBestVendor(context);
204:                 assertSame(vendor, bestVendor);
205:         }
206:
207:         @Test
208:         public void testVendorMatchingNoValuePreCondition() {
209:                 final Vendor<MyProduct> vendor = new VendorWithNoValuePrecondition();
210:                 bazaar.addVendor(vendor);
211:                 final IEclipseContext context = EclipseContextFactory.create();
212:                 context.set(VendorWithPrecondition.KEY, VendorWithPrecondition.VALUE);
213:                 final Vendor<? extends MyProduct> bestVendor = bazaar.getBestVendor(context);
214:                 assertNotNull(bestVendor);
215:         }
216:
217:         @Test
218:         public void testVendorWithNullInContextNoValuePreCondition() {
219:                 final Vendor<MyProduct> vendor = new VendorWithNoValuePrecondition();
220:                 bazaar.addVendor(vendor);
221:                 final IEclipseContext context = EclipseContextFactory.create();
222:                 context.set(VendorWithPrecondition.KEY, null);
223:                 final Vendor<? extends MyProduct> bestVendor = bazaar.getBestVendor(context);
224:                 assertNotNull(bestVendor);
225:         }
226:
227:         @Test
228:         public void testVendorNotMatchingNoValuePreCondition() {
229:                 final Vendor<MyProduct> vendor = new VendorWithNoValuePrecondition();
230:                 bazaar.addVendor(vendor);
231:                 final IEclipseContext context = EclipseContextFactory.create();
232:                 final Vendor<? extends MyProduct> bestVendor = bazaar.getBestVendor(context);
233:                 assertNull(bestVendor);
234:
235:         }
236:
237:         @Test
238:         public void testTwoVendorOneMatchingPreCondition() {
239:                 final Vendor<MyProduct> vendor = new VendorWithPrecondition(0);
240:                 final Vendor<MyProduct> vendor2 = new VendorWithTwoPreconditions(1);
241:                 bazaar.addVendor(vendor);
242:                 bazaar.addVendor(vendor2);
243:                 final IEclipseContext context = EclipseContextFactory.create();
244:                 context.set(VendorWithPrecondition.KEY, VendorWithPrecondition.VALUE);
245:                 final Vendor<? extends MyProduct> bestVendor = bazaar.getBestVendor(context);
246:                 assertSame(vendor, bestVendor);
247:         }
248:
249:         @Test
250:         public void testTwoVendorNoMatchingPreCondition() {
251:                 final Vendor<MyProduct> vendor = new VendorWithPrecondition();
252:                 final Vendor<MyProduct> vendor2 = new VendorWithTwoPreconditions();
253:                 bazaar.addVendor(vendor);
254:                 bazaar.addVendor(vendor2);
255:                 final IEclipseContext context = EclipseContextFactory.create();
256:                 final Vendor<? extends MyProduct> bestVendor = bazaar.getBestVendor(context);
257:                 assertNull(bestVendor);
258:         }
259:
260:         @Test
261:         public void testTwoVendorBothMatchingPreCondition() {
262:                 final Vendor<MyProduct> vendor = new VendorWithPrecondition(1);
263:                 final Vendor<MyProduct> vendor2 = new VendorWithTwoPreconditions(0);
264:                 bazaar.addVendor(vendor);
265:                 bazaar.addVendor(vendor2);
266:                 final IEclipseContext context = EclipseContextFactory.create();
267:                 context.set(VendorWithPrecondition.KEY, VendorWithPrecondition.VALUE);
268:                 context.set(VendorWithTwoPreconditions.KEY1, VendorWithTwoPreconditions.VALUE1);
269:                 context.set(VendorWithTwoPreconditions.KEY2, VendorWithTwoPreconditions.VALUE2);
270:                 final Vendor<? extends MyProduct> bestVendor = bazaar.getBestVendor(context);
271:                 assertSame(vendor, bestVendor);
272:         }
273:
274:         @Test
275:         public void testNoMatchingPreConditionKey() {
276:                 final Vendor<MyProduct> vendor = new VendorWithPrecondition();
277:                 bazaar.addVendor(vendor);
278:                 final IEclipseContext context = EclipseContextFactory.create();
279:                 final boolean checkPreConditions = bazaar.checkPreConditions(vendor, context);
280:                 assertFalse(checkPreConditions);
281:         }
282:
283:         @Test
284:         public void testNoMatchingPreConditionValue() {
285:                 final Vendor<MyProduct> vendor = new VendorWithPrecondition();
286:                 bazaar.addVendor(vendor);
287:                 final IEclipseContext context = EclipseContextFactory.create();
288:                 context.set(VendorWithPrecondition.KEY, mock(Object.class));
289:                 final boolean checkPreConditions = bazaar.checkPreConditions(vendor, context);
290:                 assertFalse(checkPreConditions);
291:         }
292:
293:         @Test
294:         public void testTwoPreConditionsNoMatch() {
295:                 final Vendor<MyProduct> vendor = new VendorWithTwoPreconditions();
296:                 bazaar.addVendor(vendor);
297:                 final IEclipseContext context = EclipseContextFactory.create();
298:                 context.set(VendorWithTwoPreconditions.KEY1, mock(Object.class));
299:                 final boolean checkPreConditions = bazaar.checkPreConditions(vendor, context);
300:                 assertFalse(checkPreConditions);
301:         }
302:
303:         @Test
304:         public void testTwoPreConditionsOneNoMatch() {
305:                 final Vendor<MyProduct> vendor = new VendorWithTwoPreconditions();
306:                 bazaar.addVendor(vendor);
307:                 final IEclipseContext context = EclipseContextFactory.create();
308:                 context.set(VendorWithTwoPreconditions.KEY1, VendorWithTwoPreconditions.VALUE1);
309:                 final boolean checkPreConditions = bazaar.checkPreConditions(vendor, context);
310:                 assertFalse(checkPreConditions);
311:         }
312:
313:         @Test
314:         public void testTwoPreConditionsBothMatch() {
315:                 final Vendor<MyProduct> vendor = new VendorWithTwoPreconditions();
316:                 bazaar.addVendor(vendor);
317:                 final IEclipseContext context = EclipseContextFactory.create();
318:                 context.set(VendorWithTwoPreconditions.KEY1, VendorWithTwoPreconditions.VALUE1);
319:                 context.set(VendorWithTwoPreconditions.KEY2, VendorWithTwoPreconditions.VALUE2);
320:                 final boolean checkPreConditions = bazaar.checkPreConditions(vendor, context);
321:                 assertTrue(checkPreConditions);
322:         }
323:
324:         @Test
325:         public void testMatchingPreCondition() {
326:                 final Vendor<MyProduct> vendor = new VendorWithPrecondition();
327:                 bazaar.addVendor(vendor);
328:                 final IEclipseContext context = EclipseContextFactory.create();
329:                 context.set(VendorWithPrecondition.KEY, VendorWithPrecondition.VALUE);
330:                 final boolean checkPreConditions = bazaar.checkPreConditions(vendor, context);
331:                 assertTrue(checkPreConditions);
332:         }
333:
334:         @Test
335:         public void testOneStaticBid() {
336:                 final Vendor<MyProduct> vendor = new VendorWithStaticBid0();
337:                 bazaar.addVendor(vendor);
338:                 final IEclipseContext context = EclipseContextFactory.create();
339:                 final Vendor<? extends MyProduct> bestVendor = bazaar.getBestVendor(context);
340:                 assertSame(vendor, bestVendor);
341:         }
342:
343:         @Test
344:         public void testTwoStaticBids() {
345:                 final Vendor<MyProduct> vendor = new VendorWithStaticBid0();
346:                 final Vendor<MyProduct> vendor2 = new VendorWithStaticBid2();
347:                 bazaar.addVendor(vendor);
348:                 bazaar.addVendor(vendor2);
349:                 final IEclipseContext context = EclipseContextFactory.create();
350:                 final Vendor<? extends MyProduct> bestVendor = bazaar.getBestVendor(context);
351:                 assertSame(vendor2, bestVendor);
352:         }
353:
354:         @Test
355:         public void testStaticAndDynamicBidsDynamicWins() {
356:                 final Vendor<MyProduct> vendor = new VendorWithStaticBid0();
357:                 final Vendor<MyProduct> vendor2 = new VendorPriority1Parameter0();
358:                 bazaar.addVendor(vendor);
359:                 bazaar.addVendor(vendor2);
360:                 final IEclipseContext context = EclipseContextFactory.create();
361:                 final Vendor<? extends MyProduct> bestVendor = bazaar.getBestVendor(context);
362:                 assertSame(vendor2, bestVendor);
363:         }
364:
365:         @Test
366:         public void testStaticAndDynamicBidsStaticWins() {
367:                 final Vendor<MyProduct> vendor = new VendorWithStaticBid2();
368:                 final Vendor<MyProduct> vendor2 = new VendorPriority1Parameter0();
369:                 bazaar.addVendor(vendor);
370:                 bazaar.addVendor(vendor2);
371:                 final IEclipseContext context = EclipseContextFactory.create();
372:                 final Vendor<? extends MyProduct> bestVendor = bazaar.getBestVendor(context);
373:                 assertSame(vendor, bestVendor);
374:         }
375:
376:         @Test
377:         public void testOverlappingBidsStaticDynamic() {
378:                 final Vendor<MyProduct> vendor = new VendorWithStaticBid2();
379:                 final Vendor<MyProduct> vendor2 = new VendorPriority2Parameter0();
380:                 bazaar.addVendor(vendor);
381:                 bazaar.addVendor(vendor2);
382:
383:                 @SuppressWarnings("unchecked")
384:                 final PriorityOverlapCallBack<MyProduct> priorityOverlapCallBackMock = mock(PriorityOverlapCallBack.class);
385:
386:                 bazaar.setPriorityOverlapCallBack(priorityOverlapCallBackMock);
387:
388:                 final IEclipseContext context = EclipseContextFactory.create();
389:                 final Vendor<? extends MyProduct> best = bazaar.getBestVendor(context);
390:
391:                 verify(priorityOverlapCallBackMock, times(1)).priorityOverlap(vendor, vendor2);
392:                 assertThat(best, is((Object) vendor));
393:         }
394:
395:         @Test
396:         public void testCreateProductNoParameter0() {
397:                 final MyProduct myProductMock = mock(MyProduct.class);
398:                 final Vendor<MyProduct> vendor = new VendorCreatingProductParameter0(myProductMock);
399:
400:                 final MyProduct myProduct = bazaar.createProduct(vendor, EclipseContextFactory.create());
401:                 assertSame(myProductMock, myProduct);
402:         }
403:
404:         @Test
405:         public void testCreateProductParameter1() {
406:                 final MyProduct myProductMock = mock(MyProduct.class);
407:                 final Vendor<MyProduct> vendor = new VendorCreatingProductParameter1(myProductMock);
408:
409:                 final IEclipseContext context = EclipseContextFactory.create();
410:                 context.set(String.class, TESTSTRING);
411:
412:                 final MyProduct myProduct = bazaar.createProduct(vendor, context);
413:                 assertSame(myProductMock, myProduct);
414:         }
415:
416:         @Test
417:         public void testCreateEmptyEclipseContext() {
418:                 final BazaarContext bazaarContextMock = mock(BazaarContext.class);
419:
420:                 final IEclipseContext eclipseContext = bazaar.createEclipseContext(bazaarContextMock);
421:
422:                 assertNotNull(eclipseContext);
423:         }
424:
425:         @Test
426:         public void testCreateEclipseContextOneObject() {
427:                 final Object value = new Object();
428:                 final BazaarContext bazaarContext = BazaarContext.Builder.with(
429:                         Collections.singletonMap(TESTSTRING, value)).build();
430:
431:                 final IEclipseContext eclipseContext = bazaar.createEclipseContext(bazaarContext);
432:
433:                 final Object actual = eclipseContext.get(TESTSTRING);
434:                 assertSame(value, actual);
435:         }
436:
437:         @Test
438:         public void testContextWithContextFunction() {
439:                 final Object transformed = mock(Object.class);
440:                 final BazaarContextFunction contextFunction = new BazaarContextFunctionNoParameter(transformed);
441:                 bazaar.addContextFunction(TESTSTRING, contextFunction);
442:                 final BazaarContext bazaarContextMock = mock(BazaarContext.class);
443:                 IEclipseContext eclipseContext = bazaar.createEclipseContext(bazaarContextMock);
444:                 eclipseContext = bazaar.addContextFunctions(eclipseContext);
445:                 final Object actual = eclipseContext.get(TESTSTRING);
446:                 assertSame(transformed, actual);
447:         }
448:
449:         @Test
450:         public void testContextWithContextFunctionNoMatchingParameter() {
451:                 final Object transformed = mock(Object.class);
452:                 final BazaarContextFunction contextFunction = new BazaarContextFunctionParameter1(transformed);
453:                 bazaar.addContextFunction(TESTSTRING, contextFunction);
454:                 final BazaarContext bazaarContextMock = mock(BazaarContext.class);
455:                 IEclipseContext eclipseContext = bazaar.createEclipseContext(bazaarContextMock);
456:                 eclipseContext = bazaar.addContextFunctions(eclipseContext);
457:
458:                 final Object actual = eclipseContext.get(TESTSTRING);
459:                 assertSame(null, actual);
460:         }
461:
462:         @Test
463:         public void testContextWithContextFunctionMatchingParameter() {
464:                 final Object transformed = mock(Object.class);
465:                 final BazaarContextFunction contextFunction = new BazaarContextFunctionParameter1(transformed);
466:                 bazaar.addContextFunction(TESTSTRING, contextFunction);
467:                 final BazaarContext bazaarContextMock = mock(BazaarContext.class);
468:                 IEclipseContext eclipseContext = bazaar.createEclipseContext(bazaarContextMock);
469:                 eclipseContext = bazaar.addContextFunctions(eclipseContext);
470:                 eclipseContext.set(Integer.class, new Integer(1));
471:
472:                 final Object actual = eclipseContext.get(TESTSTRING);
473:
474:                 assertSame(transformed, actual);
475:         }
476:
477:         @Test
478:         public void testContextWithContextFunctionReturningNull() {
479:                 final BazaarContextFunction contextFunction = new BazaarContextFunctionReturningNull();
480:                 bazaar.addContextFunction(TESTSTRING, contextFunction);
481:                 final BazaarContext bazaarContextMock = mock(BazaarContext.class);
482:                 IEclipseContext eclipseContext = bazaar.createEclipseContext(bazaarContextMock);
483:                 eclipseContext = bazaar.addContextFunctions(eclipseContext);
484:
485:                 final Object actual = eclipseContext.get(TESTSTRING);
486:
487:                 assertSame(null, actual);
488:         }
489:
490:         @Test
491:         public void testContextWithContextFunctionCacheValue() {
492:                 final Object transformed = mock(Object.class);
493:                 final BazaarContextFunctionWithCounter contextFunction = new BazaarContextFunctionWithCounter(transformed);
494:                 bazaar.addContextFunction(TESTSTRING, contextFunction);
495:                 final BazaarContext bazaarContextMock = mock(BazaarContext.class);
496:                 IEclipseContext eclipseContext = bazaar.createEclipseContext(bazaarContextMock);
497:                 eclipseContext = bazaar.addContextFunctions(eclipseContext);
498:
499:                 Object actual = eclipseContext.get(TESTSTRING);
500:                 actual = eclipseContext.get(TESTSTRING);
501:
502:                 assertSame(transformed, actual);
503:                 assertSame(1, contextFunction.getCount());
504:         }
505:
506:         @Test
507:         public void testCreateProduct() {
508:                 final BazaarContextFunction contextFunction = new BazaarContextFunctionParameter1(TESTSTRING);
509:                 bazaar.addContextFunction(String.class.getName(), contextFunction);
510:                 final BazaarContext bazaarContextMock = mock(BazaarContext.class);
511:                 final HashMap<String, Object> contextMap = new HashMap<String, Object>();
512:                 contextMap.put(Integer.class.getName(), new Integer(0));
513:                 when(bazaarContextMock.getContextMap()).thenReturn(contextMap);
514:
515:                 final MyProduct myProductMock = mock(MyProduct.class);
516:                 final Vendor<MyProduct> vendor = new FullVendorParameter2(myProductMock);
517:                 bazaar.addVendor(vendor);
518:
519:                 final MyProduct createdProduct = bazaar.createProduct(bazaarContextMock);
520:
521:                 assertSame(myProductMock, createdProduct);
522:         }
523:
524:         @Test
525:         public void testCreateProductNoBiddingVendor() {
526:                 final BazaarContextFunction contextFunction = new BazaarContextFunctionParameter1(TESTSTRING);
527:                 bazaar.addContextFunction(String.class.getName(), contextFunction);
528:
529:                 final BazaarContext bazaarContext = BazaarContext.Builder.empty()
530:                         .put(Integer.class, 0).build();
531:
532:                 final MyProduct myProductMock = mock(MyProduct.class);
533:                 bazaar.addVendor(new VendorCreatingProductParameter0(myProductMock) {
534:                         @Bid
535:                         public Double bid(String string) {
536:                                 return null;
537:                         }
538:                 });
539:
540:                 final MyProduct createdProduct = bazaar.createProduct(bazaarContext);
541:                 assertThat(createdProduct, nullValue());
542:         }
543:
544:         @Test
545:         public void testCreateProductNoVendor() {
546:
547:                 final BazaarContext bazaarContext = BazaarContext.Builder.empty().build();
548:
549:                 final MyProduct createdProduct = bazaar.createProduct(bazaarContext);
550:                 assertThat(createdProduct, nullValue());
551:         }
552:
553:         @Test
554:         public void testCreateProductsWithPriority() {
555:                 final BazaarContextFunction contextFunction = new BazaarContextFunctionParameter1(TESTSTRING);
556:                 bazaar.addContextFunction(String.class.getName(), contextFunction);
557:
558:                 final BazaarContext bazaarContext = BazaarContext.Builder.empty()
559:                         .add(0).build();
560:
561:                 final MyProduct myProductMock1 = mock(MyProduct.class);
562:                 bazaar.addVendor(new VendorPriority01Parameter1() {
563:                         @Create
564:                         public MyProduct make(String string) {
565:                                 return myProductMock1;
566:                         }
567:                 });
568:                 final MyProduct myProductMock2 = mock(MyProduct.class);
569:                 bazaar.addVendor(new VendorPriority1Parameter1() {
570:                         @Create
571:                         public MyProduct make(String string) {
572:                                 return myProductMock2;
573:                         }
574:                 });
575:                 final MyProduct myProductMock3 = mock(MyProduct.class);
576:                 bazaar.addVendor(new VendorPriority2Parameter1() {
577:                         @Create
578:                         public MyProduct make(String string) {
579:                                 return myProductMock3;
580:                         }
581:                 });
582:
583:                 final List<MyProduct> createdProducts = bazaar.createProducts(bazaarContext);
584:
585:                 // The vendor bids order these in reverse (high bid first)
586:                 assertThat(createdProducts, is(asList(myProductMock3, myProductMock2, myProductMock1)));
587:         }
588:
589:         @Test
590:         public void testCreateProductsNoBid() {
591:
592:                 final BazaarContext bazaarContext = BazaarContext.Builder.empty().build();
593:
594:                 final MyProduct myProductMock = mock(MyProduct.class);
595:                 bazaar.addVendor(new VendorCreatingProductParameter1(myProductMock)); // No bid
596:
597:                 final List<MyProduct> createdProducts = bazaar.createProducts(bazaarContext);
598:
599:                 assertThat(createdProducts, not(hasItem(any(MyProduct.class))));
600:         }
601:
602:         @Test
603:         public void testCreateProductsBidButNoCreate() {
604:
605:                 final BazaarContext bazaarContext = BazaarContext.Builder.empty().build();
606:
607:                 final MyProduct myProductMock = mock(MyProduct.class);
608:                 bazaar.addVendor(new VendorCreatingProductParameter1(myProductMock) {
609:                         @Bid
610:                         public double bid() {
611:                                 return 2.0;
612:                         }
613:                 });
614:
615:                 final List<MyProduct> createdProducts = bazaar.createProducts(bazaarContext);
616:
617:                 assertTrue(createdProducts.isEmpty());
618:         }
619:
620:         @Test
621:         public void testCreateProductsBidCreateNull() {
622:
623:                 final BazaarContext bazaarContext = BazaarContext.Builder.empty().build();
624:
625:                 bazaar.addVendor(new Vendor<MyProduct>() {
626:
627:                         @Bid
628:                         public double bid() {
629:                                 return 2.0;
630:                         }
631:
632:                         @Create
633:                         public MyProduct create() {
634:                                 return null;
635:                         }
636:                 });
637:
638:                 final List<MyProduct> createdProducts = bazaar.createProducts(bazaarContext);
639:
640:                 assertTrue(createdProducts.isEmpty());
641:         }
642:
643:         @Test
644:         public void testRemoveVendor() {
645:                 final Vendor<MyProduct> vendor = new VendorPriority1Parameter0();
646:                 final Vendor<MyProduct> vendor2 = new VendorPriority2Parameter1();
647:                 bazaar.addVendor(vendor);
648:                 bazaar.addVendor(vendor2);
649:
650:                 final IEclipseContext context = EclipseContextFactory.create();
651:                 context.set(String.class, TESTSTRING);
652:
653:                 Vendor<?> bestVendor = bazaar.getBestVendor(context);
654:
655:                 assumeThat(bestVendor, is((Object) vendor2));
656:
657:                 bazaar.removeVendor(vendor2);
658:
659:                 bestVendor = bazaar.getBestVendor(context);
660:
661:                 assertThat(bestVendor, is((Object) vendor));
662:         }
663:
664: }