Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6c465454a46115505d6ab82889f9468fa0b19154 (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
/*******************************************************************************
 * Copyright (c) 2012 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.util.Map;

import junit.framework.TestCase;

import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper;
import org.eclipse.mylyn.tasks.core.data.TaskCommentMapper;
import org.eclipse.mylyn.tasks.core.data.TaskData;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

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

	TaskRepository taskRepository = new TaskRepository("kind", "repository");

	TaskAttributeMapper mapper = new TaskAttributeMapper(taskRepository);

	TaskData data = new TaskData(mapper, "kind", "repository", "id");

	public void testEqualsCommentId() {
		TaskCommentMapper comment = new TaskCommentMapper();
		comment.setCommentId("commentid");
		comment.setNumber(1);

		TaskAttribute attributeOld = data.getRoot().createAttribute("1");
		comment.applyTo(attributeOld);

		TaskAttribute attributeNew = data.getRoot().createAttribute("2");
		comment.applyTo(attributeNew);
		assertTrue(mapper.equals(attributeNew, attributeOld));

		comment.setCommentId("commentid2");
		comment.applyTo(attributeOld);
		assertFalse("Expected not equals:\n" + attributeOld + "\n" + attributeNew,
				mapper.equals(attributeNew, attributeOld));
	}

	public void testEqualsCommentMylyn37() {
		TaskCommentMapper comment = new TaskCommentMapper();
		comment.setNumber(1);

		// Mylyn 3.7 before release
		TaskAttribute attributeOld = data.getRoot().createAttribute("1");
		comment.applyTo(attributeOld);
		attributeOld.setValue("1");

		// Mylyn 3.7 release
		TaskAttribute attributeNew = data.getRoot().createAttribute("2");
		comment.applyTo(attributeNew);
		assertEquals("", attributeNew.getValue());

		assertTrue("Expected equals:\n" + attributeOld + "\n" + attributeNew, mapper.equals(attributeNew, attributeOld));
		assertFalse("Expected not equals:\n" + attributeOld + "\n" + attributeNew,
				mapper.equals(attributeOld, attributeNew));
	}

	public void testEqualsCommentMylyn37CommentId() {
		TaskCommentMapper comment = new TaskCommentMapper();
		comment.setNumber(1);
		comment.setCommentId("2");

		// Mylyn 3.7 before release, but missing sub-attribute
		TaskAttribute attributeOld = data.getRoot().createAttribute("1");
		comment.applyTo(attributeOld);
		attributeOld.setValue("1");

		// Mylyn 3.7 release
		TaskAttribute attributeNew = data.getRoot().createAttribute("2");
		comment.applyTo(attributeNew);
		assertEquals("2", attributeNew.getValue());

		assertFalse("Expected not equals:\n" + attributeOld + "\n" + attributeNew,
				mapper.equals(attributeNew, attributeOld));

		// Mylyn 3.7 before release with ID sub-attribute
		TaskAttribute idAttribute = attributeOld.createAttribute("task.common.comment.id");
		idAttribute.setValue("2");
		assertTrue("Expected equals:\n" + attributeOld + "\n" + attributeNew, mapper.equals(attributeNew, attributeOld));

		// IDs should now be unequal
		idAttribute.setValue("3");
		assertFalse("Expected not equals:\n" + attributeOld + "\n" + attributeNew,
				mapper.equals(attributeNew, attributeOld));
	}

	public void testGetValueLabels() throws Exception {
		TaskAttributeMapper mapper = mapperWith2Options();
		TaskAttribute attribute = data.getRoot().createAttribute("id");

		assertEquals(ImmutableList.of(), mapper.getValueLabels(attribute));

		attribute.setValue("1");
		assertEquals(ImmutableList.of("a"), mapper.getValueLabels(attribute));

		attribute.setValue("2");
		assertEquals(ImmutableList.of("b"), mapper.getValueLabels(attribute));

		attribute = data.getRoot().createAttribute("id");
		attribute.addValue("1");
		attribute.addValue("2");
		assertEquals(ImmutableList.of("a", "b"), mapper.getValueLabels(attribute));

		attribute = data.getRoot().createAttribute("id");
		attribute.addValue("2");
		attribute.addValue("1");
		assertEquals(ImmutableList.of("b", "a"), mapper.getValueLabels(attribute));
	}

	public void testGetValueLabelsUnmappedOption() throws Exception {
		TaskAttributeMapper mapper = mapperWith2Options();
		TaskAttribute attribute = attributeWithUnmappedOption();

		assertEquals(ImmutableList.of(), mapper.getValueLabels(attribute));

		attribute.setValue("1");
		assertEquals(ImmutableList.of("a"), mapper.getValueLabels(attribute));

		attribute.setValue("unMappedOption");
		assertEquals(ImmutableList.of("unMappedOptionLabel"), mapper.getValueLabels(attribute));

		attribute = attributeWithUnmappedOption();
		attribute.addValue("unMappedOption");
		attribute.addValue("2");
		assertEquals(ImmutableList.of("unMappedOptionLabel", "b"), mapper.getValueLabels(attribute));

		attribute = attributeWithUnmappedOption();
		attribute.addValue("2");
		attribute.addValue("unMappedOption");
		assertEquals(ImmutableList.of("b", "unMappedOptionLabel"), mapper.getValueLabels(attribute));
	}

	private TaskAttributeMapper mapperWith2Options() {
		TaskAttributeMapper mapper = new TaskAttributeMapper(taskRepository) {
			@Override
			public Map<String, String> getOptions(TaskAttribute attribute) {
				return ImmutableMap.of("1", "a", "2", "b");
			}
		};
		return mapper;
	}

	private TaskAttribute attributeWithUnmappedOption() {
		TaskAttribute attribute;
		attribute = data.getRoot().createAttribute("id");
		attribute.putOption("unMappedOption", "unMappedOptionLabel");
		return attribute;
	}

}

Back to the top