Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Davis2016-07-29 19:31:42 -0400
committerGerrit Code Review @ Eclipse.org2016-08-11 19:58:27 -0400
commit87d261761bc06e87322fdd61d94b264f451c4518 (patch)
tree2e4996abe8f90b75b4f416a73815e9e59150d8ef /org.eclipse.mylyn.tasks.ui.tests
parent47a134c4d308149cba23f62df67cb8e4b19c2db8 (diff)
downloadorg.eclipse.mylyn.tasks-87d261761bc06e87322fdd61d94b264f451c4518.tar.gz
org.eclipse.mylyn.tasks-87d261761bc06e87322fdd61d94b264f451c4518.tar.xz
org.eclipse.mylyn.tasks-87d261761bc06e87322fdd61d94b264f451c4518.zip
498906: provide ability for users to migrate to new versions of
connectors Change-Id: I6980b07ab7e984f4c7ea40fd8649093196d25755 Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=498906
Diffstat (limited to 'org.eclipse.mylyn.tasks.ui.tests')
-rw-r--r--org.eclipse.mylyn.tasks.ui.tests/META-INF/MANIFEST.MF4
-rw-r--r--org.eclipse.mylyn.tasks.ui.tests/src/org/eclipse/mylyn/internal/tasks/ui/migrator/CompleteConnectorMigrationWizardTest.java261
-rw-r--r--org.eclipse.mylyn.tasks.ui.tests/src/org/eclipse/mylyn/internal/tasks/ui/migrator/ConnectorMigrationUiTest.java272
-rw-r--r--org.eclipse.mylyn.tasks.ui.tests/src/org/eclipse/mylyn/internal/tasks/ui/migrator/ConnectorMigrationWizardTest.java247
-rw-r--r--org.eclipse.mylyn.tasks.ui.tests/src/org/eclipse/mylyn/internal/tasks/ui/migrator/ConnectorMigratorTest.java607
5 files changed, 1390 insertions, 1 deletions
diff --git a/org.eclipse.mylyn.tasks.ui.tests/META-INF/MANIFEST.MF b/org.eclipse.mylyn.tasks.ui.tests/META-INF/MANIFEST.MF
index 376a98582..cc88e96b7 100644
--- a/org.eclipse.mylyn.tasks.ui.tests/META-INF/MANIFEST.MF
+++ b/org.eclipse.mylyn.tasks.ui.tests/META-INF/MANIFEST.MF
@@ -12,7 +12,9 @@ Require-Bundle: org.junit;bundle-version="4.8.2",
org.hamcrest;bundle-version="[1.0.0,2.0.0)",
org.eclipse.equinox.security,
org.eclipse.mylyn.tasks.core,
- org.eclipse.mylyn.tasks.tests
+ org.eclipse.mylyn.tasks.tests,
+ org.eclipse.mylyn.tests.util
Export-Package: org.eclipse.mylyn.internal.tasks.ui.editors;x-internal:=true,
+ org.eclipse.mylyn.internal.tasks.ui.migrator;x-internal:=true,
org.eclipse.mylyn.tasks.ui.editors,
org.eclipse.mylyn.tasks.ui.wizards
diff --git a/org.eclipse.mylyn.tasks.ui.tests/src/org/eclipse/mylyn/internal/tasks/ui/migrator/CompleteConnectorMigrationWizardTest.java b/org.eclipse.mylyn.tasks.ui.tests/src/org/eclipse/mylyn/internal/tasks/ui/migrator/CompleteConnectorMigrationWizardTest.java
new file mode 100644
index 000000000..580224e31
--- /dev/null
+++ b/org.eclipse.mylyn.tasks.ui.tests/src/org/eclipse/mylyn/internal/tasks/ui/migrator/CompleteConnectorMigrationWizardTest.java
@@ -0,0 +1,261 @@
+/*******************************************************************************
+ * 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.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.eclipse.jface.viewers.TreeViewer;
+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.RepositoryQuery;
+import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
+import org.eclipse.mylyn.internal.tasks.ui.migrator.CompleteConnectorMigrationWizard.MapContentProvider;
+import org.eclipse.mylyn.internal.tasks.ui.views.TaskListView;
+import org.eclipse.mylyn.tasks.core.TaskRepository;
+import org.eclipse.mylyn.tasks.ui.TaskElementLabelProvider;
+import org.eclipse.mylyn.tests.util.TestFixture;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Text;
+import org.eclipse.swt.widgets.Tree;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+
+public class CompleteConnectorMigrationWizardTest {
+
+ private CompleteConnectorMigrationWizard wizard;
+
+ private ConnectorMigrator migrator;
+
+ private ConnectorMigrationUi migrationUi;
+
+ @SuppressWarnings("unchecked")
+ @Before
+ public void setUp() {
+ DefaultTasksState tasksState = new DefaultTasksState();
+ migrationUi = spy(new ConnectorMigrationUi(TaskListView.getFromActivePerspective(),
+ TasksUiPlugin.getBackupManager(), tasksState));
+ doNothing().when(migrationUi).warnOfValidationFailure((List<TaskRepository>) any(List.class));
+ doNothing().when(migrationUi).notifyMigrationComplete();
+ migrator = spy(new ConnectorMigrator(ImmutableMap.of("mock", "mock.new"), "", tasksState, migrationUi));
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ TestFixture.resetTaskList();
+ }
+
+ @Test
+ public void addPages() {
+ createWizard(new CompleteConnectorMigrationWizard(migrator));
+ assertEquals(2, wizard.getPageCount());
+ }
+
+ @Test
+ public void firstPage() {
+ IWizardContainer container = createWizard(new CompleteConnectorMigrationWizard(migrator));
+ IWizardPage firstPage = container.getCurrentPage();
+ assertEquals("Have You Recreated Your Queries?", firstPage.getTitle());
+ assertEquals(
+ "Migration will remove your old queries. Please ensure you have created the new queries you want. "
+ + "Your old and new queries are shown below and you can edit them by double-clicking.",
+ firstPage.getMessage());
+ assertTrue(firstPage.getControl() instanceof Composite);
+ Composite control = (Composite) firstPage.getControl();
+ assertEquals(4, control.getChildren().length);
+ assertTrue(control.getChildren()[0] instanceof Label);
+ assertTrue(control.getChildren()[1] instanceof Label);
+ assertTrue(control.getChildren()[2] instanceof Tree);
+ assertTrue(control.getChildren()[3] instanceof Tree);
+ }
+
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ @Test
+ public void queryTreeShowsOnlySelectedConnectors() {
+ migrator = new ConnectorMigrator(ImmutableMap.of("mock", "mock.new", "kind", "kind.new"), "",
+ new DefaultTasksState(), migrationUi);
+ migrator.setConnectorsToMigrate(ImmutableList.of("kind"));
+ createWizard(new CompleteConnectorMigrationWizard(migrator));
+ ArgumentCaptor<Collection> captor = ArgumentCaptor.forClass(Collection.class);
+ verify(wizard, times(2)).createRepositoryQueryMap(captor.capture());
+ assertEquals(ImmutableSet.of("kind"), ImmutableSet.copyOf(captor.getAllValues().get(0)));
+ assertEquals(ImmutableSet.of("kind.new"), ImmutableSet.copyOf(captor.getAllValues().get(1)));
+
+ migrator = new ConnectorMigrator(ImmutableMap.of("mock", "mock.new", "kind", "kind.new"), "",
+ new DefaultTasksState(), migrationUi);
+ migrator.setConnectorsToMigrate(ImmutableList.of("mock", "kind"));
+ createWizard(new CompleteConnectorMigrationWizard(migrator));
+ captor = ArgumentCaptor.forClass(Collection.class);
+ verify(wizard, times(2)).createRepositoryQueryMap(captor.capture());
+ assertEquals(ImmutableSet.of("mock", "kind"), ImmutableSet.copyOf(captor.getAllValues().get(0)));
+ assertEquals(ImmutableSet.of("mock.new", "kind.new"), ImmutableSet.copyOf(captor.getAllValues().get(1)));
+ }
+
+ @Test
+ public void secondPage() {
+ IWizardContainer container = createWizard(new CompleteConnectorMigrationWizard(migrator));
+ IWizardPage firstPage = container.getCurrentPage();
+ IWizardPage secondPage = firstPage.getNextPage();
+ assertEquals("Complete Migration", secondPage.getTitle());
+ assertEquals("Clicking finish will migrate your tasks and private data. This may take a while.",
+ secondPage.getMessage());
+ assertTrue(secondPage.getControl() instanceof Composite);
+ Composite control = (Composite) secondPage.getControl();
+ assertEquals(1, control.getChildren().length);
+ assertTrue(control.getChildren()[0] instanceof Text);
+ String text = ((Text) control.getChildren()[0]).getText();
+ assertTrue(text.contains("When you click finish, your context, scheduled dates, private notes and other data "
+ + "will be migrated to the new connectors. Any tasks in your task list that are not included in the new "
+ + "queries you created will be downloaded using the new connectors. The old tasks, "
+ + "queries, and repositories will be deleted."));
+ assertTrue(text.contains("This may take a while. You should not use the task list or task editor while this is happening. "
+ + "You will be prompted when migration is complete."));
+ assertTrue(text.contains("You will be able to "
+ + "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. This will restore your task "
+ + "list and repositories to the state they were in before the migration, but any data stored by 3rd party "
+ + "plugins for Mylyn may be lost"));
+ }
+
+ @Test
+ public void performFinish() throws InvocationTargetException, InterruptedException, IOException {
+ createWizard(new CompleteConnectorMigrationWizard(migrator));
+ assertTrue(wizard.performFinish());
+ }
+
+ @Test
+ public void isPageComplete() throws Exception {
+ IWizardContainer container = createWizard(new CompleteConnectorMigrationWizard(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 createQueryTree() throws Exception {
+ TaskRepository repository = createRepository("mock", "http://mock");
+ Map<TaskRepository, ? extends Set<RepositoryQuery>> queries = ImmutableMap.of(repository,
+ ImmutableSet.of(new RepositoryQuery("mock", "mock")));
+ TreeViewer viewer = new CompleteConnectorMigrationWizard(migrator).createQueryTree(WorkbenchUtil.getShell(),
+ queries);
+ assertEquals(queries, viewer.getInput());
+ assertTrue(viewer.getContentProvider() instanceof MapContentProvider);
+ assertTrue(viewer.getLabelProvider() instanceof TaskElementLabelProvider);
+ assertNotNull(((TaskElementLabelProvider) viewer.getLabelProvider()).getImage(repository));
+ assertEquals("http://mock", ((TaskElementLabelProvider) viewer.getLabelProvider()).getText(repository));
+
+ assertEquals(0, viewer.getExpandedElements().length);
+ spinEventLoop();
+ assertEquals(1, viewer.getExpandedElements().length);
+ }
+
+ @Test
+ public void createRepositoryQueryMap() throws Exception {
+ TaskRepository repository1 = createRepository("mock", "http://mock");
+ TaskRepository repository2 = createRepository("mock", "http://mock2");
+ ImmutableSet<TaskRepository> repositories = ImmutableSet.of(repository1, repository2);
+ RepositoryQuery query1 = createQuery(repository1);
+ RepositoryQuery query2 = createQuery(repository1);
+ RepositoryQuery query3 = createQuery(repository2);
+ RepositoryQuery query4 = createQuery(repository2);
+
+ Map<TaskRepository, Set<RepositoryQuery>> map = //
+ new CompleteConnectorMigrationWizard(migrator).createRepositoryQueryMap(ImmutableList.of("mock"));
+ assertEquals(repositories, map.keySet());
+ assertEquals(ImmutableSet.of(query1, query2), map.get(repository1));
+ assertEquals(ImmutableSet.of(query3, query4), map.get(repository2));
+ }
+
+ @Test
+ public void createRepositoryQueryMapExcludesRepositoryWithNoQueries() throws Exception {
+ TaskRepository repository = createRepository("mock", "http://mock");
+ createRepository("mock", "http://mock2");
+ RepositoryQuery query = createQuery(repository);
+
+ Map<TaskRepository, Set<RepositoryQuery>> map = //
+ new CompleteConnectorMigrationWizard(migrator).createRepositoryQueryMap(ImmutableList.of("mock"));
+ assertEquals(ImmutableSet.of(repository), map.keySet());
+ assertEquals(ImmutableSet.of(query), map.get(repository));
+ }
+
+ @Test
+ public void createRepositoryQueryMapMigratedQuery() throws Exception {
+ TaskRepository repository = createRepository("mock", "http://mock");
+ TaskRepository migratedRepository = createRepository("mock-new", "http://mock");
+ RepositoryQuery query = createQuery(repository);
+ RepositoryQuery migratedQuery = createQuery(migratedRepository);
+
+ Map<TaskRepository, Set<RepositoryQuery>> map = //
+ new CompleteConnectorMigrationWizard(migrator).createRepositoryQueryMap(ImmutableList.of("mock"));
+ assertEquals(ImmutableSet.of(repository), map.keySet());
+ assertEquals(ImmutableSet.of(query), map.get(repository));
+
+ map = new CompleteConnectorMigrationWizard(migrator).createRepositoryQueryMap(ImmutableList.of("mock-new"));
+ assertEquals(ImmutableSet.of(migratedRepository), map.keySet());
+ assertEquals(ImmutableSet.of(migratedQuery), map.get(migratedRepository));
+ }
+
+ protected TaskRepository createRepository(String kind, String url) {
+ TaskRepository repository = new TaskRepository(kind, url);
+ migrator.getRepositoryManager().addRepository(repository);
+ return repository;
+ }
+
+ protected RepositoryQuery createQuery(TaskRepository repository) {
+ RepositoryQuery query = new RepositoryQuery(repository.getConnectorKind(), repository.getConnectorKind()
+ + repository.getRepositoryUrl() + Math.random());
+ query.setRepositoryUrl(repository.getRepositoryUrl());
+ TasksUiPlugin.getTaskList().addQuery(query);
+ return query;
+ }
+
+ private IWizardContainer createWizard(CompleteConnectorMigrationWizard 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()) {
+ }
+ }
+}
diff --git a/org.eclipse.mylyn.tasks.ui.tests/src/org/eclipse/mylyn/internal/tasks/ui/migrator/ConnectorMigrationUiTest.java b/org.eclipse.mylyn.tasks.ui.tests/src/org/eclipse/mylyn/internal/tasks/ui/migrator/ConnectorMigrationUiTest.java
new file mode 100644
index 000000000..18330c454
--- /dev/null
+++ b/org.eclipse.mylyn.tasks.ui.tests/src/org/eclipse/mylyn/internal/tasks/ui/migrator/ConnectorMigrationUiTest.java
@@ -0,0 +1,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;
+ }
+}
diff --git a/org.eclipse.mylyn.tasks.ui.tests/src/org/eclipse/mylyn/internal/tasks/ui/migrator/ConnectorMigrationWizardTest.java b/org.eclipse.mylyn.tasks.ui.tests/src/org/eclipse/mylyn/internal/tasks/ui/migrator/ConnectorMigrationWizardTest.java
new file mode 100644
index 000000000..8ffc39009
--- /dev/null
+++ b/org.eclipse.mylyn.tasks.ui.tests/src/org/eclipse/mylyn/internal/tasks/ui/migrator/ConnectorMigrationWizardTest.java
@@ -0,0 +1,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()) {
+ }
+ }
+}
diff --git a/org.eclipse.mylyn.tasks.ui.tests/src/org/eclipse/mylyn/internal/tasks/ui/migrator/ConnectorMigratorTest.java b/org.eclipse.mylyn.tasks.ui.tests/src/org/eclipse/mylyn/internal/tasks/ui/migrator/ConnectorMigratorTest.java
new file mode 100644
index 000000000..01851b881
--- /dev/null
+++ b/org.eclipse.mylyn.tasks.ui.tests/src/org/eclipse/mylyn/internal/tasks/ui/migrator/ConnectorMigratorTest.java
@@ -0,0 +1,607 @@
+/*******************************************************************************
+ * Copyright (c) 2015 Tasktop Technologies.
+ * 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.internal.tasks.ui.migrator;
+
+import static org.hamcrest.CoreMatchers.not;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.argThat;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doNothing;
+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.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Calendar;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.OperationCanceledException;
+import org.eclipse.mylyn.commons.net.AuthenticationCredentials;
+import org.eclipse.mylyn.commons.net.AuthenticationType;
+import org.eclipse.mylyn.internal.tasks.core.AbstractTask;
+import org.eclipse.mylyn.internal.tasks.core.AbstractTaskCategory;
+import org.eclipse.mylyn.internal.tasks.core.DateRange;
+import org.eclipse.mylyn.internal.tasks.core.IRepositoryConstants;
+import org.eclipse.mylyn.internal.tasks.core.RepositoryModel;
+import org.eclipse.mylyn.internal.tasks.core.RepositoryQuery;
+import org.eclipse.mylyn.internal.tasks.core.TaskActivityManager;
+import org.eclipse.mylyn.internal.tasks.core.TaskCategory;
+import org.eclipse.mylyn.internal.tasks.core.TaskJobFactory;
+import org.eclipse.mylyn.internal.tasks.core.TaskList;
+import org.eclipse.mylyn.internal.tasks.core.TaskRepositoryManager;
+import org.eclipse.mylyn.internal.tasks.core.TaskTask;
+import org.eclipse.mylyn.internal.tasks.core.data.TaskDataManager;
+import org.eclipse.mylyn.internal.tasks.ui.TaskListBackupManager;
+import org.eclipse.mylyn.internal.tasks.ui.views.TaskListView;
+import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
+import org.eclipse.mylyn.tasks.core.ITask;
+import org.eclipse.mylyn.tasks.core.ITask.SynchronizationState;
+import org.eclipse.mylyn.tasks.core.TaskRepository;
+import org.eclipse.mylyn.tasks.core.context.AbstractTaskContextStore;
+import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper;
+import org.eclipse.mylyn.tasks.core.data.TaskData;
+import org.eclipse.mylyn.tests.util.TestFixture;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.InOrder;
+
+import com.google.common.base.Optional;
+import com.google.common.base.Predicates;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
+
+public class ConnectorMigratorTest {
+ public class SpyTasksState extends DefaultTasksState {
+
+ TaskActivityManager taskActivityManager = spy(super.getTaskActivityManager());
+
+ TaskDataManager taskDataManager = spy(super.getTaskDataManager());
+
+ RepositoryModel repositoryModel = spy(super.getRepositoryModel());
+
+ TaskList taskList = spy(super.getTaskList());
+
+ AbstractTaskContextStore contextStore = spy(super.getContextStore());
+
+ TaskJobFactory taskJobFactory = spy(super.getTaskJobFactory());
+
+ TaskRepositoryManager repositoryManager = spy(super.getRepositoryManager());
+
+ @Override
+ public TaskActivityManager getTaskActivityManager() {
+ return taskActivityManager;
+ }
+
+ @Override
+ public TaskDataManager getTaskDataManager() {
+ return taskDataManager;
+ }
+
+ @Override
+ public RepositoryModel getRepositoryModel() {
+ return repositoryModel;
+ }
+
+ @Override
+ public TaskList getTaskList() {
+ return taskList;
+ }
+
+ @Override
+ public AbstractTaskContextStore getContextStore() {
+ return contextStore;
+ }
+
+ @Override
+ public TaskJobFactory getTaskJobFactory() {
+ return taskJobFactory;
+ }
+
+ @Override
+ public TaskRepositoryManager getRepositoryManager() {
+ return repositoryManager;
+ }
+
+ }
+
+ private final ImmutableMap<String, String> kinds = ImmutableMap.of("mock", "mock.new");
+
+ private final TaskRepository repository = new TaskRepository("mock", "http://mock");
+
+ private final ImmutableSet<TaskRepository> singleRepository = ImmutableSet.of(repository);
+
+ private TaskRepository migratedRepository = new TaskRepository("mock.new", "http://mock");
+
+ private final AuthenticationCredentials repoCreds = new AuthenticationCredentials("u1", "p1");
+
+ private final AuthenticationCredentials proxyCreds = new AuthenticationCredentials("u2", "p2");
+
+ private final AuthenticationCredentials httpCreds = new AuthenticationCredentials("u3", "p3");
+
+ private final AuthenticationCredentials certCreds = new AuthenticationCredentials("u4", "p4");
+
+ private final AbstractRepositoryConnector connector = mock(AbstractRepositoryConnector.class);
+
+ private final AbstractRepositoryConnector newConnector = mock(AbstractRepositoryConnector.class);
+
+ private final TaskListView taskList = mock(TaskListView.class);
+
+ private final TaskListBackupManager backupManager = mock(TaskListBackupManager.class);
+
+ DefaultTasksState tasksState = spy(new SpyTasksState());
+
+ private TaskRepositoryManager manager = tasksState.getRepositoryManager();
+
+ private final ConnectorMigrationUi migrationUi = spy(new ConnectorMigrationUi(taskList, backupManager, tasksState));
+
+ @SuppressWarnings("unchecked")
+ @Before
+ public void setUp() {
+ doNothing().when(migrationUi).warnOfValidationFailure((List<TaskRepository>) any(List.class));
+ doNothing().when(migrationUi).notifyMigrationComplete();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ TestFixture.resetTaskList();
+ new File("test-context.zip").delete();
+ }
+
+ @Test
+ public void setConnectorsToMigrate() throws Exception {
+ ConnectorMigrator migrator = createMigrator(true, true, ImmutableMap.of("mock", "mock.new", "kind", "kind.new"),
+ ImmutableSet.of(repository, new TaskRepository("kind", "http://mock")), true);
+ try {
+ migrator.setConnectorsToMigrate(ImmutableList.of("foo"));
+ fail("Expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {// NOSONAR
+ }
+ migrator.setConnectorsToMigrate(ImmutableList.of("kind"));
+ assertEquals(ImmutableMap.of("kind", "kind.new"), migrator.getSelectedConnectors());
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void migrateConnectors() throws Exception {
+ assertMigrateConnectors();
+ verify(migrationUi, never()).warnOfValidationFailure((List<TaskRepository>) any());
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void migrateConnectorsValidationFailure() throws Exception {
+ when(newConnector.validateRepository(any(TaskRepository.class), any(IProgressMonitor.class)))
+ .thenThrow(CoreException.class);
+ assertMigrateConnectors();
+ verify(migrationUi).warnOfValidationFailure(ImmutableList.of(migratedRepository));
+ }
+
+ @Test
+ public void cancelMigration() throws Exception {
+ ConnectorMigrator migrator = spy(createMigrator(true, true, kinds, singleRepository, true));
+ IProgressMonitor monitor = new NullProgressMonitor();
+ monitor.setCanceled(true);
+ IProgressMonitor spyMonitor = spy(monitor);
+ try {
+ migrator.setConnectorsToMigrate(ImmutableList.of("mock"));
+ migrator.migrateConnectors(spyMonitor);
+ fail("Expected OperationCanceledException");
+ } catch (OperationCanceledException e) {// NOSONAR
+ }
+ verify(spyMonitor).beginTask("Migrating repositories", 2);
+ verify(spyMonitor).isCanceled();
+ verify(manager, never()).getRepositoryConnector(any(String.class));
+ verify(manager, never()).addRepository(any(TaskRepository.class));
+ verify(migrator, never()).getMigratedRepository(any(String.class), any(TaskRepository.class));
+ }
+
+ private ConnectorMigrator assertMigrateConnectors() throws CoreException, IOException {
+ ConnectorMigrator migrator = spy(createMigrator(true, true, kinds, singleRepository, true));
+ IProgressMonitor monitor = migrateConnectors(migrator);
+
+ assertTrue(repository.isOffline());
+ assertEquals("http://mock (Unsupported, do not delete)", repository.getRepositoryLabel());
+ InOrder inOrder = inOrder(manager, newConnector, migrationUi, migrator, monitor);
+ inOrder.verify(monitor).beginTask("Migrating repositories", 2);
+
+ inOrder.verify(migrationUi).backupTaskList(monitor);
+ inOrder.verify(monitor).subTask("Backing up task list");
+ inOrder.verify(monitor).worked(1);
+
+ inOrder.verify(monitor).subTask("Migrating http://mock");
+ inOrder.verify(migrator).migrateRepository("mock.new", "http://mock", repository);
+ inOrder.verify(manager).addRepository(migratedRepository);
+ inOrder.verify(migrator).disconnect(repository);
+ inOrder.verify(monitor).worked(1);
+ inOrder.verify(monitor).beginTask("Validating repository connections", 1);
+ inOrder.verify(monitor).subTask("Validating connection to http://mock");
+ inOrder.verify(newConnector).validateRepository(migratedRepository, monitor);
+ inOrder.verify(monitor).done();
+ verifyNoMoreInteractions(newConnector);
+ return migrator;
+ }
+
+ private IProgressMonitor migrateConnectors(ConnectorMigrator migrator) throws IOException {
+ IProgressMonitor monitor = mock(IProgressMonitor.class);
+ migrator.setConnectorsToMigrate(ImmutableList.of("mock"));
+ migrator.migrateConnectors(monitor);
+ return monitor;
+ }
+
+ @Test
+ public void migrateRepository() throws Exception {
+ ConnectorMigrator migrator = createMigrator(true, true, kinds, singleRepository, true);
+ populateRepository();
+
+ migratedRepository = migrator.getMigratedRepository("mock.new", repository);
+ assertEquals("foovalue", migratedRepository.getProperty("foo"));
+ assertEquals("barvalue", migratedRepository.getProperty("bar"));
+ assertNull(migratedRepository.getProperty(IRepositoryConstants.PROPERTY_SYNCTIMESTAMP));
+ assertEquals("My Label", migratedRepository.getRepositoryLabel());
+ assertEquals("mock.new", migratedRepository.getConnectorKind());
+
+ assertEquals(repoCreds, migratedRepository.getCredentials(AuthenticationType.REPOSITORY));
+ assertEquals(proxyCreds, migratedRepository.getCredentials(AuthenticationType.PROXY));
+ assertEquals(httpCreds, migratedRepository.getCredentials(AuthenticationType.HTTP));
+ assertEquals(certCreds, migratedRepository.getCredentials(AuthenticationType.CERTIFICATE));
+
+ assertTrue(migratedRepository.getSavePassword(AuthenticationType.REPOSITORY));
+ assertTrue(migratedRepository.getSavePassword(AuthenticationType.PROXY));
+ assertFalse(migratedRepository.getSavePassword(AuthenticationType.HTTP));
+ assertFalse(migratedRepository.getSavePassword(AuthenticationType.CERTIFICATE));
+ }
+
+ @Test
+ public void migrateExistingRepositoryDoesNothing() throws Exception {
+ migratedRepository.setRepositoryLabel("My Old Label");
+
+ ConnectorMigrator migrator = createMigrator(true, true, kinds, singleRepository, true);
+ when(manager.getRepository("mock.new", "http://mock")).thenReturn(migratedRepository);
+ populateRepository();
+
+ assertSame(migratedRepository, migrator.getMigratedRepository("mock.new", repository));
+ assertNull(migratedRepository.getProperty("foo"));
+ assertNull(migratedRepository.getProperty("bar"));
+ assertNull(migratedRepository.getProperty(IRepositoryConstants.PROPERTY_SYNCTIMESTAMP));
+ assertEquals("My Old Label", migratedRepository.getRepositoryLabel());
+ assertEquals("mock.new", migratedRepository.getConnectorKind());
+
+ assertNull(migratedRepository.getCredentials(AuthenticationType.REPOSITORY));
+ assertNull(migratedRepository.getCredentials(AuthenticationType.PROXY));
+ assertNull(migratedRepository.getCredentials(AuthenticationType.HTTP));
+ assertNull(migratedRepository.getCredentials(AuthenticationType.CERTIFICATE));
+
+ }
+
+ private void populateRepository() {
+ repository.setProperty("foo", "foovalue");
+ repository.setProperty("bar", "barvalue");
+ repository.setProperty(IRepositoryConstants.PROPERTY_SYNCTIMESTAMP, "123");
+ repository.setRepositoryLabel("My Label");
+ repository.setCredentials(AuthenticationType.REPOSITORY, repoCreds, true);
+ repository.setCredentials(AuthenticationType.PROXY, proxyCreds, true);
+ repository.setCredentials(AuthenticationType.HTTP, httpCreds, false);
+ repository.setCredentials(AuthenticationType.CERTIFICATE, certCreds, false);
+ }
+
+ @Test
+ public void needsMigrationEmptyKinds() {
+ try {
+ createMigrator(true, true, ImmutableMap.<String, String> of(), singleRepository, true);
+ fail("Expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {// NOSONAR
+ }
+ }
+
+ @Test
+ public void needsMigrationNoRepositories() {
+ ConnectorMigrator migrator = createMigrator(true, true, kinds, ImmutableSet.<TaskRepository> of(), true);
+ assertFalse(migrator.needsMigration());
+ }
+
+ @Test
+ public void needsMigrationNoConnectors() {
+ ConnectorMigrator migrator = createMigrator(false, false, kinds, singleRepository, true);
+ assertFalse(migrator.needsMigration());
+ }
+
+ @Test
+ public void needsMigrationNoNewConnector() {
+ ConnectorMigrator migrator = createMigrator(true, false, kinds, singleRepository, true);
+ assertFalse(migrator.needsMigration());
+ }
+
+ @Test
+ public void needsMigrationNoOldConnector() {
+ ConnectorMigrator migrator = createMigrator(false, true, kinds, singleRepository, true);
+ assertFalse(migrator.needsMigration());
+ }
+
+ @Test
+ public void needsMigrationOneRepository() {
+ ConnectorMigrator migrator = createMigrator(true, true, kinds, singleRepository, true);
+ assertTrue(migrator.needsMigration());
+ }
+
+ @Test
+ public void needsMigrationMultipleRepositories() {
+ ConnectorMigrator migrator = createMigrator(true, true, kinds,
+ ImmutableSet.of(repository, new TaskRepository("mock", "http://mock2")), true);
+ assertTrue(migrator.needsMigration());
+ }
+
+ @Test
+ public void needsMigrationMultiKindsOneRepository() {
+ ImmutableMap<String, String> multiKinds = ImmutableMap.of("mock", "mock.new", "kind", "kind.new");
+ ConnectorMigrator migrator = createMigrator(true, true, multiKinds, singleRepository, true);
+ assertTrue(migrator.needsMigration());
+
+ migrator = createMigrator(true, true, multiKinds, ImmutableSet.of(new TaskRepository("kind", "http://mock")),
+ true);
+ assertTrue(migrator.needsMigration());
+ }
+
+ @Test
+ public void needsMigrationMultiKindsMultiRepositories() {
+ ConnectorMigrator migrator = createMigrator(true, true, ImmutableMap.of("mock", "mock.new", "kind", "kind.new"),
+ ImmutableSet.of(repository, new TaskRepository("kind", "http://mock")), true);
+ assertTrue(migrator.needsMigration());
+ }
+
+ @Test
+ public void migrateTasksWaitsForSyncJobs() throws Exception {
+ ConnectorMigrator migrator = spy(createMigrator(true, true, kinds, ImmutableSet.of(repository), false));
+ JobListener listener = mock(JobListener.class);
+ when(listener.isComplete()).thenReturn(false, false, true);
+ when(migrator.getSyncTaskJobListener()).thenReturn(listener);
+ migrator.migrateTasks(new NullProgressMonitor());
+ verify(listener, times(3)).isComplete();
+ }
+
+ @Test
+ public void migrateTasks() throws Exception {
+ when(newConnector.getConnectorKind()).thenReturn("mock.new");
+ ConnectorMigrator migrator = spy(createMigrator(true, true, kinds, ImmutableSet.of(repository), false));
+ TaskData taskData2 = new TaskData(mock(TaskAttributeMapper.class), "mock.new", repository.getRepositoryUrl(),
+ "2.migrated");
+ when(migrator.getTaskData(eq("key2"), eq(newConnector), any(TaskRepository.class), any(IProgressMonitor.class)))
+ .thenReturn(taskData2);
+ ITask task1 = new TaskTask("mock", "http://mock", "1");
+ task1.setTaskKey("key1");
+ ((AbstractTask) task1).setSynchronizationState(SynchronizationState.INCOMING_NEW);
+ ITask task2 = new TaskTask("mock", "http://mock", "2");
+ task2.setTaskKey("key2");
+ ((AbstractTask) task2).setSynchronizationState(SynchronizationState.INCOMING);
+ ITask task1Migrated = new TaskTask("mock.new", "http://mock", "1.migrated");
+ task1Migrated.setTaskKey("key1");
+ TaskTask taskOtherRepo = new TaskTask("mock", "http://other-mock", "1");
+ tasksState.getTaskList().addTask(task1);
+ tasksState.getTaskList().addTask(task1Migrated);
+ tasksState.getTaskList().addTask(taskOtherRepo);
+ tasksState.getTaskList().addTask(task2);
+ RepositoryQuery query = new RepositoryQuery("mock", "mock");
+ query.setRepositoryUrl("http://mock");
+ tasksState.getTaskList().addQuery(query);
+ migrateConnectors(migrator);
+ NullProgressMonitor monitor = new NullProgressMonitor();
+
+ migrator.migrateTasks(monitor);
+ verify(tasksState.getTaskActivityManager()).deactivateActiveTask();
+ TaskRepository newRepository = manager.getRepository("mock.new", "http://mock");
+ verify(migrator).migrateTasks(ImmutableSet.of(task1, task2), repository, newRepository,
+ manager.getRepositoryConnector("mock.new"), monitor);
+
+ verify(migrator, never()).getTaskData("key1", newConnector, newRepository, monitor);
+ verify(migrator, never()).createTask(argThat(not(taskData2)), any(TaskRepository.class));
+ verify(migrator).getTaskData("key2", newConnector, newRepository, monitor);
+ verify(migrator).createTask(taskData2, newRepository);
+ verify(migrator).migratePrivateData((AbstractTask) task1, (AbstractTask) task1Migrated, monitor);
+ ITask task2Migrated = new TaskTask("mock.new", "http://mock", "2.migrated");
+ task2Migrated.setTaskKey("key2");
+ verify(migrator).migratePrivateData((AbstractTask) task2, (AbstractTask) task2Migrated, monitor);
+
+ verify(migrationUi).delete(ImmutableSet.of(task1, task2), repository, newRepository, monitor);
+ assertEquals(SynchronizationState.INCOMING_NEW, tasksState.getTaskList()
+ .getTask(repository.getRepositoryUrl(), "1.migrated")
+ .getSynchronizationState());
+ assertEquals(SynchronizationState.INCOMING, tasksState.getTaskList()
+ .getTask(repository.getRepositoryUrl(), "2.migrated")
+ .getSynchronizationState());
+
+ assertEquals(ImmutableSet.of(taskOtherRepo, task1Migrated, task2Migrated),
+ ImmutableSet.copyOf(tasksState.getTaskList().getAllTasks()));
+ verify(tasksState.getRepositoryManager()).removeRepository(repository);
+ assertTrue(tasksState.getTaskList().getQueries().isEmpty());
+ assertEquals(ImmutableSet.of(newRepository), tasksState.getRepositoryManager().getRepositories("mock.new"));
+ }
+
+ @Test
+ public void migrateTasksSameId() throws Exception {
+ when(newConnector.getConnectorKind()).thenReturn("mock.new");
+ ConnectorMigrator migrator = spy(createMigrator(true, true, kinds, ImmutableSet.of(repository), false));
+ TaskData taskData1 = new TaskData(mock(TaskAttributeMapper.class), "mock.new", repository.getRepositoryUrl(),
+ "1");
+ when(migrator.getTaskData(eq("key1"), eq(newConnector), any(TaskRepository.class), any(IProgressMonitor.class)))
+ .thenReturn(taskData1);
+ TaskData taskData2 = new TaskData(mock(TaskAttributeMapper.class), "mock.new", repository.getRepositoryUrl(),
+ "2");
+ when(migrator.getTaskData(eq("key2"), eq(newConnector), any(TaskRepository.class), any(IProgressMonitor.class)))
+ .thenReturn(taskData2);
+ ITask task1 = new TaskTask("mock", "http://mock", "1");
+ task1.setTaskKey("key1");
+ ((AbstractTask) task1).setSynchronizationState(SynchronizationState.INCOMING_NEW);
+ ITask task2 = new TaskTask("mock", "http://mock", "2");
+ task2.setTaskKey("key2");
+ ((AbstractTask) task2).setSynchronizationState(SynchronizationState.INCOMING);
+ ITask task1Migrated = new TaskTask("mock.new", "http://mock", "1");
+ task1Migrated.setTaskKey("key1");
+ TaskTask taskOtherRepo = new TaskTask("mock", "http://other-mock", "1");
+ tasksState.getTaskList().addTask(task1);
+ tasksState.getTaskList().addTask(task1Migrated);
+ tasksState.getTaskList().addTask(taskOtherRepo);
+ tasksState.getTaskList().addTask(task2);
+ RepositoryQuery query = new RepositoryQuery("mock", "mock");
+ query.setRepositoryUrl("http://mock");
+ tasksState.getTaskList().addQuery(query);
+ migrateConnectors(migrator);
+ NullProgressMonitor monitor = new NullProgressMonitor();
+
+ migrator.migrateTasks(monitor);
+ verify(tasksState.getTaskActivityManager()).deactivateActiveTask();
+ TaskRepository newRepository = manager.getRepository("mock.new", "http://mock");
+ verify(migrator).migrateTasks(ImmutableSet.of(task1, task2), repository, newRepository,
+ manager.getRepositoryConnector("mock.new"), monitor);
+
+ verify(migrator).getTaskData("key1", newConnector, newRepository, monitor);
+ verify(migrator).getTaskData("key2", newConnector, newRepository, monitor);
+ verify(migrator).createTask(taskData2, newRepository);
+ ITask task2Migrated = new TaskTask("mock.new", "http://mock", "2");
+ task2Migrated.setTaskKey("key2");
+ verify(migrator).migratePrivateData((AbstractTask) task2, (AbstractTask) task2Migrated, monitor);
+ verify(migrator).migratePrivateData((AbstractTask) task1, (AbstractTask) task1Migrated, monitor);
+
+ verify(migrationUi).delete(ImmutableSet.of(task1, task2), repository, newRepository, monitor);
+ assertEquals(SynchronizationState.INCOMING_NEW,
+ tasksState.getTaskList().getTask(repository.getRepositoryUrl(), "1").getSynchronizationState());
+ assertEquals(SynchronizationState.INCOMING,
+ tasksState.getTaskList().getTask(repository.getRepositoryUrl(), "2").getSynchronizationState());
+
+ assertEquals(ImmutableSet.of(taskOtherRepo, task1Migrated, task2Migrated),
+ ImmutableSet.copyOf(tasksState.getTaskList().getAllTasks()));
+ verify(tasksState.getRepositoryManager()).removeRepository(repository);
+ assertTrue(tasksState.getTaskList().getQueries().isEmpty());
+ assertEquals(ImmutableSet.of(newRepository), tasksState.getRepositoryManager().getRepositories("mock.new"));
+ }
+
+ @Test
+ public void migratePrivateData() throws Exception {
+ ConnectorMigrator migrator = createMigrator(true, true, kinds, ImmutableSet.of(repository), false);
+ AbstractTask oldTask = new TaskTask("mock", "http://mock", "1");
+ AbstractTask newTask = new TaskTask("mock.new", "http://mock", "1.migrated");
+
+ oldTask.setNotes("some notes");
+ DateRange scheduledDate = new DateRange(createCalendar(3));
+ Calendar dueDate = createCalendar(5);
+ tasksState.getTaskActivityManager().setScheduledFor(oldTask, scheduledDate);
+ tasksState.getTaskActivityManager().setDueDate(oldTask, dueDate.getTime());
+ oldTask.setEstimatedTimeHours(7);
+
+ migrator.migratePrivateData(oldTask, newTask, new NullProgressMonitor());
+ assertEquals("some notes", newTask.getNotes());
+ assertEquals(scheduledDate, newTask.getScheduledForDate());
+ assertEquals(dueDate.getTime(), newTask.getDueDate());
+ assertEquals(7, newTask.getEstimatedTimeHours());
+ }
+
+ @Test
+ public void migrateCategories() throws Exception {
+ ConnectorMigrator migrator = createMigrator(true, true, kinds, ImmutableSet.of(repository), false);
+ AbstractTask oldTask1 = new TaskTask("mock", "http://mock", "1");
+ AbstractTask oldTask2 = new TaskTask("mock", "http://mock", "2");
+ AbstractTask newTask1 = new TaskTask("mock.new", "http://mock", "1.migrated");
+ AbstractTask newTask2 = new TaskTask("mock.new", "http://mock", "2.migrated");
+
+ TaskCategory category1 = new TaskCategory("category1");
+ TaskCategory category2 = new TaskCategory("category2");
+ tasksState.getTaskList().addCategory(category1);
+ tasksState.getTaskList().addCategory(category2);
+ tasksState.getTaskList().addTask(oldTask1, category1);
+ tasksState.getTaskList().addTask(oldTask2, category2);
+
+ migrator.migratePrivateData(oldTask1, newTask1, new NullProgressMonitor());
+ migrator.migratePrivateData(oldTask2, newTask2, new NullProgressMonitor());
+ assertEquals(category1, getCategory(newTask1));
+ assertEquals(category2, getCategory(newTask2));
+ }
+
+ @Test
+ public void getCategories() throws Exception {
+ ConnectorMigrator migrator = createMigrator(true, true, kinds, ImmutableSet.of(repository), false);
+ AbstractTask oldTask1 = new TaskTask("mock", "http://mock", "1");
+ AbstractTask oldTask2 = new TaskTask("mock", "http://mock", "2");
+ AbstractTask newTask1 = new TaskTask("mock.new", "http://mock", "1.migrated");
+ AbstractTask newTask2 = new TaskTask("mock.new", "http://mock", "2.migrated");
+
+ TaskCategory category1 = new TaskCategory("category1");
+ TaskCategory category2 = new TaskCategory("category2");
+ tasksState.getTaskList().addCategory(category1);
+ tasksState.getTaskList().addCategory(category2);
+ tasksState.getTaskList().addTask(oldTask1, category1);
+ tasksState.getTaskList().addTask(newTask1, category1);
+ tasksState.getTaskList().addTask(oldTask2, category2);
+ tasksState.getTaskList().addTask(newTask2, category2);
+ tasksState.getTaskList().addTask(new TaskTask("mock.new", "http://mock", "not categorized"));
+
+ ImmutableMap<AbstractTask, TaskCategory> expected = ImmutableMap.of(oldTask1, category1, oldTask2, category2,
+ newTask1, category1, newTask2, category2);
+ assertEquals(expected, migrator.getCategories());
+
+ tasksState.getTaskList().addTask(new TaskTask("mock.new", "http://mock", "3"), category1);
+ assertEquals(expected, migrator.getCategories());
+ }
+
+ private AbstractTaskCategory getCategory(AbstractTask newTask) {
+ for (AbstractTaskCategory category : tasksState.getTaskList().getCategories()) {
+ Optional<ITask> task = Iterables.tryFind(category.getChildren(), Predicates.<ITask> equalTo(newTask));
+ if (task.isPresent()) {
+ return category;
+ }
+ }
+ return null;
+ }
+
+ private Calendar createCalendar(int daysInFuture) {
+ Calendar cal = Calendar.getInstance();
+ cal.add(Calendar.DAY_OF_MONTH, daysInFuture);
+ return cal;
+ }
+
+ private ConnectorMigrator createMigrator(boolean hasOldConnector, boolean hasNewConnector,
+ Map<String, String> kinds, Set<TaskRepository> repositories, boolean mockManager) {
+ if (mockManager) {
+ manager = mock(TaskRepositoryManager.class);
+ }
+ when(tasksState.getRepositoryManager()).thenReturn(manager);
+ ConnectorMigrator migrator = new ConnectorMigrator(kinds, "", tasksState, 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(newConnector);
+ when(manager.getRepositoryConnector("kind.new")).thenReturn(newConnector);
+ }
+ return migrator;
+ }
+
+}

Back to the top