Skip to content

Package: EClassLabelProvider_PTest

EClassLabelProvider_PTest

nameinstructionbranchcomplexitylinemethod
EClassLabelProvider_PTest()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
configureL10nService()
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%
containsRegex(String)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
createLabelProvider()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getImage_editImagesAvailable()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getImage_editImagesNonEClass()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getImage_editImagesUnavailable()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getText_editLabelsAvailable()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getText_editLabelsUnavailable()
M: 0 C: 47
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
getText_nonEClass()
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
verifyL10nService()
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.internal.core.services.datatemplate;
15:
16: import static org.hamcrest.CoreMatchers.is;
17: import static org.hamcrest.CoreMatchers.notNullValue;
18: import static org.hamcrest.CoreMatchers.nullValue;
19: import static org.junit.Assert.assertThat;
20: import static org.mockito.Matchers.any;
21: import static org.mockito.Matchers.anyString;
22: import static org.mockito.Mockito.atLeastOnce;
23: import static org.mockito.Mockito.never;
24: import static org.mockito.Mockito.spy;
25: import static org.mockito.Mockito.verify;
26: import static org.mockito.Mockito.when;
27:
28: import java.util.regex.Pattern;
29:
30: import org.eclipse.emf.ecore.EClass;
31: import org.eclipse.emf.ecore.EPackage;
32: import org.eclipse.emf.ecore.EcoreFactory;
33: import org.eclipse.emf.ecore.EcorePackage;
34: import org.eclipse.emfforms.core.services.datatemplate.test.model.audit.AuditPackage;
35: import org.eclipse.emfforms.spi.localization.EMFFormsLocalizationService;
36: import org.hamcrest.CustomTypeSafeMatcher;
37: import org.hamcrest.Matcher;
38: import org.junit.After;
39: import org.junit.Before;
40: import org.junit.Test;
41: import org.junit.runner.RunWith;
42: import org.mockito.Mock;
43: import org.mockito.runners.MockitoJUnitRunner;
44: import org.osgi.framework.Bundle;
45:
46: /**
47: * Unit tests for the {@link EClassLabelProvider} class.
48: */
49: @SuppressWarnings("nls")
50: @RunWith(MockitoJUnitRunner.class)
51: public class EClassLabelProvider_PTest {
52:
53:         @Mock
54:         private EMFFormsLocalizationService l10nService;
55:
56:         private EClassLabelProvider labelProvider;
57:
58:         /**
59:          * Initializes me.
60:          */
61:         public EClassLabelProvider_PTest() {
62:                 super();
63:         }
64:
65:         /**
66:          * Test the provision of text for classes that have edit bundles.
67:          */
68:         @Test
69:         public void getText_editLabelsAvailable() {
70:                 assertThat(labelProvider.getText(EcorePackage.Literals.EDATA_TYPE), is("PASS"));
71:         }
72:
73:         /**
74:          * Test the provision of text for classes that do not have edit bundles.
75:          */
76:         @Test
77:         public void getText_editLabelsUnavailable() {
78:                 final EPackage ePackage = spy(EcoreFactory.eINSTANCE.createEPackage());
79:                 ePackage.setNsURI("http://www.example.com/test/foo");
80:                 ePackage.setName("foo");
81:                 final EClass eClass = EcoreFactory.eINSTANCE.createEClass();
82:                 eClass.setName("FooCamelCase");
83:                 ePackage.getEClassifiers().add(eClass);
84:
85:                 assertThat(labelProvider.getText(eClass), is("FooCamelCase"));
86:
87:                 verify(ePackage, atLeastOnce()).getNsURI();
88:
89:                 // We didn't attempt to get a string from any bundle
90:                 verify(l10nService, never()).getString(any(Bundle.class), anyString());
91:         }
92:
93:         /**
94:          * Test the provision of text for non-classes.
95:          */
96:         @Test
97:         public void getText_nonEClass() {
98:                 assertThat(labelProvider.getText(new Object()), containsRegex("@\\p{XDigit}+"));
99:
100:                 // We didn't attempt to get a string from any bundle
101:                 verify(l10nService, never()).getString(any(Bundle.class), anyString());
102:         }
103:
104:         /**
105:          * Test the provision of images for classes that have edit bundles.
106:          */
107:         @Test
108:         public void getImage_editImagesAvailable() {
109:                 assertThat(labelProvider.getImage(EcorePackage.Literals.EDATA_TYPE), notNullValue());
110:         }
111:
112:         /**
113:          * Test the provision of images for classes that do not have edit bundles.
114:          */
115:         @Test
116:         public void getImage_editImagesUnavailable() {
117:                 assertThat(labelProvider.getImage(AuditPackage.Literals.GUEST_USER), nullValue());
118:         }
119:
120:         /**
121:          * Test the provision of images for non-classes.
122:          */
123:         @Test
124:         public void getImage_editImagesNonEClass() {
125:                 assertThat(labelProvider.getImage(new Object()), nullValue());
126:         }
127:
128:         //
129:         // Test framework
130:         //
131:
132:         @Before
133:         public void createLabelProvider() {
134:                 labelProvider = new EClassLabelProvider(l10nService);
135:         }
136:
137:         @Before
138:         public void configureL10nService() {
139:                 when(l10nService.getString(any(Bundle.class), anyString())).thenReturn("PASS");
140:         }
141:
142:         @After
143:         public void verifyL10nService() {
144:                 verify(l10nService, never()).getString(any(Class.class), anyString());
145:         }
146:
147:         static Matcher<String> containsRegex(String regex) {
148:                 final Pattern pattern = Pattern.compile(regex);
149:                 return new CustomTypeSafeMatcher<String>("=~ " + pattern) {
150:                         @Override
151:                         protected boolean matchesSafely(String item) {
152:                                 return pattern.matcher(item).find();
153:                         }
154:                 };
155:         }
156: }