1 /*******************************************************************************
2 * Copyright (c) 2011 Benjamin Muskalla and others
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License 2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-2.0/
8 * SPDX-License-Identifier: EPL-2.0
11 * Benjamin Muskalla <benjamin.muskalla@tasktop.com> - initial implementation
12 *******************************************************************************/
13 package org
.eclipse
.egit
.internal
.mylyn
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertNull
;
18 import org
.eclipse
.egit
.internal
.mylyn
.ui
.CommitHyperlinkDetector
;
19 import org
.eclipse
.jface
.text
.Document
;
20 import org
.eclipse
.jface
.text
.Region
;
21 import org
.eclipse
.jface
.text
.TextViewer
;
22 import org
.eclipse
.jface
.text
.hyperlink
.IHyperlink
;
23 import org
.eclipse
.swt
.SWT
;
24 import org
.eclipse
.swt
.widgets
.Shell
;
25 import org
.junit
.Before
;
26 import org
.junit
.Test
;
27 import org
.junit
.runner
.RunWith
;
28 import org
.junit
.runners
.BlockJUnit4ClassRunner
;
30 @RunWith(BlockJUnit4ClassRunner
.class)
31 public class CommitHyperlinkDetectorTest
{
33 private static final String OTHER_EXAMPLE_ID
= "3de38c8898c74b867cb6f06f7907e0719d9d4c0c";
34 private static final String EXAMPLE_ID
= "2de0ab486c66566ae1ad36b73bfc9d99e14eb195";
35 private TextViewer textViewer
;
36 private CommitHyperlinkDetector detector
;
39 public void testNoDocument() {
40 textViewer
.setDocument(null);
41 IHyperlink
[] hyperlinks
= detectHyperlinks(0,0);
42 assertNull(hyperlinks
);
46 public void testBadLocation() {
47 textViewer
.setDocument(null);
48 IHyperlink
[] hyperlinks
= detectHyperlinks(10,0);
49 assertNull(hyperlinks
);
53 public void testEmpty() {
55 IHyperlink
[] hyperlinks
= detectHyperlinks();
56 assertNull(hyperlinks
);
60 public void testSimpleId() {
62 IHyperlink
[] hyperlinks
= detectHyperlinks();
63 assertEquals(1, hyperlinks
.length
);
64 assertEquals(EXAMPLE_ID
, hyperlinks
[0].getHyperlinkText());
68 public void testMultiId() {
69 setText(EXAMPLE_ID
+ " and " + OTHER_EXAMPLE_ID
);
70 IHyperlink
[] hyperlinks
= detectHyperlinks();
71 assertEquals(2, hyperlinks
.length
);
72 assertEquals(EXAMPLE_ID
, hyperlinks
[0].getHyperlinkText());
73 assertEquals(OTHER_EXAMPLE_ID
, hyperlinks
[1].getHyperlinkText());
77 public void testEndLine() {
78 setText("Merged as " + EXAMPLE_ID
);
79 IHyperlink
[] hyperlinks
= detectHyperlinks();
80 assertEquals(1, hyperlinks
.length
);
81 assertEquals(EXAMPLE_ID
, hyperlinks
[0].getHyperlinkText());
85 public void testMiddleLine() {
86 setText("Merged as " + EXAMPLE_ID
+ " and something else");
87 IHyperlink
[] hyperlinks
= detectHyperlinks();
88 assertEquals(1, hyperlinks
.length
);
89 assertEquals(EXAMPLE_ID
, hyperlinks
[0].getHyperlinkText());
93 public void testBeginSentence() {
94 setText("end of sentence." + EXAMPLE_ID
);
95 IHyperlink
[] hyperlinks
= detectHyperlinks();
96 assertEquals(1, hyperlinks
.length
);
97 assertEquals(EXAMPLE_ID
, hyperlinks
[0].getHyperlinkText());
101 public void testEndSentence() {
102 setText("Merged as " + EXAMPLE_ID
+ ".");
103 IHyperlink
[] hyperlinks
= detectHyperlinks();
104 assertEquals(1, hyperlinks
.length
);
105 assertEquals(EXAMPLE_ID
, hyperlinks
[0].getHyperlinkText());
109 public void testOffsetMiddle() {
111 IHyperlink
[] hyperlinks
= detectHyperlinks(3,0);
112 assertEquals(1, hyperlinks
.length
);
113 assertEquals(EXAMPLE_ID
, hyperlinks
[0].getHyperlinkText());
117 public void testOffsetOff() {
118 setText("some bla " + EXAMPLE_ID
);
119 IHyperlink
[] hyperlinks
= detectHyperlinks(3,0);
120 assertNull(hyperlinks
);
124 public void testMultiLine() {
125 setText("Test multi-line text\n" + EXAMPLE_ID
);
126 IHyperlink
[] hyperlinks
= detectHyperlinks(0,textViewer
.getDocument().getLength());
127 assertEquals(1, hyperlinks
.length
);
128 assertEquals(EXAMPLE_ID
, hyperlinks
[0].getHyperlinkText());
129 assertEquals(new Region(21, EXAMPLE_ID
.length()), hyperlinks
[0].getHyperlinkRegion());
133 public void testGerritId() {
134 setText("I" + EXAMPLE_ID
);
135 IHyperlink
[] hyperlinks
= detectHyperlinks(0,textViewer
.getDocument().getLength());
136 assertNull(hyperlinks
);
140 public void testGerritIdWithinText() {
141 setText("abc I" + EXAMPLE_ID
);
142 IHyperlink
[] hyperlinks
= detectHyperlinks(5,textViewer
.getDocument().getLength());
143 assertNull(hyperlinks
);
147 private IHyperlink
[] detectHyperlinks() {
148 return detectHyperlinks(0, textViewer
.getDocument().getLength());
151 private IHyperlink
[] detectHyperlinks(int offset
, int length
) {
152 return detector
.detectHyperlinks(textViewer
,
153 new Region(offset
, length
), false);
156 private void setText(String text
) {
157 textViewer
.getDocument().set(text
);
161 public void setUp() throws Exception
{
162 detector
= new CommitHyperlinkDetector();
163 Shell shell
= new Shell();
164 textViewer
= new TextViewer(shell
, SWT
.NONE
);
165 textViewer
.setDocument(new Document());