Skip to content

Package: Bazaar_PTest$1

Bazaar_PTest$1

nameinstructionbranchcomplexitylinemethod
bid(Class)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
{...}
M: 0 C: 7
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: ******************************************************************************/
14: package org.eclipse.emfforms.bazaar.internal;
15:
16: import static org.hamcrest.CoreMatchers.is;
17: import static org.hamcrest.MatcherAssert.assertThat;
18: import static org.junit.Assert.assertTrue;
19: import static org.mockito.Mockito.mock;
20:
21: import javax.inject.Named;
22:
23: import org.eclipse.e4.core.contexts.EclipseContextFactory;
24: import org.eclipse.e4.core.contexts.IEclipseContext;
25: import org.eclipse.emfforms.bazaar.BazaarContext;
26: import org.eclipse.emfforms.bazaar.BazaarContextFunction;
27: import org.eclipse.emfforms.bazaar.Bid;
28: import org.eclipse.emfforms.bazaar.Vendor;
29: import org.junit.Before;
30: import org.junit.Test;
31:
32: /**
33: * @author jonas
34: *
35: */
36: public class Bazaar_PTest {
37:
38:         public static final String TESTSTRING = ""; //$NON-NLS-1$
39:         private BazaarImpl<MyProduct> bazaar;
40:
41:         /**
42:          * @throws java.lang.Exception
43:          */
44:         @Before
45:         public void setUp() throws Exception {
46:                 bazaar = new BazaarImpl<MyProduct>();
47:         }
48:
49:         /**
50:          * Can be moved to ITest once BR 513883 is fixed in platform
51:          */
52:         @Test
53:         public void testNamedContextVariable() {
54:
55:                 final BazaarContextFunction contextFunction = new BazaarContextFunctionParameter1(TESTSTRING);
56:                 bazaar.addContextFunction(String.class.getName(), contextFunction);
57:
58:                 final BazaarContext bazaarContext = BazaarContext.Builder.empty()
59:                         .put(Integer.class, 0)
60:                         .put("superclass", Number.class) //$NON-NLS-1$
61:                         .build();
62:
63:                 final MyProduct myProductMock1 = mock(MyProduct.class);
64:                 bazaar.addVendor(new VendorCreatingProductParameter0(myProductMock1) {
65:                         @Bid
66:                         public double bid(Class<?> clazz) {
67:                                 return 3.0;
68:                         }
69:                 });
70:                 final MyProduct myProductMock2 = mock(MyProduct.class);
71:                 bazaar.addVendor(new VendorCreatingProductParameter0(myProductMock2) {
72:                         @Bid
73:                         public double bid(@Named("superclass") Class<?> superclass) {
74:                                 return 2.0; // Lesser bid but the other isn't eligible
75:                         }
76:                 });
77:
78:                 final MyProduct createdProduct = bazaar.createProduct(bazaarContext);
79:                 assertThat(createdProduct, is(myProductMock2));
80:         }
81:
82:         @Test
83:         public void testPreConditionPerformanceNoMatch() {
84:                 final IEclipseContext context = EclipseContextFactory.create();
85:                 // Add another value, otherwise, empty context is too fast
86:                 context.set(TESTSTRING, mock(Object.class));
87:                 doComparison(true, context);
88:         }
89:
90:         @Test
91:         public void testPreConditionPerformanceMatch() {
92:                 final IEclipseContext context = EclipseContextFactory.create();
93:                 // Add another value, otherwise, empty context is too fast
94:                 context.set(VendorWithPrecondition.KEY, VendorWithPrecondition.VALUE);
95:                 doComparison(false, context);
96:         }
97:
98:         /**
99:          * If expectedTheWithPreConditionsBetter, asserts to true if that is the case.
100:          * Otherwise asserts to true, if with precondition is not more than 60% slower.
101:          */
102:         private void doComparison(boolean expectedTheWithPreConditionsBetter, IEclipseContext context) {
103:                 final int iterations = 50000;
104:                 final Vendor<MyProduct> vendor = new VendorWithPrecondition();
105:                 bazaar.addVendor(vendor);
106:                 long currentTimeMillis = System.currentTimeMillis();
107:                 for (int i = 0; i < iterations; i++) {
108:                         bazaar.getBestVendor(context);
109:                 }
110:                 final long withPreConditions = System.currentTimeMillis() - currentTimeMillis;
111:                 bazaar.removeVendor(vendor);
112:
113:                 final Vendor<MyProduct> vendor2 = new VendorWithoutPrecondition();
114:                 bazaar.addVendor(vendor2);
115:                 currentTimeMillis = System.currentTimeMillis();
116:                 for (int i = 0; i < iterations; i++) {
117:                         bazaar.getBestVendor(context);
118:                 }
119:                 final long withoutPreConditions = System.currentTimeMillis() - currentTimeMillis;
120:
121:                 if (expectedTheWithPreConditionsBetter) {
122:                         assertTrue(
123:                                 String.format("The preCondition evaluation (%1$s ms) is slower then withoutPreCondition (%2$s ms)!", //$NON-NLS-1$
124:                                         withPreConditions, withoutPreConditions),
125:                                 withPreConditions < withoutPreConditions);
126:                 } else {
127:                         final double withoutPreConditionsSlack = 1.6 * withoutPreConditions;
128:                         assertTrue(String.format(
129:                                 "The preCondition evaluation (%1$s ms) is slower then withoutPreCondition with 60%% slack (%2$s ms)!", //$NON-NLS-1$
130:                                 withPreConditions, withoutPreConditionsSlack), withPreConditions < withoutPreConditionsSlack);
131:                 }
132:         }
133: }