Skip to content

Package: BazaarBuilder_Test

BazaarBuilder_Test

nameinstructionbranchcomplexitylinemethod
BazaarBuilder_Test(BazaarVariant)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
addAllVendors()
M: 0 C: 63
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
addThreeVendors()
M: 0 C: 56
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
addTwoVendors()
M: 0 C: 41
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
addVendor()
M: 0 C: 23
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
basicFixture()
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
context()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
onPriorityOverlap()
M: 0 C: 44
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
onPriorityOverlapAlreadySet()
M: 5 C: 15
75%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 4
67%
M: 0 C: 1
100%
parameters()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
vendorFixture(Vendor[])
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
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.bazaar.internal;
15:
16: import static java.util.Arrays.asList;
17: import static org.hamcrest.CoreMatchers.hasItems;
18: import static org.hamcrest.CoreMatchers.is;
19: import static org.hamcrest.MatcherAssert.assertThat;
20: import static org.mockito.Mockito.mock;
21: import static org.mockito.Mockito.times;
22: import static org.mockito.Mockito.verify;
23:
24: import java.util.Arrays;
25:
26: import org.eclipse.e4.core.contexts.EclipseContextFactory;
27: import org.eclipse.emfforms.bazaar.Bazaar;
28: import org.eclipse.emfforms.bazaar.Bazaar.PriorityOverlapCallBack;
29: import org.eclipse.emfforms.bazaar.BazaarContext;
30: import org.eclipse.emfforms.bazaar.Vendor;
31: import org.junit.Test;
32: import org.junit.runner.RunWith;
33: import org.junit.runners.Parameterized;
34: import org.junit.runners.Parameterized.Parameters;
35:
36: /**
37: * Test cases for the {@link org.eclipse.emfforms.bazaar.Bazaar.Builder} class.
38: *
39: * @author Christian W. Damus
40: */
41: @RunWith(Parameterized.class)
42: public class BazaarBuilder_Test {
43:
44:         private final BazaarVariant variant;
45:
46:         /**
47:          * Initializes me.
48:          */
49:         public BazaarBuilder_Test(BazaarVariant variant) {
50:                 super();
51:
52:                 this.variant = variant;
53:         }
54:
55:         @Test
56:         public void addVendor() {
57:                 final MyProduct myProductMock = mock(MyProduct.class);
58:                 final Bazaar.Builder<MyProduct> fixture = basicFixture();
59:                 final Bazaar<MyProduct> bazaar = fixture
60:                         .add(new FullVendorParameter2(myProductMock))
61:                         .build();
62:
63:                 assertThat(bazaar.createProduct(context()), is(myProductMock));
64:         }
65:
66:         @Test
67:         public void addTwoVendors() {
68:                 final MyProduct myProductMock1 = mock(MyProduct.class);
69:                 final MyProduct myProductMock2 = mock(MyProduct.class);
70:                 final Bazaar.Builder<MyProduct> fixture = basicFixture();
71:                 @SuppressWarnings("unchecked")
72:                 final Bazaar<MyProduct> bazaar = fixture
73:                         .add(new FullVendorParameter2(myProductMock1), new FullVendorParameter2(myProductMock2))
74:                         .build();
75:
76:                 assertThat(bazaar.createProducts(context()), hasItems(myProductMock1, myProductMock2));
77:         }
78:
79:         @Test
80:         public void addThreeVendors() {
81:                 final MyProduct myProductMock1 = mock(MyProduct.class);
82:                 final MyProduct myProductMock2 = mock(MyProduct.class);
83:                 final MyProduct myProductMock3 = mock(MyProduct.class);
84:                 final Bazaar.Builder<MyProduct> fixture = basicFixture();
85:                 @SuppressWarnings("unchecked")
86:                 final Bazaar<MyProduct> bazaar = fixture
87:                         .add(new FullVendorParameter2(myProductMock1),
88:                                 new FullVendorParameter2(myProductMock2),
89:                                 new FullVendorParameter2(myProductMock3))
90:                         .build();
91:
92:                 assertThat(bazaar.createProducts(context()), hasItems(myProductMock1, myProductMock2, myProductMock3));
93:         }
94:
95:         @Test
96:         public void addAllVendors() {
97:                 final MyProduct myProductMock1 = mock(MyProduct.class);
98:                 final MyProduct myProductMock2 = mock(MyProduct.class);
99:                 final MyProduct myProductMock3 = mock(MyProduct.class);
100:                 final Bazaar.Builder<MyProduct> fixture = basicFixture();
101:                 final Bazaar<MyProduct> bazaar = fixture
102:                         .addAll(Arrays.asList(new FullVendorParameter2(myProductMock1),
103:                                 new FullVendorParameter2(myProductMock2),
104:                                 new FullVendorParameter2(myProductMock3)))
105:                         .build();
106:
107:                 assertThat(bazaar.createProducts(context()), hasItems(myProductMock1, myProductMock2, myProductMock3));
108:         }
109:
110:         @Test
111:         public void onPriorityOverlap() {
112:                 final Vendor<MyProduct> vendor = new VendorPriority1Parameter0();
113:                 final Vendor<MyProduct> vendor2 = new VendorPriority1Parameter0();
114:                 @SuppressWarnings("unchecked")
115:                 final Bazaar.Builder<MyProduct> fixture = vendorFixture(vendor, vendor2);
116:
117:                 @SuppressWarnings("unchecked")
118:                 final PriorityOverlapCallBack<MyProduct> priorityOverlapCallBackMock = mock(PriorityOverlapCallBack.class);
119:                 final Bazaar<MyProduct> bazaar = fixture
120:                         .onPriorityOverlap(priorityOverlapCallBackMock)
121:                         .build();
122:
123:                 ((BazaarImpl<?>) bazaar).getBestVendor(EclipseContextFactory.create());
124:
125:                 verify(priorityOverlapCallBackMock, times(1)).priorityOverlap(vendor, vendor2);
126:         }
127:
128:         @Test(expected = IllegalStateException.class)
129:         public void onPriorityOverlapAlreadySet() {
130:                 final Bazaar.Builder<MyProduct> fixture = basicFixture();
131:
132:                 @SuppressWarnings("unchecked")
133:                 final PriorityOverlapCallBack<MyProduct> priorityOverlapCallBackMock1 = mock(PriorityOverlapCallBack.class);
134:                 @SuppressWarnings("unchecked")
135:                 final PriorityOverlapCallBack<MyProduct> priorityOverlapCallBackMock2 = mock(PriorityOverlapCallBack.class);
136:
137:                 fixture.onPriorityOverlap(priorityOverlapCallBackMock1);
138:                 fixture.onPriorityOverlap(priorityOverlapCallBackMock2);
139:         }
140:
141:         //
142:         // Test framework
143:         //
144:
145:         @Parameters(name = "{0}")
146:         public static Object[] parameters() {
147:                 return BazaarVariant.values();
148:         }
149:
150:         Bazaar.Builder<MyProduct> basicFixture() {
151:                 return variant.<MyProduct> builder()
152:                         .addContextFunction(String.class, new BazaarContextFunctionParameter1("")); //$NON-NLS-1$
153:         }
154:
155:         Bazaar.Builder<MyProduct> vendorFixture(Vendor<? extends MyProduct>... vendors) {
156:                 return variant.builder(asList(vendors))
157:                         .addContextFunction(String.class, new BazaarContextFunctionParameter1("")); //$NON-NLS-1$
158:         }
159:
160:         static BazaarContext context() {
161:                 return BazaarContext.Builder.empty().add(0).build();
162:         }
163: }