Skip to main content
summaryrefslogtreecommitdiffstats
blob: cb72bc7f74321df20b3fa5260a0f1a870ce9932f (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
/*******************************************************************************
 * Copyright (c) 2004, 2010 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.data;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.zip.ZipInputStream;

import junit.framework.TestCase;

import org.eclipse.mylyn.commons.sdk.util.CommonTestUtil;
import org.eclipse.mylyn.internal.tasks.core.TaskRepositoryManager;
import org.eclipse.mylyn.internal.tasks.core.data.TaskDataExternalizer;
import org.eclipse.mylyn.internal.tasks.core.data.TaskDataExternalizer.Xml11InputStream;
import org.eclipse.mylyn.internal.tasks.core.data.TaskDataState;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.ITaskDataWorkingCopy;
import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper;
import org.eclipse.mylyn.tasks.core.data.TaskData;
import org.eclipse.mylyn.tasks.tests.connector.MockRepositoryConnector;
import org.xml.sax.SAXParseException;

/**
 * @author Steffen Pingel
 */
public class TaskDataExternalizerTest extends TestCase {

	private TaskDataExternalizer externalizer;

	private TaskRepository repository;

	@Override
	protected void setUp() throws Exception {
		TaskRepositoryManager taskRepositoryManager = new TaskRepositoryManager();
		taskRepositoryManager.addRepositoryConnector(new MockRepositoryConnector());
		repository = new TaskRepository(MockRepositoryConnector.CONNECTOR_KIND, MockRepositoryConnector.REPOSITORY_URL);
		taskRepositoryManager.addRepository(repository);
		externalizer = new TaskDataExternalizer(taskRepositoryManager);
	}

	public void testRead() throws Exception {
		File file = CommonTestUtil.getFile(this, "testdata/taskdata-1.0-bug-219897.zip");
		ZipInputStream in = new ZipInputStream(new FileInputStream(file));
		try {
			in.getNextEntry();
			@SuppressWarnings("unused")
			ITaskDataWorkingCopy state = externalizer.readState(in);
		} finally {
			in.close();
		}
	}

	public void testReadWrite() throws Exception {
		File file = CommonTestUtil.getFile(this, "testdata/taskdata-1.0-bug-219897.zip");
		ZipInputStream in = new ZipInputStream(new FileInputStream(file));
		ITaskDataWorkingCopy state;
		try {
			in.getNextEntry();
			state = externalizer.readState(in);
		} finally {
			in.close();
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		externalizer.writeState(out, state);
		TaskDataState state2 = externalizer.readState(new ByteArrayInputStream(out.toByteArray()));
		assertEquals(state.getConnectorKind(), state2.getConnectorKind());
		assertEquals(state.getRepositoryUrl(), state2.getRepositoryUrl());
		assertEquals(state.getTaskId(), state2.getTaskId());

		assertEquals(state.getRepositoryData().getRoot().toString(), state2.getRepositoryData().getRoot().toString());
	}

	public void testReadWriteInvalidCharacters() throws Exception {
		TaskData data = new TaskData(new TaskAttributeMapper(repository), repository.getConnectorKind(),
				repository.getRepositoryUrl(), "1");
		data.getRoot().createAttribute("attribute").setValue("\u0001\u001F");

		TaskDataState state = new TaskDataState(repository.getConnectorKind(), repository.getRepositoryUrl(), "1");
		state.setRepositoryData(data);

		ByteArrayOutputStream out = new ByteArrayOutputStream();
		externalizer.writeState(out, state);
		try {
			externalizer.readState(new ByteArrayInputStream(out.toByteArray()));
			fail("Expected SAXParseException");
		} catch (SAXParseException expected) {
		}

		TaskDataState state2 = externalizer.readState(new Xml11InputStream(new ByteArrayInputStream(out.toByteArray())));
		assertEquals(state.getRepositoryData().getRoot().toString(), state2.getRepositoryData().getRoot().toString());
		assertEquals("\u0001\u001F", state2.getRepositoryData().getRoot().getAttribute("attribute").getValue());
	}

}

Back to the top