Skip to content

Package: EMFFormsSpreadsheetMultiReferenceConverter_Test

EMFFormsSpreadsheetMultiReferenceConverter_Test

nameinstructionbranchcomplexitylinemethod
EMFFormsSpreadsheetMultiReferenceConverter_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%
static {...}
M: 0 C: 30
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
task(String)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
testApplicableMultiReference()
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%
testApplicableNoReference()
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%
testApplicableSingleReference()
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%
testFromString()
M: 0 C: 41
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
testToString()
M: 0 C: 32
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.List;
25:
26: import org.apache.poi.hssf.usermodel.HSSFWorkbook;
27: import org.apache.poi.ss.usermodel.BuiltinFormats;
28: import org.apache.poi.ss.usermodel.Cell;
29: import org.apache.poi.ss.usermodel.CellStyle;
30: import org.apache.poi.ss.usermodel.Row;
31: import org.apache.poi.ss.usermodel.Sheet;
32: import org.apache.poi.ss.usermodel.Workbook;
33: import org.eclipse.emf.ecore.EObject;
34: import org.eclipse.emf.ecore.EStructuralFeature;
35: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
36: import org.eclipse.emf.ecore.util.EcoreUtil;
37: import org.eclipse.emf.ecp.makeithappen.model.task.Task;
38: import org.eclipse.emf.ecp.makeithappen.model.task.TaskFactory;
39: import org.eclipse.emf.ecp.makeithappen.model.task.TaskPackage;
40: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
41: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
42: import org.eclipse.emfforms.spi.common.report.ReportService;
43: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
44: import org.eclipse.emfforms.spi.core.services.databinding.emf.EMFFormsDatabindingEMF;
45: import org.eclipse.emfforms.spi.spreadsheet.core.converter.EMFFormsCellStyleConstants;
46: import org.eclipse.emfforms.spi.spreadsheet.core.converter.EMFFormsConverterException;
47: import org.eclipse.emfforms.spi.spreadsheet.core.converter.EMFFormsSpreadsheetValueConverter;
48: import org.junit.Before;
49: import org.junit.Test;
50:
51: public class EMFFormsSpreadsheetMultiReferenceConverter_Test {
52:
53:         private static final String LINE_SEP = System.getProperty("line.separator"); //$NON-NLS-1$
54:         private static final String EXPECTED = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LINE_SEP + //$NON-NLS-1$
55:                 "<org.eclipse.emf.ecp.makeithappen.model.task:Task xmi:version=\"2.0\" xmlns:xmi=\"http://www.omg.org/XMI\" xmlns:org.eclipse.emf.ecp.makeithappen.model.task=\"http://eclipse/org/emf/ecp/makeithappen/model/task\" description=\"1\"/>" //$NON-NLS-1$
56:                 + LINE_SEP +
57:                 "\n" + //$NON-NLS-1$
58:                 "\n" + //$NON-NLS-1$
59:                 "\n" + //$NON-NLS-1$
60:                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LINE_SEP + //$NON-NLS-1$
61:                 "<org.eclipse.emf.ecp.makeithappen.model.task:Task xmi:version=\"2.0\" xmlns:xmi=\"http://www.omg.org/XMI\" xmlns:org.eclipse.emf.ecp.makeithappen.model.task=\"http://eclipse/org/emf/ecp/makeithappen/model/task\" description=\"2\"/>" //$NON-NLS-1$
62:                 + LINE_SEP;
63:
64:         private ReportService reportService;
65:         private EMFFormsDatabindingEMF databinding;
66:         private EMFFormsSpreadsheetMultiReferenceConverter converter;
67:         private EObject domainObject;
68:         private VDomainModelReference dmr;
69:         private Cell cell;
70:         private ViewModelContext viewModelContext;
71:
72:         @Before
73:         public void before() {
74:                 converter = new EMFFormsSpreadsheetMultiReferenceConverter();
75:                 reportService = mock(ReportService.class);
76:                 databinding = mock(EMFFormsDatabindingEMF.class);
77:                 domainObject = mock(EObject.class);
78:                 dmr = mock(VDomainModelReference.class);
79:
80:                 final Workbook wb = new HSSFWorkbook();
81:                 final CellStyle cellStyle = wb.createCellStyle();
82:                 cellStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("text")); //$NON-NLS-1$
83:
84:                 final Sheet sheet = wb.createSheet("test"); //$NON-NLS-1$
85:
86:                 // Create a row and put some cells in it. Rows are 0 based.
87:                 final Row row = sheet.createRow((short) 0);
88:                 cell = row.createCell(0);
89:
90:                 viewModelContext = mock(ViewModelContext.class);
91:                 when(viewModelContext.getContextValue(EMFFormsCellStyleConstants.TEXT)).thenReturn(cellStyle);
92:         }
93:
94:         @Test
95:         public void testApplicableNoFeature() throws DatabindingFailedException {
96:                 when(databinding.getSetting(any(VDomainModelReference.class), any(EObject.class)))
97:                         .thenThrow(new DatabindingFailedException("")); //$NON-NLS-1$
98:                 converter.setDatabinding(databinding);
99:                 converter.setReportService(reportService);
100:                 assertEquals(EMFFormsSpreadsheetValueConverter.NOT_APPLICABLE, converter.isApplicable(domainObject, dmr), 0d);
101:         }
102:
103:         @Test
104:         public void testApplicableNoReference() throws DatabindingFailedException {
105:                 final Setting setting = mock(Setting.class);
106:                 when(setting.getEStructuralFeature()).thenReturn(TaskPackage.eINSTANCE.getUser_Active());
107:                 when(databinding.getSetting(any(VDomainModelReference.class), any(EObject.class)))
108:                         .thenReturn(setting);
109:                 converter.setDatabinding(databinding);
110:                 converter.setReportService(reportService);
111:                 assertEquals(EMFFormsSpreadsheetValueConverter.NOT_APPLICABLE, converter.isApplicable(domainObject, dmr), 0d);
112:         }
113:
114:         @Test
115:         public void testApplicableSingleReference() throws DatabindingFailedException {
116:                 final Setting setting = mock(Setting.class);
117:                 when(setting.getEStructuralFeature()).thenReturn(TaskPackage.eINSTANCE.getTask_Assignee());
118:                 when(databinding.getSetting(any(VDomainModelReference.class), any(EObject.class)))
119:                         .thenReturn(setting);
120:                 converter.setDatabinding(databinding);
121:                 converter.setReportService(reportService);
122:                 assertEquals(EMFFormsSpreadsheetValueConverter.NOT_APPLICABLE, converter.isApplicable(domainObject, dmr), 0d);
123:         }
124:
125:         @Test
126:         public void testApplicableMultiReference() throws DatabindingFailedException {
127:                 final Setting setting = mock(Setting.class);
128:                 when(setting.getEStructuralFeature()).thenReturn(TaskPackage.eINSTANCE.getTask_SubTasks());
129:                 when(databinding.getSetting(any(VDomainModelReference.class), any(EObject.class)))
130:                         .thenReturn(setting);
131:                 converter.setDatabinding(databinding);
132:                 converter.setReportService(reportService);
133:                 assertEquals(0d, converter.isApplicable(domainObject, dmr), 0d);
134:         }
135:
136:         @Test
137:         public void testToString() throws DatabindingFailedException, EMFFormsConverterException {
138:                 final Collection<Task> cellValues = Arrays.asList(task("1"), task("2")); //$NON-NLS-1$ //$NON-NLS-2$
139:                 final EStructuralFeature eStructuralFeature = TaskPackage.eINSTANCE.getTask_Assignee();
140:                 converter.setCellValue(cell, cellValues, eStructuralFeature, viewModelContext);
141:                 assertEquals(EXPECTED, cell.getStringCellValue());
142:         }
143:
144:         @SuppressWarnings("unchecked")
145:         @Test
146:         public void testFromString() throws DatabindingFailedException, EMFFormsConverterException {
147:                 cell.setCellValue(EXPECTED);
148:                 final EStructuralFeature eStructuralFeature = TaskPackage.eINSTANCE.getTask_Assignee();
149:                 final Object value = converter.getCellValue(cell, eStructuralFeature);
150:                 final List<EObject> tasks = List.class.cast(value);
151:                 assertEquals(2, tasks.size());
152:                 assertTrue(EcoreUtil.equals(task("1"), tasks.get(0))); //$NON-NLS-1$
153:                 assertTrue(EcoreUtil.equals(task("2"), tasks.get(1))); //$NON-NLS-1$
154:         }
155:
156:         private static Task task(String desc) {
157:                 final Task task = TaskFactory.eINSTANCE.createTask();
158:                 task.setDescription(desc);
159:                 return task;
160:
161:         }
162:
163: }