Skip to content

Package: DmrToSegmentsMigratorImpl_ITest

DmrToSegmentsMigratorImpl_ITest

nameinstructionbranchcomplexitylinemethod
DmrToSegmentsMigratorImpl_ITest()
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%
afterClass()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
beforeClass()
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
copy(InputStream, File)
M: 3 C: 27
90%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 2 C: 6
75%
M: 0 C: 1
100%
loadViewResource(URI)
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%
smokeTest()
M: 0 C: 302
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 66
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.emfforms.internal.ide.view.segments;
15:
16: import static org.junit.Assert.assertEquals;
17: import static org.junit.Assert.assertNotNull;
18: import static org.junit.Assert.assertSame;
19: import static org.junit.Assert.assertTrue;
20:
21: import java.io.File;
22: import java.io.FileOutputStream;
23: import java.io.IOException;
24: import java.io.InputStream;
25: import java.io.OutputStream;
26: import java.util.Map;
27:
28: import org.eclipse.core.runtime.Platform;
29: import org.eclipse.emf.common.util.URI;
30: import org.eclipse.emf.ecore.resource.Resource;
31: import org.eclipse.emf.ecore.resource.ResourceSet;
32: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
33: import org.eclipse.emf.ecp.view.spi.group.model.VGroup;
34: import org.eclipse.emf.ecp.view.spi.horizontal.model.VHorizontalLayout;
35: import org.eclipse.emf.ecp.view.spi.model.VControl;
36: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
37: import org.eclipse.emf.ecp.view.spi.model.VFeatureDomainModelReferenceSegment;
38: import org.eclipse.emf.ecp.view.spi.model.VView;
39: import org.eclipse.emf.ecp.view.spi.model.VViewPackage;
40: import org.eclipse.emf.ecp.view.spi.model.util.VViewResourceFactoryImpl;
41: import org.eclipse.emf.ecp.view.spi.table.model.VReadOnlyColumnConfiguration;
42: import org.eclipse.emf.ecp.view.spi.table.model.VTableControl;
43: import org.eclipse.emf.ecp.view.spi.table.model.VWidthConfiguration;
44: import org.eclipse.emf.ecp.view.spi.vertical.model.VVerticalLayout;
45: import org.eclipse.emfforms.spi.ide.view.segments.DmrToSegmentsMigrationException;
46: import org.eclipse.emfforms.spi.ide.view.segments.DmrToSegmentsMigrator;
47: import org.eclipse.emfforms.view.spi.multisegment.model.VMultiDomainModelReferenceSegment;
48: import org.junit.AfterClass;
49: import org.junit.BeforeClass;
50: import org.junit.Rule;
51: import org.junit.Test;
52: import org.junit.rules.TemporaryFolder;
53: import org.osgi.framework.Bundle;
54: import org.osgi.framework.BundleContext;
55: import org.osgi.framework.FrameworkUtil;
56: import org.osgi.framework.ServiceReference;
57:
58: /**
59: * Integration tests for {@link DmrToSegmentsMigratorImpl}. This tests a "realistic" migration like it would occur in
60: * the tooling.
61: *
62: * @author Lucas Koehler
63: *
64: */
65: public class DmrToSegmentsMigratorImpl_ITest {
66:
67:         private static final String PLUGIN_ID = "org.eclipse.emfforms.ide.view.segments.test"; //$NON-NLS-1$
68:
69:         private static Bundle bundle;
70:
71:         @Rule
72:         public TemporaryFolder testFolder = new TemporaryFolder();
73:
74:         private static BundleContext migratorBundleContext;
75:         private static ServiceReference<DmrToSegmentsMigrator> serviceReference;
76:         private static DmrToSegmentsMigrator segmentsMigrator;
77:
78:         @BeforeClass
79:         public static void beforeClass() {
80:                 bundle = Platform.getBundle(PLUGIN_ID);
81:                 // Need to get service from its bundle because a fragment does not have a bundle context
82:                 migratorBundleContext = FrameworkUtil.getBundle(DmrToSegmentsMigratorImpl.class).getBundleContext();
83:                 serviceReference = migratorBundleContext.getServiceReference(DmrToSegmentsMigrator.class);
84:                 segmentsMigrator = migratorBundleContext.getService(serviceReference);
85:         }
86:
87:         @AfterClass
88:         public static void afterClass() {
89:                 segmentsMigrator = null;
90:                 migratorBundleContext.ungetService(serviceReference);
91:         }
92:
93:         @Test
94:         public void smokeTest() throws IOException, DmrToSegmentsMigrationException {
95:                 final File newFile = testFolder.newFile();
96:                 newFile.deleteOnExit();
97:                 final InputStream openStream = bundle.getEntry("data/Fan.view").openStream(); //$NON-NLS-1$
98:                 copy(openStream, newFile);
99:                 final URI resourceURI = URI.createFileURI(newFile.getAbsolutePath());
100:
101:                 final boolean needsMigration = segmentsMigrator.needsMigration(resourceURI);
102:                 assertTrue("Fan.view should need migration.", needsMigration); //$NON-NLS-1$
103:
104:                 segmentsMigrator.performMigration(resourceURI);
105:
106:                 final Resource resource = loadViewResource(resourceURI);
107:                 assertEquals(1, resource.getContents().size());
108:
109:                 final VView view = (VView) resource.getContents().get(0);
110:                 assertEquals(2, view.getChildren().size());
111:                 final VHorizontalLayout horizontal = (VHorizontalLayout) view.getChildren().get(0);
112:                 assertEquals(2, horizontal.getChildren().size());
113:
114:                 // Check control dmr in group
115:                 final VGroup group = (VGroup) horizontal.getChildren().get(0);
116:                 assertEquals("test", group.getName()); //$NON-NLS-1$
117:                 assertEquals(1, group.getChildren().size());
118:                 final VControl groupControl = (VControl) group.getChildren().get(0);
119:                 final VDomainModelReference groupControlDmr = groupControl.getDomainModelReference();
120:                 assertNotNull(groupControlDmr);
121:                 assertSame(VViewPackage.Literals.DOMAIN_MODEL_REFERENCE, groupControlDmr.eClass());
122:                 assertEquals(1, groupControlDmr.getSegments().size());
123:                 final VFeatureDomainModelReferenceSegment groupControlDmrSegment = (VFeatureDomainModelReferenceSegment) groupControlDmr
124:                         .getSegments().get(0);
125:                 assertEquals("name", groupControlDmrSegment.getDomainModelFeature()); //$NON-NLS-1$
126:
127:                 // Check control dmr in vertical layout
128:                 final VVerticalLayout vertical = (VVerticalLayout) horizontal.getChildren().get(1);
129:                 assertEquals(1, vertical.getChildren().size());
130:                 final VControl verticalControl = (VControl) vertical.getChildren().get(0);
131:                 final VDomainModelReference verticalControlDmr = verticalControl.getDomainModelReference();
132:                 assertNotNull(verticalControlDmr);
133:                 assertSame(VViewPackage.Literals.DOMAIN_MODEL_REFERENCE, verticalControlDmr.eClass());
134:                 assertEquals(1, verticalControlDmr.getSegments().size());
135:                 final VFeatureDomainModelReferenceSegment verticalControlDmrSegment = (VFeatureDomainModelReferenceSegment) verticalControlDmr
136:                         .getSegments().get(0);
137:                 assertEquals("dateOfBirth", verticalControlDmrSegment.getDomainModelFeature()); //$NON-NLS-1$
138:
139:                 // Check table control
140:                 final VTableControl table = (VTableControl) view.getChildren().get(1);
141:                 final VDomainModelReference tableDmr = table.getDomainModelReference();
142:                 assertNotNull(tableDmr);
143:                 assertSame(VViewPackage.Literals.DOMAIN_MODEL_REFERENCE, tableDmr.eClass());
144:                 assertEquals(1, tableDmr.getSegments().size());
145:                 final VMultiDomainModelReferenceSegment multiSegment = (VMultiDomainModelReferenceSegment) tableDmr
146:                         .getSegments().get(0);
147:                 assertEquals("visitedTournaments", multiSegment.getDomainModelFeature()); //$NON-NLS-1$
148:                 assertEquals(3, multiSegment.getChildDomainModelReferences().size());
149:
150:                 // check child dmrs
151:                 final VDomainModelReference childDmr1 = multiSegment.getChildDomainModelReferences().get(0);
152:                 assertEquals(1, childDmr1.getSegments().size());
153:                 assertEquals("type", //$NON-NLS-1$
154:                         VFeatureDomainModelReferenceSegment.class.cast(childDmr1.getSegments().get(0)).getDomainModelFeature());
155:                 final VDomainModelReference childDmr2 = multiSegment.getChildDomainModelReferences().get(1);
156:                 assertEquals(1, childDmr2.getSegments().size());
157:                 assertEquals("priceMoney", //$NON-NLS-1$
158:                         VFeatureDomainModelReferenceSegment.class.cast(childDmr2.getSegments().get(0)).getDomainModelFeature());
159:                 final VDomainModelReference childDmr3 = multiSegment.getChildDomainModelReferences().get(2);
160:                 assertEquals(1, childDmr3.getSegments().size());
161:                 assertEquals("receivesTrophy", //$NON-NLS-1$
162:                         VFeatureDomainModelReferenceSegment.class.cast(childDmr3.getSegments().get(0)).getDomainModelFeature());
163:
164:                 // check column configurations
165:                 assertEquals(2, table.getColumnConfigurations().size());
166:                 final VReadOnlyColumnConfiguration readOnlyConfig = (VReadOnlyColumnConfiguration) table
167:                         .getColumnConfigurations().get(0);
168:                 assertEquals(2, readOnlyConfig.getColumnDomainReferences().size());
169:                 assertSame(childDmr3, readOnlyConfig.getColumnDomainReferences().get(0));
170:                 assertSame(childDmr2, readOnlyConfig.getColumnDomainReferences().get(1));
171:                 final VWidthConfiguration widthConfig = (VWidthConfiguration) table.getColumnConfigurations().get(1);
172:                 assertEquals(75, widthConfig.getMinWidth());
173:                 assertSame(childDmr1, widthConfig.getColumnDomainReference());
174:         }
175:
176:         private static Resource loadViewResource(URI resourceUri) throws IOException {
177:                 final ResourceSet resourceSet = new ResourceSetImpl();
178:                 final Map<String, Object> extensionToFactoryMap = resourceSet
179:                         .getResourceFactoryRegistry().getExtensionToFactoryMap();
180:                 extensionToFactoryMap.put(Resource.Factory.Registry.DEFAULT_EXTENSION,
181:                         new VViewResourceFactoryImpl());
182:                 resourceSet.getPackageRegistry().put(VViewPackage.eNS_URI,
183:                         VViewPackage.eINSTANCE);
184:                 final Resource resource = resourceSet.createResource(resourceUri);
185:                 resource.load(null);
186:                 return resource;
187:         }
188:
189:         private static void copy(InputStream in, File file) {
190:                 try (OutputStream out = new FileOutputStream(file)) {
191:                         final byte[] buf = new byte[1024];
192:                         int len;
193:•                        while ((len = in.read(buf)) > 0) {
194:                                 out.write(buf, 0, len);
195:                         }
196:                         in.close();
197:                 }
198:                 // BEGIN SUPRESS CATCH EXCEPTION
199:                 catch (final Exception e) {// END SUPRESS CATCH EXCEPTION
200:                         e.printStackTrace();
201:                 }
202:         }
203: }