Skip to content

Package: TemplateTest

TemplateTest

nameinstructionbranchcomplexitylinemethod
TemplateTest(String)
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%
getFixture()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getResourceURL(Class, String)
M: 75 C: 8
10%
M: 3 C: 1
25%
M: 2 C: 1
33%
M: 11 C: 3
21%
M: 0 C: 1
100%
main(String[])
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setFixture(Template)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
setUp()
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
tearDown()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
testDynamicInstance()
M: 12 C: 121
91%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 28
93%
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: * EclipseSource Muenchen GmbH - initial API and implementation
13: */
14: package org.eclipse.emfforms.datatemplate.tests;
15:
16: import static org.hamcrest.CoreMatchers.is;
17: import static org.hamcrest.MatcherAssert.assertThat;
18:
19: import java.io.IOException;
20: import java.net.MalformedURLException;
21: import java.net.URL;
22:
23: import org.eclipse.emf.common.util.URI;
24: import org.eclipse.emf.ecore.EClass;
25: import org.eclipse.emf.ecore.EObject;
26: import org.eclipse.emf.ecore.EOperation;
27: import org.eclipse.emf.ecore.EPackage;
28: import org.eclipse.emf.ecore.EReference;
29: import org.eclipse.emf.ecore.EcoreFactory;
30: import org.eclipse.emf.ecore.EcorePackage;
31: import org.eclipse.emf.ecore.resource.Resource;
32: import org.eclipse.emf.ecore.resource.ResourceSet;
33: import org.eclipse.emf.ecore.resource.URIConverter;
34: import org.eclipse.emf.ecore.resource.impl.ExtensibleURIConverterImpl;
35: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
36: import org.eclipse.emf.ecore.util.EcoreUtil;
37: import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
38: import org.eclipse.emfforms.datatemplate.DataTemplateFactory;
39: import org.eclipse.emfforms.datatemplate.DataTemplatePackage;
40: import org.eclipse.emfforms.datatemplate.Template;
41:
42: import junit.framework.TestCase;
43: import junit.textui.TestRunner;
44:
45: /**
46: * <!-- begin-user-doc --> A test case for the model object
47: * '<em><b>Template</b></em>'. <!-- end-user-doc -->
48: *
49: * @generated
50: */
51: @SuppressWarnings("nls")
52: public class TemplateTest extends TestCase {
53:
54:         /**
55:          * The fixture for this Template test case. <!-- begin-user-doc --> <!--
56:          * end-user-doc -->
57:          *
58:          * @generated
59:          */
60:         protected Template fixture = null;
61:
62:         /**
63:          * <!-- begin-user-doc --> <!-- end-user-doc -->
64:          *
65:          * @generated
66:          */
67:         public static void main(String[] args) {
68:                 TestRunner.run(TemplateTest.class);
69:         }
70:
71:         /**
72:          * Constructs a new Template test case with the given name. <!-- begin-user-doc
73:          * --> <!-- end-user-doc -->
74:          *
75:          * @generated
76:          */
77:         public TemplateTest(String name) {
78:                 super(name);
79:         }
80:
81:         /**
82:          * Sets the fixture for this Template test case. <!-- begin-user-doc --> <!--
83:          * end-user-doc -->
84:          *
85:          * @generated
86:          */
87:         protected void setFixture(Template fixture) {
88:                 this.fixture = fixture;
89:         }
90:
91:         /**
92:          * Returns the fixture for this Template test case. <!-- begin-user-doc --> <!--
93:          * end-user-doc -->
94:          *
95:          * @generated
96:          */
97:         protected Template getFixture() {
98:                 return fixture;
99:         }
100:
101:         /**
102:          * <!-- begin-user-doc --> <!-- end-user-doc -->
103:          *
104:          * @see junit.framework.TestCase#setUp()
105:          * @generated
106:          */
107:         @Override
108:         protected void setUp() throws Exception {
109:                 setFixture(DataTemplateFactory.eINSTANCE.createTemplate());
110:         }
111:
112:         /**
113:          * <!-- begin-user-doc --> <!-- end-user-doc -->
114:          *
115:          * @see junit.framework.TestCase#tearDown()
116:          * @generated
117:          */
118:         @Override
119:         protected void tearDown() throws Exception {
120:                 setFixture(null);
121:         }
122:
123:         static URL getResourceURL(Class<?> contextClass, String path) {
124:                 URL result = contextClass.getResource(path);
125:
126:•                if (result == null) {
127:                         // We're in Eclipse
128:                         final String contextURL = contextClass.getResource(contextClass.getSimpleName() + ".class")
129:                                 .toExternalForm();
130:
131:                         try {
132:•                                if (path.startsWith("/")) {
133:                                         final String baseURL = contextURL.substring(0, contextURL.lastIndexOf("/bin/"));
134:                                         result = new URL(baseURL + path);
135:                                 } else {
136:                                         final String baseURL = contextURL.substring(0, contextURL.lastIndexOf("/") + 1);
137:                                         result = new URL(baseURL + path);
138:                                 }
139:                         } catch (final MalformedURLException e) {
140:                                 e.printStackTrace();
141:                                 fail("Could not get resource '" + path + "': " + e.getMessage());
142:                         }
143:                 }
144:
145:                 return result;
146:         }
147:
148:         /**
149:          * Per bug 550814, dynamic instances of the template model must be supported.
150:          * This test fails without the fix for 550814 because the EObject class in the
151:          * dynamic implementation of Ecore has no relation to the static EObject class.
152:          *
153:          * @see <a href="http://eclip.se/550814">bug 550814</a>
154:          */
155:         public void testDynamicInstance() throws IOException {
156:                 // Load the dynamic Ecore model
157:                 final ResourceSet rset = new ResourceSetImpl();
158:                 final URIConverter converter = new ExtensibleURIConverterImpl();
159:                 rset.setURIConverter(converter);
160:                 rset.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ecore", new EcoreResourceFactoryImpl());
161:                 final URL modelURL = getResourceURL(DataTemplatePackage.class, "/model/datatemplate.ecore");
162:                 URI physicalURI = URI.createURI(modelURL.toExternalForm(), true);
163:                 final URI modelURI = URI
164:                         .createPlatformPluginURI("org.eclipse.emfforms.datatemplate.model/model/datatemplate.ecore", true);
165:                 converter.getURIMap().put(modelURI.trimSegments(2).appendSegment(""),
166:                         physicalURI.trimSegments(2).appendSegment(""));
167:                 final Resource model = rset.getResource(modelURI, true);
168:
169:                 // Give the dynamic model the dynamic Ecore that it expects
170:                 final URL ecoreURL = getResourceURL(EcorePackage.class, "/model/Ecore.ecore");
171:                 physicalURI = URI.createURI(ecoreURL.toExternalForm(), true);
172:                 final URI ecoreURI = URI.createPlatformPluginURI("org.eclipse.emf.ecore/model/Ecore.ecore", true);
173:                 converter.getURIMap().put(ecoreURI.trimSegments(2).appendSegment(""),
174:                         physicalURI.trimSegments(2).appendSegment(""));
175:                 rset.getResource(ecoreURI, true);
176:
177:                 // Resolve cross-references to Ecore
178:                 EcoreUtil.resolveAll(model);
179:
180:                 final EPackage package_ = (EPackage) EcoreUtil.getObjectByType(model.getContents(),
181:                         EcorePackage.Literals.EPACKAGE);
182:                 final EClass templateClass = (EClass) package_.getEClassifier("Template");
183:                 final EReference instance = (EReference) templateClass.getEStructuralFeature("instance");
184:
185:                 try {
186:                         final EObject template = EcoreUtil.create(templateClass);
187:                         final EOperation operation = EcoreFactory.eINSTANCE.createEOperation();
188:
189:                         // an EOperation is not an EObject in the Ecore model, but it is in the Java
190:                         // implementation
191:                         template.eSet(instance, operation);
192:
193:                         assertThat("Template instance not correctly set", template.eGet(instance), is(operation));
194:                 } catch (final Exception e) {
195:                         e.printStackTrace();
196:                         fail("Failed to set template instance: " + e.getMessage());
197:                 }
198:         }
199:
200: } // TemplateTest