From dda6a4ec7842fc0367332aff5e0cd9ad4c41dfd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Krein=C3=B8e?= Date: Mon, 30 May 2016 12:45:39 +0200 Subject: [PATCH 1/3] Made searches return an empty SearchResult instance instead of throwing an exception on no result. This fixes #158 --- src/main/java/net/rcarz/jiraclient/Issue.java | 2 +- .../java/net/rcarz/jiraclient/SearchTest.java | 55 ++++++++++++++++++- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/rcarz/jiraclient/Issue.java b/src/main/java/net/rcarz/jiraclient/Issue.java index eafb6e8..039a05d 100644 --- a/src/main/java/net/rcarz/jiraclient/Issue.java +++ b/src/main/java/net/rcarz/jiraclient/Issue.java @@ -595,7 +595,7 @@ public class Issue extends Resource { // first call if (currentPage == null) { currentPage = getNextIssues().iterator(); - if (currentPage == null) { + if (currentPage == null || !currentPage.hasNext()) { return null; } else { return currentPage.next(); diff --git a/src/test/java/net/rcarz/jiraclient/SearchTest.java b/src/test/java/net/rcarz/jiraclient/SearchTest.java index 0c0ccac..942841c 100644 --- a/src/test/java/net/rcarz/jiraclient/SearchTest.java +++ b/src/test/java/net/rcarz/jiraclient/SearchTest.java @@ -2,9 +2,13 @@ package net.rcarz.jiraclient; import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import java.nio.channels.Pipe; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.NoSuchElementException; + +import static org.junit.Assert.*; public class SearchTest { @@ -21,6 +25,51 @@ public class SearchTest { assertEquals("and resolution Fixed", "Fixed", searchResult.issues.get(0).getResolution().getName()); } + @Test + public void testEmptySearchGivesEmptyResult() throws JiraException { + final JiraClient jira = new JiraClient("https://jira.atlassian.com/", null); + + //Valid jql query that will always yield no results. + final String q = "key = NotExisting-9999999 AND key = blah-8833772"; + Issue.SearchResult searchResult = jira.searchIssues(q); + + final String assertMsg = "Searches that yield no results, should return an empty " + + Issue.SearchResult.class.getSimpleName() + " instance"; + assertTrue(assertMsg, searchResult.issues.isEmpty()); + assertFalse(assertMsg, searchResult.issues.iterator().hasNext()); + assertEquals(assertMsg, 0, searchResult.total); + assertEquals(assertMsg, 0, searchResult.start); + } + + @Test + public void testSearchResultIteratorWithinMaxResultLimit() throws JiraException { + final JiraClient jira = new JiraClient("https://jira.atlassian.com/", null); + final int usedMax = 2; + //Will return everything from the public Jira for Jira + final Issue.SearchResult searchResult = jira.searchIssues("", usedMax); + final List iterResults = new ArrayList(usedMax); + final Iterator iterator = searchResult.iterator(); + for (int i = 0 ; i < usedMax ; i++) { + iterResults.add(iterator.next()); + } + assertEquals(searchResult.issues, iterResults); + } + + @Test + public void testIterateBeyondMaxResult() throws JiraException { + final JiraClient jira = new JiraClient("https://jira.atlassian.com/", null); + + //Will return everything from the public Jira for Jira (at the time of writing 163697 issues). + final int usedMax = 2; + Issue.SearchResult searchResult = jira.searchIssues("", usedMax); + final Iterator iterator = searchResult.iterator(); + System.out.println(searchResult.issues); + for (int i = 0 ; i < 3 ; i++) { + //Running this 3 times without failing, ensures the it can fetch issues beyond the first fetch batch size, as the used max result is only 2. + iterator.next(); + } + } + @Test public void testExpandingChangeLogInSearch() throws JiraException { JiraClient jira = new JiraClient("https://jira.atlassian.com/", null); From cadd67d17a943999ce260e972bf83ef9401e96b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Krein=C3=B8e?= Date: Mon, 30 May 2016 13:04:18 +0200 Subject: [PATCH 2/3] Added more detailed javadoc to Issue.SearchResult. This work is part of fixing #158 --- src/main/java/net/rcarz/jiraclient/Issue.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/java/net/rcarz/jiraclient/Issue.java b/src/main/java/net/rcarz/jiraclient/Issue.java index 039a05d..f26f591 100644 --- a/src/main/java/net/rcarz/jiraclient/Issue.java +++ b/src/main/java/net/rcarz/jiraclient/Issue.java @@ -658,6 +658,17 @@ public class Issue extends Resource { /** * Issue search results structure. + * + * The issues of the result can be retrived from this class in 2 ways. + * + * The first is to access the issues field directly. This is a list of Issue instances. + * Note however that this will only contain the issues fetched in the initial search, + * so its size will be the same as the max result value or below. + * + * The second way is to use the iterator methods. This will return an Iterator instance, + * that will iterate over every result of the search, even if there is more than the max + * result value. The price for this, is that the call to next has none determistic performence, + * as it sometimes need to fetch a new batch of issues from Jira. */ public static class SearchResult { public int start = 0; From 5af0411af5b9d55704204ab90f4e2b9c175ec742 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Krein=C3=B8e?= Date: Mon, 30 May 2016 13:06:22 +0200 Subject: [PATCH 3/3] Made Issue.SearchResult.iterator() return an Iterator instead of IssueIterator, as this is a private inner class, and can be accessed by clients anyway. This work is part of fixing #158 --- src/main/java/net/rcarz/jiraclient/Issue.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/rcarz/jiraclient/Issue.java b/src/main/java/net/rcarz/jiraclient/Issue.java index f26f591..00e4936 100644 --- a/src/main/java/net/rcarz/jiraclient/Issue.java +++ b/src/main/java/net/rcarz/jiraclient/Issue.java @@ -705,7 +705,7 @@ public class Issue extends Resource { * * @return All issues found. */ - public IssueIterator iterator() { + public Iterator iterator() { return issueIterator; } }