Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1b26119a77900e5e54442471c09c20f1c71b79f8 (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
/*******************************************************************************
 * Copyright (c) 2004, 2008 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;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.mylyn.commons.tests.support.CommonsTestUtil;
import org.eclipse.mylyn.context.tests.AbstractContextTest;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.internal.tasks.ui.util.TaskDataExportOperation;
import org.eclipse.mylyn.internal.tasks.ui.util.TaskDataSnapshotOperation;
import org.eclipse.mylyn.internal.tasks.ui.wizards.TaskDataExportWizard;
import org.eclipse.mylyn.internal.tasks.ui.wizards.TaskDataExportWizardPage;
import org.eclipse.swt.widgets.Shell;

/**
 * Test case for the Task Export Wizard
 * 
 * @author Wesley Coelho
 * @author Mik Kersten (fixes)
 */
public class TaskDataExportTest extends AbstractContextTest {

	private TaskDataExportWizard wizard;

	private TaskDataExportWizardPage wizardPage;

	private File destinationDir;

	@Override
	protected void setUp() throws Exception {
		super.setUp();

		// Create the export wizard
		wizard = new TaskDataExportWizard();
		wizard.addPages();
		wizard.createPageControls(new Shell());
		wizardPage = (TaskDataExportWizardPage) wizard.getPage("org.eclipse.mylyn.tasklist.exportPage");
		assertNotNull(wizardPage);

		// Create test export destination directory
		File mylynFolder = new File(TasksUiPlugin.getDefault().getDataDirectory());
		destinationDir = new File(mylynFolder.getParent(), "TestDir");
		CommonsTestUtil.deleteFolder(destinationDir);
		destinationDir.mkdir();

		// Create folder/file structure
		File tasklist = new File(mylynFolder, "tasks.xml.zip");
		if (!tasklist.exists()) {
			assertTrue(tasklist.createNewFile());
		}
		File hidden = new File(mylynFolder, ".hidden");
		if (!hidden.exists()) {
			assertTrue(hidden.createNewFile());
		}
		File tasksandstuff = new File(mylynFolder, "tasksandstuff");
		if (!tasksandstuff.exists()) {
			assertTrue(tasksandstuff.mkdir());
		}
		File backup = new File(mylynFolder, "backup");
		if (!backup.exists()) {
			assertTrue(backup.mkdir());
		}
		File tasksFile = new File(tasksandstuff, "file1.xml.zip");
		if (!tasksFile.exists()) {
			assertTrue(tasksFile.createNewFile());
		}

		File tasksSubDir = new File(tasksandstuff, "sub");
		if (!tasksSubDir.exists()) {
			assertTrue(tasksSubDir.mkdir());
		}

		File tasksSubDirFile = new File(tasksSubDir, "file2.xml.zip");
		if (!tasksSubDirFile.exists()) {
			assertTrue(tasksSubDirFile.createNewFile());
		}

	}

	@Override
	protected void tearDown() throws Exception {
		wizard.dispose();
		wizardPage.dispose();
		CommonsTestUtil.deleteFolder(destinationDir);
		File mylynFolder = new File(TasksUiPlugin.getDefault().getDataDirectory());
		// Create folder/file structure
		File tasklist = new File(mylynFolder, "tasks.xml.zip");
		tasklist.delete();
		File hidden = new File(mylynFolder, ".hidden");
		hidden.delete();
		File tasks = new File(mylynFolder, "tasksandstuff");
		File tasksSubDir = new File(tasks, "sub");
		File backup = new File(mylynFolder, "backup");
		CommonsTestUtil.deleteFolder(backup);
		CommonsTestUtil.deleteFolder(tasksSubDir);
		CommonsTestUtil.deleteFolder(tasks);
		super.tearDown();
	}

	/**
	 * Tests the wizard when it has been asked to export all task data to a zip file.
	 */
	public void testExportAllToZip() throws Exception {
		// set parameters in the wizard to simulate a user setting them and clicking "Finish"
		wizardPage.setDestinationDirectory(destinationDir.getPath());
		wizard.performFinish();

		// check that the task list file was exported
		File[] files = destinationDir.listFiles();
		assertEquals(1, files.length);
		ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(files[0]));
		try {
			ArrayList<String> entries = new ArrayList<String>();
			ZipEntry entry = zipInputStream.getNextEntry();
			while (entry != null) {
				entries.add(entry.getName());
				entry = zipInputStream.getNextEntry();
			}
			assertFalse(entries.contains(".hidden"));
			assertTrue(entries.contains("tasks.xml.zip"));
			assertTrue(entries.contains("tasksandstuff/file1.xml.zip"));
			assertTrue(entries.contains("tasksandstuff/sub/file2.xml.zip"));
			assertFalse(entries.contains("backup"));
		} finally {
			zipInputStream.close();
		}
	}

	public void testSnapshot() throws Exception {
		final TaskDataExportOperation backupJob = new TaskDataSnapshotOperation(destinationDir.getPath(),
				"testBackup.zip");
		backupJob.run(new NullProgressMonitor());
		// check that the task list file was exported
		File[] files = destinationDir.listFiles();
		assertEquals(1, files.length);
		ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(files[0]));
		try {
			ArrayList<String> entries = new ArrayList<String>();
			ZipEntry entry = zipInputStream.getNextEntry();
			while (entry != null) {
				entries.add(entry.getName());
				entry = zipInputStream.getNextEntry();
			}
			assertFalse(entries.contains(".hidden"));
			assertTrue(entries.contains("tasks.xml.zip"));
			assertTrue(entries.contains("repositories.xml.zip"));
			assertTrue(entries.contains("contexts/activity.xml.zip"));
			assertFalse(entries.contains("tasks"));
			assertEquals(3, entries.size());
		} finally {
			zipInputStream.close();
		}
	}
}

Back to the top