Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3167d467aeb816a278ae3c5c88b218d8819683d4 (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*******************************************************************************
 * Copyright (c) 2004 - 2006 University Of British Columbia 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:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/
package org.eclipse.mylar.tests.integration;

import java.io.File;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import junit.framework.TestCase;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.mylar.internal.bugzilla.ui.tasklist.BugzillaTask;
import org.eclipse.mylar.internal.core.MylarContextManager;
import org.eclipse.mylar.internal.monitor.MylarMonitorPlugin;
import org.eclipse.mylar.provisional.core.InteractionEvent;
import org.eclipse.mylar.provisional.core.MylarPlugin;
import org.eclipse.mylar.provisional.tasklist.AbstractRepositoryTask;
import org.eclipse.mylar.provisional.tasklist.ITask;
import org.eclipse.mylar.provisional.tasklist.MylarTaskListPlugin;
import org.eclipse.mylar.provisional.tasklist.Task;
import org.eclipse.mylar.provisional.tasklist.TaskListManager;

/**
 * Tests changes to the main mylar data directory location.
 * 
 * @author Wesley Coelho
 * @author Mik Kersten (rewrites)
 */
public class ChangeDataDirTest extends TestCase {

	private String newDataDir = null;

	private final String defaultDir = MylarPlugin.getDefault().getDefaultDataDirectory();

	private TaskListManager manager = MylarTaskListPlugin.getTaskListManager();

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

		newDataDir = ResourcesPlugin.getWorkspace().getRoot().getLocation().toString() + '/'
				+ ChangeDataDirTest.class.getSimpleName();
		File dir = new File(newDataDir);
		dir.mkdir();
		dir.deleteOnExit();
		manager.resetTaskList();
		assertTrue(manager.getTaskList().isEmpty());
		MylarTaskListPlugin.getDefault().getTaskListSaveManager().saveTaskListAndContexts();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
		manager.resetTaskList();
		MylarPlugin.getDefault().setDataDirectory(defaultDir);
	}

	public void testMonitorFileMove() {
		MylarMonitorPlugin.getDefault().startMonitoring();
		MylarMonitorPlugin.getDefault().getInteractionLogger().interactionObserved(
				InteractionEvent.makeCommand("id", "delta"));
		String oldPath = MylarMonitorPlugin.getDefault().getInteractionLogger().getOutputFile().getAbsolutePath();
		assertTrue(new File(oldPath).exists());

		MylarPlugin.getDefault().setDataDirectory(newDataDir);

		assertFalse(new File(oldPath).exists());
		String newPath = MylarMonitorPlugin.getDefault().getInteractionLogger().getOutputFile().getAbsolutePath();
		assertTrue(new File(newPath).exists());

		assertTrue(MylarMonitorPlugin.getDefault().getInteractionLogger().getOutputFile().exists());
		String monitorFileName = MylarMonitorPlugin.MONITOR_LOG_NAME + MylarContextManager.CONTEXT_FILE_EXTENSION;
		List<String> newFiles = Arrays.asList(new File(newDataDir).list());
		assertTrue(newFiles.toString(), newFiles.contains(monitorFileName));

		List<String> filesLeft = Arrays.asList(new File(defaultDir).list());
		assertFalse(filesLeft.toString(), filesLeft.contains(monitorFileName));
		MylarMonitorPlugin.getDefault().stopMonitoring();
	}

	public void testDefaultDataDirectoryMove() {
		String workspaceRelativeDir = ResourcesPlugin.getWorkspace().getRoot().getLocation().toString() + '/'
				+ ".mylar";
		assertEquals(defaultDir, workspaceRelativeDir);

		MylarPlugin.getDefault().setDataDirectory(newDataDir);
		assertEquals(MylarPlugin.getDefault().getDataDirectory(), newDataDir);
	}

	public void testTaskMove() {
		String handle = "task-1";
		ITask task = new Task(handle, "label", true);
		manager.getTaskList().moveToRoot(task);

		ITask readTaskBeforeMove = manager.getTaskList().getTask(handle);
		MylarTaskListPlugin.getDefault().getTaskListSaveManager().copyDataDirContentsTo(newDataDir);
		MylarPlugin.getDefault().setDataDirectory(newDataDir);

		ITask readTaskAfterMove = manager.getTaskList().getTask(handle);
		assertNotNull(readTaskAfterMove);
		assertEquals(readTaskBeforeMove.getCreationDate(), readTaskAfterMove.getCreationDate());
	}

	// TODO: delete?  using lastOpened date wrong
	public void testBugzillaTaskMove() {
		String handle = AbstractRepositoryTask.getHandle("server", 1);
		BugzillaTask bugzillaTask = new BugzillaTask(handle, "bug1", true);
		addBugzillaTask(bugzillaTask);
		Date refreshDate = new Date();
		bugzillaTask.setLastSynchronized(refreshDate);

		BugzillaTask readTaskBeforeMove = (BugzillaTask) manager.getTaskList().getTask(handle);
		assertNotNull(readTaskBeforeMove);
		assertEquals(refreshDate, readTaskBeforeMove.getLastSynchronized());

		MylarTaskListPlugin.getDefault().getTaskListSaveManager().copyDataDirContentsTo(newDataDir);
		MylarPlugin.getDefault().setDataDirectory(newDataDir);
  
		BugzillaTask readTaskAfterMove = (BugzillaTask) manager.getTaskList().getTask(handle);
		assertNotNull(readTaskAfterMove);
		// HACK: should be checking date equality, but millis seem to differ?
		assertEquals(refreshDate.toString(), readTaskAfterMove.getLastSynchronized().toString());
	}

	private void addBugzillaTask(BugzillaTask newTask) {
//		AbstractRepositoryClient client = MylarTaskListPlugin.getRepositoryManager().getRepositoryClient(BugzillaPlugin.REPOSITORY_KIND);
//		client.addTaskToArchive(newTask);
		
		// TODO: put back?
//		MylarTaskListPlugin.getTaskListManager().getTaskList().internalAddTask(newTask);

//		BugzillaTaskHandler handler = new BugzillaTaskHandler();
//		handler.addTaskToArchive(newTask);
		MylarTaskListPlugin.getTaskListManager().getTaskList().moveToRoot(newTask);
	}

	// /**
	// * Tests moving the main mylar data directory to another location (Without
	// * copying existing data to the new directory)
	// */
	// public void testChangeMainDataDir() {
	//
	// ITask mainDataDirTask = createAndSaveTask("Main Task", false);
	//
	// // MylarPlugin.getDefault().setDataDirectory(newDataDir);
	// assertEquals(0, manager.getTaskList().getRootTasks().size());
	//
	// // Check that the main data dir task isn't in the list or the folder
	// File taskFile = new File(MylarPlugin.getDefault().getDataDirectory() +
	// File.separator
	// + mainDataDirTask.getContextPath() + MylarTaskListPlugin.FILE_EXTENSION);
	// assertFalse(taskFile.exists());
	// assertNull(manager.getTaskForHandle(mainDataDirTask.getHandleIdentifier(),
	// false));
	//
	// // Check that a newly created task appears in the right place (method
	// // will check)
	// ITask newDataDirTask = createAndSaveTask("New Data Dir", false);
	// taskFile = new File(MylarPlugin.getDefault().getDataDirectory() +
	// File.separator
	// + newDataDirTask.getContextPath() + MylarTaskListPlugin.FILE_EXTENSION);
	// assertTrue(taskFile.exists());
	//
	// // Check for other the tasklist file in the new dir
	// File destTaskListFile = new
	// File(MylarPlugin.getDefault().getDataDirectory() + File.separator
	// + MylarTaskListPlugin.DEFAULT_TASK_LIST_FILE);
	// assertTrue(destTaskListFile.exists());
	//
	// // Switch back to the main task directory
	// MylarPlugin.getDefault().setDataDirectory(MylarPlugin.getDefault().getDefaultDataDirectory());
	//		
	// // Check that the previously created main dir task is in the task list
	// and its file exists
	// assertNotNull(manager.getTaskForHandle(mainDataDirTask.getHandleIdentifier(),
	// false));
	// taskFile = new File(MylarPlugin.getDefault().getDataDirectory() +
	// File.separator
	// + mainDataDirTask.getContextPath() + MylarTaskListPlugin.FILE_EXTENSION);
	// assertTrue(taskFile.exists());
	//
	// // Check that the task created in the "New Data Dir" isn't there now
	// // that we're back to the main dir
	// assertNull(manager.getTaskForHandle(newDataDirTask.getHandleIdentifier(),
	// false));
	//
	// }
	//
	// /**
	// * Creates a task with an interaction event and checks that it has been
	// * properly saved in the currently active data directory
	// */
	// protected ITask createAndSaveTask(String taskName, boolean
	// createBugzillaTask) {
	//
	// // Create the task and add it to the root of the task list
	// BugzillaTask newTask = null;
	// if (!createBugzillaTask) {
	// String handle =
	// MylarTaskListPlugin.getTaskListManager().genUniqueTaskHandle();
	// newTask = new BugzillaTask(handle, "bug1", true, true);//new
	// Task(MylarTaskListPlugin.getTaskListManager().genUniqueTaskHandle(),
	// taskName, true);
	// manager.moveToRoot(newTask);
	// } else {
	// newTask = new
	// BugzillaTask(MylarTaskListPlugin.getTaskListManager().genUniqueTaskHandle(),
	// taskName, true,
	// true);
	// addBugzillaTask(newTask);
	// }
	//
	// MylarContext mockContext =
	// MylarPlugin.getContextManager().loadContext(newTask.getHandleIdentifier(),
	// newTask.getContextPath());
	// InteractionEvent event = new InteractionEvent(InteractionEvent.Kind.EDIT,
	// "structureKind", "handle", "originId");
	// mockContext.parseEvent(event);
	// MylarPlugin.getContextManager().contextActivated(mockContext);
	//
	// // Save the context file and check that it exists
	// MylarPlugin.getContextManager().saveContext(mockContext.getId(),
	// newTask.getContextPath());
	// File taskFile = new File(MylarPlugin.getDefault().getDataDirectory() +
	// File.separator
	// + newTask.getContextPath() + MylarContextManager.CONTEXT_FILE_EXTENSION);
	// assertTrue(MylarPlugin.getContextManager().hasContext(newTask.getContextPath()));
	// assertTrue(taskFile.exists());
	//
	// return newTask;
	// }
	//	
	// /**
	// * Same as above but using bugzilla tasks Tests moving the main mylar data
	// * directory to another location (Without copying existing data to the new
	// * directory)
	// */
	// public void testChangeMainDataDirBugzilla() {
	//
	// // Create a task in the main dir and context with an interaction event
	// // to be saved
	// ITask mainDataDirTask = createAndSaveTask("Main Task", true);
	//
	// // Set time to see if the right task data is returned by the registry
	// // mechanism
	// mainDataDirTask.setElapsedTime(ELAPSED_TIME1);
	//
	// // Save tasklist
	// MylarTaskListPlugin.getDefault().getTaskListSaveManager().saveTaskListAndContexts();
	//
	// // Temp check that the task is there
	// assertNotNull(manager.getTaskForHandle(mainDataDirTask.getHandleIdentifier(),
	// false));
	//
	// // Switch task directory
	// MylarPlugin.getDefault().setDataDirectory(newDataDir);
	//
	// // Check that there are no tasks in the tasklist after switching to the
	// // empty dir
	// assertTrue(manager.getTaskList().getRootTasks().size() == 0);
	//
	// // Check that the main data dir task isn't in the list or the folder
	// File taskFile = new File(MylarPlugin.getDefault().getDataDirectory() +
	// File.separator
	// + mainDataDirTask.getContextPath() + MylarTaskListPlugin.FILE_EXTENSION);
	// assertFalse(taskFile.exists());
	// assertNull(manager.getTaskForHandle(mainDataDirTask.getHandleIdentifier(),
	// false));
	//
	// // Check that a newly created task appears in the right place (method
	// // will check)
	// ITask newDataDirTask = createAndSaveTask("New Data Dir", true);
	// taskFile = new File(MylarPlugin.getDefault().getDataDirectory() +
	// File.separator
	// + newDataDirTask.getContextPath() + MylarTaskListPlugin.FILE_EXTENSION);
	// assertTrue(taskFile.exists());
	//
	// // Check for tasklist file in the new dir
	// File destTaskListFile = new
	// File(MylarPlugin.getDefault().getDataDirectory() + File.separator
	// + MylarTaskListPlugin.DEFAULT_TASK_LIST_FILE);
	// assertTrue(destTaskListFile.exists());
	//
	// MylarPlugin.getDefault().setDataDirectory(MylarPlugin.getDefault().getDefaultDataDirectory());
	//
	// // Check that the previously created main dir task is in the task list
	// // and its file exists
	// assertNotNull(manager.getTaskForHandle(mainDataDirTask.getHandleIdentifier(),
	// false));
	// taskFile = new File(MylarPlugin.getDefault().getDataDirectory() +
	// File.separator
	// + mainDataDirTask.getContextPath() + MylarTaskListPlugin.FILE_EXTENSION);
	// assertTrue(taskFile.exists());
	//
	// // Check that the elapsed time is still right
	// assertTrue(manager.getTaskForHandle(mainDataDirTask.getHandleIdentifier(),
	// false).getElapsedTime() == ELAPSED_TIME1);
	//
	// // Check that the task created in the "New Data Dir" isn't there now
	// // that we're back to the main dir
	// assertNull(manager.getTaskForHandle(newDataDirTask.getHandleIdentifier(),
	// false));
	// }
}

Back to the top