Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: be8d06309437af8e5ffda14511ff18a0609b959e (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
/*******************************************************************************
 * Copyright (c) 2008, 2010 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.tests.updatechecker;

import java.util.Arrays;
import junit.framework.Assert;
import org.eclipse.equinox.internal.provisional.p2.updatechecker.IUpdateListener;
import org.eclipse.equinox.internal.provisional.p2.updatechecker.UpdateEvent;

/**
 * Test implementation of {@link IUpdateListener}.
 */
public class TestUpdateListener implements IUpdateListener {
	private static final long MAX_WAIT = 5000;

	private UpdateEvent expectedEvent;

	int expectedCount = 0;
	int unexpectedCount = 0;

	public TestUpdateListener(UpdateEvent event) {
		this.expectedEvent = event;
	}

	public boolean matches(Object o) {
		if (!(o instanceof UpdateEvent))
			return false;
		UpdateEvent actual = (UpdateEvent) o;
		return Arrays.equals(expectedEvent.getIUs().toArray(), actual.getIUs().toArray()) && expectedEvent.getProfileId().equals(actual.getProfileId());
	}

	public void updatesAvailable(UpdateEvent event) {
		if (matches(event))
			expectedCount++;
		else
			unexpectedCount++;
	}

	/**
	 * Verifies that the given number of matching events occurred, and that no
	 * unmatching events occurred.
	 */
	public void verify(int expected) {
		Assert.assertEquals(expected, expectedCount);
		Assert.assertEquals(0, unexpectedCount);
	}

	public void waitForEvent() {
		long waitStart = System.currentTimeMillis();
		while (expectedCount == 0 && unexpectedCount == 0 && ((System.currentTimeMillis() - waitStart) < MAX_WAIT)) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				//ignore
			}
		}
	}

	public void reset() {
		expectedCount = unexpectedCount = 0;
	}
}

Back to the top