Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 750a2cb74e109604cf0e0117a88f40b5fa56a1ac (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*****************************************************************************
 * Copyright (c) 2015 Christian W. Damus 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:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.commands.util;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import java.util.Iterator;
import java.util.Set;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.command.AbstractCommand;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.diagram.ui.commands.CommandProxy;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import org.eclipse.papyrus.commands.wrappers.EMFtoGEFCommandWrapper;
import org.eclipse.papyrus.infra.emf.gmf.command.EMFtoGMFCommandWrapper;
import org.eclipse.papyrus.commands.wrappers.GEFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.emf.gmf.command.GMFtoEMFCommandWrapper;
import org.eclipse.papyrus.junit.framework.classification.tests.AbstractPapyrusTest;
import org.junit.Before;
import org.junit.Test;

import com.google.common.collect.Sets;

/**
 * Test suite for the {@link CommandTreeIterator} class.
 */
public class CommandTreeIteratorTest extends AbstractPapyrusTest {

	private Object fixture;

	private int gefCount;

	private int emfCount;

	private int gmfCount;

	public CommandTreeIteratorTest() {
		super();
	}

	@Test
	public void gefCompoundsAndWrappers() {
		assertThat(collectAll(fixture, Command.class).size(), is(gefCount));
	}

	@Test
	public void emfCompoundsAndWrappers() {
		assertThat(collectAll(fixture, org.eclipse.emf.common.command.Command.class).size(), is(emfCount));
	}

	@Test
	public void gmfCompoundsAndWrappers() {
		assertThat(collectAll(fixture, ICommand.class).size(), is(gmfCount));
	}

	@Test
	public void genericCompoundsAndWrappers() {
		assertThat(collectAll(fixture).size(), is(gefCount + emfCount + gmfCount));
	}

	//
	// Test framework
	//

	@Before
	public void createFixture() {
		CompoundCommand compoundGEFCommand = new CompoundCommand();

		// Add a simple EMF command wrapped
		compoundGEFCommand.add(EMFtoGEFCommandWrapper.wrap(new TestEMFCommand()));

		// Add a double-wrapped GMF command
		compoundGEFCommand.add(EMFtoGEFCommandWrapper.wrap(GMFtoEMFCommandWrapper.wrap(new TestGMFCommand())));

		// Add a (wrapped differently) GMF compound
		CompositeCommand compoundGMFCommand = new CompositeCommand("composite");
		compoundGEFCommand.add(new ICommandProxy(compoundGMFCommand));

		// Add a GEF command (wrapped) to the GMF compound
		compoundGMFCommand.add(new CommandProxy(new TestGEFCommand()));
		compoundGMFCommand.add(EMFtoGMFCommandWrapper.wrap(GEFtoEMFCommandWrapper.wrap(new TestGEFCommand())));

		// And now an EMF compound
		org.eclipse.emf.common.command.CompoundCommand compoundEMFCommand = new org.eclipse.emf.common.command.CompoundCommand();
		compoundGMFCommand.add(EMFtoGMFCommandWrapper.wrap(compoundEMFCommand));

		// And an EMF command to the EMF compound
		compoundEMFCommand.append(new TestEMFCommand());

		// and a GEF command
		compoundEMFCommand.append(GEFtoEMFCommandWrapper.wrap(new TestGEFCommand()));

		// Top it off with a GMF command on the GMF compound
		compoundGMFCommand.add(new TestGMFCommand());

		fixture = compoundGEFCommand;
	}

	<C> Set<C> collectAll(Object command, Class<C> type) {
		Set<C> result = Sets.newLinkedHashSet();

		for (Iterator<C> iter = CommandTreeIterator.iterate(command, type); iter.hasNext();) {
			result.add(iter.next());
		}

		return result;
	}

	Set<?> collectAll(Object command) {
		Set<Object> result = Sets.newLinkedHashSet();

		for (Iterator<?> iter = CommandTreeIterator.iterate(command); iter.hasNext();) {
			result.add(iter.next());
		}

		return result;
	}

	//
	// Nested types
	//

	private class TestEMFCommand extends AbstractCommand {
		TestEMFCommand() {
			super();

			if (fixture == null) {
				emfCount++;
			}
		}

		public void execute() {
			// Pass
		}

		public void redo() {
			// Pass
		}
	}

	private class TestGEFCommand extends Command {
		TestGEFCommand() {
			super();

			if (fixture == null) {
				gefCount++;
			}
		}
	}

	private class TestGMFCommand extends org.eclipse.gmf.runtime.common.core.command.AbstractCommand {
		TestGMFCommand() {
			super("test");

			if (fixture == null) {
				gmfCount++;
			}
		}

		@Override
		protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
			return CommandResult.newCancelledCommandResult();
		}

		@Override
		protected CommandResult doRedoWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
			return CommandResult.newCancelledCommandResult();
		}

		@Override
		protected CommandResult doUndoWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
			return CommandResult.newCancelledCommandResult();
		}
	}
}

Back to the top