Skip to content

Package: BlankTemplateProvider_PTest$3

BlankTemplateProvider_PTest$3

nameinstructionbranchcomplexitylinemethod
matchesSafely(EObject)
M: 2 C: 12
86%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 0 C: 2
100%
M: 0 C: 1
100%
{...}
M: 0 C: 10
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: * Lucas Koehler - initial API and implementation
13: * Christian W. Damus - bug 529138
14: ******************************************************************************/
15: package org.eclipse.emfforms.internal.core.services.datatemplate;
16:
17: import static java.lang.Double.POSITIVE_INFINITY;
18: import static org.hamcrest.CoreMatchers.both;
19: import static org.hamcrest.CoreMatchers.hasItem;
20: import static org.hamcrest.CoreMatchers.is;
21: import static org.hamcrest.MatcherAssert.assertThat;
22: import static org.mockito.Matchers.any;
23: import static org.mockito.Matchers.anyCollectionOf;
24: import static org.mockito.Mockito.mock;
25: import static org.mockito.Mockito.when;
26:
27: import java.util.Collection;
28: import java.util.Collections;
29: import java.util.Set;
30:
31: import org.eclipse.emf.ecore.EClass;
32: import org.eclipse.emf.ecore.EEnum;
33: import org.eclipse.emf.ecore.ENamedElement;
34: import org.eclipse.emf.ecore.EObject;
35: import org.eclipse.emf.ecore.EPackage;
36: import org.eclipse.emf.ecore.EReference;
37: import org.eclipse.emf.ecore.EcoreFactory;
38: import org.eclipse.emf.ecore.EcorePackage;
39: import org.eclipse.emf.ecore.resource.Resource;
40: import org.eclipse.emf.ecp.ui.view.swt.reference.DefaultCreateNewModelElementStrategyProvider;
41: import org.eclipse.emf.ecp.ui.view.swt.reference.EClassSelectionStrategy;
42: import org.eclipse.emfforms.bazaar.Bid;
43: import org.eclipse.emfforms.bazaar.Create;
44: import org.eclipse.emfforms.datatemplate.Template;
45: import org.eclipse.emfforms.spi.common.BundleResolver;
46: import org.eclipse.emfforms.spi.localization.EMFFormsLocalizationService;
47: import org.hamcrest.CoreMatchers;
48: import org.hamcrest.CustomTypeSafeMatcher;
49: import org.hamcrest.FeatureMatcher;
50: import org.hamcrest.Matcher;
51: import org.junit.Before;
52: import org.junit.Test;
53: import org.mockito.invocation.InvocationOnMock;
54: import org.mockito.stubbing.Answer;
55: import org.osgi.framework.Bundle;
56:
57: /**
58: * Unit tests for {@link BlankTemplateProvider}.
59: *
60: * @author Lucas Koehler
61: *
62: */
63: @SuppressWarnings({ "nls" })
64: public class BlankTemplateProvider_PTest {
65:
66:         private BlankTemplateProvider provider;
67:         private Resource resource;
68:
69:         @Test
70:         public void provideEMFEditConfiguredObject() {
71:                 final EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
72:                 resource.getContents().add(ePackage);
73:
74:                 assertThat(provider.canProvideTemplates(ePackage, EcorePackage.Literals.EPACKAGE__ECLASSIFIERS), is(true));
75:                 final Set<Template> templates = provider.provideTemplates(ePackage,
76:                         EcorePackage.Literals.EPACKAGE__ECLASSIFIERS);
77:
78:                 assertThat(templates,
79:                         hasItem(templateThat(both(CoreMatchers.<EObject> instanceOf(EEnum.class))
80:                                 .and(named("NewEnum1")))));
81:         }
82:
83:         //
84:         // Test framework
85:         //
86:
87:         @Before
88:         public void createTemplateProvider() {
89:                 provider = new BlankTemplateProvider();
90:                 final EMFFormsLocalizationService localizationService = mock(EMFFormsLocalizationService.class);
91:                 when(localizationService.getString(BlankTemplateProvider.class,
92:                         MessageKeys.BlankTemplateProvider_blankTemplateLabel)).thenReturn("Blank {0}");
93:                 when(localizationService.getString(any(Bundle.class), any(String.class))).thenAnswer(new Answer<String>() {
94:
95:                         @Override
96:                         public String answer(InvocationOnMock invocation) throws Throwable {
97:                                 final String classKey = (String) invocation.getArguments()[1];
98:                                 return classKey.substring(4, classKey.length() - 5);
99:                         }
100:                 });
101:
102:                 final BundleResolver bundleResolver = mock(BundleResolver.class);
103:                 provider.setBundleResolver(bundleResolver);
104:                 provider.setLocalizationService(localizationService);
105:
106:                 final DefaultCreateNewModelElementStrategyProvider dcnmesp = new DefaultCreateNewModelElementStrategyProvider();
107:                 dcnmesp.addEClassSelectionStrategyProvider(new TestEClassSelectionStrategyProvider(
108:                         POSITIVE_INFINITY, forceEClass(EcorePackage.Literals.EENUM)));
109:                 provider.setDefaultNewElementStrategyProvider(dcnmesp);
110:         }
111:
112:         @Before
113:         public void createResource() {
114:                 final EEnum template = EcoreFactory.eINSTANCE.createEEnum();
115:                 template.setName("NewEnum1");
116:
117:                 resource = new EMFEditNewChildFactoryBuilder()
118:                         .addTemplate(EPackage.class, EcorePackage.Literals.EPACKAGE__ECLASSIFIERS, template)
119:                         .buildResource();
120:         }
121:
122:         /**
123:          * Obtain a Hamcrest matcher for templates whose instances match the given instance matcher.
124:          *
125:          * @param instanceMatcher a template instance matcher
126:          * @return the template matcher
127:          */
128:         Matcher<Template> templateThat(Matcher<? super EObject> instanceMatcher) {
129:                 return new FeatureMatcher<Template, EObject>(instanceMatcher, "object", "object") {
130:                         @Override
131:                         protected EObject featureValueOf(Template actual) {
132:                                 return actual.getInstance();
133:                         }
134:                 };
135:         }
136:
137:         /**
138:          * Obtain a Hamcrest matcher for objects that the EMF named elements having the given name.
139:          *
140:          * @param name the element name to match
141:          * @return the matcher
142:          */
143:         Matcher<EObject> named(final String name) {
144:                 return new CustomTypeSafeMatcher<EObject>("named " + name) {
145:                         @Override
146:                         protected boolean matchesSafely(EObject item) {
147:•                                return item instanceof ENamedElement
148:•                                        && name.equals(((ENamedElement) item).getName());
149:                         }
150:                 };
151:         }
152:
153:         /**
154:          * Create a mock EClass selection strategy (suitable for verification) that forces
155:          * selection of the given class.
156:          *
157:          * @param eClass the class to force
158:          * @return the mocl strategy
159:          */
160:         EClassSelectionStrategy forceEClass(EClass eClass) {
161:                 final EClassSelectionStrategy result = mock(EClassSelectionStrategy.class);
162:                 final Collection<EClass> eClasses = Collections.singleton(eClass);
163:                 when(result.collectEClasses(any(EObject.class), any(EReference.class),
164:                         anyCollectionOf(EClass.class))).thenReturn(eClasses);
165:
166:                 return result;
167:         }
168:
169:         //
170:         // Nested types
171:         //
172:
173:         class TestEClassSelectionStrategyProvider implements EClassSelectionStrategy.Provider {
174:                 private final Double bid;
175:                 private final EClassSelectionStrategy strategy;
176:
177:                 TestEClassSelectionStrategyProvider(Double bid, EClassSelectionStrategy strategy) {
178:                         super();
179:
180:                         this.bid = bid;
181:                         this.strategy = strategy;
182:                 }
183:
184:                 @Bid
185:                 public Double bid() {
186:                         return bid;
187:                 }
188:
189:                 @Create
190:                 public EClassSelectionStrategy create() {
191:                         return strategy;
192:                 }
193:         }
194: }