Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fb7cc23035d4d4b75584869bba8692c1fa12ce7a (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
/*
 * Copyright (c) 2014 CEA and others.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Christian W. Damus (CEA) - Initial API and implementation
 *
 */
package org.eclipse.papyrus.junit.utils.resources;

import static org.hamcrest.CoreMatchers.anything;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;

import java.io.IOException;
import java.util.Set;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.util.URI;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.junit.utils.rules.AbstractHouseKeeperRule;
import org.hamcrest.CoreMatchers;

import com.google.common.collect.Sets;

/**
 * A simple fixture for making assertions on the workspace resource changes made (or not) by {@link ModelSet}s upon saving.
 */
public class WorkspaceModificationAssertion implements IResourceChangeListener {

	private final IWorkspace ws = ResourcesPlugin.getWorkspace();

	private final Set<URI> requireChange = Sets.newHashSet();

	private final Set<URI> requireNoChange = Sets.newHashSet();

	private final Set<URI> changed = Sets.newHashSet();

	private CoreException exception;

	public WorkspaceModificationAssertion(AbstractHouseKeeperRule houseKeeper) {
		ws.addResourceChangeListener(this);
		houseKeeper.cleanUpLater(this);
	}

	public void dispose() {
		ws.removeResourceChangeListener(this);
		reset();
	}

	public void requireChange(URI uri) {
		assertThat("conflicting change requirement for " + uri, requireNoChange, not(hasItem(uri)));
		requireChange.add(uri);
	}

	public void requireNoChange(URI uri) {
		assertThat("conflicting change requirement for " + uri, requireChange, not(hasItem(uri)));
		requireNoChange.add(uri);
	}

	public void resourceChanged(IResourceChangeEvent event) {
		try {
			event.getDelta().accept(new IResourceDeltaVisitor() {

				public boolean visit(IResourceDelta delta) throws CoreException {
					if(delta.getResource().getType() == IResource.FILE) {
						changed.add(URI.createPlatformResourceURI(delta.getFullPath().toString(), true));
					}
					return true;
				}
			});
		} catch (CoreException e) {
			e.printStackTrace();
			exception = e;
		}
	}

	public void reset() {
		requireChange.clear();
		requireNoChange.clear();
		changed.clear();
		exception = null;
	}

	public void save(final ModelSet modelSet) {
		try {
			try {
				ws.run(new IWorkspaceRunnable() {

					public void run(IProgressMonitor monitor) throws CoreException {
						try {
							modelSet.save(monitor);
						} catch (IOException e) {
							throw new CoreException(new Status(IStatus.ERROR, "org.eclipse.papyrus.junit.utils", "Save failed.", e));
						}
					}
				}, new NullProgressMonitor());
			} catch (CoreException e) {
				e.printStackTrace();
				exception = e;
			}

			assertThat("Resource(s) saved that should not have been", Sets.intersection(requireNoChange, changed), not(CoreMatchers.<URI> hasItem(anything())));
			assertThat("Resource(s) not saved that should have been", Sets.difference(requireChange, changed), not(CoreMatchers.<URI> hasItem(anything())));
			assertThat("Save assertion failed with an exception", exception, nullValue());
		} finally {
			reset();
		}
	}
}

Back to the top