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] 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);