diff options
2 files changed, 331 insertions, 121 deletions
diff --git a/org.eclipse.mylyn.bugzilla.tests/src/org/eclipse/mylyn/bugzilla/tests/ui/BugzillaTaskHyperlinkDetectorTest.java b/org.eclipse.mylyn.bugzilla.tests/src/org/eclipse/mylyn/bugzilla/tests/ui/BugzillaTaskHyperlinkDetectorTest.java index 49a2c83dd..f28a29c62 100644 --- a/org.eclipse.mylyn.bugzilla.tests/src/org/eclipse/mylyn/bugzilla/tests/ui/BugzillaTaskHyperlinkDetectorTest.java +++ b/org.eclipse.mylyn.bugzilla.tests/src/org/eclipse/mylyn/bugzilla/tests/ui/BugzillaTaskHyperlinkDetectorTest.java @@ -7,6 +7,7 @@ * * Contributors: * Tasktop Technologies - initial API and implementation + * Frank Becker - improvements *******************************************************************************/ package org.eclipse.mylyn.bugzilla.tests.ui; @@ -22,15 +23,18 @@ import org.eclipse.mylyn.internal.bugzilla.core.BugzillaCorePlugin; import org.eclipse.mylyn.internal.tasks.core.TaskRepositoryManager; import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin; import org.eclipse.mylyn.internal.tasks.ui.editors.TaskHyperlinkDetector; +import org.eclipse.mylyn.tasks.core.ITask; import org.eclipse.mylyn.tasks.core.TaskRepository; import org.eclipse.mylyn.tasks.core.data.TaskAttribute; import org.eclipse.mylyn.tasks.ui.TaskHyperlink; +import org.eclipse.mylyn.tasks.ui.TasksUi; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Shell; /** * @author Rob Elves * @author Terry Hon + * @author Frank Becker */ public class BugzillaTaskHyperlinkDetectorTest extends TestCase { @@ -44,73 +48,98 @@ public class BugzillaTaskHyperlinkDetectorTest extends TestCase { private static final String ATTACHMENT_NEW = "Created attachment" + ATTACHMENT_NUMBER; - private final String TASK_FORMAT_1 = "task#1"; + private final String TASK_FORMAT_1 = "task#123"; - private final String TASK_FORMAT_2 = "task# 1"; + private final String TASK_FORMAT_2 = "task# 123"; - private final String TASK_FORMAT_3 = "task1"; + private final String TASK_FORMAT_3 = "task123"; - private final String TASK_FORMAT_4 = "task #1"; + private final String TASK_FORMAT_4 = "task #123"; - private final String BUG_FORMAT_1 = "bug# 1"; + private final String BUG_FORMAT_1 = "bug# 123"; - private final String BUG_FORMAT_2 = "bug # 1"; + private final String BUG_FORMAT_2 = "bug # 123"; - private final String BUG_FORMAT_3 = "bug1"; + private final String BUG_FORMAT_3 = "bug123"; - private final String BUG_FORMAT_4 = "bug #1"; + private final String BUG_FORMAT_4 = "bug #123"; private final String BUG_FORMAT_1_2 = "bug# 2"; private final String TASK_FORMAT_1_COMMENT_1 = "task#123c#44556677"; - private final String TASK_FORMAT_2_COMMENT_1 = "task# 1c#44556677"; + private final String TASK_FORMAT_2_COMMENT_1 = "task# 123c#44556677"; - private final String TASK_FORMAT_3_COMMENT_1 = "task1c#44556677"; + private final String TASK_FORMAT_3_COMMENT_1 = "task123c#44556677"; - private final String TASK_FORMAT_4_COMMENT_1 = "task #1c#44556677"; + private final String TASK_FORMAT_4_COMMENT_1 = "task #123c#44556677"; - private final String BUG_FORMAT_1_COMMENT_1 = "bug# 1c#44556677"; + private final String BUG_FORMAT_1_COMMENT_1 = "bug# 123c#44556677"; - private final String BUG_FORMAT_2_COMMENT_1 = "bug # 1c#44556677"; + private final String BUG_FORMAT_2_COMMENT_1 = "bug # 123c#44556677"; - private final String BUG_FORMAT_3_COMMENT_1 = "bug1c#44556677"; + private final String BUG_FORMAT_3_COMMENT_1 = "bug123c#44556677"; - private final String BUG_FORMAT_4_COMMENT_1 = "bug #1c#44556677"; + private final String BUG_FORMAT_4_COMMENT_1 = "bug #123c#44556677"; private final String TASK_FORMAT_1_COMMENT_2 = "task#123 comment #44556677"; - private final String TASK_FORMAT_2_COMMENT_2 = "task# 1 comment #44556677"; + private final String TASK_FORMAT_2_COMMENT_2 = "task# 123 comment #44556677"; - private final String TASK_FORMAT_3_COMMENT_2 = "task1 comment #44556677"; + private final String TASK_FORMAT_3_COMMENT_2 = "task123 comment #44556677"; - private final String TASK_FORMAT_4_COMMENT_2 = "task #1 comment #44556677"; + private final String TASK_FORMAT_4_COMMENT_2 = "task #123 comment #44556677"; - private final String BUG_FORMAT_1_COMMENT_2 = "bug# 1 comment #44556677"; + private final String BUG_FORMAT_1_COMMENT_2 = "bug# 123 comment #44556677"; - private final String BUG_FORMAT_2_COMMENT_2 = "bug # 1 comment #44556677"; + private final String BUG_FORMAT_2_COMMENT_2 = "bug # 123 comment #44556677"; - private final String BUG_FORMAT_3_COMMENT_2 = "bug1 comment #44556677"; + private final String BUG_FORMAT_3_COMMENT_2 = "bug123 comment #44556677"; - private final String BUG_FORMAT_4_COMMENT_2 = "bug #1 comment #44556677"; + private final String BUG_FORMAT_4_COMMENT_2 = "bug #123 comment #44556677"; private final String TASK_FORMAT_1_COMMENT_3 = "task#123 comment#44556677"; - private final String TASK_FORMAT_2_COMMENT_3 = "task# 1 comment#44556677"; + private final String TASK_FORMAT_2_COMMENT_3 = "task# 123 comment#44556677"; - private final String TASK_FORMAT_3_COMMENT_3 = "task1 comment#44556677"; + private final String TASK_FORMAT_3_COMMENT_3 = "task123 comment#44556677"; - private final String TASK_FORMAT_4_COMMENT_3 = "task #1 comment#44556677"; + private final String TASK_FORMAT_4_COMMENT_3 = "task #123 comment#44556677"; - private final String BUG_FORMAT_1_COMMENT_3 = "bug# 1 comment#44556677"; + private final String BUG_FORMAT_1_COMMENT_3 = "bug# 123 comment#44556677"; - private final String BUG_FORMAT_2_COMMENT_3 = "bug # 1 comment#44556677"; + private final String BUG_FORMAT_2_COMMENT_3 = "bug # 123 comment#44556677"; - private final String BUG_FORMAT_3_COMMENT_3 = "bug1 comment#44556677"; + private final String BUG_FORMAT_3_COMMENT_3 = "bug123 comment#44556677"; - private final String BUG_FORMAT_4_COMMENT_3 = "bug #1 comment#44556677"; + private final String BUG_FORMAT_4_COMMENT_3 = "bug #123 comment#44556677"; + + private final String TASK_FORMAT_1_COMMENT_4 = "task#123 comment # 44556677"; + + private final String TASK_FORMAT_2_COMMENT_4 = "task# 123 comment # 44556677"; + + private final String TASK_FORMAT_3_COMMENT_4 = "task123 comment # 44556677"; + + private final String TASK_FORMAT_4_COMMENT_4 = "task #123 comment # 44556677"; + + private final String BUG_FORMAT_1_COMMENT_4 = "bug# 123 comment # 44556677"; + + private final String BUG_FORMAT_2_COMMENT_4 = "bug # 123 comment # 44556677"; + + private final String BUG_FORMAT_3_COMMENT_4 = "bug123 comment # 44556677"; + + private final String BUG_FORMAT_4_COMMENT_4 = "bug #123 comment # 44556677"; + + private final String COMMENT_1 = "comment#44556677"; + + private final String COMMENT_2 = "comment #44556677"; + + private final String COMMENT_3 = "comment # 44556677"; + + private final String COMMENT_4 = "comment# 44556677"; + + private final String COMMENT_5 = "comment 44556677"; - //private BugzillaTaskHyperlinkDetector detector = new BugzillaTaskHyperlinkDetector(); private TaskHyperlinkDetector detector; private TaskRepository repository1; @@ -119,16 +148,20 @@ public class BugzillaTaskHyperlinkDetectorTest extends TestCase { private TextViewer viewer; - private String[] formats; - private String[] commentFormats; + private String[] bugFormats; + + private String[] bugCommentFormats; + private TaskRepositoryManager repositoryManager; private Shell shell; protected TaskRepository activeRepository; + protected ITask task; + @Override protected void setUp() throws Exception { super.setUp(); @@ -137,11 +170,16 @@ public class BugzillaTaskHyperlinkDetectorTest extends TestCase { repository1 = new TaskRepository(BugzillaCorePlugin.CONNECTOR_KIND, "repository_url1"); repository2 = new TaskRepository(BugzillaCorePlugin.CONNECTOR_KIND, "repository_url2"); - + task = TasksUi.getRepositoryModel().createTask(repository1, "123"); detector.setContext(new IAdaptable() { @SuppressWarnings("rawtypes") public Object getAdapter(Class adapter) { - return (adapter == TaskRepository.class) ? activeRepository : null; + if (adapter == TaskRepository.class) { + return activeRepository; + } else if (adapter == ITask.class) { + return task; + } + return null; } }); setRepository(repository1); @@ -152,15 +190,18 @@ public class BugzillaTaskHyperlinkDetectorTest extends TestCase { repositoryManager = TasksUiPlugin.getRepositoryManager(); repositoryManager.clearRepositories(TasksUiPlugin.getDefault().getRepositoriesFilePath()); - formats = new String[] { TASK_FORMAT_1, TASK_FORMAT_2, TASK_FORMAT_3, TASK_FORMAT_4, BUG_FORMAT_1, + commentFormats = new String[] { COMMENT_1, COMMENT_2, COMMENT_3, COMMENT_4 }; + bugFormats = new String[] { TASK_FORMAT_1, TASK_FORMAT_2, TASK_FORMAT_3, TASK_FORMAT_4, BUG_FORMAT_1, BUG_FORMAT_2, BUG_FORMAT_3, BUG_FORMAT_4 }; - commentFormats = new String[] { TASK_FORMAT_1_COMMENT_1, TASK_FORMAT_2_COMMENT_1, TASK_FORMAT_3_COMMENT_1, + bugCommentFormats = new String[] { TASK_FORMAT_1_COMMENT_1, TASK_FORMAT_2_COMMENT_1, TASK_FORMAT_3_COMMENT_1, TASK_FORMAT_4_COMMENT_1, BUG_FORMAT_1_COMMENT_1, BUG_FORMAT_2_COMMENT_1, BUG_FORMAT_3_COMMENT_1, BUG_FORMAT_4_COMMENT_1, TASK_FORMAT_1_COMMENT_2, TASK_FORMAT_2_COMMENT_2, TASK_FORMAT_3_COMMENT_2, TASK_FORMAT_4_COMMENT_2, BUG_FORMAT_1_COMMENT_2, BUG_FORMAT_2_COMMENT_2, BUG_FORMAT_3_COMMENT_2, BUG_FORMAT_4_COMMENT_2, TASK_FORMAT_1_COMMENT_3, TASK_FORMAT_2_COMMENT_3, TASK_FORMAT_3_COMMENT_3, TASK_FORMAT_4_COMMENT_3, BUG_FORMAT_1_COMMENT_3, BUG_FORMAT_2_COMMENT_3, BUG_FORMAT_3_COMMENT_3, - BUG_FORMAT_4_COMMENT_3 }; + BUG_FORMAT_4_COMMENT_3, TASK_FORMAT_1_COMMENT_4, TASK_FORMAT_2_COMMENT_4, TASK_FORMAT_3_COMMENT_4, + TASK_FORMAT_4_COMMENT_4, BUG_FORMAT_1_COMMENT_4, BUG_FORMAT_2_COMMENT_4, BUG_FORMAT_3_COMMENT_4, + BUG_FORMAT_4_COMMENT_4 }; } private void setRepository(final TaskRepository repository) { @@ -178,17 +219,35 @@ public class BugzillaTaskHyperlinkDetectorTest extends TestCase { } public void testBeginningWithSpecialChars() { - for (String format : formats) { + for (String format : bugFormats) { String testString = "First line\n:" + format + " is at the beginning"; viewer.setDocument(new Document(testString)); Region region = new Region(0, testString.length()); IHyperlink[] links = detector.detectHyperlinks(viewer, region, false); assertNotNull(links); assertEquals(1, links.length); - assertEquals(testString.indexOf(format), links[0].getHyperlinkRegion().getOffset()); - Object comment = ((TaskHyperlink) links[0]).getSelection(); + assertTrue(links[0] instanceof TaskHyperlink); + TaskHyperlink taskLink = (TaskHyperlink) links[0]; + assertEquals("123", taskLink.getTaskId()); + assertEquals(testString.indexOf(format), taskLink.getHyperlinkRegion().getOffset()); + Object comment = taskLink.getSelection(); assertNull(comment); } + for (String format : bugCommentFormats) { + String testString = "First line\n:" + format + " is at the beginning"; + viewer.setDocument(new Document(testString)); + Region region = new Region(0, testString.length()); + IHyperlink[] links = detector.detectHyperlinks(viewer, region, false); + assertNotNull(links); + assertEquals(1, links.length); + assertTrue(links[0] instanceof TaskHyperlink); + TaskHyperlink taskLink = (TaskHyperlink) links[0]; + assertEquals("123", taskLink.getTaskId()); + assertEquals(testString.indexOf(format), taskLink.getHyperlinkRegion().getOffset()); + Object comment = taskLink.getSelection(); + assertNotNull(comment); + assertEquals(TaskAttribute.PREFIX_COMMENT + "44556677", comment); + } for (String format : commentFormats) { String testString = "First line\n:" + format + " is at the beginning"; viewer.setDocument(new Document(testString)); @@ -196,25 +255,46 @@ public class BugzillaTaskHyperlinkDetectorTest extends TestCase { IHyperlink[] links = detector.detectHyperlinks(viewer, region, false); assertNotNull(links); assertEquals(1, links.length); - assertEquals(testString.indexOf(format), links[0].getHyperlinkRegion().getOffset()); - Object comment = ((TaskHyperlink) links[0]).getSelection(); + assertTrue(links[0] instanceof TaskHyperlink); + TaskHyperlink taskLink = (TaskHyperlink) links[0]; + assertEquals("123", taskLink.getTaskId()); + assertEquals(testString.indexOf(format), taskLink.getHyperlinkRegion().getOffset()); + Object comment = taskLink.getSelection(); assertNotNull(comment); assertEquals(TaskAttribute.PREFIX_COMMENT + "44556677", comment); } } public void testBeginningOfSecondLine() { - for (String format : formats) { + for (String format : bugFormats) { String testString = "First line\n" + format + " is at the beginning"; viewer.setDocument(new Document(testString)); Region region = new Region(0, testString.length()); IHyperlink[] links = detector.detectHyperlinks(viewer, region, false); assertNotNull(links); assertEquals(1, links.length); - assertEquals(testString.indexOf(format), links[0].getHyperlinkRegion().getOffset()); - Object comment = ((TaskHyperlink) links[0]).getSelection(); + assertTrue(links[0] instanceof TaskHyperlink); + TaskHyperlink taskLink = (TaskHyperlink) links[0]; + assertEquals("123", taskLink.getTaskId()); + assertEquals(testString.indexOf(format), taskLink.getHyperlinkRegion().getOffset()); + Object comment = taskLink.getSelection(); assertNull(comment); } + for (String format : bugCommentFormats) { + String testString = "First line\n" + format + " is at the beginning"; + viewer.setDocument(new Document(testString)); + Region region = new Region(0, testString.length()); + IHyperlink[] links = detector.detectHyperlinks(viewer, region, false); + assertNotNull(links); + assertEquals(1, links.length); + assertTrue(links[0] instanceof TaskHyperlink); + TaskHyperlink taskLink = (TaskHyperlink) links[0]; + assertEquals("123", taskLink.getTaskId()); + assertEquals(testString.indexOf(format), taskLink.getHyperlinkRegion().getOffset()); + Object comment = taskLink.getSelection(); + assertNotNull(comment); + assertEquals(TaskAttribute.PREFIX_COMMENT + "44556677", comment); + } for (String format : commentFormats) { String testString = "First line\n" + format + " is at the beginning"; viewer.setDocument(new Document(testString)); @@ -222,25 +302,46 @@ public class BugzillaTaskHyperlinkDetectorTest extends TestCase { IHyperlink[] links = detector.detectHyperlinks(viewer, region, false); assertNotNull(links); assertEquals(1, links.length); - assertEquals(testString.indexOf(format), links[0].getHyperlinkRegion().getOffset()); - Object comment = ((TaskHyperlink) links[0]).getSelection(); + assertTrue(links[0] instanceof TaskHyperlink); + TaskHyperlink taskLink = (TaskHyperlink) links[0]; + assertEquals("123", taskLink.getTaskId()); + assertEquals(testString.indexOf(format), taskLink.getHyperlinkRegion().getOffset()); + Object comment = taskLink.getSelection(); assertNotNull(comment); assertEquals(TaskAttribute.PREFIX_COMMENT + "44556677", comment); } } public void testBeginningOfSecondLineWithisWhitespace() { - for (String format : formats) { + for (String format : bugFormats) { String testString = "First line\n \t " + format + " is at the beginning"; viewer.setDocument(new Document(testString)); Region region = new Region(0, testString.length()); IHyperlink[] links = detector.detectHyperlinks(viewer, region, false); assertNotNull(links); assertEquals(1, links.length); - assertEquals(testString.indexOf(format), links[0].getHyperlinkRegion().getOffset()); - Object comment = ((TaskHyperlink) links[0]).getSelection(); + assertTrue(links[0] instanceof TaskHyperlink); + TaskHyperlink taskLink = (TaskHyperlink) links[0]; + assertEquals("123", taskLink.getTaskId()); + assertEquals(testString.indexOf(format), taskLink.getHyperlinkRegion().getOffset()); + Object comment = taskLink.getSelection(); assertNull(comment); } + for (String format : bugCommentFormats) { + String testString = "First line\n \t " + format + " is at the beginning"; + viewer.setDocument(new Document(testString)); + Region region = new Region(0, testString.length()); + IHyperlink[] links = detector.detectHyperlinks(viewer, region, false); + assertNotNull(links); + assertEquals(1, links.length); + assertTrue(links[0] instanceof TaskHyperlink); + TaskHyperlink taskLink = (TaskHyperlink) links[0]; + assertEquals("123", taskLink.getTaskId()); + assertEquals(testString.indexOf(format), taskLink.getHyperlinkRegion().getOffset()); + Object comment = taskLink.getSelection(); + assertNotNull(comment); + assertEquals(TaskAttribute.PREFIX_COMMENT + "44556677", comment); + } for (String format : commentFormats) { String testString = "First line\n \t " + format + " is at the beginning"; viewer.setDocument(new Document(testString)); @@ -248,25 +349,46 @@ public class BugzillaTaskHyperlinkDetectorTest extends TestCase { IHyperlink[] links = detector.detectHyperlinks(viewer, region, false); assertNotNull(links); assertEquals(1, links.length); - assertEquals(testString.indexOf(format), links[0].getHyperlinkRegion().getOffset()); - Object comment = ((TaskHyperlink) links[0]).getSelection(); + assertTrue(links[0] instanceof TaskHyperlink); + TaskHyperlink taskLink = (TaskHyperlink) links[0]; + assertEquals("123", taskLink.getTaskId()); + assertEquals(testString.indexOf(format), taskLink.getHyperlinkRegion().getOffset()); + Object comment = taskLink.getSelection(); assertNotNull(comment); assertEquals(TaskAttribute.PREFIX_COMMENT + "44556677", comment); } } public void testBeginning() { - for (String format : formats) { + for (String format : bugFormats) { String testString = format + " is at the beginning"; viewer.setDocument(new Document(testString)); Region region = new Region(0, testString.length()); IHyperlink[] links = detector.detectHyperlinks(viewer, region, false); assertNotNull(links); assertEquals(1, links.length); - assertEquals(testString.indexOf(format), links[0].getHyperlinkRegion().getOffset()); - Object comment = ((TaskHyperlink) links[0]).getSelection(); + assertTrue(links[0] instanceof TaskHyperlink); + TaskHyperlink taskLink = (TaskHyperlink) links[0]; + assertEquals("123", taskLink.getTaskId()); + assertEquals(testString.indexOf(format), taskLink.getHyperlinkRegion().getOffset()); + Object comment = taskLink.getSelection(); assertNull(comment); } + for (String format : bugCommentFormats) { + String testString = format + " is at the beginning"; + viewer.setDocument(new Document(testString)); + Region region = new Region(0, testString.length()); + IHyperlink[] links = detector.detectHyperlinks(viewer, region, false); + assertNotNull(links); + assertEquals(1, links.length); + assertTrue(links[0] instanceof TaskHyperlink); + TaskHyperlink taskLink = (TaskHyperlink) links[0]; + assertEquals("123", taskLink.getTaskId()); + assertEquals(testString.indexOf(format), taskLink.getHyperlinkRegion().getOffset()); + Object comment = taskLink.getSelection(); + assertNotNull(comment); + assertEquals(TaskAttribute.PREFIX_COMMENT + "44556677", comment); + } for (String format : commentFormats) { String testString = format + " is at the beginning"; viewer.setDocument(new Document(testString)); @@ -274,15 +396,18 @@ public class BugzillaTaskHyperlinkDetectorTest extends TestCase { IHyperlink[] links = detector.detectHyperlinks(viewer, region, false); assertNotNull(links); assertEquals(1, links.length); - assertEquals(testString.indexOf(format), links[0].getHyperlinkRegion().getOffset()); - Object comment = ((TaskHyperlink) links[0]).getSelection(); + assertTrue(links[0] instanceof TaskHyperlink); + TaskHyperlink taskLink = (TaskHyperlink) links[0]; + assertEquals("123", taskLink.getTaskId()); + assertEquals(testString.indexOf(format), taskLink.getHyperlinkRegion().getOffset()); + Object comment = taskLink.getSelection(); assertNotNull(comment); assertEquals(TaskAttribute.PREFIX_COMMENT + "44556677", comment); } } public void testEnd() { - for (String format : formats) { + for (String format : bugFormats) { String testString = "is ends with " + format; viewer.setDocument(new Document(testString)); int i = testString.indexOf(format); @@ -290,10 +415,29 @@ public class BugzillaTaskHyperlinkDetectorTest extends TestCase { IHyperlink[] links = detector.detectHyperlinks(viewer, region, false); assertNotNull(links); assertEquals(1, links.length); - assertEquals(i, links[0].getHyperlinkRegion().getOffset()); - Object comment = ((TaskHyperlink) links[0]).getSelection(); + assertTrue(links[0] instanceof TaskHyperlink); + TaskHyperlink taskLink = (TaskHyperlink) links[0]; + assertEquals("123", taskLink.getTaskId()); + assertEquals(testString.indexOf(format), taskLink.getHyperlinkRegion().getOffset()); + Object comment = taskLink.getSelection(); assertNull(comment); } + for (String format : bugCommentFormats) { + String testString = "is ends with " + format; + viewer.setDocument(new Document(testString)); + int i = testString.indexOf(format); + Region region = new Region(i, testString.length() - i); + IHyperlink[] links = detector.detectHyperlinks(viewer, region, false); + assertNotNull(links); + assertEquals(1, links.length); + assertTrue(links[0] instanceof TaskHyperlink); + TaskHyperlink taskLink = (TaskHyperlink) links[0]; + assertEquals("123", taskLink.getTaskId()); + assertEquals(testString.indexOf(format), taskLink.getHyperlinkRegion().getOffset()); + Object comment = taskLink.getSelection(); + assertNotNull(comment); + assertEquals(TaskAttribute.PREFIX_COMMENT + "44556677", comment); + } for (String format : commentFormats) { String testString = "is ends with " + format; viewer.setDocument(new Document(testString)); @@ -301,15 +445,18 @@ public class BugzillaTaskHyperlinkDetectorTest extends TestCase { IHyperlink[] links = detector.detectHyperlinks(viewer, region, false); assertNotNull(links); assertEquals(1, links.length); - assertEquals(testString.indexOf(format), links[0].getHyperlinkRegion().getOffset()); - Object comment = ((TaskHyperlink) links[0]).getSelection(); + assertTrue(links[0] instanceof TaskHyperlink); + TaskHyperlink taskLink = (TaskHyperlink) links[0]; + assertEquals("123", taskLink.getTaskId()); + assertEquals(testString.indexOf(format), taskLink.getHyperlinkRegion().getOffset()); + Object comment = taskLink.getSelection(); assertNotNull(comment); assertEquals(TaskAttribute.PREFIX_COMMENT + "44556677", comment); } } public void testMiddle() { - for (String format : formats) { + for (String format : bugFormats) { String testString = "is a " + format + " in the middle"; viewer.setDocument(new Document(testString)); int i = testString.indexOf(format); @@ -317,10 +464,28 @@ public class BugzillaTaskHyperlinkDetectorTest extends TestCase { IHyperlink[] links = detector.detectHyperlinks(viewer, region, false); assertNotNull(links); assertEquals(1, links.length); - assertEquals(testString.indexOf(format), links[0].getHyperlinkRegion().getOffset()); - Object comment = ((TaskHyperlink) links[0]).getSelection(); + assertTrue(links[0] instanceof TaskHyperlink); + TaskHyperlink taskLink = (TaskHyperlink) links[0]; + assertEquals("123", taskLink.getTaskId()); + assertEquals(testString.indexOf(format), taskLink.getHyperlinkRegion().getOffset()); + Object comment = taskLink.getSelection(); assertNull(comment); } + for (String format : bugCommentFormats) { + String testString = "is a " + format + " in the middle"; + viewer.setDocument(new Document(testString)); + Region region = new Region(0, testString.length()); + IHyperlink[] links = detector.detectHyperlinks(viewer, region, false); + assertNotNull(links); + assertEquals(1, links.length); + assertTrue(links[0] instanceof TaskHyperlink); + TaskHyperlink taskLink = (TaskHyperlink) links[0]; + assertEquals("123", taskLink.getTaskId()); + assertEquals(testString.indexOf(format), taskLink.getHyperlinkRegion().getOffset()); + Object comment = taskLink.getSelection(); + assertNotNull(comment); + assertEquals(TaskAttribute.PREFIX_COMMENT + "44556677", comment); + } for (String format : commentFormats) { String testString = "is a " + format + " in the middle"; viewer.setDocument(new Document(testString)); @@ -328,8 +493,11 @@ public class BugzillaTaskHyperlinkDetectorTest extends TestCase { IHyperlink[] links = detector.detectHyperlinks(viewer, region, false); assertNotNull(links); assertEquals(1, links.length); - assertEquals(testString.indexOf(format), links[0].getHyperlinkRegion().getOffset()); - Object comment = ((TaskHyperlink) links[0]).getSelection(); + assertTrue(links[0] instanceof TaskHyperlink); + TaskHyperlink taskLink = (TaskHyperlink) links[0]; + assertEquals("123", taskLink.getTaskId()); + assertEquals(testString.indexOf(format), taskLink.getHyperlinkRegion().getOffset()); + Object comment = taskLink.getSelection(); assertNotNull(comment); assertEquals(TaskAttribute.PREFIX_COMMENT + "44556677", comment); } @@ -473,7 +641,6 @@ public class BugzillaTaskHyperlinkDetectorTest extends TestCase { public void testAttachmentOld() { String testString = ATTACHMENT_OLD; viewer.setDocument(new Document(testString)); - int i = testString.indexOf(ATTACHMENT_NUMBER); Region region = new Region(0, testString.length()); IHyperlink[] links = detector.detectHyperlinks(viewer, region, false); assertNotNull(links); @@ -484,7 +651,6 @@ public class BugzillaTaskHyperlinkDetectorTest extends TestCase { public void testAttachmentNew() { String testString = ATTACHMENT_NEW; viewer.setDocument(new Document(testString)); - int i = testString.indexOf(ATTACHMENT_NUMBER); Region region = new Region(0, testString.length()); IHyperlink[] links = detector.detectHyperlinks(viewer, region, false); assertNotNull(links); diff --git a/org.eclipse.mylyn.bugzilla.ui/src/org/eclipse/mylyn/internal/bugzilla/ui/tasklist/BugzillaConnectorUi.java b/org.eclipse.mylyn.bugzilla.ui/src/org/eclipse/mylyn/internal/bugzilla/ui/tasklist/BugzillaConnectorUi.java index ec6468170..fe575da9d 100644 --- a/org.eclipse.mylyn.bugzilla.ui/src/org/eclipse/mylyn/internal/bugzilla/ui/tasklist/BugzillaConnectorUi.java +++ b/org.eclipse.mylyn.bugzilla.ui/src/org/eclipse/mylyn/internal/bugzilla/ui/tasklist/BugzillaConnectorUi.java @@ -8,6 +8,7 @@ * Contributors: * Tasktop Technologies - initial API and implementation * Eugene Kuleshov - improvements + * Frank Becker - improvements *******************************************************************************/ package org.eclipse.mylyn.internal.bugzilla.ui.tasklist; @@ -48,18 +49,24 @@ import org.eclipse.mylyn.tasks.ui.wizards.RepositoryQueryWizard; /** * @author Mik Kersten + * @author Robert Elves + * @author Frank Becker */ public class BugzillaConnectorUi extends AbstractRepositoryConnectorUi { - private static final int TASK_NUM_GROUP = 5; + private static final int GET_TASK_NUM_GROUP = 8; - private static final int COMMENT_NUM_GROUP = 10; + private static final int GET_COMMENT_NUM_GROUP = 13; - private static final int ATTACHMENT_NUM_GROUP = 14; + private static final int GET_ATTACHMENT_NUM_GROUP = 3; - private static final String regexp = "(?:(duplicate of|(\\W||^)+bug|(\\W|^)+task)( ?#? ?)(\\d+)((\\s)*(comment|c)(\\s#|#|#\\s|\\s|)(\\s\\d+|\\d+))?)|(?:(Created (an )?attachment\\s*(\\(id=)?(\\d+)))"; //$NON-NLS-1$ + private static final String regexp_bug = "(((duplicate of|((\\W||^)+(bug|task)))( ?#? ?)(\\d+))?((\\W||\\s)*(comment|c)??(\\s*#\\s*)(\\d+))?)"; //$NON-NLS-1$ - private static final Pattern PATTERN = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE); + private static final String regexp_attachment = "Created (an )?attachment\\s*(\\(id=)?(\\d+)"; //$NON-NLS-1$ + + private static final Pattern PATTERN_BUG = Pattern.compile(regexp_bug, Pattern.CASE_INSENSITIVE); + + private static final Pattern PATTERN_ATTACHMENT = Pattern.compile(regexp_attachment, Pattern.CASE_INSENSITIVE); @Override public String getAccountCreationUrl(TaskRepository taskRepository) { @@ -122,26 +129,6 @@ public class BugzillaConnectorUi extends AbstractRepositoryConnectorUi { } @Override - public IHyperlink[] findHyperlinks(TaskRepository repository, String text, int index, int textOffset) { - ArrayList<IHyperlink> hyperlinksFound = null; - - Matcher m = PATTERN.matcher(text); - while (m.find()) { - if (index == -1 || (index >= m.start() && index <= m.end())) { - IHyperlink link = extractHyperlink(repository, textOffset, m); - if (link != null) { - if (hyperlinksFound == null) { - hyperlinksFound = new ArrayList<IHyperlink>(); - } - hyperlinksFound.add(link); - } - } - } - - return (hyperlinksFound != null) ? hyperlinksFound.toArray(new IHyperlink[0]) : null; - } - - @Override public String getTaskKindLabel(ITask repositoryTask) { return IBugzillaConstants.BUGZILLA_TASK_KIND; } @@ -191,14 +178,47 @@ public class BugzillaConnectorUi extends AbstractRepositoryConnectorUi { return custom != null && custom.equals(Boolean.TRUE.toString()); } - private static IHyperlink extractHyperlink(TaskRepository repository, int regionOffset, Matcher m) { + @Override + public IWizardPage getTaskAttachmentPage(TaskAttachmentModel model) { + return new BugzillaTaskAttachmentPage(model); + } - int start = -1; + @Override + public IHyperlink[] findHyperlinks(TaskRepository repository, ITask task, String text, int index, int textOffset) { + ArrayList<IHyperlink> hyperlinksFound = null; + Matcher mb = PATTERN_BUG.matcher(text); + while (mb.find()) { + if (index == -1 || (index >= mb.start() && index <= mb.end())) { + IHyperlink link = extractHyperlinkBug(repository, task, textOffset, mb); + if (link != null) { + if (hyperlinksFound == null) { + hyperlinksFound = new ArrayList<IHyperlink>(); + } + hyperlinksFound.add(link); + } + } + } + Matcher ma = PATTERN_ATTACHMENT.matcher(text); + while (ma.find()) { + if (index == -1 || (index >= ma.start() && index <= ma.end())) { + IHyperlink link = extractHyperlinkAttachment(repository, textOffset, ma); + if (link != null) { + if (hyperlinksFound == null) { + hyperlinksFound = new ArrayList<IHyperlink>(); + } + hyperlinksFound.add(link); + } + } + } + + return (hyperlinksFound != null) ? hyperlinksFound.toArray(new IHyperlink[0]) : null; + } - int a = m.groupCount(); + private static IHyperlink extractHyperlinkBug(TaskRepository repository, ITask task, int regionOffset, Matcher m) { + int start = m.start(); if (m.group().startsWith("duplicate")) { //$NON-NLS-1$ - start = m.start() + m.group().indexOf(m.group(TASK_NUM_GROUP)); + start = m.start() + m.group().indexOf(m.group(GET_TASK_NUM_GROUP)); } else { start = m.start(); for (int index = 0; index < m.group().length() && !Character.isLetter(m.group().charAt(index)); index++, start++) { @@ -211,38 +231,62 @@ public class BugzillaConnectorUi extends AbstractRepositoryConnectorUi { end = m.group().length(); } - try { - start += regionOffset; - end += regionOffset; - - String bugId = m.group(TASK_NUM_GROUP); - if (bugId == null) { - String attachmentId = m.group(ATTACHMENT_NUM_GROUP); - if (attachmentId != null) { - start = start + m.group().indexOf(m.group(ATTACHMENT_NUM_GROUP)); - - IRegion sregion = new Region(start, end - start); - return new TaskAttachmentHyperlink(sregion, repository, attachmentId); + start += regionOffset; + end += regionOffset; + String bugId = m.group(GET_TASK_NUM_GROUP); + if (bugId == null) { + String commentId = m.group(GET_COMMENT_NUM_GROUP); + IRegion sregion = new Region(start, end - start); + if (commentId != null) { + String taskID; + if (task != null) { + taskID = task.getTaskId(); + } else { + taskID = ""; //$NON-NLS-1$ } - } else { - bugId.trim(); - IRegion sregion = new Region(start, end - start); - TaskHyperlink taskHyperLink = new TaskHyperlink(sregion, repository, bugId); - String commentId = m.group(COMMENT_NUM_GROUP); + TaskHyperlink taskHyperLink = new TaskHyperlink(sregion, repository, taskID); if (commentId != null) { taskHyperLink.setSelection(TaskAttribute.PREFIX_COMMENT + commentId); } return taskHyperLink; - } - } catch (NumberFormatException e) { + } + } else { + bugId.trim(); + IRegion sregion = new Region(start, end - start); + TaskHyperlink taskHyperLink = new TaskHyperlink(sregion, repository, bugId); + String commentId = m.group(GET_COMMENT_NUM_GROUP); + if (commentId != null) { + taskHyperLink.setSelection(TaskAttribute.PREFIX_COMMENT + commentId); + } + return taskHyperLink; } return null; } - @Override - public IWizardPage getTaskAttachmentPage(TaskAttachmentModel model) { - return new BugzillaTaskAttachmentPage(model); - } + private static IHyperlink extractHyperlinkAttachment(TaskRepository repository, int regionOffset, Matcher m) { + int start = -1; + start = m.start(); + for (int index = 0; index < m.group().length() && !Character.isLetter(m.group().charAt(index)); index++, start++) { + } + + int end = m.end(); + + if (end == -1) { + end = m.group().length(); + } + + start += regionOffset; + end += regionOffset; + + String attachmentId = m.group(GET_ATTACHMENT_NUM_GROUP); + if (attachmentId != null) { + start = start + m.group().indexOf(m.group(GET_ATTACHMENT_NUM_GROUP)); + + IRegion sregion = new Region(start, end - start); + return new TaskAttachmentHyperlink(sregion, repository, attachmentId); + } + return null; + } } |