Skip to content

Package: ItemProviderEnumCellEditor_PTest

ItemProviderEnumCellEditor_PTest

nameinstructionbranchcomplexitylinemethod
ItemProviderEnumCellEditor_PTest()
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%
disposeShell()
M: 0 C: 11
100%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 0 C: 3
100%
M: 0 C: 1
100%
getFormatedString_localized()
M: 0 C: 59
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
getFormatedString_noBundle()
M: 0 C: 51
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
getFormatedString_noLocalizedString()
M: 0 C: 58
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
setUp()
M: 0 C: 97
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 20
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2019 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: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.spi.table.swt;
15:
16: import static org.junit.Assert.assertEquals;
17: import static org.mockito.Matchers.any;
18: import static org.mockito.Mockito.mock;
19: import static org.mockito.Mockito.never;
20: import static org.mockito.Mockito.times;
21: import static org.mockito.Mockito.verify;
22: import static org.mockito.Mockito.when;
23:
24: import org.eclipse.emf.ecore.EClass;
25: import org.eclipse.emf.ecore.EEnum;
26: import org.eclipse.emf.ecore.EEnumLiteral;
27: import org.eclipse.emf.ecore.EStructuralFeature;
28: import org.eclipse.emf.ecore.EcoreFactory;
29: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
30: import org.eclipse.emfforms.spi.common.BundleResolver;
31: import org.eclipse.emfforms.spi.common.BundleResolver.NoBundleFoundException;
32: import org.eclipse.emfforms.spi.common.report.ReportService;
33: import org.eclipse.emfforms.spi.localization.EMFFormsLocalizationService;
34: import org.eclipse.swt.SWT;
35: import org.eclipse.swt.widgets.Display;
36: import org.eclipse.swt.widgets.Shell;
37: import org.junit.After;
38: import org.junit.Before;
39: import org.junit.Test;
40: import org.osgi.framework.Bundle;
41:
42: /**
43: * Unit tests for {@link ItemProviderEnumCellEditor}. Run as plugin test to be able to create a new Shell.
44: *
45: * @author Lucas Koehler
46: *
47: */
48: public class ItemProviderEnumCellEditor_PTest {
49:
50:         private static final String TEST_ENUM = "TestEnum";
51:         private static final String TEST_LITERAL = "test literal";
52:         private static final String TEST_NAME = "testName";
53:         private static final String KEY_TEMPLATE = "_UI_" + TEST_ENUM + "_%s_literal";
54:
55:         private ItemProviderEnumCellEditor cellEditor;
56:         private Shell shell;
57:         private BundleResolver bundleResolver;
58:         private EMFFormsLocalizationService localization;
59:         private EStructuralFeature feature;
60:         private ViewModelContext viewContext;
61:         private ReportService reportService;
62:
63:         private EEnumLiteral testLiteral;
64:
65:         @Before
66:         public void setUp() {
67:                 shell = new Shell(Display.getDefault());
68:                 bundleResolver = mock(BundleResolver.class);
69:                 localization = mock(EMFFormsLocalizationService.class);
70:                 viewContext = mock(ViewModelContext.class);
71:                 reportService = mock(ReportService.class);
72:                 when(viewContext.getService(ReportService.class)).thenReturn(reportService);
73:                 cellEditor = new ItemProviderEnumCellEditor(shell, SWT.NONE, bundleResolver, localization);
74:
75:                 final EEnum testEnum = EcoreFactory.eINSTANCE.createEEnum();
76:                 testEnum.setName(TEST_ENUM);
77:                 testLiteral = EcoreFactory.eINSTANCE.createEEnumLiteral();
78:                 testLiteral.setName(TEST_NAME);
79:                 testLiteral.setLiteral(TEST_LITERAL);
80:                 testEnum.getELiterals().add(testLiteral);
81:
82:                 feature = EcoreFactory.eINSTANCE.createEAttribute();
83:                 feature.setName("TestAttribute");
84:                 feature.setEType(testEnum);
85:
86:                 final EClass containerClass = EcoreFactory.eINSTANCE.createEClass();
87:                 containerClass.setName("ContainerClass");
88:                 containerClass.getEStructuralFeatures().add(feature);
89:         }
90:
91:         @After
92:         public void disposeShell() {
93:•                if (shell != null && !shell.isDisposed()) {
94:                         shell.dispose();
95:                 }
96:         }
97:
98:         @SuppressWarnings("unchecked")
99:         @Test
100:         public void getFormatedString_noBundle() throws NoBundleFoundException {
101:                 when(bundleResolver.getEditBundle(any())).thenThrow(NoBundleFoundException.class);
102:                 cellEditor.instantiate(feature, viewContext);
103:                 final String result = cellEditor.getFormatedString(testLiteral);
104:
105:                 verify(localization, never()).getString((Bundle) any(), any());
106:                 verify(reportService, times(1)).report(any());
107:                 // original literal text
108:                 assertEquals(TEST_LITERAL, result);
109:         }
110:
111:         @Test
112:         public void getFormatedString_localized() throws NoBundleFoundException {
113:                 final String key = String.format(KEY_TEMPLATE, TEST_NAME);
114:                 final String localized = "some localization";
115:
116:                 final Bundle bundle = mock(Bundle.class);
117:                 when(bundleResolver.getEditBundle(feature.getEType())).thenReturn(bundle);
118:                 when(localization.getString(bundle, key)).thenReturn(localized);
119:
120:                 cellEditor.instantiate(feature, viewContext);
121:                 final String result = cellEditor.getFormatedString(testLiteral);
122:
123:                 verify(reportService, never()).report(any());
124:                 assertEquals(localized, result);
125:         }
126:
127:         @Test
128:         public void getFormatedString_noLocalizedString() throws NoBundleFoundException {
129:                 final String key = String.format(KEY_TEMPLATE, TEST_NAME);
130:
131:                 final Bundle bundle = mock(Bundle.class);
132:                 when(bundleResolver.getEditBundle(feature.getEType())).thenReturn(bundle);
133:
134:                 cellEditor.instantiate(feature, viewContext);
135:                 final String result = cellEditor.getFormatedString(testLiteral);
136:
137:                 // once during init, once during getFormatedString
138:                 verify(localization, times(2)).getString(bundle, key);
139:                 verify(reportService, never()).report(any());
140:                 assertEquals(TEST_LITERAL, result);
141:         }
142: }