From aae0586200f131a124db927e92fef3a0e767db6a Mon Sep 17 00:00:00 2001 From: Bernard Labno Date: Mon, 5 Nov 2012 12:23:53 +0000 Subject: [PATCH] Safer URI building - adjust Filter.toString --- src/main/java/pl/com/it_crowd/youtrack/api/Filter.java | 15 ++------------- src/test/java/pl/com/it_crowd/youtrack/api/rest/FilterTest.java | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------------------------------------------- src/test/java/pl/com/it_crowd/youtrack/api/rest/URIUtilsTest.java | 22 +++++++++++----------- 3 files changed, 79 insertions(+), 104 deletions(-) diff --git a/src/main/java/pl/com/it_crowd/youtrack/api/Filter.java b/src/main/java/pl/com/it_crowd/youtrack/api/Filter.java index b0721c5..6431592 100644 --- a/src/main/java/pl/com/it_crowd/youtrack/api/Filter.java +++ b/src/main/java/pl/com/it_crowd/youtrack/api/Filter.java @@ -4,8 +4,6 @@ import pl.com.it_crowd.youtrack.api.defaults.DateValues; import pl.com.it_crowd.youtrack.api.defaults.Fields; import pl.com.it_crowd.youtrack.api.defaults.StateValues; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; @@ -106,23 +104,14 @@ public final class Filter { public String toString() { StringBuilder builder = new StringBuilder(); - String space; - try { - space = URLEncoder.encode(" ", "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } + String space = " "; for (Condition condition : conditions) { builder.append(space); if (condition.field != null) { builder.append(condition.field.getCommand()); builder.append(":"); } - try { - builder.append(URLEncoder.encode(condition.value, "UTF-8")); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } + builder.append(condition.value); } if (maxResults > 0) { builder.append("&max=").append(maxResults); diff --git a/src/test/java/pl/com/it_crowd/youtrack/api/rest/FilterTest.java b/src/test/java/pl/com/it_crowd/youtrack/api/rest/FilterTest.java index 0e71f20..7f49ad3 100644 --- a/src/test/java/pl/com/it_crowd/youtrack/api/rest/FilterTest.java +++ b/src/test/java/pl/com/it_crowd/youtrack/api/rest/FilterTest.java @@ -6,21 +6,7 @@ import pl.com.it_crowd.youtrack.api.Filter; import pl.com.it_crowd.youtrack.api.defaults.DateValues; import pl.com.it_crowd.youtrack.api.defaults.StateValues; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; - public class FilterTest { -// -------------------------- STATIC METHODS -------------------------- - - private static String encodeURL(String s) - { - try { - return URLEncoder.encode(s, "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - } - // -------------------------- OTHER METHODS -------------------------- @Test @@ -30,10 +16,10 @@ public class FilterTest { final String updaterJacek = "updated by:Jacek"; final String projectTST_QA = "project:TST_QA"; final String resolvedSunday = "resolved date:Sunday"; - final String updatedThreeWeeksAgo = "updated:" + encodeURL("{Three weeks ago}"); - final String createdLastMonth = "created:" + encodeURL("{Last month}"); - final String stateDuplicate = "state:" + encodeURL("Duplicate"); - final String space = encodeURL(" "); + final String updatedThreeWeeksAgo = "updated:{Three weeks ago}"; + final String createdLastMonth = "created:{Last month}"; + final String stateDuplicate = "state:Duplicate"; + final String space = " "; Assert.assertEquals(stateDuplicate + space + createdLastMonth, Filter.stateFilter(StateValues.Duplicate).created(DateValues.LastMonth).toString()); Assert.assertEquals(createdLastMonth + space + stateDuplicate, Filter.createdFilter(DateValues.LastMonth).state(StateValues.Duplicate).toString()); Assert.assertEquals(createdLastMonth + space + stateDuplicate + space + "description:pikachu", @@ -49,26 +35,26 @@ public class FilterTest { @Test public void createdFilter() { - Assert.assertEquals("created:" + encodeURL("2012-01-01"), Filter.createdFilter("2012-01-01").toString()); - Assert.assertEquals("created:" + encodeURL("{Last month}"), Filter.createdFilter(DateValues.LastMonth).toString()); - Assert.assertEquals("created:" + encodeURL("{This month}"), Filter.createdFilter(DateValues.ThisMonth).toString()); - Assert.assertEquals("created:" + encodeURL("{Two weeks ago}"), Filter.createdFilter(DateValues.TwoWeeksAgo).toString()); - Assert.assertEquals("created:" + encodeURL("{Three weeks ago}"), Filter.createdFilter(DateValues.ThreeWeeksAgo).toString()); - Assert.assertEquals("created:" + encodeURL("{Last week}"), Filter.createdFilter(DateValues.LastWeek).toString()); - Assert.assertEquals("created:" + encodeURL("{This week}"), Filter.createdFilter(DateValues.ThisWeek).toString()); - Assert.assertEquals("created:" + encodeURL("Saturday"), Filter.createdFilter(DateValues.Saturday).toString()); - Assert.assertEquals("created:" + encodeURL("Friday"), Filter.createdFilter(DateValues.Friday).toString()); - Assert.assertEquals("created:" + encodeURL("Thursday"), Filter.createdFilter(DateValues.Thursday).toString()); - Assert.assertEquals("created:" + encodeURL("Wednesday"), Filter.createdFilter(DateValues.Wednesday).toString()); - Assert.assertEquals("created:" + encodeURL("Tuesday"), Filter.createdFilter(DateValues.Tuesday).toString()); - Assert.assertEquals("created:" + encodeURL("Monday"), Filter.createdFilter(DateValues.Monday).toString()); - Assert.assertEquals("created:" + encodeURL("Sunday"), Filter.createdFilter(DateValues.Sunday).toString()); + Assert.assertEquals("created:2012-01-01", Filter.createdFilter("2012-01-01").toString()); + Assert.assertEquals("created:{Last month}", Filter.createdFilter(DateValues.LastMonth).toString()); + Assert.assertEquals("created:{This month}", Filter.createdFilter(DateValues.ThisMonth).toString()); + Assert.assertEquals("created:{Two weeks ago}", Filter.createdFilter(DateValues.TwoWeeksAgo).toString()); + Assert.assertEquals("created:{Three weeks ago}", Filter.createdFilter(DateValues.ThreeWeeksAgo).toString()); + Assert.assertEquals("created:{Last week}", Filter.createdFilter(DateValues.LastWeek).toString()); + Assert.assertEquals("created:{This week}", Filter.createdFilter(DateValues.ThisWeek).toString()); + Assert.assertEquals("created:Saturday", Filter.createdFilter(DateValues.Saturday).toString()); + Assert.assertEquals("created:Friday", Filter.createdFilter(DateValues.Friday).toString()); + Assert.assertEquals("created:Thursday", Filter.createdFilter(DateValues.Thursday).toString()); + Assert.assertEquals("created:Wednesday", Filter.createdFilter(DateValues.Wednesday).toString()); + Assert.assertEquals("created:Tuesday", Filter.createdFilter(DateValues.Tuesday).toString()); + Assert.assertEquals("created:Monday", Filter.createdFilter(DateValues.Monday).toString()); + Assert.assertEquals("created:Sunday", Filter.createdFilter(DateValues.Sunday).toString()); } @Test public void descriptionFilter() { - Assert.assertEquals("description:" + encodeURL("Wacek Ramtamtamski's"), Filter.descriptionFilter("Wacek Ramtamtamski's").toString()); + Assert.assertEquals("description:Wacek Ramtamtamski's", Filter.descriptionFilter("Wacek Ramtamtamski's").toString()); } @Test @@ -86,64 +72,64 @@ public class FilterTest { @Test public void resolvedFilter() { - Assert.assertEquals("resolved date:" + encodeURL("2012-01-01"), Filter.resolvedFilter("2012-01-01").toString()); - Assert.assertEquals("resolved date:" + encodeURL("{Last month}"), Filter.resolvedFilter(DateValues.LastMonth).toString()); - Assert.assertEquals("resolved date:" + encodeURL("{This month}"), Filter.resolvedFilter(DateValues.ThisMonth).toString()); - Assert.assertEquals("resolved date:" + encodeURL("{Two weeks ago}"), Filter.resolvedFilter(DateValues.TwoWeeksAgo).toString()); - Assert.assertEquals("resolved date:" + encodeURL("{Three weeks ago}"), Filter.resolvedFilter(DateValues.ThreeWeeksAgo).toString()); - Assert.assertEquals("resolved date:" + encodeURL("{Last week}"), Filter.resolvedFilter(DateValues.LastWeek).toString()); - Assert.assertEquals("resolved date:" + encodeURL("{This week}"), Filter.resolvedFilter(DateValues.ThisWeek).toString()); - Assert.assertEquals("resolved date:" + encodeURL("Saturday"), Filter.resolvedFilter(DateValues.Saturday).toString()); - Assert.assertEquals("resolved date:" + encodeURL("Friday"), Filter.resolvedFilter(DateValues.Friday).toString()); - Assert.assertEquals("resolved date:" + encodeURL("Thursday"), Filter.resolvedFilter(DateValues.Thursday).toString()); - Assert.assertEquals("resolved date:" + encodeURL("Wednesday"), Filter.resolvedFilter(DateValues.Wednesday).toString()); - Assert.assertEquals("resolved date:" + encodeURL("Tuesday"), Filter.resolvedFilter(DateValues.Tuesday).toString()); - Assert.assertEquals("resolved date:" + encodeURL("Monday"), Filter.resolvedFilter(DateValues.Monday).toString()); - Assert.assertEquals("resolved date:" + encodeURL("Sunday"), Filter.resolvedFilter(DateValues.Sunday).toString()); + Assert.assertEquals("resolved date:2012-01-01", Filter.resolvedFilter("2012-01-01").toString()); + Assert.assertEquals("resolved date:{Last month}", Filter.resolvedFilter(DateValues.LastMonth).toString()); + Assert.assertEquals("resolved date:{This month}", Filter.resolvedFilter(DateValues.ThisMonth).toString()); + Assert.assertEquals("resolved date:{Two weeks ago}", Filter.resolvedFilter(DateValues.TwoWeeksAgo).toString()); + Assert.assertEquals("resolved date:{Three weeks ago}", Filter.resolvedFilter(DateValues.ThreeWeeksAgo).toString()); + Assert.assertEquals("resolved date:{Last week}", Filter.resolvedFilter(DateValues.LastWeek).toString()); + Assert.assertEquals("resolved date:{This week}", Filter.resolvedFilter(DateValues.ThisWeek).toString()); + Assert.assertEquals("resolved date:Saturday", Filter.resolvedFilter(DateValues.Saturday).toString()); + Assert.assertEquals("resolved date:Friday", Filter.resolvedFilter(DateValues.Friday).toString()); + Assert.assertEquals("resolved date:Thursday", Filter.resolvedFilter(DateValues.Thursday).toString()); + Assert.assertEquals("resolved date:Wednesday", Filter.resolvedFilter(DateValues.Wednesday).toString()); + Assert.assertEquals("resolved date:Tuesday", Filter.resolvedFilter(DateValues.Tuesday).toString()); + Assert.assertEquals("resolved date:Monday", Filter.resolvedFilter(DateValues.Monday).toString()); + Assert.assertEquals("resolved date:Sunday", Filter.resolvedFilter(DateValues.Sunday).toString()); } @Test public void stateFilter() { - Assert.assertEquals("state:" + encodeURL("{Can't Reproduce}"), Filter.stateFilter(StateValues.CantReproduce).toString()); - Assert.assertEquals("state:" + encodeURL("Verified"), Filter.stateFilter(StateValues.Verified).toString()); - Assert.assertEquals("state:" + encodeURL("Submitted"), Filter.stateFilter(StateValues.Submitted).toString()); - Assert.assertEquals("state:" + encodeURL("Reopened"), Filter.stateFilter(StateValues.Reopened).toString()); - Assert.assertEquals("state:" + encodeURL("{To be discussed}"), Filter.stateFilter(StateValues.ToBeDiscussed).toString()); - Assert.assertEquals("state:" + encodeURL("Duplicate"), Filter.stateFilter(StateValues.Duplicate).toString()); - Assert.assertEquals("state:" + encodeURL("Resolved"), Filter.stateFilter(StateValues.Resolved).toString()); - Assert.assertEquals("state:" + encodeURL("Fixed"), Filter.stateFilter(StateValues.Fixed).toString()); - Assert.assertEquals("state:" + encodeURL("Incomplete"), Filter.stateFilter(StateValues.Incomplete).toString()); - Assert.assertEquals("state:" + encodeURL("{In Progress}"), Filter.stateFilter(StateValues.InProgress).toString()); - Assert.assertEquals("state:" + encodeURL("New"), Filter.stateFilter(StateValues.New).toString()); - Assert.assertEquals("state:" + encodeURL("Obsolete"), Filter.stateFilter(StateValues.Obsolete).toString()); - Assert.assertEquals("state:" + encodeURL("Open"), Filter.stateFilter(StateValues.Open).toString()); - Assert.assertEquals("state:" + encodeURL("{Won't fix}"), Filter.stateFilter(StateValues.WontFix).toString()); - Assert.assertEquals("state:" + encodeURL("-{Won't fix}"), Filter.stateFilter(StateValues.NotWontFix).toString()); - Assert.assertEquals("state:" + encodeURL("-Verified"), Filter.stateFilter(StateValues.NotVerified).toString()); - Assert.assertEquals("state:" + encodeURL("-Verified,{Can't Reproduce},Obsolete,{Won't fix}"), + Assert.assertEquals("state:{Can't Reproduce}", Filter.stateFilter(StateValues.CantReproduce).toString()); + Assert.assertEquals("state:Verified", Filter.stateFilter(StateValues.Verified).toString()); + Assert.assertEquals("state:Submitted", Filter.stateFilter(StateValues.Submitted).toString()); + Assert.assertEquals("state:Reopened", Filter.stateFilter(StateValues.Reopened).toString()); + Assert.assertEquals("state:{To be discussed}", Filter.stateFilter(StateValues.ToBeDiscussed).toString()); + Assert.assertEquals("state:Duplicate", Filter.stateFilter(StateValues.Duplicate).toString()); + Assert.assertEquals("state:Resolved", Filter.stateFilter(StateValues.Resolved).toString()); + Assert.assertEquals("state:Fixed", Filter.stateFilter(StateValues.Fixed).toString()); + Assert.assertEquals("state:Incomplete", Filter.stateFilter(StateValues.Incomplete).toString()); + Assert.assertEquals("state:{In Progress}", Filter.stateFilter(StateValues.InProgress).toString()); + Assert.assertEquals("state:New", Filter.stateFilter(StateValues.New).toString()); + Assert.assertEquals("state:Obsolete", Filter.stateFilter(StateValues.Obsolete).toString()); + Assert.assertEquals("state:Open", Filter.stateFilter(StateValues.Open).toString()); + Assert.assertEquals("state:{Won't fix}", Filter.stateFilter(StateValues.WontFix).toString()); + Assert.assertEquals("state:-{Won't fix}", Filter.stateFilter(StateValues.NotWontFix).toString()); + Assert.assertEquals("state:-Verified", Filter.stateFilter(StateValues.NotVerified).toString()); + Assert.assertEquals("state:-Verified,{Can't Reproduce},Obsolete,{Won't fix}", Filter.stateFilter(StateValues.NotVerified, StateValues.CantReproduce, StateValues.Obsolete, StateValues.WontFix).toString()); } @Test public void updatedFilter() { - Assert.assertEquals("updated:" + encodeURL("Today"), Filter.updatedFilter(DateValues.Today).toString()); - Assert.assertEquals("updated:" + encodeURL("Yesterday"), Filter.updatedFilter(DateValues.Yesterday).toString()); - Assert.assertEquals("updated:" + encodeURL("2012-01-01"), Filter.updatedFilter("2012-01-01").toString()); - Assert.assertEquals("updated:" + encodeURL("{Last month}"), Filter.updatedFilter(DateValues.LastMonth).toString()); - Assert.assertEquals("updated:" + encodeURL("{This month}"), Filter.updatedFilter(DateValues.ThisMonth).toString()); - Assert.assertEquals("updated:" + encodeURL("{Two weeks ago}"), Filter.updatedFilter(DateValues.TwoWeeksAgo).toString()); - Assert.assertEquals("updated:" + encodeURL("{Three weeks ago}"), Filter.updatedFilter(DateValues.ThreeWeeksAgo).toString()); - Assert.assertEquals("updated:" + encodeURL("{Last week}"), Filter.updatedFilter(DateValues.LastWeek).toString()); - Assert.assertEquals("updated:" + encodeURL("{This week}"), Filter.updatedFilter(DateValues.ThisWeek).toString()); - Assert.assertEquals("updated:" + encodeURL("Saturday"), Filter.updatedFilter(DateValues.Saturday).toString()); - Assert.assertEquals("updated:" + encodeURL("Friday"), Filter.updatedFilter(DateValues.Friday).toString()); - Assert.assertEquals("updated:" + encodeURL("Thursday"), Filter.updatedFilter(DateValues.Thursday).toString()); - Assert.assertEquals("updated:" + encodeURL("Wednesday"), Filter.updatedFilter(DateValues.Wednesday).toString()); - Assert.assertEquals("updated:" + encodeURL("Tuesday"), Filter.updatedFilter(DateValues.Tuesday).toString()); - Assert.assertEquals("updated:" + encodeURL("Monday"), Filter.updatedFilter(DateValues.Monday).toString()); - Assert.assertEquals("updated:" + encodeURL("Sunday"), Filter.updatedFilter(DateValues.Sunday).toString()); + Assert.assertEquals("updated:Today", Filter.updatedFilter(DateValues.Today).toString()); + Assert.assertEquals("updated:Yesterday", Filter.updatedFilter(DateValues.Yesterday).toString()); + Assert.assertEquals("updated:2012-01-01", Filter.updatedFilter("2012-01-01").toString()); + Assert.assertEquals("updated:{Last month}", Filter.updatedFilter(DateValues.LastMonth).toString()); + Assert.assertEquals("updated:{This month}", Filter.updatedFilter(DateValues.ThisMonth).toString()); + Assert.assertEquals("updated:{Two weeks ago}", Filter.updatedFilter(DateValues.TwoWeeksAgo).toString()); + Assert.assertEquals("updated:{Three weeks ago}", Filter.updatedFilter(DateValues.ThreeWeeksAgo).toString()); + Assert.assertEquals("updated:{Last week}", Filter.updatedFilter(DateValues.LastWeek).toString()); + Assert.assertEquals("updated:{This week}", Filter.updatedFilter(DateValues.ThisWeek).toString()); + Assert.assertEquals("updated:Saturday", Filter.updatedFilter(DateValues.Saturday).toString()); + Assert.assertEquals("updated:Friday", Filter.updatedFilter(DateValues.Friday).toString()); + Assert.assertEquals("updated:Thursday", Filter.updatedFilter(DateValues.Thursday).toString()); + Assert.assertEquals("updated:Wednesday", Filter.updatedFilter(DateValues.Wednesday).toString()); + Assert.assertEquals("updated:Tuesday", Filter.updatedFilter(DateValues.Tuesday).toString()); + Assert.assertEquals("updated:Monday", Filter.updatedFilter(DateValues.Monday).toString()); + Assert.assertEquals("updated:Sunday", Filter.updatedFilter(DateValues.Sunday).toString()); } @Test diff --git a/src/test/java/pl/com/it_crowd/youtrack/api/rest/URIUtilsTest.java b/src/test/java/pl/com/it_crowd/youtrack/api/rest/URIUtilsTest.java index e38bded..bfc6730 100644 --- a/src/test/java/pl/com/it_crowd/youtrack/api/rest/URIUtilsTest.java +++ b/src/test/java/pl/com/it_crowd/youtrack/api/rest/URIUtilsTest.java @@ -13,20 +13,20 @@ import static org.junit.Assert.assertEquals; public class URIUtilsTest { // -------------------------- OTHER METHODS -------------------------- - @Test - public void buildURI() throws URISyntaxException - { + @Test + public void buildURI() throws URISyntaxException + { // Given - final URI base = new URI("http://localhost:8080/youtrack/"); - final String pathA = "/rest/admin/bundle/QA note types"; - final String queryA = "q=" + Filter.stateFilter(StateValues.NotWontFix).maxResults(20); + final URI base = new URI("http://localhost:8080/youtrack/"); + final String pathA = "/rest/admin/bundle/QA note types"; + final String queryA = "q=" + Filter.stateFilter(StateValues.NotWontFix).maxResults(20); // When - final URI uriA = URIUtils.buildURI(base, pathA); - final URI uriB = URIUtils.buildURI(base, pathA, queryA); + final URI uriA = URIUtils.buildURI(base, pathA); + final URI uriB = URIUtils.buildURI(base, pathA, queryA); // Then - assertEquals("http://localhost/youtrack/rest/admin/bundle/QA%20note%20types", uriA.toString()); - assertEquals("http://localhost/youtrack/rest/admin/bundle/QA%20note%20types?q=state:-%257BWon%2527t+fix%257D&max=20", uriB.toString()); - } + assertEquals("http://localhost/youtrack/rest/admin/bundle/QA%20note%20types", uriA.toString()); + assertEquals("http://localhost/youtrack/rest/admin/bundle/QA%20note%20types?q=state:-%7BWon't%20fix%7D&max=20", uriB.toString()); + } } -- libgit2 0.24.0