Skip to content

Package: TestUtil

TestUtil

nameinstructionbranchcomplexitylinemethod
createResourceWithEditingDomain()
M: 0 C: 63
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 13
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.test.common;
15:
16: import org.eclipse.emf.common.command.BasicCommandStack;
17: import org.eclipse.emf.common.notify.AdapterFactory;
18: import org.eclipse.emf.common.util.URI;
19: import org.eclipse.emf.ecore.resource.Resource;
20: import org.eclipse.emf.ecore.resource.ResourceSet;
21: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
22: import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
23: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
24: import org.eclipse.emf.edit.domain.EditingDomain;
25: import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
26: import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory;
27:
28: /**
29: * Utility class providing common functionality for test.
30: * <p>
31: * For SWT and View related utils see <code>SWTTestUtil</code> and <code>SWTViewTestHelper</code>.
32: *
33: * @author Lucas Koehler
34: *
35: */
36: public final class TestUtil {
37:         private TestUtil() {
38:                 // Do not instantiate util class
39:         }
40:
41:         /**
42:          * Creates a virtual {@link Resource} contained in a new {@link ResourceSet} with a working {@link EditingDomain}.
43:          * <p>
44:          * <strong>Note:</strong> The returned resource cannot be safed
45:          *
46:          * @return The created {@link Resource}
47:          */
48:         public static Resource createResourceWithEditingDomain() {
49:                 final ResourceSet rs = new ResourceSetImpl();
50:                 final ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(new AdapterFactory[] {
51:                         new ReflectiveItemProviderAdapterFactory(),
52:                         new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE) });
53:                 final AdapterFactoryEditingDomain editingDomain = new AdapterFactoryEditingDomain(
54:                         adapterFactory, new BasicCommandStack(), rs);
55:                 rs.eAdapters().add(new AdapterFactoryEditingDomain.EditingDomainProvider(editingDomain));
56:                 // Set default xmi factory to guarantee reproducible results and make resource creation work in plain unit tests
57:                 final Object oldFactory = Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("*", //$NON-NLS-1$
58:                         new XMIResourceFactoryImpl());
59:                 final Resource resource = rs.createResource(URI.createURI("VIRTUAL_URI")); //$NON-NLS-1$
60:•                if (oldFactory != null) {
61:                         Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("*", oldFactory); //$NON-NLS-1$
62:                 }
63:                 return resource;
64:         }
65: }