Skip to main content
summaryrefslogtreecommitdiffstats
blob: 950cf87fa37513738b23049c4bf67514d16a20d9 (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
/*******************************************************************************
 * Copyright (c) 2014 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
 *     Guy Perron 423242: Add ability to edit comment from compare navigator popup
 *******************************************************************************/

package org.eclipse.mylyn.reviews.tests.ui;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Iterator;

import junit.framework.TestCase;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.NotificationImpl;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.mylyn.internal.reviews.ui.annotations.CommentAnnotation;
import org.eclipse.mylyn.internal.reviews.ui.annotations.ReviewAnnotationModel;
import org.eclipse.mylyn.reviews.core.model.IComment;
import org.eclipse.mylyn.reviews.core.model.IFileVersion;
import org.eclipse.mylyn.reviews.core.model.ILineLocation;
import org.eclipse.mylyn.reviews.core.model.ILineRange;
import org.eclipse.mylyn.reviews.core.model.ILocation;
import org.eclipse.mylyn.reviews.core.model.IReviewItem;
import org.eclipse.mylyn.reviews.core.model.IReviewsFactory;
import org.eclipse.mylyn.reviews.core.model.IUser;
import org.eclipse.mylyn.reviews.ui.ReviewBehavior;
import org.eclipse.team.core.history.IFileRevision;

/**
 * @author Leo Dos Santos
 * @author Guy Perron
 */
public class ReviewAnnotationModelTest extends TestCase {

	private final static String DEFAULT_TEXT = "This change looks good.";

	private final static long DEFAULT_TIMESTAMP = 1388577600000L;

	private IDocument doc;

	private IReviewItem review;

	private ReviewAnnotationModel model;

	private class MockReviewBehaviour extends ReviewBehavior {

		public MockReviewBehaviour() {
			super(null);
		}

		@Override
		public IStatus addComment(IReviewItem fileItem, IComment comment, IProgressMonitor monitor) {
			return null;
		}

		@Override
		public IFileRevision getFileRevision(IFileVersion reviewFileVersion) {
			return null;
		}

		@Override
		public IStatus discardComment(IReviewItem fileItem, IComment comment, IProgressMonitor monitor) {
			return null;
		}

	}

	@Override
	protected void setUp() throws Exception {
		doc = new Document("A test document, nothing special.");
		review = IReviewsFactory.INSTANCE.createFileItem();
		generateComment(DEFAULT_TEXT, new Date(DEFAULT_TIMESTAMP));

		model = new ReviewAnnotationModel();
		model.setItem(review, new MockReviewBehaviour());
		model.connect(doc);
	}

	@Override
	protected void tearDown() throws Exception {
		model.disconnect(doc);
	}

	public void testConnect() {
		Iterator<CommentAnnotation> iter = model.getAnnotationIterator();
		assertEquals(1, getCount(iter));

		model.disconnect(doc);
		iter = model.getAnnotationIterator();
		assertEquals(0, getCount(iter));

		model.connect(doc);
		iter = model.getAnnotationIterator();
		assertEquals(1, getCount(iter));
	}

	public void testNotifyChanged() {
		Iterator<CommentAnnotation> iter = model.getAnnotationIterator();
		assertEquals(1, getCount(iter));

		// Comments sometimes come in with Dates and sometimes with Timestamps,
		// so we need to be able to handle both.
		IComment clone = generateComment(DEFAULT_TEXT, new Timestamp(DEFAULT_TIMESTAMP));
		Notification notification = new NotificationImpl(Notification.ADD, null, clone);
		model.getItem().eNotify(notification);

		iter = model.getAnnotationIterator();
		assertEquals(1, getCount(iter));

		IComment newComment = generateComment("Actually, maybe it needs more work.", new Date());
		notification = new NotificationImpl(Notification.ADD, null, newComment);
		model.getItem().eNotify(notification);

		iter = model.getAnnotationIterator();
		assertEquals(2, getCount(iter));

		notification = new NotificationImpl(Notification.REMOVE, newComment, null);
		model.getItem().eNotify(notification);

		iter = model.getAnnotationIterator();
		assertEquals(1, getCount(iter));

	}

	private int getCount(Iterator<CommentAnnotation> iter) {
		int count = 0;
		while (iter.hasNext()) {
			count++;
			iter.next();
		}
		return count;
	}

	private IUser generateUser() {
		IUser user = IReviewsFactory.INSTANCE.createUser();
		user.setId("1");
		user.setDisplayName("Leo Dos Santos");
		user.setEmail("leo@testkop.com");
		return user;
	}

	private IComment generateComment(String text, Date date) {
		IComment comment = IReviewsFactory.INSTANCE.createComment();
		comment.getLocations().add(generateLocation());
		comment.setDescription(text);
		comment.setCreationDate(date);
		comment.setDraft(false);
		comment.setId("12345");
		comment.setAuthor(generateUser());
		comment.setItem(review);
		return comment;
	}

	private ILocation generateLocation() {
		ILineRange range = IReviewsFactory.INSTANCE.createLineRange();
		range.setStart(0);
		range.setEnd(10);
		ILineLocation location = IReviewsFactory.INSTANCE.createLineLocation();
		location.getRanges().add(range);
		return location;
	}

}

Back to the top