Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7b0c6660d2a48f45593c3c90a31dc6b9ab8f9e11 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*******************************************************************************
 * Copyright (c) 2011, 2012 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.tests.statushandler;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.tcf.te.runtime.interfaces.IConditionTester;
import org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer;
import org.eclipse.tcf.te.runtime.properties.PropertiesContainer;
import org.eclipse.tcf.te.runtime.statushandler.StatusHandlerManager;
import org.eclipse.tcf.te.runtime.statushandler.activator.CoreBundleActivator;
import org.eclipse.tcf.te.runtime.statushandler.interfaces.IStatusHandler;
import org.eclipse.tcf.te.runtime.statushandler.interfaces.IStatusHandlerConstants;
import org.eclipse.tcf.te.runtime.statushandler.nls.Messages;
import org.eclipse.tcf.te.runtime.statushandler.status.QuestionStatus;
import org.eclipse.tcf.te.runtime.statushandler.status.YesNoCancelStatus;
import org.eclipse.tcf.te.runtime.utils.Host;
import org.eclipse.tcf.te.tests.CoreTestCase;
import org.eclipse.tcf.te.tests.activator.UIPlugin;
import org.osgi.framework.BundleContext;

/**
 * Status handler test cases.
 */
@SuppressWarnings("restriction")
public class StatusHandlerTestCase extends CoreTestCase {

	/**
	 * Provides a test suite to the caller which combines all single
	 * test bundled within this category.
	 *
	 * @return Test suite containing all test for this test category.
	 */
	public static Test getTestSuite() {
		TestSuite testSuite = new TestSuite("Test status handler contributions"); //$NON-NLS-1$

			// add ourself to the test suite
			testSuite.addTestSuite(StatusHandlerTestCase.class);

		return testSuite;
	}

	/**
	 * Test the basic status handler extension contribution mechanism.
	 */
	public void testContributions() {
		assertNotNull("Unexpected return value 'null'.", StatusHandlerManager.getInstance()); //$NON-NLS-1$

		int testHandlerCount = 0;

		IStatusHandler[] handlers = StatusHandlerManager.getInstance().getHandlers(false);
		for (IStatusHandler handler : handlers) {
			if (handler.getId().startsWith("org.eclipse.tcf.te.tests")) { //$NON-NLS-1$
				testHandlerCount++;
			}
		}

		assertEquals("Unexpected number of contributed test status handler.", 2, testHandlerCount); //$NON-NLS-1$

		IStatusHandler[] handlers2 = StatusHandlerManager.getInstance().getHandlers(true);
		assertEquals("Unexpected number of status handler returned.", handlers.length, handlers2.length); //$NON-NLS-1$
	}

	/**
	 * Test status handler extension contribution mechanism with context objects.
	 */
	public void testContributionsWithContext() {
		assertNotNull("Unexpected return value 'null'.", StatusHandlerManager.getInstance()); //$NON-NLS-1$

		List<String> handlerIds = new ArrayList<String>();

		IStatusHandler[] handlers = StatusHandlerManager.getInstance().getHandler(this);
		for (IStatusHandler handler : handlers) {
			handlerIds.add(handler.getId());
		}

		assertTrue("Test case enabled test status handler not active.", handlerIds.contains("org.eclipse.tcf.te.tests.handler1")); //$NON-NLS-1$ //$NON-NLS-2$
		assertFalse("Interrupt condition enabled test status handler is active.", handlerIds.contains("org.eclipse.tcf.te.tests.handler2")); //$NON-NLS-1$ //$NON-NLS-2$

		handlerIds.clear();

		IConditionTester context = new IConditionTester() {
			/* (non-Javadoc)
			 * @see org.eclipse.tcf.te.runtime.interfaces.IConditionTester#isConditionFulfilled()
			 */
			@Override
			public boolean isConditionFulfilled() {
			    return false;
			}
			/* (non-Javadoc)
			 * @see org.eclipse.tcf.te.runtime.interfaces.IConditionTester#cleanup()
			 */
			@Override
			public void cleanup() {
			}
		};

		handlers = StatusHandlerManager.getInstance().getHandler(context);
		for (IStatusHandler handler : handlers) {
			handlerIds.add(handler.getId());
		}

		assertTrue("Test case enabled test status handler not active.", handlerIds.contains("org.eclipse.tcf.te.tests.handler1")); //$NON-NLS-1$ //$NON-NLS-2$
		assertTrue("Interrupt condition enabled test status handler not active.", handlerIds.contains("org.eclipse.tcf.te.tests.handler2")); //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
	 * Test the default status handler.
	 */
	public void testDefaultStatusHandler() {
		assertNotNull("Unexpected return value 'null'.", StatusHandlerManager.getInstance()); //$NON-NLS-1$
		assertFalse("Failed to toggle interactive mode.", Host.isInteractive()); //$NON-NLS-1$

		IStatusHandler handler = StatusHandlerManager.getInstance().getHandler("org.eclipse.tcf.te.statushandler.default", false); //$NON-NLS-1$
		assertNotNull("Failed to determine default status handler.", handler); //$NON-NLS-1$
		IStatusHandler handler2 = StatusHandlerManager.getInstance().getHandler("org.eclipse.tcf.te.statushandler.default", true); //$NON-NLS-1$
		assertNotSame("Failed to get unique instance of the default status handler.", handler, handler2); //$NON-NLS-1$

		IStatusHandler[] handler3 = StatusHandlerManager.getInstance().getHandler(this);
		assertNotNull("Unexpected return value null.", handler3); //$NON-NLS-1$

		IPropertiesContainer data = new PropertiesContainer();
		data.setProperty(IStatusHandlerConstants.PROPERTY_TITLE, "Statushandler Test"); //$NON-NLS-1$

		IStatus status = new Status(IStatus.INFO, UIPlugin.getUniqueIdentifier(), "Info"); //$NON-NLS-1$
		handler.handleStatus(status, data, null);

		status = new Status(IStatus.WARNING, UIPlugin.getUniqueIdentifier(), "Warning"); //$NON-NLS-1$
		handler.handleStatus(status, data, null);

		status = new Status(IStatus.ERROR, UIPlugin.getUniqueIdentifier(), "Error"); //$NON-NLS-1$
		handler.handleStatus(status, data, null);

		status = new QuestionStatus(UIPlugin.getUniqueIdentifier(), "Question"); //$NON-NLS-1$
		handler.handleStatus(status, data, null);

		status = new QuestionStatus(UIPlugin.getUniqueIdentifier(), "Question", new Throwable()); //$NON-NLS-1$
		handler.handleStatus(status, data, null);

		status = new QuestionStatus(UIPlugin.getUniqueIdentifier(), 1, "Question", new Throwable()); //$NON-NLS-1$
		handler.handleStatus(status, data, null);

		status = new YesNoCancelStatus(UIPlugin.getUniqueIdentifier(), "YesNoCancel"); //$NON-NLS-1$
		handler.handleStatus(status, data, null);

		status = new YesNoCancelStatus(UIPlugin.getUniqueIdentifier(), "YesNoCancel", new Throwable()); //$NON-NLS-1$
		handler.handleStatus(status, data, null);

		status = new YesNoCancelStatus(UIPlugin.getUniqueIdentifier(), 1, "YesNoCancel", new Throwable()); //$NON-NLS-1$
		handler.handleStatus(status, data, null);
	}

	/**
	 * More status handler tests.
	 */
    public void testStatusHandler() {
		Messages m = new Messages();
		assertNotNull("Failed to instantiate message bundle.", m); //$NON-NLS-1$

		BundleContext context = CoreBundleActivator.getContext();
		assertNotNull("Failed to get status handler bundle context.", context); //$NON-NLS-1$

		String id = CoreBundleActivator.getUniqueIdentifier();
		assertNotNull("Failed to get status handler bundle id.", id); //$NON-NLS-1$
		assertEquals("Unexpected unique identifier: ", "org.eclipse.tcf.te.runtime.statushandler", id); //$NON-NLS-1$ //$NON-NLS-2$

		// Force the context to become null
		try {
	        new CoreBundleActivator().start(null);
        } catch (Exception e) {}

		id = CoreBundleActivator.getUniqueIdentifier();
		assertNotNull("Failed to get status handler bundle id.", id); //$NON-NLS-1$
		assertEquals("Unexpected unique identifier: ", "org.eclipse.tcf.te.runtime.statushandler", id); //$NON-NLS-1$ //$NON-NLS-2$

		// Restore the original context
		try {
	        new CoreBundleActivator().start(context);
        } catch (Exception e) {}

		Class<?>[] clazzes = StatusHandlerManager.class.getDeclaredClasses();
		assertNotNull("Failed to get declared classes of StatusHandlerManager!", clazzes); //$NON-NLS-1$
		assertEquals("Unexpected number of declared classes of StatusHandlerManager!", 1, clazzes.length); //$NON-NLS-1$
		for (Class<?> clazz : clazzes) {
			if (clazz.getName().endsWith("LazyInstanceHolder")) { //$NON-NLS-1$
				try {
					Constructor<?>[] constructors = clazz.getDeclaredConstructors();
					for (Constructor<?> constructor : constructors) {
						assertNotNull("Failed to get default constructor of StatusHandlerManager$LazyInstanceHolder!", constructors); //$NON-NLS-1$
						constructor.setAccessible(true);
						Object instance = constructor.newInstance((Object[])null);
						assertNotNull("Failed to invoke default constructor of StatusHandlerManager$LazyInstanceHolder!", instance); //$NON-NLS-1$
					}
				} catch (Exception e) {}
			}
		}
	}
}

Back to the top