Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a2c0ae106e86ae6a252a70ee4ce2efab2135f164 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/*******************************************************************************
 * 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;

import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URL;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Properties;

import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestListener;
import junit.framework.TestResult;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.swt.widgets.Display;
import org.eclipse.tcf.te.runtime.interfaces.IConditionTester;
import org.eclipse.tcf.te.runtime.utils.Host;
import org.eclipse.tcf.te.tests.activator.UIPlugin;
import org.eclipse.tcf.te.tests.interfaces.IConfigurationProperties;
import org.eclipse.tcf.te.ui.views.interfaces.IUIConstants;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;
import org.osgi.framework.Bundle;

/**
 * Core test case implementation.
 */
public class CoreTestCase extends TestCase {
	// Internal property id to store if or if not the views zoom state
	// got changed before executing the test case.
	private final static String VIEW_ZOOM_STATE_CHANGED = "viewZoomStateChanged"; //$NON-NLS-1$

	// The test configuration
	private final Properties configuration = new Properties();

	// The internal test listener.
	private final TestListener listener = new InternalTestListener();

	/**
	 * Listens to the test executions and logs the failures.
	 */
	private class InternalTestListener implements TestListener {

		/**
		 * Constructor.
		 */
		public InternalTestListener() {
		}

		/* (non-Javadoc)
		 * @see junit.framework.TestListener#startTest(junit.framework.Test)
		 */
		@Override
		public void startTest(Test test) {
		}

		/* (non-Javadoc)
		 * @see junit.framework.TestListener#addError(junit.framework.Test, java.lang.Throwable)
		 */
		@Override
		public synchronized void addError(Test test, Throwable error) {
			if (test != null && error != null) {
				// Log the error to the error log.
				IStatus status = new Status(IStatus.ERROR,
				                            UIPlugin.getUniqueIdentifier(),
				                            1,
				                            "Test case '" + test + "' failed with error. Possible cause: " + error.getLocalizedMessage(), //$NON-NLS-1$ //$NON-NLS-2$
				                            error
				                           );
				UIPlugin.getDefault().getLog().log(status);
			}
		}

		/* (non-Javadoc)
		 * @see junit.framework.TestListener#addFailure(junit.framework.Test, junit.framework.AssertionFailedError)
		 */
		@Override
		public synchronized void addFailure(Test test, AssertionFailedError failure) {
			if (test != null && failure != null) {
				// Log the failure to the error log.
				IStatus status = new Status(IStatus.ERROR,
											UIPlugin.getUniqueIdentifier(),
				                            1,
				                            "Test case '" + test + "' failed. Failure: " + failure.getLocalizedMessage(), //$NON-NLS-1$ //$NON-NLS-2$
				                            failure
				                           );
				UIPlugin.getDefault().getLog().log(status);
			}
		}

		/* (non-Javadoc)
		 * @see junit.framework.TestListener#endTest(junit.framework.Test)
		 */
		@Override
		public void endTest(Test test) {
			// nothing to do on end test
		}
	}

	/**
	 * Constructor.
	 */
	public CoreTestCase() {
		this(null);
	}

	/**
	 * Constructor.
	 *
	 * @param name The test name.
	 */
	public CoreTestCase(String name) {
		super(name);

		// Setup the test case configuration. Clear out the old configuration.
		configuration.clear();
		initialize();
	}

	/**
	 * Initialize the test configuration.
	 * <p>
	 * Clients may overwrite this method to modify the base configuration.
	 */
	protected void initialize() {
		Assert.isNotNull(configuration);

		setProperty(VIEW_ZOOM_STATE_CHANGED, false);
		setProperty(IConfigurationProperties.MAXIMIZE_VIEW, false);

		setProperty(IConfigurationProperties.TARGET_PERSPECTIVE, "org.eclipse.tcf.te.ui.perspective"); //$NON-NLS-1$
		setProperty(IConfigurationProperties.TARGET_VIEW, IUIConstants.ID_EXPLORER);
	}

	/**
	 * Sets a boolean configuration property.
	 *
	 * @param key The property key. Must not be <code>null</code>!
	 * @param value The property value.
	 */
	protected final void setProperty(String key, boolean value) {
		Assert.isNotNull(key);
		setProperty(key, value ? Boolean.TRUE.toString() : Boolean.FALSE.toString());
	}

	/**
	 * Checks if a boolean configuration property has been set to the
	 * given property value.
	 *
	 * @param key The property key. Must not be <code>null</code>!
	 * @param value The property value.
	 *
	 * @return <code>True</code> if the property values match, <code>false</code> if not.
	 */
	protected final boolean isProperty(String key, boolean value) {
		Assert.isNotNull(key);
		return (value ? Boolean.TRUE : Boolean.FALSE).equals(Boolean.valueOf(configuration.getProperty(key, "false"))); //$NON-NLS-1$
	}

	/**
	 * Sets a string configuration property.
	 * <p>
	 * If the value is <code>null</code>, the configuration property
	 * will be removed.
	 *
	 * @param key The property key. Must not be <code>null</code>!
	 * @param value The property value.
	 */
	protected final void setProperty(String key, String value) {
		Assert.isNotNull(key);
		if (value != null) configuration.setProperty(key, value);
		else configuration.remove(key);
	}

	/**
	 * Checks if a string configuration property has been set to the given property
	 * value (case insensitive).
	 * <p>
	 * If the value is <code>null</code>, the method returns <code>true</code> if
	 * the configuration property cannot be found.
	 *
	 * @param key The property key. Must not be <code>null</code>!
	 * @param value The property value.
	 *
	 * @return <code>True</code> if the property values match, <code>false</code> if not.
	 */
	protected final boolean isProperty(String key, String value) {
		Assert.isNotNull(key);
		return value != null ? value.equalsIgnoreCase(configuration.getProperty(key)) : !configuration.containsKey(key);
	}

	/**
	 * Returns the value of a string configuration property.
	 *
	 * @param key The property key. Must not be <code>null</code>!
	 * @return The property value or <code>null</code> if not set.
	 */
	protected final String getProperty(String key) {
		Assert.isNotNull(key);
		return configuration.getProperty(key, null);
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#run(junit.framework.TestResult)
	 */
	@Override
	public void run(TestResult result) {
		if (result != null) result.addListener(listener);
		super.run(result);
		if (result != null) result.removeListener(listener);
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#runBare()
	 */
	@Override
	public void runBare() throws Throwable {
		long start = printStart(getName());

		boolean toggleInteractiveMode = Host.isInteractive();
	    if (toggleInteractiveMode) {
	    	System.setProperty("NOINTERACTIVE", "true"); //$NON-NLS-1$ //$NON-NLS-2$
	    	try {
	    		Field f = Host.class.getDeclaredField("isInteractive"); //$NON-NLS-1$
	    		f.setAccessible(true);
	    		f.set(null, null);
	    	}
	    	catch (Exception e) { /* ignored on purpose */ }
	    }

		try {
			super.runBare();
		} finally {
			if (toggleInteractiveMode) {
		    	System.setProperty("NOINTERACTIVE", "false"); //$NON-NLS-1$ //$NON-NLS-2$
		    	try {
		    		Field f = Host.class.getDeclaredField("isInteractive"); //$NON-NLS-1$
		    		f.setAccessible(true);
		    		f.set(null, null);
		    	}
		    	catch (Exception e) { /* ignored on purpose */ }
			}

			printEnd(getName(), start);
		}
	}

	// Local date format presenting long date and time format.
	private final DateFormat DATE_FORMAT = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.getDefault());

	/**
	 * Prints out the test start time.
	 *
	 * @param name The name. Must not be <code>null</code>.
	 * @return The start time in milliseconds.
	 */
	protected long printStart(String name) {
		Assert.isNotNull(name);
		long startTime = System.currentTimeMillis();
		System.out.println("\n=== " + name + " started at: " + DATE_FORMAT.format(new Date(startTime))); //$NON-NLS-1$ //$NON-NLS-2$
		return startTime;
	}

	/**
	 * Prints out the test end time together with the test duration in milliseconds.
	 *
	 * @param name The name. Must not be <code>null</code>.
	 * @param startTime The start time in milliseconds.
	 */
	protected void printEnd(String name, long startTime) {
		Assert.isNotNull(name);
		long endTime = System.currentTimeMillis();
		long duration = endTime - startTime;
		System.out.println("=== " + name + " finished at: " + DATE_FORMAT.format(new Date(endTime)) + " (duration: " + duration + " ms)"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
	}

	/**
	 * Prints out the given message.
	 *
	 * @param message The message. Must not be <code>null</code>.
	 */
	protected void printMessage(String message) {
		Assert.isNotNull(message);
		System.out.println(message);
	}

	/**
	 * Prints out the given debug message.
	 * <p>
	 * Debug messages are printed only if the platform is in debug mode.
	 *
	 * @param message The message. Must not be <code>null</code>.
	 */
	protected void printDebugMessage(String message) {
		Assert.isNotNull(message);
		if (Platform.inDebugMode()) System.out.println(message);
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#setUp()
	 */
	@Override
	protected void setUp() throws Exception {
		super.setUp();

		// View handling must run in the UI thread
		if (Display.findDisplay(Thread.currentThread()) == null) {
			PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
				@Override
				public void run() {
					maximizeView();
				}
			});
		} else {
			maximizeView();
		}
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#tearDown()
	 */
	@Override
	protected void tearDown() throws Exception {
		// View handling must run in the UI thread
		if (Display.findDisplay(Thread.currentThread()) == null) {
			PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
				@Override
				public void run() {
					restoreView();
				}
			});
		} else {
			restoreView();
		}

		flushEventQueue();
		super.tearDown();
	}

	/**
	 * Flush the display event queue.
	 * <p>
	 * Unhandled exceptions in the event loop event are caught as follows:
	 * In case multiple events from the event loop throw exceptions these are printed
	 * to stdout. The first exception found in the event loop is thrown to the caller.
	 *
	 * @throws Exception in case an unhandled event loop exception was found.
	 */
	protected void flushEventQueue() throws Exception {
		Display display = Display.getCurrent();
		if (display!=null) {
			//on the dispatch thread already
			Exception eventLoopException = null;
			while(!display.isDisposed()) {
				//loop until event queue is flushed
				try {
					if (!display.readAndDispatch()) {
						break;
					}
				} catch(Exception e) {
					if (eventLoopException==null) {
						eventLoopException = e;
					} else {
						System.out.println("Multiple unhandled event loop exceptions:"); //$NON-NLS-1$
						e.printStackTrace();
					}
				}
			}
			if (eventLoopException!=null) {
				throw eventLoopException;
			}
		} else {
			//calling from background thread
			final Exception[] ex = new Exception[1];
			display = Display.getDefault();
			display.syncExec(new Runnable() {
				@Override
				public void run() {
					try {
						flushEventQueue();
					} catch(Exception e) {
						ex[0] = e;
					}
				}
			});
			if (ex[0]!=null) throw ex[0];
		}
	}

	/**
	 * Bring the main view to front.
	 * <p>
	 * If the property {@link IConfigurationProperties#MAXIMIZE_VIEW} is set, the
	 * view will be maximized.
	 * <p>
	 * A possibly open Eclipse Intro View will be hidden automatically.
	 */
	protected void maximizeView() {
		Assert.isNotNull(Display.findDisplay(Thread.currentThread()), "Illegal Thread Access"); //$NON-NLS-1$

		final String perspectiveId = getProperty(IConfigurationProperties.TARGET_PERSPECTIVE);
		assertNotNull("Invalid null-value for test case perspective id!", perspectiveId); //$NON-NLS-1$
		final String viewId = getProperty(IConfigurationProperties.TARGET_VIEW);
		assertNotNull("Invalid null-value for test case view id!", perspectiveId); //$NON-NLS-1$

		// Find the Eclipse Intro page and hide it.
		hideView("org.eclipse.ui.internal.introview", perspectiveId); //$NON-NLS-1$

		// Show the main view
		setProperty(VIEW_ZOOM_STATE_CHANGED, false);
		IViewPart part = showView(viewId, perspectiveId);
		assertNotNull("Main view is not available!", part); //$NON-NLS-1$

		// Get the view reference for setting the maximized state
		IViewReference reference = findView(viewId, perspectiveId);
		assertNotNull("Failed to lookup view reference for main view!", reference); //$NON-NLS-1$
		if (reference.getPage().getPartState(reference) != IWorkbenchPage.STATE_MAXIMIZED
				&& isProperty(IConfigurationProperties.MAXIMIZE_VIEW, true)) {
			reference.getPage().toggleZoom(reference);
			setProperty(VIEW_ZOOM_STATE_CHANGED, true);
		} else if (reference.getPage().getPartState(reference) == IWorkbenchPage.STATE_MAXIMIZED
				&& isProperty(IConfigurationProperties.MAXIMIZE_VIEW, false)) {
			reference.getPage().toggleZoom(reference);
			setProperty(VIEW_ZOOM_STATE_CHANGED, true);
		}

		// Give the UI a chance to repaint if the view zoom state changed
		if (isProperty(VIEW_ZOOM_STATE_CHANGED, true)) {
			waitAndDispatch(1000);
		}
	}

	/**
	 * Restore the main view state.
	 */
	protected void restoreView() {
		Assert.isNotNull(Display.findDisplay(Thread.currentThread()), "Illegal Thread Access"); //$NON-NLS-1$

		// restore the original view zoom state
		if (isProperty(VIEW_ZOOM_STATE_CHANGED, true)) {
			final String perspectiveId = getProperty(IConfigurationProperties.TARGET_PERSPECTIVE);
			assertNotNull("Invalid null-value for test case perspective id!", perspectiveId); //$NON-NLS-1$
			final String viewId = getProperty(IConfigurationProperties.TARGET_VIEW);
			assertNotNull("Invalid null-value for test case view id!", perspectiveId); //$NON-NLS-1$

			IViewReference reference = findView(viewId, perspectiveId);
			assertNotNull("Failed to lookup view reference for RSE Remote Systems View!", reference); //$NON-NLS-1$
			if (reference.getPage().getPartState(reference) == IWorkbenchPage.STATE_MAXIMIZED
					&& isProperty(IConfigurationProperties.MAXIMIZE_VIEW, true)) {
				reference.getPage().toggleZoom(reference);
			} else if (reference.getPage().getPartState(reference) != IWorkbenchPage.STATE_MAXIMIZED
					&& isProperty(IConfigurationProperties.MAXIMIZE_VIEW, false)) {
				reference.getPage().toggleZoom(reference);
			}
			setProperty(VIEW_ZOOM_STATE_CHANGED, false);
		}
	}

	/**
	 * Lookup the view reference for the given view and perspective id's.
	 *
	 * @param viewId The view id. Must not be <code>null</code>.
	 * @param perspectiveId The perspective id. Must not be <code>null</code>.
	 *
	 * @return The view reference instance to the view or <code>null</code> if not available.
	 */
	public final IViewReference findView(String viewId, String perspectiveId) {
		Assert.isNotNull(Display.findDisplay(Thread.currentThread()), "Illegal Thread Access"); //$NON-NLS-1$
		Assert.isNotNull(viewId);
		Assert.isNotNull(perspectiveId);

		IWorkbench workbench = PlatformUI.getWorkbench();
		assertNotNull("Failed to query current workbench instance!", workbench); //$NON-NLS-1$
		IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
		assertNotNull("Failed to query currently active workbench window!", window); //$NON-NLS-1$

		try {
			workbench.showPerspective(perspectiveId, window);
		} catch (WorkbenchException e) {
			IStatus status = new Status(IStatus.ERROR, UIPlugin.getUniqueIdentifier(),
										"Failed to switch to requested perspective (id = " + perspectiveId + ")!", e); //$NON-NLS-1$ //$NON-NLS-2$
			UIPlugin.getDefault().getLog().log(status);
		}

		IWorkbenchPage page = window.getActivePage();
		assertNotNull("Failed to query currently active workbench page!", page); //$NON-NLS-1$

		return page.findViewReference(viewId);
	}

	/**
	 * Shows the view.
	 *
	 * @param viewId The view id. Must not be <code>null</code>.
	 * @param perspectiveId The perspective id. Must not be <code>null</code>.
	 *
	 * @return The view part instance to the view or <code>null</code> if it cannot be shown.
	 */
	public final IViewPart showView(String viewId, String perspectiveId) {
		Assert.isNotNull(Display.findDisplay(Thread.currentThread()), "Illegal Thread Access"); //$NON-NLS-1$
		Assert.isNotNull(viewId);
		Assert.isNotNull(perspectiveId);

		IWorkbench workbench = PlatformUI.getWorkbench();
		assertNotNull("Failed to query current workbench instance!", workbench); //$NON-NLS-1$
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		assertNotNull("Failed to query currently active workbench window!", window); //$NON-NLS-1$

		try {
			workbench.showPerspective(perspectiveId, window);
		} catch (WorkbenchException e) {
			IStatus status = new Status(IStatus.ERROR, UIPlugin.getUniqueIdentifier(),
										"Failed to switch to requested perspective (id = " + perspectiveId + ")!", e); //$NON-NLS-1$ //$NON-NLS-2$
			UIPlugin.getDefault().getLog().log(status);
		}

		IWorkbenchPage page = window.getActivePage();
		assertNotNull("Failed to query currently active workbench page!", page); //$NON-NLS-1$

		IViewPart part = null;
		try {
			part = page.showView(viewId);
		} catch (PartInitException e) {
			IStatus status = new Status(IStatus.ERROR, UIPlugin.getUniqueIdentifier(),
										"Failed to switch to requested perspective (id = " + perspectiveId + ")!", e); //$NON-NLS-1$ //$NON-NLS-2$
			UIPlugin.getDefault().getLog().log(status);
		}

		return part;
	}

	/**
	 * Hides the view.
	 *
	 * @param viewId The view id. Must not be <code>null</code>.
	 * @param perspectiveId The perspective id. Must not be <code>null</code>.
	 */
	public final void hideView(String viewId, String perspectiveId) {
		Assert.isNotNull(Display.findDisplay(Thread.currentThread()), "Illegal Thread Access"); //$NON-NLS-1$
		Assert.isNotNull(viewId);
		Assert.isNotNull(perspectiveId);

		IViewReference viewReference = findView(viewId, perspectiveId);
		if (viewReference != null) {
			PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(viewReference);
			waitAndDispatch(1000);
		}
	}

	/**
	 * Convenience method to call {@link #waitAndDispatch(long, null)}.
	 *
	 * @param timeout The timeout in milliseconds. Must be larger than 0.
	 *
	 * @return <code>True</code> if the method returned because of the timeout, <code>false</code> if the
	 *         method returned because of the condition became true.
	 */
	public boolean waitAndDispatch(long timeout) {
		Assert.isTrue(timeout > 0);
		return waitAndDispatch(timeout, null);
	}

	/**
	 * Method does not return until either the timeout has been exceeded or
	 * the interrupt condition is fulfilled. If the method called in a UI thread,
	 * the method keeps the UI thread dispatching running. If called in a
	 * non-UI thread, the method uses {@link Thread#sleep(long)}.
	 * <p>
	 * If a timeout of 0 milliseconds is specified, the method waits until the
	 * given interrupt condition is fulfilled.
	 *
	 * @param timeout The timeout in milliseconds. Must be larger or equals than 0.
	 * @param condition The condition to test. Must not be <code>null</code> if the timeout is 0.
	 *
	 * @return <code>True</code> if the method returned because of the timeout, <code>false</code> if the
	 *         method returned because of the condition became true.
	 */
	public boolean waitAndDispatch(long timeout, IConditionTester condition) {
		Assert.isTrue(timeout >= 0);
		if (timeout == 0) Assert.isNotNull(condition);

		boolean isTimedOut= false;
		if (timeout >= 0) {
			long start = System.currentTimeMillis();
			final Display display = Display.findDisplay(Thread.currentThread());
			if (display != null) {
				long current = System.currentTimeMillis();
				while (timeout == 0 || (current - start) < timeout && !display.isDisposed()) {
					if (condition != null && condition.isConditionFulfilled()) break;
					if (!display.readAndDispatch()) display.sleep();
					current = System.currentTimeMillis();
				}
				isTimedOut = (current - start) >= timeout && timeout > 0;
			} else {
				long current = System.currentTimeMillis();
				while (timeout == 0 || (current - start) < timeout) {
					if (condition != null && condition.isConditionFulfilled()) break;
					try { Thread.sleep(50); } catch (InterruptedException e) { /* ignored on purpose */ }
					current = System.currentTimeMillis();
				}
				isTimedOut = (current - start) >= timeout && timeout > 0;
			}
		}
		if (condition != null) condition.cleanup();

		return isTimedOut;
	}

	/**
	 * Calculates the absolute path to load additional data from.
	 * <p>
	 * The returned path is calculated as follow:<br>
	 * <ul>
	 *     <li>Add org.eclipse.tcf.te.tests bundle location</li>
	 *     <li>Add &quot;data&quot;</li>
	 *     <li>Add the given relative path</li>
	 *     <li>If &quot;hostSpecific&quot; is true, add &quot;Platform.getOS()&quot;</li>
	 *     <li>If &quot;archSpecific&quot; is true, add &quot;Platform.getOSArch()&quot;</li>
	 * </ul>
	 * <p>
	 * The calculated path must be a readable directory, otherwise the method will
	 * return <code>null</code>.
	 *
	 * @param path The relative path segment to append. Must not be <code>null</code>.
	 * @param hostSpecific Specify <code>true</code> to include {@link Platform#getOS()}, <code>false</code> if not.
	 * @param archSpecific Specify <code>true</code> to include {@link Platform#getOSArch()}, <code>false</code> if not.
	 *
	 * @return The absolute path to the data location or <code>null</code>.
	 */
	protected final IPath getDataLocation(String path, boolean hostSpecific, boolean archSpecific) {
		Assert.isNotNull(path);
		IPath root = null;

		Bundle bundle = getTestBundle();
		if (bundle != null) {
			IPath relative = new Path ("data").append(path); //$NON-NLS-1$
			if (hostSpecific) relative = relative.append(Platform.getOS());
			if (archSpecific) relative = relative.append(Platform.getOSArch());

			URL url = FileLocator.find(bundle, relative, null);
			if (url != null) {
				try {
					root = new Path(FileLocator.resolve(url).getFile());
					if (!root.toFile().isDirectory() || !root.toFile().canRead()) {
						root = null;
					}
				} catch (IOException e) { /* ignored on purpose */ }
			}
		}

		return root;
	}

	/**
	 * Returns the test bundle.
	 *
	 * @return The test bundle.
	 */
	protected Bundle getTestBundle() {
		return UIPlugin.getDefault().getBundle();
	}
}

Back to the top