Skip to main content
summaryrefslogtreecommitdiffstats
blob: 266a0101e4650e1d2043e08016368699dc067f18 (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
/*******************************************************************************
 * Copyright (c) 2008, 2012 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.utility.tests.internal.command;

import org.eclipse.jpt.common.utility.command.Command;
import org.eclipse.jpt.common.utility.command.CommandExecutor;
import org.eclipse.jpt.common.utility.internal.command.CommandRunnable;
import org.eclipse.jpt.common.utility.internal.command.RunnableCommand;
import org.eclipse.jpt.common.utility.internal.command.ThreadLocalCommand;
import org.eclipse.jpt.common.utility.tests.internal.MultiThreadedTestCase;
import org.eclipse.jpt.common.utility.tests.internal.TestTools;

public class CommandTests
	extends MultiThreadedTestCase
{
	public CommandTests(String name) {
		super(name);
	}

	public void testNullCommand() {
		Command command = Command.Null.instance();
		command.execute();  // just make sure it doesn't blow up?
	}

	public void testNullCommand_toString() {
		Command command = Command.Null.instance();
		assertNotNull(command.toString());
	}

	public void testNullCommand_serialization() throws Exception {
		Command command1 = Command.Null.instance();
		Command command2 = TestTools.serialize(command1);
		assertSame(command1, command2);
	}

	public void testDisabledCommand() {
		Command command = Command.Disabled.instance();
		boolean exCaught = false;
		try {
			command.execute();
			fail();
		} catch (UnsupportedOperationException ex) {
			exCaught = true;
		}
		assertTrue(exCaught);
	}

	public void testDisabledCommand_toString() {
		Command command = Command.Disabled.instance();
		assertNotNull(command.toString());
	}

	public void testDisabledCommand_serialization() throws Exception {
		Command command1 = Command.Disabled.instance();
		Command command2 = TestTools.serialize(command1);
		assertSame(command1, command2);
	}

	public void testRunnableCommand() {
		SimpleTestRunnable testRunnable = new SimpleTestRunnable();
		assertFalse(testRunnable.ran);
		Command command = new RunnableCommand(testRunnable);
		command.execute();
		assertTrue(testRunnable.ran);
	}

	static class SimpleTestRunnable implements Runnable {
		boolean ran = false;
		public void run() {
			this.ran = true;
		}
	}

	public void testCommandRunnable() {
		TestCommand testCommand = new TestCommand();
		assertEquals(0, testCommand.count);
		Runnable runnable = new CommandRunnable(testCommand);
		runnable.run();
		assertEquals(1, testCommand.count);
	}

	static class TestCommand implements Command {
		int count = 0;
		public void execute() {
			this.count++;
		}
	}

	public void testThreadLocalCommand() throws Exception {
		ThreadLocalCommand threadLocalCommand = new ThreadLocalCommand();
		TestRunnable testRunnable1 = new TestRunnable(threadLocalCommand, 1);
		Thread thread1 = this.buildThread(testRunnable1);
		thread1.run();

		TestRunnable testRunnable2 = new TestRunnable(threadLocalCommand, 2);
		Thread thread2 = this.buildThread(testRunnable2);
		thread2.run();

		thread1.join();
		thread2.join();

		assertEquals(1, testRunnable1.testCommand.count);

		assertEquals(2, testRunnable2.testCommand.count);
	}

	static class TestCommandExecutor implements CommandExecutor {
		int count = 0;
		public void execute(Command command) {
			this.count++;
			command.execute();
		}
	}

	static class TestRunnable implements Runnable {
		final ThreadLocalCommand threadLocalCommand;
		final int executionCount;
		final TestCommand testCommand = new TestCommand();
		TestRunnable(ThreadLocalCommand threadLocalCommand, int executionCount) {
			super();
			this.threadLocalCommand = threadLocalCommand;
			this.executionCount = executionCount;
		}
		public void run() {
			this.threadLocalCommand.set(this.testCommand);
			for (int i = 0; i < this.executionCount; i++) {
				this.threadLocalCommand.execute();
			}
		}
	}
}

Back to the top