Skip to main content
summaryrefslogtreecommitdiffstats
blob: 18330c454924e9671d3080d6f3a3b0ae9d9ba3ba (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
/*******************************************************************************
 * Copyright (c) 2015 Tasktop Technologies.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Tasktop EULA
 * which accompanies this distribution, and is available at
 * http://tasktop.com/legal
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.ui.migrator;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.mylyn.internal.commons.notifications.feed.ServiceMessage;
import org.eclipse.mylyn.internal.tasks.ui.TaskListBackupManager;
import org.eclipse.mylyn.internal.tasks.ui.migrator.ConnectorMigrationUi.CompleteMigrationJob;
import org.eclipse.mylyn.internal.tasks.ui.notifications.TaskListServiceMessageControl;
import org.eclipse.mylyn.internal.tasks.ui.views.TaskListView;
import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
import org.eclipse.mylyn.tasks.core.IRepositoryManager;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.swt.widgets.Display;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

public class ConnectorMigrationUiTest {

	private ConnectorMigrator migrator;

	private final TaskListView taskList = mock(TaskListView.class);

	private final TaskListBackupManager backupManager = mock(TaskListBackupManager.class);

	private final ConnectorMigrationUi migrationUi = spy(
			new ConnectorMigrationUi(taskList, backupManager, new DefaultTasksState()));

	private CompleteMigrationJob completeMigrationJob;

	@SuppressWarnings("unchecked")
	@Before
	public void setUp() {
		doNothing().when(migrationUi).warnOfValidationFailure((List<TaskRepository>) any(List.class));
		doNothing().when(migrationUi).notifyMigrationComplete();
	}

	@After
	public void tearDown() {
		if (completeMigrationJob != null) {
			completeMigrationJob.dispose();
			// otherwise it will keep rescheduling itself and interfere with other tests
		}
	}

	@Test
	public void promptToMigrate() {
		ServiceMessage message = openMigrationPrompt(Window.OK);

		assertEquals("End of Connector Support", message.getTitle());
		assertTrue(message.getDescription().contains("<a href=\"migrate\">Click here</a>"));
		assertEquals(Dialog.DLG_IMG_MESSAGE_INFO, message.getImage());
		assertFalse(message.openLink("foo"));
		verify(migrationUi, never()).createMigrationWizard(any(ConnectorMigrator.class));
		verify(migrationUi, never()).createPromptToCompleteMigrationJob(any(ConnectorMigrator.class));

		message.openLink("migrate");
	}

	@Test
	public void finishMigrationWizard() throws Exception {
		ServiceMessage message = openMigrationPrompt(Window.OK);
		assertTrue(message.openLink("migrate"));
		verify(migrationUi).createPromptToCompleteMigrationJob(any(ConnectorMigrator.class));
	}

	@Test
	public void cancelMigrationWizard() throws Exception {
		ServiceMessage message = openMigrationPrompt(Window.CANCEL);
		assertFalse(message.openLink("migrate"));
		verify(migrationUi, never()).createPromptToCompleteMigrationJob(any(ConnectorMigrator.class));
	}

	@Test
	public void promptToMigrateOpensSecondMessage() {
		ServiceMessage message = openMigrationPrompt(Window.OK);
		Job job = mock(Job.class);
		doReturn(job).when(migrationUi).createPromptToCompleteMigrationJob(any(ConnectorMigrator.class));

		message.openLink("migrate");
		verify(migrationUi).createMigrationWizard(any(ConnectorMigrator.class));
		verify(migrationUi).createPromptToCompleteMigrationJob(any(ConnectorMigrator.class));
		verify(job).schedule();
	}

	@Test
	public void promptToCompleteMigration() throws InterruptedException {
		ServiceMessage message = openCompleteMigrationPrompt(Window.OK);

		assertEquals("Connector Migration", message.getTitle());
		assertTrue(message.getDescription().contains("<a href=\"complete-migration\">complete migration</a>"));
		assertEquals(Dialog.DLG_IMG_MESSAGE_WARNING, message.getImage());
		assertFalse(message.openLink("foo"));
		verify(migrationUi, never()).createCompleteMigrationWizard(any(ConnectorMigrator.class));

		message.openLink("complete-migration");
		verify(migrationUi).createCompleteMigrationWizard(any(ConnectorMigrator.class));
	}

	@Test
	public void finishCompleteMigrationWizard() throws Exception {
		ServiceMessage message = openCompleteMigrationPrompt(Window.OK);
		assertTrue(message.openLink("complete-migration"));
	}

	@Test
	public void cancelCompleteMigrationWizard() throws Exception {
		ServiceMessage message = openCompleteMigrationPrompt(Window.CANCEL);
		assertFalse(message.openLink("complete-migration"));
	}

	@Test
	public void secondMessageReopensAfterDelay() throws Exception {
		ServiceMessage message = openCompleteMigrationPrompt(Window.OK);
		TaskListServiceMessageControl messageControl = createMessageControl();

		Thread.sleep(2100);
		ServiceMessage message2 = captureMessage(messageControl);
		assertTrue(message2 != message);

		Thread.sleep(2100);
		ServiceMessage message3 = captureMessage(messageControl);
		assertTrue(message3 != message2);

		message3.openLink("complete-migration");
		Thread.sleep(2100);
		ServiceMessage message4 = captureMessage(messageControl);
		assertTrue(message4 == message3);
	}

	@Test
	public void backupTaskList() throws Exception {
		ImmutableMap<String, String> kinds = ImmutableMap.of("mock", "mock.new");
		TaskRepository repository = new TaskRepository("mock", "http://mock");
		ConnectorMigrator migrator = spy(createMigrator(true, true, kinds, ImmutableSet.of(repository)));
		IProgressMonitor monitor = mock(IProgressMonitor.class);
		migrationUi.backupTaskList(monitor);
		InOrder inOrder = inOrder(backupManager, migrationUi, migrator, monitor);
		inOrder.verify(monitor).subTask("Backing up task list");
		inOrder.verify(migrationUi).getBackupFileName(any(Date.class));
		inOrder.verify(backupManager).backupNow(true);
		inOrder.verify(monitor).worked(1);
	}

	@Test
	public void getBackupFileName() throws Exception {
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.MILLISECOND, 0);
		date = cal.getTime();
		String fileName = migrationUi.getBackupFileName(date);
		Matcher m = Pattern.compile("connector-migration-(\\d{4}_\\d{2}_\\d{2}_\\d{6}).zip").matcher(fileName);
		Matcher dateFormatMatcher = Pattern.compile("\\d{4}-\\d{2}-\\d{2}-\\d{6}.zip").matcher(fileName);
		Matcher oldDateFormatMatcher = Pattern.compile("\\d{4}-\\d{2}-\\d{2}").matcher(fileName);
		assertTrue(m.matches());
		assertFalse(dateFormatMatcher.find());
		assertFalse(oldDateFormatMatcher.find());
		Date fileNameTime = new SimpleDateFormat("yyyy_MM_dd_HHmmss").parse(m.group(1));
		assertEquals(date, fileNameTime);
	}

	/**
	 * @param returnCode
	 *            the return code of the wizard that opens when the link in the prompt is clicked
	 */
	private ServiceMessage openMigrationPrompt(int returnCode) {
		TaskListServiceMessageControl messageControl = createMessageControl();
		migrator = spy(
				new ConnectorMigrator(ImmutableMap.of("mock", "mock.new"), "", new DefaultTasksState(), migrationUi));
		WizardDialog wizard = mock(WizardDialog.class);
		when(wizard.open()).thenReturn(returnCode);
		doReturn(wizard).when(migrationUi).createMigrationWizard(any(ConnectorMigrator.class));
		migrationUi.promptToMigrate(migrator);
		return captureMessage(messageControl);
	}

	/**
	 * @param returnCode
	 *            the return code of the wizard that opens when the link in the prompt is clicked
	 */
	private ServiceMessage openCompleteMigrationPrompt(int returnCode) throws InterruptedException {
		TaskListServiceMessageControl messageControl = createMessageControl();
		migrator = spy(
				new ConnectorMigrator(ImmutableMap.of("mock", "mock.new"), "", new DefaultTasksState(), migrationUi));
		when(migrationUi.getCompletionPromptFrequency()).thenReturn(2);
		WizardDialog wizard = mock(WizardDialog.class);
		when(wizard.open()).thenReturn(returnCode);
		doReturn(wizard).when(migrationUi).createCompleteMigrationWizard(any(ConnectorMigrator.class));
		completeMigrationJob = (CompleteMigrationJob) migrationUi.createPromptToCompleteMigrationJob(migrator);
		assertTrue(completeMigrationJob.isSystem());
		assertFalse(completeMigrationJob.isUser());
		completeMigrationJob.schedule();
		completeMigrationJob.join();
		return captureMessage(messageControl);
	}

	private TaskListServiceMessageControl createMessageControl() {
		TaskListServiceMessageControl messageControl = mock(TaskListServiceMessageControl.class);
		when(taskList.getServiceMessageControl()).thenReturn(messageControl);
		return messageControl;
	}

	private ServiceMessage captureMessage(TaskListServiceMessageControl messageControl) {
		spinEventLoop();
		ArgumentCaptor<ServiceMessage> messageCaptor = ArgumentCaptor.forClass(ServiceMessage.class);
		verify(messageControl, atLeastOnce()).setMessage(messageCaptor.capture());
		ServiceMessage message = messageCaptor.getValue();
		return message;
	}

	private void spinEventLoop() {
		while (Display.getCurrent().readAndDispatch()) {
		}
	}

	private ConnectorMigrator createMigrator(boolean hasOldConnector, boolean hasNewConnector,
			Map<String, String> kinds, Set<TaskRepository> repositories) {
		IRepositoryManager manager = mock(IRepositoryManager.class);
		AbstractRepositoryConnector connector = mock(AbstractRepositoryConnector.class);
		ConnectorMigrator migrator = new ConnectorMigrator(kinds, "", new DefaultTasksState(), migrationUi);
		when(manager.getRepositories("mock")).thenReturn(repositories);
		if (hasOldConnector) {
			when(manager.getRepositoryConnector("mock")).thenReturn(connector);
			when(manager.getRepositoryConnector("kind")).thenReturn(connector);
		}
		if (hasNewConnector) {
			when(manager.getRepositoryConnector("mock.new")).thenReturn(connector);
			when(manager.getRepositoryConnector("kind.new")).thenReturn(connector);
		}
		return migrator;
	}
}

Back to the top