|
Removed
Link Here
|
| 1 |
package org.eclipse.mylar.sandbox.tests; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.FileInputStream; |
| 5 |
import java.io.FileOutputStream; |
| 6 |
import java.io.IOException; |
| 7 |
import java.io.InputStream; |
| 8 |
import java.io.OutputStream; |
| 9 |
|
| 10 |
import junit.framework.TestCase; |
| 11 |
|
| 12 |
import org.eclipse.mylar.core.InteractionEvent; |
| 13 |
import org.eclipse.mylar.core.MylarPlugin; |
| 14 |
import org.eclipse.mylar.internal.core.MylarContext; |
| 15 |
import org.eclipse.mylar.internal.monitor.reports.MylarReportsPlugin; |
| 16 |
import org.eclipse.mylar.internal.sandbox.MylarSandboxPlugin; |
| 17 |
import org.eclipse.mylar.internal.sandbox.share.SwitchTaskDataFolderAction; |
| 18 |
import org.eclipse.mylar.internal.tasklist.ITask; |
| 19 |
import org.eclipse.mylar.internal.tasklist.MylarTaskListPlugin; |
| 20 |
import org.eclipse.mylar.internal.tasklist.Task; |
| 21 |
import org.eclipse.mylar.internal.tasklist.TaskListManager; |
| 22 |
|
| 23 |
/** |
| 24 |
* Tests changing the shared task directory |
| 25 |
* currently in use. |
| 26 |
* |
| 27 |
* @author Wesley Coelho |
| 28 |
*/ |
| 29 |
public class SharedTaskFolderTest extends TestCase{ |
| 30 |
|
| 31 |
private File sharedDataRootDir = null; |
| 32 |
private File bobsDataDir = null; |
| 33 |
private File jillsDataDir = null; |
| 34 |
private String originalMainDataDir = null; |
| 35 |
private String originalSharedDataDir = null; |
| 36 |
private TaskListManager manager = MylarTaskListPlugin.getTaskListManager(); |
| 37 |
|
| 38 |
/** |
| 39 |
* Set up a shared task directory structure by creating some data |
| 40 |
* in the main directory and copying it to the shared directories. |
| 41 |
*/ |
| 42 |
protected void setUp() throws Exception { |
| 43 |
super.setUp(); |
| 44 |
|
| 45 |
//Get the original main data directory so that it can be reset later |
| 46 |
originalMainDataDir = MylarPlugin.getDefault().getDataDirectory(); |
| 47 |
|
| 48 |
//Create a task to make sure there is some data in the main directory |
| 49 |
createAndSaveTask("Task1"); |
| 50 |
|
| 51 |
//Create the shared data directory structure |
| 52 |
sharedDataRootDir = new File(MylarPlugin.getDefault().getDataDirectory() + File.separator + "SharedDataDir"); |
| 53 |
sharedDataRootDir.mkdir(); |
| 54 |
assertTrue(sharedDataRootDir.exists()); |
| 55 |
|
| 56 |
bobsDataDir = new File(sharedDataRootDir.getPath() + File.separator + "Bob"); |
| 57 |
bobsDataDir.mkdir(); |
| 58 |
assertTrue(bobsDataDir.exists()); |
| 59 |
|
| 60 |
jillsDataDir = new File(sharedDataRootDir.getPath() + File.separator + "Jill"); |
| 61 |
jillsDataDir.mkdir(); |
| 62 |
assertTrue(jillsDataDir.exists()); |
| 63 |
|
| 64 |
//Start the shared data dirs off with copies of the main data |
| 65 |
File mainDataDir = new File(originalMainDataDir); |
| 66 |
for ( File currFile : mainDataDir.listFiles()) { |
| 67 |
File destFile = new File(bobsDataDir.getPath() + File.separator + currFile.getName()); |
| 68 |
copy(currFile, destFile); |
| 69 |
destFile = new File(jillsDataDir.getPath() + File.separator + currFile.getName()); |
| 70 |
copy(currFile, destFile); |
| 71 |
} |
| 72 |
|
| 73 |
//Set the shared data dir |
| 74 |
originalSharedDataDir = MylarSandboxPlugin.getDefault().getSharedDataDirectoryManager().getSharedDataDirectory(); |
| 75 |
MylarReportsPlugin.getDefault().getPreferenceStore().setValue(MylarReportsPlugin.SHARED_TASK_DATA_ROOT_DIR, sharedDataRootDir.getPath()); |
| 76 |
MylarSandboxPlugin.getDefault().getSharedDataDirectoryManager().setSharedDataDirectory(sharedDataRootDir.getPath()); |
| 77 |
assertFalse(MylarPlugin.getDefault().getDataDirectory().equals(sharedDataRootDir.getPath())); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Tests moving the main mylar data directory to another location |
| 82 |
*/ |
| 83 |
public void testSharedDataDirSwitching(){ |
| 84 |
SwitchTaskDataFolderAction switchAction = new SwitchTaskDataFolderAction(); |
| 85 |
|
| 86 |
//Create a task to appear in the main data dir only |
| 87 |
ITask mainDataDirTask = createAndSaveTask("Main Dir Task"); |
| 88 |
|
| 89 |
//Check the options of folders to switch to |
| 90 |
String[] sharedDataFolderOptions = switchAction.getFolderStrings(); |
| 91 |
//Note that index 0 is a string indicating a switch back to the main data directory |
| 92 |
assertTrue(sharedDataFolderOptions[0].equals("Bob")); |
| 93 |
assertTrue(sharedDataFolderOptions[1].equals("Jill")); |
| 94 |
|
| 95 |
//Switch to Bob's folder |
| 96 |
switchAction.switchTaskDataFolder(sharedDataFolderOptions[0]); |
| 97 |
|
| 98 |
//Check that the task created in the main data dir isn't there |
| 99 |
File mainDataDirTaskFile = MylarPlugin.getContextManager().getFileForContext(mainDataDirTask.getHandleIdentifier()); |
| 100 |
// File mainDataDirTaskFile = new File(MylarPlugin.getDefault().getDataDirectory() + File.separator + mainDataDirTask.getContextPath() + MylarTaskListPlugin.FILE_EXTENSION); |
| 101 |
assertFalse(mainDataDirTaskFile.exists()); |
| 102 |
assertNull(manager.getTaskForHandle(mainDataDirTask.getHandleIdentifier(), false)); |
| 103 |
|
| 104 |
fail(); // uncomment below |
| 105 |
//Create a new task in bob's task data folder only and check that it exists in the right place |
| 106 |
// ITask bobsTask = createAndSaveTask("Bob's Task"); |
| 107 |
// |
| 108 |
// File bobsTaskFile = new File(bobsDataDir.getPath() + File.separator + |
| 109 |
// MylarPlugin.getContextManager().getFileForContext(mainDataDirTask.getHandleIdentifier()).getName()); |
| 110 |
// assertTrue(bobsTaskFile.exists()); |
| 111 |
// bobsTaskFile = new File(MylarPlugin.getDefault().getDataDirectory() + File.separator + bobsTask.getContextPath() + MylarTaskListPlugin.FILE_EXTENSION); |
| 112 |
// assertTrue(bobsTaskFile.exists()); |
| 113 |
// assertNotNull(manager.getTaskForHandle(bobsTask.getHandleIdentifier(), false)); |
| 114 |
// |
| 115 |
// //Switch to Jill's folder |
| 116 |
// switchAction.switchTaskDataFolder(sharedDataFolderOptions[1]); |
| 117 |
// |
| 118 |
// //Check that Bob's task isn't there |
| 119 |
// bobsTaskFile = new File(MylarPlugin.getDefault().getDataDirectory() + File.separator + bobsTask.getContextPath() + MylarTaskListPlugin.FILE_EXTENSION); |
| 120 |
// assertFalse(bobsTaskFile.exists()); |
| 121 |
// assertNull(manager.getTaskForHandle(bobsTask.getHandleIdentifier(), false)); |
| 122 |
// |
| 123 |
// //Switch back to Bob's folder |
| 124 |
// switchAction.switchTaskDataFolder(sharedDataFolderOptions[0]); |
| 125 |
// |
| 126 |
// //Check that bob's task is still there |
| 127 |
// bobsTaskFile = new File(MylarPlugin.getDefault().getDataDirectory() + File.separator + bobsTask.getContextPath() + MylarTaskListPlugin.FILE_EXTENSION); |
| 128 |
// assertTrue(bobsTaskFile.exists()); |
| 129 |
// assertNotNull(manager.getTaskForHandle(bobsTask.getHandleIdentifier(), false)); |
| 130 |
// |
| 131 |
// //Switch back to the main data folder |
| 132 |
// sharedDataFolderOptions = switchAction.getFolderStrings(); |
| 133 |
// switchAction.switchTaskDataFolder(sharedDataFolderOptions[0]); |
| 134 |
// |
| 135 |
// //Check that the main task is there |
| 136 |
// mainDataDirTaskFile = new File(MylarPlugin.getDefault().getDataDirectory() + File.separator + mainDataDirTask.getContextPath() + MylarTaskListPlugin.FILE_EXTENSION); |
| 137 |
// assertTrue(mainDataDirTaskFile.exists()); |
| 138 |
// assertNotNull(manager.getTaskForHandle(mainDataDirTask.getHandleIdentifier(), false)); |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Creates a task with an interaction event and checks that it has |
| 144 |
* been properly saved in the currently active data directory |
| 145 |
*/ |
| 146 |
protected ITask createAndSaveTask(String taskName){ |
| 147 |
|
| 148 |
//Create the task and add it to the root of the task list |
| 149 |
ITask newTask = new Task(MylarTaskListPlugin.getTaskListManager().genUniqueTaskHandle(), taskName, true); |
| 150 |
manager.moveToRoot(newTask); |
| 151 |
MylarContext mockContext = MylarPlugin.getContextManager().loadContext(newTask.getHandleIdentifier());//, newTask.getContextPath()); |
| 152 |
InteractionEvent event = new InteractionEvent(InteractionEvent.Kind.EDIT,"structureKind","handle","originId"); |
| 153 |
mockContext.parseEvent(event); |
| 154 |
MylarPlugin.getContextManager().contextActivated(mockContext); |
| 155 |
|
| 156 |
fail(); // uncomment below |
| 157 |
// //Save the context file and check that it exists |
| 158 |
// MylarPlugin.getContextManager().saveContext(mockContext.getId());//, newTask.getContextPath()); |
| 159 |
// File taskFile = new File(MylarPlugin.getDefault().getDataDirectory() + File.separator + newTask.getContextPath() + MylarContextManager.CONTEXT_FILE_EXTENSION); |
| 160 |
// assertTrue(MylarPlugin.getContextManager().hasContext(newTask.getContextPath())); |
| 161 |
// assertTrue(taskFile.exists()); |
| 162 |
|
| 163 |
return newTask; |
| 164 |
} |
| 165 |
|
| 166 |
protected void tearDown() throws Exception{ |
| 167 |
|
| 168 |
//Reset the shared directory to the original value |
| 169 |
MylarSandboxPlugin.getDefault().getSharedDataDirectoryManager().setSharedDataDirectory(originalSharedDataDir); |
| 170 |
MylarReportsPlugin.getDefault().getPreferenceStore().setValue(MylarReportsPlugin.SHARED_TASK_DATA_ROOT_DIR, originalSharedDataDir); |
| 171 |
|
| 172 |
//Delete the test shared data directories |
| 173 |
deleteDir(bobsDataDir); |
| 174 |
deleteDir(jillsDataDir); |
| 175 |
deleteDir(sharedDataRootDir); |
| 176 |
|
| 177 |
super.tearDown(); |
| 178 |
} |
| 179 |
|
| 180 |
private void deleteDir(File targetDir){ |
| 181 |
File[] files = targetDir.listFiles(); |
| 182 |
for (File file : files) { |
| 183 |
file.delete(); |
| 184 |
} |
| 185 |
|
| 186 |
targetDir.delete(); |
| 187 |
assertFalse(targetDir.exists()); |
| 188 |
} |
| 189 |
|
| 190 |
// Note: Copied from MylarTaskListPlugin |
| 191 |
private boolean copy(File src, File dst) { |
| 192 |
try { |
| 193 |
InputStream in = new FileInputStream(src); |
| 194 |
OutputStream out = new FileOutputStream(dst); |
| 195 |
|
| 196 |
// Transfer bytes from in to out |
| 197 |
byte[] buf = new byte[1024]; |
| 198 |
int len; |
| 199 |
while ((len = in.read(buf)) > 0) { |
| 200 |
out.write(buf, 0, len); |
| 201 |
} |
| 202 |
in.close(); |
| 203 |
out.close(); |
| 204 |
return true; |
| 205 |
} catch (IOException ioe) { |
| 206 |
return false; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
} |