<commit_before>/*******************************************************************************
 * Copyright (c) 2004, 2007 Mylyn project committers 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
 *******************************************************************************/
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.mylyn.context.core.ContextCore;
import org.eclipse.mylyn.context.tests.AbstractContextTest;
import org.eclipse.mylyn.internal.context.core.ContextCorePlugin;
import org.eclipse.mylyn.internal.context.core.InteractionContext;
import org.eclipse.mylyn.internal.tasks.core.AbstractTask;
import org.eclipse.mylyn.internal.tasks.ui.TaskListManager;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiInternal;
import org.eclipse.mylyn.internal.tasks.ui.wizards.TaskDataExportWizard;
import org.eclipse.mylyn.internal.tasks.ui.wizards.TaskDataExportWizardPage;
import org.eclipse.mylyn.monitor.core.InteractionEvent;
import org.eclipse.swt.widgets.Shell;

/**
 * Test case for the Task Export Wizard
 * 
 * @author <NAME>
 * @author <NAME> (fixes)
 */
public class TaskDataExportTest extends AbstractContextTest {

	private TaskDataExportWizard wizard = null;

	private TaskDataExportWizardPage wizardPage = null;

	private File destinationDir = null;

	private AbstractTask task1 = null;

	private final TaskListManager manager = TasksUiPlugin.getTaskListManager();

	private InteractionContext mockContext;

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

		removeFiles(new File(TasksUiPlugin.getDefault().getDataDirectory()));
//		ContextCore.getContextStore().init();

		// Create the export wizard
		wizard = new TaskDataExportWizard();
		wizard.addPages();
		wizard.createPageControls(new Shell());
		wizardPage = (TaskDataExportWizardPage) wizard.getPage(TaskDataExportWizardPage.PAGE_NAME);
		assertNotNull(wizardPage);

		// Create test export destination directory
		destinationDir = new File(TasksUiPlugin.getDefault().getDataDirectory() + File.separator + "TestDir");
		if (destinationDir.exists()) {
			removeFiles(destinationDir);
		} else {
			destinationDir.mkdir();
		}
		assertTrue(destinationDir.exists());

		// Create a task and context with an interaction event to be saved
		task1 = TasksUiInternal.createNewLocalTask("Export Test Task");
		manager.getTaskList().addTask(task1);
		mockContext = (InteractionContext) ContextCorePlugin.getContextStore().loadContext(task1.getHandleIdentifier());
		InteractionEvent event = new InteractionEvent(InteractionEvent.Kind.EDIT, "structureKind", "handle", "originId");
		mockContext.parseEvent(event);
		ContextCorePlugin.getContextManager().internalActivateContext(mockContext);

		// Save the context file and check that it exists
		assertTrue(ContextCorePlugin.getContextStore().getContextDirectory().exists());
		ContextCorePlugin.getContextStore().saveContext(mockContext.getHandleIdentifier());
		File taskFile = ContextCorePlugin.getContextStore().getFileForContext(task1.getHandleIdentifier());
		assertTrue(ContextCore.getContextManager().hasContext(task1.getHandleIdentifier()));
		assertTrue(taskFile.exists());
	}

	@Override
	protected void tearDown() throws Exception {
		removeFiles(destinationDir);
		destinationDir.delete();
		assertFalse(destinationDir.exists());
		ContextCore.getContextManager().deactivateContext(mockContext.getHandleIdentifier());
		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.setParameters(true, true, true, true, true, destinationDir.getPath());
		wizard.performFinish();

		// Check that the task list file was exported
		File destZipFile = new File(destinationDir + File.separator + TaskDataExportWizard.getZipFileName());
		assertTrue(destZipFile.exists());
		ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(destZipFile));
		ArrayList<String> entries = new ArrayList<String>();

		ZipEntry entry = zipInputStream.getNextEntry();
		while (entry != null) {
			entries.add(entry.getName());
			entry = zipInputStream.getNextEntry();<commit_msg>NEW - bug 158067: improve tasklist backup snapshot functionality and ui https://bugs.eclipse.org/bugs/show_bug.cgi?id=158067<commit_after>/*******************************************************************************
 * Copyright (c) 2004, 2007 Mylyn project committers 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
 *******************************************************************************/
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.mylyn.context.core.ContextCore;
import org.eclipse.mylyn.context.tests.AbstractContextTest;
import org.eclipse.mylyn.internal.context.core.ContextCorePlugin;
import org.eclipse.mylyn.internal.context.core.InteractionContext;
import org.eclipse.mylyn.internal.tasks.core.AbstractTask;
import org.eclipse.mylyn.internal.tasks.ui.TaskListBackupManager;
import org.eclipse.mylyn.internal.tasks.ui.TaskListManager;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiInternal;
import org.eclipse.mylyn.internal.tasks.ui.wizards.TaskDataExportWizard;
import org.eclipse.mylyn.internal.tasks.ui.wizards.TaskDataExportWizardPage;
import org.eclipse.mylyn.monitor.core.InteractionEvent;
import org.eclipse.swt.widgets.Shell;

/**
 * Test case for the Task Export Wizard
 * 
 * @author <NAME>
 * @author <NAME> (fixes)
 */
public class TaskDataExportTest extends AbstractContextTest {

	private TaskDataExportWizard wizard = null;

	private TaskDataExportWizardPage wizardPage = null;

	private File destinationDir = null;

	private AbstractTask task1 = null;

	private final TaskListManager manager = TasksUiPlugin.getTaskListManager();

	private InteractionContext mockContext;

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

		removeFiles(new File(TasksUiPlugin.getDefault().getDataDirectory()));
//		ContextCore.getContextStore().init();

		// Create the export wizard
		wizard = new TaskDataExportWizard();
		wizard.addPages();
		wizard.createPageControls(new Shell());
		wizardPage = (TaskDataExportWizardPage) wizard.getPage(TaskDataExportWizardPage.PAGE_NAME);
		assertNotNull(wizardPage);

		// Create test export destination directory
		destinationDir = new File(TasksUiPlugin.getDefault().getDataDirectory() + File.separator + "TestDir");
		if (destinationDir.exists()) {
			removeFiles(destinationDir);
		} else {
			destinationDir.mkdir();
		}
		assertTrue(destinationDir.exists());

		// Create a task and context with an interaction event to be saved
		task1 = TasksUiInternal.createNewLocalTask("Export Test Task");
		manager.getTaskList().addTask(task1);
		mockContext = (InteractionContext) ContextCorePlugin.getContextStore().loadContext(task1.getHandleIdentifier());
		InteractionEvent event = new InteractionEvent(InteractionEvent.Kind.EDIT, "structureKind", "handle", "originId");
		mockContext.parseEvent(event);
		ContextCorePlugin.getContextManager().internalActivateContext(mockContext);

		// Save the context file and check that it exists
		assertTrue(ContextCorePlugin.getContextStore().getContextDirectory().exists());
		ContextCorePlugin.getContextStore().saveContext(mockContext.getHandleIdentifier());
		File taskFile = ContextCorePlugin.getContextStore().getFileForContext(task1.getHandleIdentifier());
		assertTrue(ContextCore.getContextManager().hasContext(task1.getHandleIdentifier()));
		assertTrue(taskFile.exists());
	}

	@Override
	protected void tearDown() throws Exception {
		removeFiles(destinationDir);
		destinationDir.delete();
		assertFalse(destinationDir.exists());
		ContextCore.getContextManager().deactivateContext(mockContext.getHandleIdentifier());
		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.setParameters(true, true, true, true, true, destinationDir.getPath());
		wizard.performFinish();

		// Check that the task list file was exported
		File destZipFile = new File(destinationDir + File.separator + TaskListBackupManager.getBackupFileName());
		assertTrue(destZipFile.exists());
		ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(destZipFile));
		ArrayList<String> entries = new ArrayList<String>();

		ZipEntry entry = zipInputStream.getNextEntry();
		while (entry != null) {
			entries.add(entry.getName());
			entry = zipInputStream.getNextEntry();
{'fraction_non_alphanumeric': 0.1361492506026622, 'fraction_numerical': 0.005030919190860497, 'mean_word_length': 5.995601173020527, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7063762', 'n_tokens_mistral': 2746, 'n_tokens_neox': 2537, 'n_words': 589}