Skip to main content
summaryrefslogtreecommitdiffstats
blob: 26bb13e97ff0a5c3f7c149a3f3b24e4a935a7114 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*****************************************************************************
 * Copyright (c) 2013, 2015 CEA LIST and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   CEA LIST - Initial API and implementation
 *   Christian W. Damus (CEA) - bug 422257
 *   Eike Stepper (CEA) - bug 466520
 *
 *****************************************************************************/
package org.eclipse.papyrus.cdo.internal.core.exporter.tests;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.io.File;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.papyrus.cdo.core.exporter.IModelExportMapping;
import org.eclipse.papyrus.cdo.core.exporter.IModelExporter;
import org.eclipse.papyrus.cdo.core.importer.IModelTransferConfiguration;
import org.eclipse.papyrus.cdo.core.importer.IModelTransferNode;
import org.eclipse.papyrus.cdo.core.importer.IModelTransferOperation;
import org.eclipse.papyrus.cdo.core.tests.TestProject;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;

/**
 * This is the ModelImporterTest type. Enjoy.
 */
public class ModelExporterTest extends AbstractModelExportTest {

	private IModelTransferConfiguration config;

	private IModelExporter fixture;

	@Rule
	public TestProject hasDependencies = new TestProject("has_dependencies");

	@Rule
	public TestProject hasDependents = new TestProject("has_dependents");

	public ModelExporterTest() {
		super();
	}

	@Test
	public void testExportWithDependencies() {
		URI uri1 = getTestResourceURI(DEPENDENT_MODEL_DI);
		URI uri2 = getTestResourceURI(DEPENDENCY_MODEL_DI);

		config.addModelToTransfer(uri1);
		config.addModelToTransfer(uri2);

		IModelExportMapping mapping = IModelExportMapping.Factory.DEFAULT.create(config);
		mapping.setCheckout(getCheckout());
		mapping.mapTo(getNode(uri1), new Path("/has_dependencies/exported.di"));
		mapping.mapTo(getNode(uri2), new Path("/has_dependents/exported.di"));

		Diagnostic problems = fixture.exportModels(mapping);
		assertThat(problems.getSeverity(), is(Diagnostic.OK));
		assertThat(problems.getChildren().size(), is(0));

		assertResource(new Path("/has_dependencies/exported.di"), "di");
		assertResource(new Path("/has_dependencies/exported.uml"), "uml");
		assertResource(new Path("/has_dependencies/exported.notation"), "notation");

		assertResource(new Path("/has_dependents/exported.di"), "di");
		assertResource(new Path("/has_dependents/exported.uml"), "uml");
		assertResource(new Path("/has_dependents/exported.notation"), "notation");
	}

	//
	// Test framework
	//

	@Before
	public void createTestFixture() throws Exception {
		ResourceSet rset = getInternalCheckout().getView().getResourceSet();
		createModels();

		config = IModelTransferConfiguration.Factory.EXPORT.create(new IModelTransferOperation.Context() {

			@Override
			public Diagnostic run(IModelTransferOperation operation) {
				operation.run(new NullProgressMonitor());
				return Diagnostic.OK_INSTANCE;
			}
		}, rset);

		fixture = IModelExporter.Factory.DEFAULT.create();
	}

	@After
	public void disposeTestFixture() {
		fixture = null;

		config.dispose();
		config = null;
	}

	void assertResource(IPath path, final String metamodel) {
		IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
		assertThat("Does not exist: " + file.getLocation(), file.exists(), is(true));

		File osFile = new File(file.getLocationURI());
		assertThat(osFile.length() > 0L, is(true));

		ResourceSet rset = new ResourceSetImpl();
		try {
			Resource resource = rset.getResource(URI.createPlatformResourceURI(path.toString(), true), true);

			assertThat(Iterables.size(Iterables.filter(resource.getContents(), new Predicate<EObject>() {

				@Override
				public boolean apply(EObject input) {
					boolean result = false;

					if (input != null) {
						EPackage epackage = input.eClass().getEPackage();
						result = epackage.getName().equalsIgnoreCase(metamodel);
					}

					return result;
				}
			})), is(1));

		} finally {
			cleanUp(rset);
		}
	}

	protected void cleanUp(ResourceSet rset) {
		EMFHelper.unload(rset);
	}

	IModelTransferNode getNode(URI uri) {
		return getNode(config, uri);
	}
}

Back to the top