Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8ffc39009c3041499fd945c5cb0d0d45ce6ddf10 (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
/*******************************************************************************
 * 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.Matchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.wizard.IWizardContainer;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.mylyn.commons.workbench.WorkbenchUtil;
import org.eclipse.mylyn.internal.tasks.core.TaskRepositoryManager;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
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.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.PlatformUI;
import org.junit.Before;
import org.junit.Test;

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

public class ConnectorMigrationWizardTest {
	public class TestConnectorMigrationWizard extends ConnectorMigrationWizard {
		private TestConnectorMigrationWizard(ConnectorMigrator migrator) {
			super(migrator);
		}

		@Override
		protected CheckboxTreeViewer createConnectorList(Composite parent, List<String> kinds) {
			return new CheckboxTreeViewer(parent) {
				{
					PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
						@Override
						public void run() {
							fireCheckStateChanged(null);
						}
					});
				}

				@Override
				public Object[] getCheckedElements() {
					return new String[] { "foo", "bar" };
				}
			};
		}
	}

	private ConnectorMigrationWizard wizard;

	private final ConnectorMigrationUi migrationUi = spy(new ConnectorMigrationUi(
			TaskListView.getFromActivePerspective(), TasksUiPlugin.getBackupManager(), new DefaultTasksState()));

	private ConnectorMigrator migrator;

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

	@Test
	public void addPages() {
		createWizard(new ConnectorMigrationWizard(migrator));
		assertEquals(2, wizard.getPageCount());
	}

	@Test
	public void firstPage() {
		IWizardContainer container = createWizard(new ConnectorMigrationWizard(migrator));
		IWizardPage firstPage = container.getCurrentPage();
		assertEquals("End of Connector Support", firstPage.getTitle());
		assertEquals(
				"Support is ending for some connectors, but replacement connectors are installed. This wizard will help you "
						+ "migrate your configuration and data to the new connectors.",
				firstPage.getMessage());
		assertTrue(firstPage.getControl() instanceof Composite);
		Composite control = (Composite) firstPage.getControl();
		assertEquals(1, control.getChildren().length);
		assertTrue(control.getChildren()[0] instanceof Link);
	}

	@Test
	public void secondPage() {
		IWizardContainer container = createWizard(new ConnectorMigrationWizard(migrator));
		IWizardPage firstPage = container.getCurrentPage();
		IWizardPage secondPage = firstPage.getNextPage();
		assertEquals("Select Connectors", secondPage.getTitle());
		assertEquals(
				"Select the connectors to migrate. Your task list and repositories will be backed up before migration; you can "
						+ "undo the migration by selecting \"Restore Tasks from History\" in the Task List view "
						+ "menu and choosing the "
						+ "connector-migration-*.zip file stored in <workspace>/.metadata/.mylyn/backup.",
				secondPage.getDescription());
		assertTrue(secondPage.getControl() instanceof Composite);
		Composite control = (Composite) secondPage.getControl();
		assertEquals(1, control.getChildren().length);
		assertTrue(control.getChildren()[0] instanceof Tree);
	}

	@Test
	public void performFinishAfterConnectorsSelected()
			throws InvocationTargetException, InterruptedException, IOException {
		createMigrator(ImmutableMap.of("foo", "foo.new", "bar", "bar.new", "baz", "baz.new"));
		IWizardContainer container = createWizard(new TestConnectorMigrationWizard(migrator));
		spinEventLoop();
		wizard.performFinish();
		verify(container).run(eq(true), eq(true), any(IRunnableWithProgress.class));
		verify(migrator).setConnectorsToMigrate(eq(ImmutableList.of("foo", "bar")));
		verify(migrator).migrateConnectors(any(IProgressMonitor.class));
	}

	protected void createMigrator(Map<String, String> connectors) {
		TaskRepositoryManager manager = spy(new TaskRepositoryManager());
		createMigrator(connectors, manager);
	}

	private void createMigrator(Map<String, String> connectors, TaskRepositoryManager manager) {
		DefaultTasksState tasksState = spy(new DefaultTasksState());
		when(tasksState.getRepositoryManager()).thenReturn(manager);
		migrator = spy(new ConnectorMigrator(connectors, "", tasksState, migrationUi));
	}

	@Test
	public void performFinishNoConnectorsSelectedByDefault()
			throws InvocationTargetException, InterruptedException, IOException {
		createMigrator(ImmutableMap.of("foo", "foo.new", "bar", "bar.new", "baz", "baz.new"));
		IWizardContainer container = createWizard(new ConnectorMigrationWizard(migrator));
		wizard.performFinish();
		verify(container).run(eq(true), eq(true), any(IRunnableWithProgress.class));
		verify(migrator).setConnectorsToMigrate(eq(ImmutableList.<String> of()));
		verify(migrator).migrateConnectors(any(IProgressMonitor.class));
	}

	@Test
	public void performFinishSelectsRelevantConnectors()
			throws InvocationTargetException, InterruptedException, IOException {
		TaskRepositoryManager manager = spy(new TaskRepositoryManager());

		createAndAddConnector(manager, "mock", "Mock Connector");
		createAndAddConnector(manager, "foo", "Foo Connector");
		createAndAddConnector(manager, "bar", "Bar Connector");

		manager.addRepository(new TaskRepository("mock", "http://mock"));
		manager.addRepository(new TaskRepository("bar", "http://bar"));

		createMigrator(ImmutableMap.of("foo", "foo.new", "bar", "bar.new", "mock", "mock.new"), manager);

		IWizardContainer container = createWizard(new ConnectorMigrationWizard(migrator));
		wizard.performFinish();
		verify(container).run(eq(true), eq(true), any(IRunnableWithProgress.class));
		verify(migrator).setConnectorsToMigrate(eq(ImmutableList.of("bar", "mock")));
		verify(migrator).migrateConnectors(any(IProgressMonitor.class));
	}

	private void createAndAddConnector(TaskRepositoryManager manager, String kind, String label) {
		AbstractRepositoryConnector mockConnector = mock(AbstractRepositoryConnector.class);
		when(mockConnector.getLabel()).thenReturn(label);
		when(manager.getRepositoryConnector(kind)).thenReturn(mockConnector);
	}

	@Test
	public void performFinishSetsErrorMessage() throws InvocationTargetException, InterruptedException, IOException {
		IWizardContainer container = createWizard(new ConnectorMigrationWizard(migrator));
		doThrow(new InvocationTargetException(new IOException("Backup failed"))).when(container).run(any(Boolean.class),
				any(Boolean.class), any(IRunnableWithProgress.class));
		wizard.performFinish();
		assertEquals("Backup failed", container.getCurrentPage().getErrorMessage());
	}

	@Test
	public void isPageComplete() throws Exception {
		IWizardContainer container = createWizard(new ConnectorMigrationWizard(migrator));
		IWizardPage firstPage = container.getCurrentPage();
		IWizardPage secondPage = firstPage.getNextPage();
		assertTrue(firstPage.isPageComplete());
		assertFalse(secondPage.isPageComplete());

		container.showPage(secondPage);
		assertTrue(firstPage.isPageComplete());
		assertTrue(secondPage.isPageComplete());
	}

	@Test
	public void createConnectorList() throws Exception {
		CheckboxTreeViewer viewer = new ConnectorMigrationWizard(migrator).createConnectorList(WorkbenchUtil.getShell(),
				ImmutableList.of("mock"));
		IRepositoryManager manager = migrator.getRepositoryManager();
		assertEquals(ImmutableList.of("mock"), viewer.getInput());
		assertTrue(viewer.getLabelProvider() instanceof LabelProvider);
		assertEquals("mock", ((LabelProvider) viewer.getLabelProvider()).getText("mock"));

		AbstractRepositoryConnector connector = mock(AbstractRepositoryConnector.class);
		when(connector.getLabel()).thenReturn("My Connector");
		when(manager.getRepositoryConnector("mock")).thenReturn(connector);
		manager.addRepository(new TaskRepository("mock", "http://mock"));
		manager.addRepository(new TaskRepository("mock", "http://mock2"));
		assertEquals("My Connector (used by 2 repositories)",
				((LabelProvider) viewer.getLabelProvider()).getText("mock"));
	}

	private IWizardContainer createWizard(ConnectorMigrationWizard wiz) {
		wizard = spy(wiz);
		WizardDialog dialog = new WizardDialog(WorkbenchUtil.getShell(), wizard);
		dialog.create();
		IWizardContainer container = spy(wizard.getContainer());
		when(wizard.getContainer()).thenReturn(container);
		return container;
	}

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

Back to the top