Skip to main content
summaryrefslogtreecommitdiffstats
blob: d6b6345b6b689b70c8a1de0c0f262720471e6154 (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
/*******************************************************************************
 * Copyright (c) 2009 Tasktop Technologies 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:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.tasks.tests.ui;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import junit.framework.AssertionFailedError;
import junit.framework.TestCase;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.mylyn.internal.tasks.ui.ITasksUiPreferenceConstants;
import org.eclipse.mylyn.internal.tasks.ui.TaskListSynchronizationScheduler;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.sync.ISynchronizationSession;
import org.eclipse.mylyn.tasks.tests.TaskTestUtil;
import org.eclipse.mylyn.tasks.tests.connector.MockRepositoryConnector;

/**
 * @author Steffen Pingel
 */
public class TaskListSynchronizationSchedulerTest extends TestCase {

	private class TestConnector extends MockRepositoryConnector {

		CountDownLatch latch = new CountDownLatch(1);

		Throwable e;

		@Override
		public void preSynchronization(ISynchronizationSession event, IProgressMonitor monitor) throws CoreException {
			latch.countDown();
			try {
				if (!mainLatch.await(10, TimeUnit.SECONDS)) {
					this.e = new AssertionFailedError("Timed out waiting for main latch");
				}
			} catch (InterruptedException e) {
				this.e = e;
			}
		}

		public void reset() {
			latch = new CountDownLatch(1);
		}

	}

	private TaskRepository repository;

	private TestConnector connector;

	private AbstractRepositoryConnector oldConnector;

	private final CountDownLatch mainLatch = new CountDownLatch(1);

	@Override
	protected void setUp() throws Exception {
		TasksUiPlugin.getDefault()
				.getPreferenceStore()
				.setValue(ITasksUiPreferenceConstants.REPOSITORY_SYNCH_SCHEDULE_ENABLED, false);

		TaskTestUtil.resetTaskListAndRepositories();
		repository = TaskTestUtil.createMockRepository();
		TasksUiPlugin.getRepositoryManager().addRepository(repository);

		oldConnector = TasksUiPlugin.getRepositoryManager().removeRepositoryConnector(repository.getConnectorKind());
		connector = new TestConnector();
		TasksUiPlugin.getRepositoryManager().addRepositoryConnector(connector);
	}

	@Override
	protected void tearDown() throws Exception {
		release();
		TasksUiPlugin.getRepositoryManager().removeRepositoryConnector(repository.getConnectorKind());
		TasksUiPlugin.getRepositoryManager().addRepositoryConnector(oldConnector);
	}

	public void testSynchronization() throws Exception {
		TaskListSynchronizationScheduler scheduler = new TaskListSynchronizationScheduler(
				TasksUiPlugin.getTaskJobFactory());
		scheduler.setInterval(1);
		assertTrue(connector.latch.await(10, TimeUnit.SECONDS));

		// cancel and reschedule
		scheduler.userAttentionLost();
		scheduler.userAttentionGained();
		release();

		assertTrue("Expected synchronization to run again", connector.latch.await(5, TimeUnit.SECONDS));
	}

	private void release() throws Exception {
		if (connector.e instanceof Error) {
			throw (Error) connector.e;
		}
		if (connector.e instanceof Exception) {
			throw (Exception) connector.e;
		}
		connector.reset();
		mainLatch.countDown();
	}

}

Back to the top