Skip to content

Package: EMFFormsSpreadsheetMultiAttributeConverter_Test

EMFFormsSpreadsheetMultiAttributeConverter_Test

nameinstructionbranchcomplexitylinemethod
EMFFormsSpreadsheetMultiAttributeConverter_Test()
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%
before()
M: 0 C: 64
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
testApplicableMultiEAttribute()
M: 0 C: 45
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
testApplicableNoEAttribute()
M: 0 C: 45
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
testApplicableNoFeature()
M: 0 C: 42
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
testApplicableSingleEAttribute()
M: 0 C: 45
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
testDoubleToStringEnglish()
M: 0 C: 46
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
testDoubleToStringGerman()
M: 0 C: 46
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
testFromString()
M: 0 C: 37
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
testFromStringEmpty()
M: 0 C: 25
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
testFromStringNull()
M: 0 C: 25
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
testToStringEmptyList()
M: 0 C: 20
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
testToStringNonEmptyList()
M: 0 C: 30
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 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: * jfaltermeier - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.spreadsheet.core.converter;
15:
16: import static org.junit.Assert.assertEquals;
17: import static org.junit.Assert.assertTrue;
18: import static org.mockito.Matchers.any;
19: import static org.mockito.Mockito.mock;
20: import static org.mockito.Mockito.when;
21:
22: import java.util.Arrays;
23: import java.util.Collection;
24: import java.util.Collections;
25: import java.util.List;
26: import java.util.Locale;
27:
28: import org.apache.poi.hssf.usermodel.HSSFWorkbook;
29: import org.apache.poi.ss.usermodel.BuiltinFormats;
30: import org.apache.poi.ss.usermodel.Cell;
31: import org.apache.poi.ss.usermodel.CellStyle;
32: import org.apache.poi.ss.usermodel.Row;
33: import org.apache.poi.ss.usermodel.Sheet;
34: import org.apache.poi.ss.usermodel.Workbook;
35: import org.eclipse.emf.ecore.EObject;
36: import org.eclipse.emf.ecore.EStructuralFeature;
37: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
38: import org.eclipse.emf.ecp.makeithappen.model.task.TaskPackage;
39: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
40: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
41: import org.eclipse.emf.emfstore.bowling.BowlingPackage;
42: import org.eclipse.emfforms.spi.common.locale.EMFFormsLocaleProvider;
43: import org.eclipse.emfforms.spi.common.report.ReportService;
44: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
45: import org.eclipse.emfforms.spi.core.services.databinding.emf.EMFFormsDatabindingEMF;
46: import org.eclipse.emfforms.spi.spreadsheet.core.converter.EMFFormsCellStyleConstants;
47: import org.eclipse.emfforms.spi.spreadsheet.core.converter.EMFFormsConverterException;
48: import org.eclipse.emfforms.spi.spreadsheet.core.converter.EMFFormsSpreadsheetValueConverter;
49: import org.junit.Before;
50: import org.junit.Test;
51:
52: public class EMFFormsSpreadsheetMultiAttributeConverter_Test {
53:
54:         private ReportService reportService;
55:         private EMFFormsDatabindingEMF databinding;
56:         private EMFFormsSpreadsheetMultiAttributeConverter converter;
57:         private EObject domainObject;
58:         private VDomainModelReference dmr;
59:         private Cell cell;
60:         private ViewModelContext viewModelContext;
61:
62:         @Before
63:         public void before() {
64:                 converter = new EMFFormsSpreadsheetMultiAttributeConverter();
65:                 reportService = mock(ReportService.class);
66:                 databinding = mock(EMFFormsDatabindingEMF.class);
67:                 domainObject = mock(EObject.class);
68:                 dmr = mock(VDomainModelReference.class);
69:
70:                 final Workbook wb = new HSSFWorkbook();
71:                 final CellStyle cellStyle = wb.createCellStyle();
72:                 cellStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("text")); //$NON-NLS-1$
73:
74:                 final Sheet sheet = wb.createSheet("test"); //$NON-NLS-1$
75:
76:                 // Create a row and put some cells in it. Rows are 0 based.
77:                 final Row row = sheet.createRow((short) 0);
78:                 cell = row.createCell(0);
79:
80:                 viewModelContext = mock(ViewModelContext.class);
81:                 when(viewModelContext.getContextValue(EMFFormsCellStyleConstants.TEXT)).thenReturn(cellStyle);
82:         }
83:
84:         @Test
85:         public void testApplicableNoFeature() throws DatabindingFailedException {
86:                 when(databinding.getSetting(any(VDomainModelReference.class), any(EObject.class)))
87:                         .thenThrow(new DatabindingFailedException("")); //$NON-NLS-1$
88:                 converter.setDatabinding(databinding);
89:                 converter.setReportService(reportService);
90:                 assertEquals(EMFFormsSpreadsheetValueConverter.NOT_APPLICABLE, converter.isApplicable(domainObject, dmr), 0d);
91:         }
92:
93:         @Test
94:         public void testApplicableNoEAttribute() throws DatabindingFailedException {
95:                 final Setting setting = mock(Setting.class);
96:                 when(setting.getEStructuralFeature()).thenReturn(TaskPackage.eINSTANCE.getUser_Tasks());
97:                 when(databinding.getSetting(any(VDomainModelReference.class), any(EObject.class)))
98:                         .thenReturn(setting);
99:                 converter.setDatabinding(databinding);
100:                 converter.setReportService(reportService);
101:                 assertEquals(EMFFormsSpreadsheetValueConverter.NOT_APPLICABLE, converter.isApplicable(domainObject, dmr), 0d);
102:         }
103:
104:         @Test
105:         public void testApplicableSingleEAttribute() throws DatabindingFailedException {
106:                 final Setting setting = mock(Setting.class);
107:                 when(setting.getEStructuralFeature()).thenReturn(TaskPackage.eINSTANCE.getUser_Active());
108:                 when(databinding.getSetting(any(VDomainModelReference.class), any(EObject.class)))
109:                         .thenReturn(setting);
110:                 converter.setDatabinding(databinding);
111:                 converter.setReportService(reportService);
112:                 assertEquals(EMFFormsSpreadsheetValueConverter.NOT_APPLICABLE, converter.isApplicable(domainObject, dmr), 0d);
113:         }
114:
115:         @Test
116:         public void testApplicableMultiEAttribute() throws DatabindingFailedException {
117:                 final Setting setting = mock(Setting.class);
118:                 when(setting.getEStructuralFeature()).thenReturn(BowlingPackage.eINSTANCE.getPlayer_EMails());
119:                 when(databinding.getSetting(any(VDomainModelReference.class), any(EObject.class)))
120:                         .thenReturn(setting);
121:                 converter.setDatabinding(databinding);
122:                 converter.setReportService(reportService);
123:                 assertEquals(0d, converter.isApplicable(domainObject, dmr), 0d);
124:         }
125:
126:         @Test
127:         public void testToStringEmptyList() throws DatabindingFailedException, EMFFormsConverterException {
128:                 final Collection<String> cellValues = Collections.emptyList();
129:                 final EStructuralFeature eStructuralFeature = BowlingPackage.eINSTANCE.getPlayer_EMails();
130:                 converter.setCellValue(cell, cellValues, eStructuralFeature, viewModelContext);
131:                 assertEquals("", cell.getStringCellValue()); //$NON-NLS-1$
132:         }
133:
134:         @Test
135:         public void testToStringNonEmptyList() throws DatabindingFailedException, EMFFormsConverterException {
136:                 final Collection<String> cellValues = Arrays.asList("foo@bar.org", "foo@bar.com"); //$NON-NLS-1$//$NON-NLS-2$
137:                 final EStructuralFeature eStructuralFeature = BowlingPackage.eINSTANCE.getPlayer_EMails();
138:                 converter.setCellValue(cell, cellValues, eStructuralFeature, viewModelContext);
139:                 assertEquals("foo@bar.org foo@bar.com", cell.getStringCellValue()); //$NON-NLS-1$
140:         }
141:
142:         @Test
143:         public void testFromStringEmpty() throws DatabindingFailedException, EMFFormsConverterException {
144:                 final String cellValue = ""; //$NON-NLS-1$
145:                 cell.setCellValue(cellValue);
146:                 final EStructuralFeature eStructuralFeature = BowlingPackage.eINSTANCE.getPlayer_EMails();
147:                 final Object value = converter.getCellValue(cell, eStructuralFeature);
148:                 final List<?> list = List.class.cast(value);
149:                 assertTrue(list.isEmpty());
150:         }
151:
152:         @Test
153:         public void testFromStringNull() throws DatabindingFailedException, EMFFormsConverterException {
154:                 final String cellValue = null;
155:                 cell.setCellValue(cellValue);
156:                 final EStructuralFeature eStructuralFeature = BowlingPackage.eINSTANCE.getPlayer_EMails();
157:                 final Object value = converter.getCellValue(cell, eStructuralFeature);
158:                 final List<?> list = List.class.cast(value);
159:                 assertTrue(list.isEmpty());
160:         }
161:
162:         @Test
163:         public void testFromString() throws DatabindingFailedException, EMFFormsConverterException {
164:                 final String cellValue = "foo@bar.org foo@bar.com"; //$NON-NLS-1$
165:                 cell.setCellValue(cellValue);
166:                 final EStructuralFeature eStructuralFeature = BowlingPackage.eINSTANCE.getPlayer_EMails();
167:                 final Object value = converter.getCellValue(cell, eStructuralFeature);
168:                 final List<?> list = List.class.cast(value);
169:                 assertEquals(2, list.size());
170:                 assertEquals("foo@bar.org", list.get(0)); //$NON-NLS-1$
171:                 assertEquals("foo@bar.com", list.get(1)); //$NON-NLS-1$
172:         }
173:
174:         @Test
175:         public void testDoubleToStringGerman() throws DatabindingFailedException, EMFFormsConverterException {
176:                 final EMFFormsLocaleProvider localeProvider = mock(EMFFormsLocaleProvider.class);
177:                 when(localeProvider.getLocale()).thenReturn(Locale.GERMAN);
178:                 converter.setEMFFormsLocaleProvider(localeProvider);
179:                 final Collection<Double> cellValues = Arrays.asList(1.1, 2.2);
180:                 final EStructuralFeature eStructuralFeature = BowlingPackage.eINSTANCE.getPlayer_Height();
181:                 converter.setCellValue(cell, cellValues, eStructuralFeature, viewModelContext);
182:                 assertEquals("1,1 2,2", cell.getStringCellValue()); //$NON-NLS-1$
183:         }
184:
185:         @Test
186:         public void testDoubleToStringEnglish() throws DatabindingFailedException, EMFFormsConverterException {
187:                 final EMFFormsLocaleProvider localeProvider = mock(EMFFormsLocaleProvider.class);
188:                 when(localeProvider.getLocale()).thenReturn(Locale.ENGLISH);
189:                 converter.setEMFFormsLocaleProvider(localeProvider);
190:                 final Collection<Double> cellValues = Arrays.asList(1.1, 2.2);
191:                 final EStructuralFeature eStructuralFeature = BowlingPackage.eINSTANCE.getPlayer_Height();
192:                 converter.setCellValue(cell, cellValues, eStructuralFeature, viewModelContext);
193:                 assertEquals("1.1 2.2", cell.getStringCellValue()); //$NON-NLS-1$
194:         }
195: }