Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8a179583418cc88115ce08a7612275b2248da1ce (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*******************************************************************************
1 * Copyright (c) 2012, 2015 Tasktop Technologies and others.
 * 
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * https://www.eclipse.org/legal/epl-2.0
 * 
 * SPDX-License-Identifier: EPL-2.0
 *
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.gerrit.ui;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Arrays;

import org.eclipse.mylyn.commons.workbench.EditorHandle;
import org.eclipse.mylyn.internal.gerrit.core.GerritConnector;
import org.eclipse.mylyn.internal.gerrit.ui.editor.GerritTaskEditorPage;
import org.eclipse.mylyn.reviews.core.model.IFileItem;
import org.eclipse.mylyn.reviews.core.model.IReview;
import org.eclipse.mylyn.reviews.core.model.IReviewItemSet;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.ui.TasksUi;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.junit.Test;

/**
 * @author Steffen Pingel
 */
public class GerritUrlHandlerTest {

	private final TaskRepository repository = new TaskRepository(GerritConnector.CONNECTOR_KIND,
			"http://review.mylyn.org");

	private final GerritUrlHandler handler = new GerritUrlHandler();

	private IWorkbenchPage page;

	private String taskId;

	private int patchSetNumber;

	private String path;

	@Test
	public void testGetTaskId() {
		assertEquals("123", handler.getTaskId(repository, "http://review.mylyn.org/123"));
	}

	@Test
	public void testGetTaskIdTrailingSlashAfterId() {
		assertEquals("123", handler.getTaskId(repository, "http://review.mylyn.org/123/foo/bar"));
	}

	@Test
	public void testGetTaskIdInvalidId() {
		assertEquals(null, handler.getTaskId(repository, "http://review.mylyn.org/ab123"));
	}

	@Test
	public void testGetTaskIdRepositoryMismatch() {
		assertEquals(null, handler.getTaskId(repository, "http://mylyn.org/reviews/123"));
	}

	@Test
	public void testGetTaskIdSubPath() {
		assertEquals("123", handler.getTaskId(repository, "http://review.mylyn.org/123"));
	}

	@Test
	public void testGetTaskIdTrailingSlash() {
		TaskRepository trailingSlashRepository = new TaskRepository(GerritConnector.CONNECTOR_KIND,
				"http://review.mylyn.org/");
		assertEquals("123", handler.getTaskId(trailingSlashRepository, "http://review.mylyn.org/123"));
	}

	@Test
	public void testGetTaskIdAbsolute() {
		assertEquals("123", handler.getTaskId(repository, "http://review.mylyn.org/#/c/123"));
	}

	@Test
	public void testGetTaskIdLetters() {
		assertEquals(null, handler.getTaskId(repository, "http://review.mylyn.org/#/c/abc/"));
	}

	@Test
	public void testGetTaskIdEmpty() {
		assertEquals(null, handler.getTaskId(repository, "http://review.mylyn.org/#/c//"));
	}

	@Test
	public void testGetTaskIdAbsoluteTrailingSlash() {
		assertEquals("123", handler.getTaskId(repository, "http://review.mylyn.org/#/c/123/"));
	}

	@Test
	public void testGetTaskIdPatchSet() {
		assertEquals("4698", handler.getTaskId(repository, "http://review.mylyn.org/#/c/4698/5"));
	}

	@Test
	public void testGetTaskIdFile() {
		assertEquals("4698", handler.getTaskId(repository, "http://review.mylyn.org/#/c/4698/5/foo/bar"));
	}

	@Test
	public void testGetPatchSetNumberPatchSet() {
		String url = "http://review.mylyn.org/#/c/4698/5";
		taskId = handler.getTaskId(repository, url);
		assertEquals(5, handler.getPatchSetNumber(repository, url, taskId));
	}

	@Test
	public void testGetPatchSetNumberPatchSetTrailingSlash() {
		String url = "http://review.mylyn.org/#/c/4698/5/";
		taskId = handler.getTaskId(repository, url);
		assertEquals(5, handler.getPatchSetNumber(repository, url, taskId));
	}

	@Test
	public void testGetPatchSetNumberPatchSetFile() {
		String url = "http://review.mylyn.org/#/c/4698/5/foo/bar";
		taskId = handler.getTaskId(repository, url);
		assertEquals(5, handler.getPatchSetNumber(repository, url, taskId));
	}

	@Test
	public void testGetPatchSetNumberNoneSpecified() {
		String url = "http://review.mylyn.org/#/c/4698";
		taskId = handler.getTaskId(repository, url);
		assertEquals(-1, handler.getPatchSetNumber(repository, url, taskId));
	}

	@Test
	public void testGetPatchSetNumberNoneSpecifiedTrailingSlash() {
		String url = "http://review.mylyn.org/#/c/4698/";
		taskId = handler.getTaskId(repository, url);
		assertEquals(-1, handler.getPatchSetNumber(repository, url, taskId));
	}

	@Test
	public void testGetPatchSetNumberNoneSpecifiedNotAnInteger() {
		String url = "http://review.mylyn.org/#/c/A1";
		taskId = handler.getTaskId(repository, url);
		assertEquals(-1, handler.getPatchSetNumber(repository, url, taskId));
	}

	@Test
	public void testGetPathNoneSpecified() {
		assertPath(null, "http://review.mylyn.org/#/c/4698/5");
	}

	@Test
	public void testGetPathNoneSpecifiedTrailingSlash() {
		assertPath(null, "http://review.mylyn.org/#/c/4698/5/");
	}

	@Test
	public void testGetPathNoneSpecifiedInvalidPatchNumber() {
		assertPath(null, "http://review.mylyn.org/#/c/4698/-1abcd");
	}

	@Test
	public void testGetPathNoneSpecifiedInvalidPatchNumberTrailingSlash() {
		assertPath(null, "http://review.mylyn.org/#/c/4698/-1abcd/");
	}

	@Test
	public void testGetPath() {
		assertPath("foo/bar.java", "http://review.mylyn.org/#/c/4698/5/foo/bar.java");
	}

	@Test
	public void testGetPathWithTrailingSlash() {
		assertPath("foo/bar.java", "http://review.mylyn.org/#/c/4698/5/foo/bar.java/");
	}

	@Test
	public void testOpenUrlWithInvalidReview() throws Exception {
		String url = "http://review.mylyn.org/#/c/4698/1/foo/bar.java";
		GerritUrlHandler spy = setUpOpenUrlTests(url);
		doReturn(null).when(spy).revealPatchSet(any(EditorHandle.class), anyInt());

		spy.openUrl(page, url, 0);

		verify(spy, times(1)).revealPatchSet(any(EditorHandle.class), anyInt());
		verify(spy, never()).getFileItem(any(IReview.class), anyInt(), anyString());
		verify(spy, never()).openCompareEditor(null);
	}

	@Test
	public void testOpenUrlWithValidPathOpenReview() throws Exception {
		String url = "http://review.mylyn.org/#/c/4698/1/foo/bar.java";
		GerritUrlHandler spy = setUpOpenUrlTests(url);
		GerritTaskEditorPage page = mock(GerritTaskEditorPage.class);
		IReview review = createMockReview();
		when(page.getReview()).thenReturn(review);
		doReturn(page).when(spy).revealPatchSet(any(EditorHandle.class), anyInt());
		openReviewTest(spy, url);

		verify(spy, times(1)).getFileItem(review, 1, "foo/bar.java");
		assertEquals(path, spy.getFileItem(review, patchSetNumber, path).getName());
	}

	@Test
	public void testOpenUrlWithInvalidPatchSetNumberOpenReview() throws Exception {
		String url = "http://review.mylyn.org/#/c/4698/5/foo/bar.java";
		GerritUrlHandler spy = setUpOpenUrlTests(url);
		GerritTaskEditorPage page = mock(GerritTaskEditorPage.class);
		IReview review = createMockReview();
		when(page.getReview()).thenReturn(review);
		doReturn(page).when(spy).revealPatchSet(any(EditorHandle.class), anyInt());
		openReviewTest(spy, url);

		verify(spy, times(1)).getFileItem(review, 5, "foo/bar.java");
		assertNull(spy.getFileItem(review, patchSetNumber, path));
	}

	@Test
	public void testOpenUrlWithInvalidPathOpenReview() throws Exception {
		String url = "http://review.mylyn.org/#/c/4698/1/foo/bar.jav";
		GerritUrlHandler spy = setUpOpenUrlTests(url);
		GerritTaskEditorPage page = mock(GerritTaskEditorPage.class);
		IReview review = createMockReview();
		when(page.getReview()).thenReturn(review);
		doReturn(page).when(spy).revealPatchSet(any(EditorHandle.class), anyInt());
		openReviewTest(spy, url);

		verify(spy, times(1)).getFileItem(review, 1, "foo/bar.jav");
		assertNull(spy.getFileItem(review, patchSetNumber, path));
	}

	private GerritUrlHandler setUpOpenUrlTests(String url) {
		page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		GerritUrlHandler spy = spy(handler);
		TasksUi.getRepositoryManager().addRepository(repository);
		taskId = spy.getTaskId(repository, url);
		patchSetNumber = spy.getPatchSetNumber(repository, url, taskId);
		path = spy.getFilePath(repository, url, taskId, patchSetNumber);

		return spy;
	}

	private IReview createMockReview() {
		IReview mockReview = mock(IReview.class);
		IReviewItemSet mockSet = mock(IReviewItemSet.class);
		IFileItem mockFile = mock(IFileItem.class);

		when(mockFile.getName()).thenReturn("foo/bar.java");
		when(mockSet.getId()).thenReturn("1");
		when(mockSet.getItems()).thenReturn(Arrays.asList(mockFile));
		when(mockReview.getSets()).thenReturn(Arrays.asList(mockSet));

		return mockReview;
	}

	private void openReviewTest(GerritUrlHandler spy, String url) {
		doNothing().when(spy).openCompareEditor(any(IFileItem.class));
		spy.openUrl(page, url, 0);

		verify(spy, times(1)).revealPatchSet(any(EditorHandle.class), anyInt());
		verify(spy, times(1)).openCompareEditor(any(IFileItem.class));
	}

	private void assertPath(String expectedPath, String testUrl) {
		taskId = handler.getTaskId(repository, testUrl);
		patchSetNumber = handler.getPatchSetNumber(repository, testUrl, taskId);
		assertEquals(expectedPath, handler.getFilePath(repository, testUrl, taskId, patchSetNumber));
	}
}

Back to the top