Commit aae0586200f131a124db927e92fef3a0e767db6a

Authored by bernard
1 parent 1d7db5f9

Safer URI building - adjust Filter.toString

@@ -4,8 +4,6 @@ import pl.com.it_crowd.youtrack.api.defaults.DateValues; @@ -4,8 +4,6 @@ import pl.com.it_crowd.youtrack.api.defaults.DateValues;
4 import pl.com.it_crowd.youtrack.api.defaults.Fields; 4 import pl.com.it_crowd.youtrack.api.defaults.Fields;
5 import pl.com.it_crowd.youtrack.api.defaults.StateValues; 5 import pl.com.it_crowd.youtrack.api.defaults.StateValues;
6 6
7 -import java.io.UnsupportedEncodingException;  
8 -import java.net.URLEncoder;  
9 import java.util.ArrayList; 7 import java.util.ArrayList;
10 import java.util.List; 8 import java.util.List;
11 9
@@ -106,23 +104,14 @@ public final class Filter { @@ -106,23 +104,14 @@ public final class Filter {
106 public String toString() 104 public String toString()
107 { 105 {
108 StringBuilder builder = new StringBuilder(); 106 StringBuilder builder = new StringBuilder();
109 - String space;  
110 - try {  
111 - space = URLEncoder.encode(" ", "UTF-8");  
112 - } catch (UnsupportedEncodingException e) {  
113 - throw new RuntimeException(e);  
114 - } 107 + String space = " ";
115 for (Condition condition : conditions) { 108 for (Condition condition : conditions) {
116 builder.append(space); 109 builder.append(space);
117 if (condition.field != null) { 110 if (condition.field != null) {
118 builder.append(condition.field.getCommand()); 111 builder.append(condition.field.getCommand());
119 builder.append(":"); 112 builder.append(":");
120 } 113 }
121 - try {  
122 - builder.append(URLEncoder.encode(condition.value, "UTF-8"));  
123 - } catch (UnsupportedEncodingException e) {  
124 - throw new RuntimeException(e);  
125 - } 114 + builder.append(condition.value);
126 } 115 }
127 if (maxResults > 0) { 116 if (maxResults > 0) {
128 builder.append("&max=").append(maxResults); 117 builder.append("&max=").append(maxResults);
@@ -6,21 +6,7 @@ import pl.com.it_crowd.youtrack.api.Filter; @@ -6,21 +6,7 @@ import pl.com.it_crowd.youtrack.api.Filter;
6 import pl.com.it_crowd.youtrack.api.defaults.DateValues; 6 import pl.com.it_crowd.youtrack.api.defaults.DateValues;
7 import pl.com.it_crowd.youtrack.api.defaults.StateValues; 7 import pl.com.it_crowd.youtrack.api.defaults.StateValues;
8 8
9 -import java.io.UnsupportedEncodingException;  
10 -import java.net.URLEncoder;  
11 -  
12 public class FilterTest { 9 public class FilterTest {
13 -// -------------------------- STATIC METHODS --------------------------  
14 -  
15 - private static String encodeURL(String s)  
16 - {  
17 - try {  
18 - return URLEncoder.encode(s, "UTF-8");  
19 - } catch (UnsupportedEncodingException e) {  
20 - throw new RuntimeException(e);  
21 - }  
22 - }  
23 -  
24 // -------------------------- OTHER METHODS -------------------------- 10 // -------------------------- OTHER METHODS --------------------------
25 11
26 @Test 12 @Test
@@ -30,10 +16,10 @@ public class FilterTest { @@ -30,10 +16,10 @@ public class FilterTest {
30 final String updaterJacek = "updated by:Jacek"; 16 final String updaterJacek = "updated by:Jacek";
31 final String projectTST_QA = "project:TST_QA"; 17 final String projectTST_QA = "project:TST_QA";
32 final String resolvedSunday = "resolved date:Sunday"; 18 final String resolvedSunday = "resolved date:Sunday";
33 - final String updatedThreeWeeksAgo = "updated:" + encodeURL("{Three weeks ago}");  
34 - final String createdLastMonth = "created:" + encodeURL("{Last month}");  
35 - final String stateDuplicate = "state:" + encodeURL("Duplicate");  
36 - final String space = encodeURL(" "); 19 + final String updatedThreeWeeksAgo = "updated:{Three weeks ago}";
  20 + final String createdLastMonth = "created:{Last month}";
  21 + final String stateDuplicate = "state:Duplicate";
  22 + final String space = " ";
37 Assert.assertEquals(stateDuplicate + space + createdLastMonth, Filter.stateFilter(StateValues.Duplicate).created(DateValues.LastMonth).toString()); 23 Assert.assertEquals(stateDuplicate + space + createdLastMonth, Filter.stateFilter(StateValues.Duplicate).created(DateValues.LastMonth).toString());
38 Assert.assertEquals(createdLastMonth + space + stateDuplicate, Filter.createdFilter(DateValues.LastMonth).state(StateValues.Duplicate).toString()); 24 Assert.assertEquals(createdLastMonth + space + stateDuplicate, Filter.createdFilter(DateValues.LastMonth).state(StateValues.Duplicate).toString());
39 Assert.assertEquals(createdLastMonth + space + stateDuplicate + space + "description:pikachu", 25 Assert.assertEquals(createdLastMonth + space + stateDuplicate + space + "description:pikachu",
@@ -49,26 +35,26 @@ public class FilterTest { @@ -49,26 +35,26 @@ public class FilterTest {
49 @Test 35 @Test
50 public void createdFilter() 36 public void createdFilter()
51 { 37 {
52 - Assert.assertEquals("created:" + encodeURL("2012-01-01"), Filter.createdFilter("2012-01-01").toString());  
53 - Assert.assertEquals("created:" + encodeURL("{Last month}"), Filter.createdFilter(DateValues.LastMonth).toString());  
54 - Assert.assertEquals("created:" + encodeURL("{This month}"), Filter.createdFilter(DateValues.ThisMonth).toString());  
55 - Assert.assertEquals("created:" + encodeURL("{Two weeks ago}"), Filter.createdFilter(DateValues.TwoWeeksAgo).toString());  
56 - Assert.assertEquals("created:" + encodeURL("{Three weeks ago}"), Filter.createdFilter(DateValues.ThreeWeeksAgo).toString());  
57 - Assert.assertEquals("created:" + encodeURL("{Last week}"), Filter.createdFilter(DateValues.LastWeek).toString());  
58 - Assert.assertEquals("created:" + encodeURL("{This week}"), Filter.createdFilter(DateValues.ThisWeek).toString());  
59 - Assert.assertEquals("created:" + encodeURL("Saturday"), Filter.createdFilter(DateValues.Saturday).toString());  
60 - Assert.assertEquals("created:" + encodeURL("Friday"), Filter.createdFilter(DateValues.Friday).toString());  
61 - Assert.assertEquals("created:" + encodeURL("Thursday"), Filter.createdFilter(DateValues.Thursday).toString());  
62 - Assert.assertEquals("created:" + encodeURL("Wednesday"), Filter.createdFilter(DateValues.Wednesday).toString());  
63 - Assert.assertEquals("created:" + encodeURL("Tuesday"), Filter.createdFilter(DateValues.Tuesday).toString());  
64 - Assert.assertEquals("created:" + encodeURL("Monday"), Filter.createdFilter(DateValues.Monday).toString());  
65 - Assert.assertEquals("created:" + encodeURL("Sunday"), Filter.createdFilter(DateValues.Sunday).toString()); 38 + Assert.assertEquals("created:2012-01-01", Filter.createdFilter("2012-01-01").toString());
  39 + Assert.assertEquals("created:{Last month}", Filter.createdFilter(DateValues.LastMonth).toString());
  40 + Assert.assertEquals("created:{This month}", Filter.createdFilter(DateValues.ThisMonth).toString());
  41 + Assert.assertEquals("created:{Two weeks ago}", Filter.createdFilter(DateValues.TwoWeeksAgo).toString());
  42 + Assert.assertEquals("created:{Three weeks ago}", Filter.createdFilter(DateValues.ThreeWeeksAgo).toString());
  43 + Assert.assertEquals("created:{Last week}", Filter.createdFilter(DateValues.LastWeek).toString());
  44 + Assert.assertEquals("created:{This week}", Filter.createdFilter(DateValues.ThisWeek).toString());
  45 + Assert.assertEquals("created:Saturday", Filter.createdFilter(DateValues.Saturday).toString());
  46 + Assert.assertEquals("created:Friday", Filter.createdFilter(DateValues.Friday).toString());
  47 + Assert.assertEquals("created:Thursday", Filter.createdFilter(DateValues.Thursday).toString());
  48 + Assert.assertEquals("created:Wednesday", Filter.createdFilter(DateValues.Wednesday).toString());
  49 + Assert.assertEquals("created:Tuesday", Filter.createdFilter(DateValues.Tuesday).toString());
  50 + Assert.assertEquals("created:Monday", Filter.createdFilter(DateValues.Monday).toString());
  51 + Assert.assertEquals("created:Sunday", Filter.createdFilter(DateValues.Sunday).toString());
66 } 52 }
67 53
68 @Test 54 @Test
69 public void descriptionFilter() 55 public void descriptionFilter()
70 { 56 {
71 - Assert.assertEquals("description:" + encodeURL("Wacek Ramtamtamski's"), Filter.descriptionFilter("Wacek Ramtamtamski's").toString()); 57 + Assert.assertEquals("description:Wacek Ramtamtamski's", Filter.descriptionFilter("Wacek Ramtamtamski's").toString());
72 } 58 }
73 59
74 @Test 60 @Test
@@ -86,64 +72,64 @@ public class FilterTest { @@ -86,64 +72,64 @@ public class FilterTest {
86 @Test 72 @Test
87 public void resolvedFilter() 73 public void resolvedFilter()
88 { 74 {
89 - Assert.assertEquals("resolved date:" + encodeURL("2012-01-01"), Filter.resolvedFilter("2012-01-01").toString());  
90 - Assert.assertEquals("resolved date:" + encodeURL("{Last month}"), Filter.resolvedFilter(DateValues.LastMonth).toString());  
91 - Assert.assertEquals("resolved date:" + encodeURL("{This month}"), Filter.resolvedFilter(DateValues.ThisMonth).toString());  
92 - Assert.assertEquals("resolved date:" + encodeURL("{Two weeks ago}"), Filter.resolvedFilter(DateValues.TwoWeeksAgo).toString());  
93 - Assert.assertEquals("resolved date:" + encodeURL("{Three weeks ago}"), Filter.resolvedFilter(DateValues.ThreeWeeksAgo).toString());  
94 - Assert.assertEquals("resolved date:" + encodeURL("{Last week}"), Filter.resolvedFilter(DateValues.LastWeek).toString());  
95 - Assert.assertEquals("resolved date:" + encodeURL("{This week}"), Filter.resolvedFilter(DateValues.ThisWeek).toString());  
96 - Assert.assertEquals("resolved date:" + encodeURL("Saturday"), Filter.resolvedFilter(DateValues.Saturday).toString());  
97 - Assert.assertEquals("resolved date:" + encodeURL("Friday"), Filter.resolvedFilter(DateValues.Friday).toString());  
98 - Assert.assertEquals("resolved date:" + encodeURL("Thursday"), Filter.resolvedFilter(DateValues.Thursday).toString());  
99 - Assert.assertEquals("resolved date:" + encodeURL("Wednesday"), Filter.resolvedFilter(DateValues.Wednesday).toString());  
100 - Assert.assertEquals("resolved date:" + encodeURL("Tuesday"), Filter.resolvedFilter(DateValues.Tuesday).toString());  
101 - Assert.assertEquals("resolved date:" + encodeURL("Monday"), Filter.resolvedFilter(DateValues.Monday).toString());  
102 - Assert.assertEquals("resolved date:" + encodeURL("Sunday"), Filter.resolvedFilter(DateValues.Sunday).toString()); 75 + Assert.assertEquals("resolved date:2012-01-01", Filter.resolvedFilter("2012-01-01").toString());
  76 + Assert.assertEquals("resolved date:{Last month}", Filter.resolvedFilter(DateValues.LastMonth).toString());
  77 + Assert.assertEquals("resolved date:{This month}", Filter.resolvedFilter(DateValues.ThisMonth).toString());
  78 + Assert.assertEquals("resolved date:{Two weeks ago}", Filter.resolvedFilter(DateValues.TwoWeeksAgo).toString());
  79 + Assert.assertEquals("resolved date:{Three weeks ago}", Filter.resolvedFilter(DateValues.ThreeWeeksAgo).toString());
  80 + Assert.assertEquals("resolved date:{Last week}", Filter.resolvedFilter(DateValues.LastWeek).toString());
  81 + Assert.assertEquals("resolved date:{This week}", Filter.resolvedFilter(DateValues.ThisWeek).toString());
  82 + Assert.assertEquals("resolved date:Saturday", Filter.resolvedFilter(DateValues.Saturday).toString());
  83 + Assert.assertEquals("resolved date:Friday", Filter.resolvedFilter(DateValues.Friday).toString());
  84 + Assert.assertEquals("resolved date:Thursday", Filter.resolvedFilter(DateValues.Thursday).toString());
  85 + Assert.assertEquals("resolved date:Wednesday", Filter.resolvedFilter(DateValues.Wednesday).toString());
  86 + Assert.assertEquals("resolved date:Tuesday", Filter.resolvedFilter(DateValues.Tuesday).toString());
  87 + Assert.assertEquals("resolved date:Monday", Filter.resolvedFilter(DateValues.Monday).toString());
  88 + Assert.assertEquals("resolved date:Sunday", Filter.resolvedFilter(DateValues.Sunday).toString());
103 } 89 }
104 90
105 @Test 91 @Test
106 public void stateFilter() 92 public void stateFilter()
107 { 93 {
108 - Assert.assertEquals("state:" + encodeURL("{Can't Reproduce}"), Filter.stateFilter(StateValues.CantReproduce).toString());  
109 - Assert.assertEquals("state:" + encodeURL("Verified"), Filter.stateFilter(StateValues.Verified).toString());  
110 - Assert.assertEquals("state:" + encodeURL("Submitted"), Filter.stateFilter(StateValues.Submitted).toString());  
111 - Assert.assertEquals("state:" + encodeURL("Reopened"), Filter.stateFilter(StateValues.Reopened).toString());  
112 - Assert.assertEquals("state:" + encodeURL("{To be discussed}"), Filter.stateFilter(StateValues.ToBeDiscussed).toString());  
113 - Assert.assertEquals("state:" + encodeURL("Duplicate"), Filter.stateFilter(StateValues.Duplicate).toString());  
114 - Assert.assertEquals("state:" + encodeURL("Resolved"), Filter.stateFilter(StateValues.Resolved).toString());  
115 - Assert.assertEquals("state:" + encodeURL("Fixed"), Filter.stateFilter(StateValues.Fixed).toString());  
116 - Assert.assertEquals("state:" + encodeURL("Incomplete"), Filter.stateFilter(StateValues.Incomplete).toString());  
117 - Assert.assertEquals("state:" + encodeURL("{In Progress}"), Filter.stateFilter(StateValues.InProgress).toString());  
118 - Assert.assertEquals("state:" + encodeURL("New"), Filter.stateFilter(StateValues.New).toString());  
119 - Assert.assertEquals("state:" + encodeURL("Obsolete"), Filter.stateFilter(StateValues.Obsolete).toString());  
120 - Assert.assertEquals("state:" + encodeURL("Open"), Filter.stateFilter(StateValues.Open).toString());  
121 - Assert.assertEquals("state:" + encodeURL("{Won't fix}"), Filter.stateFilter(StateValues.WontFix).toString());  
122 - Assert.assertEquals("state:" + encodeURL("-{Won't fix}"), Filter.stateFilter(StateValues.NotWontFix).toString());  
123 - Assert.assertEquals("state:" + encodeURL("-Verified"), Filter.stateFilter(StateValues.NotVerified).toString());  
124 - Assert.assertEquals("state:" + encodeURL("-Verified,{Can't Reproduce},Obsolete,{Won't fix}"), 94 + Assert.assertEquals("state:{Can't Reproduce}", Filter.stateFilter(StateValues.CantReproduce).toString());
  95 + Assert.assertEquals("state:Verified", Filter.stateFilter(StateValues.Verified).toString());
  96 + Assert.assertEquals("state:Submitted", Filter.stateFilter(StateValues.Submitted).toString());
  97 + Assert.assertEquals("state:Reopened", Filter.stateFilter(StateValues.Reopened).toString());
  98 + Assert.assertEquals("state:{To be discussed}", Filter.stateFilter(StateValues.ToBeDiscussed).toString());
  99 + Assert.assertEquals("state:Duplicate", Filter.stateFilter(StateValues.Duplicate).toString());
  100 + Assert.assertEquals("state:Resolved", Filter.stateFilter(StateValues.Resolved).toString());
  101 + Assert.assertEquals("state:Fixed", Filter.stateFilter(StateValues.Fixed).toString());
  102 + Assert.assertEquals("state:Incomplete", Filter.stateFilter(StateValues.Incomplete).toString());
  103 + Assert.assertEquals("state:{In Progress}", Filter.stateFilter(StateValues.InProgress).toString());
  104 + Assert.assertEquals("state:New", Filter.stateFilter(StateValues.New).toString());
  105 + Assert.assertEquals("state:Obsolete", Filter.stateFilter(StateValues.Obsolete).toString());
  106 + Assert.assertEquals("state:Open", Filter.stateFilter(StateValues.Open).toString());
  107 + Assert.assertEquals("state:{Won't fix}", Filter.stateFilter(StateValues.WontFix).toString());
  108 + Assert.assertEquals("state:-{Won't fix}", Filter.stateFilter(StateValues.NotWontFix).toString());
  109 + Assert.assertEquals("state:-Verified", Filter.stateFilter(StateValues.NotVerified).toString());
  110 + Assert.assertEquals("state:-Verified,{Can't Reproduce},Obsolete,{Won't fix}",
125 Filter.stateFilter(StateValues.NotVerified, StateValues.CantReproduce, StateValues.Obsolete, StateValues.WontFix).toString()); 111 Filter.stateFilter(StateValues.NotVerified, StateValues.CantReproduce, StateValues.Obsolete, StateValues.WontFix).toString());
126 } 112 }
127 113
128 @Test 114 @Test
129 public void updatedFilter() 115 public void updatedFilter()
130 { 116 {
131 - Assert.assertEquals("updated:" + encodeURL("Today"), Filter.updatedFilter(DateValues.Today).toString());  
132 - Assert.assertEquals("updated:" + encodeURL("Yesterday"), Filter.updatedFilter(DateValues.Yesterday).toString());  
133 - Assert.assertEquals("updated:" + encodeURL("2012-01-01"), Filter.updatedFilter("2012-01-01").toString());  
134 - Assert.assertEquals("updated:" + encodeURL("{Last month}"), Filter.updatedFilter(DateValues.LastMonth).toString());  
135 - Assert.assertEquals("updated:" + encodeURL("{This month}"), Filter.updatedFilter(DateValues.ThisMonth).toString());  
136 - Assert.assertEquals("updated:" + encodeURL("{Two weeks ago}"), Filter.updatedFilter(DateValues.TwoWeeksAgo).toString());  
137 - Assert.assertEquals("updated:" + encodeURL("{Three weeks ago}"), Filter.updatedFilter(DateValues.ThreeWeeksAgo).toString());  
138 - Assert.assertEquals("updated:" + encodeURL("{Last week}"), Filter.updatedFilter(DateValues.LastWeek).toString());  
139 - Assert.assertEquals("updated:" + encodeURL("{This week}"), Filter.updatedFilter(DateValues.ThisWeek).toString());  
140 - Assert.assertEquals("updated:" + encodeURL("Saturday"), Filter.updatedFilter(DateValues.Saturday).toString());  
141 - Assert.assertEquals("updated:" + encodeURL("Friday"), Filter.updatedFilter(DateValues.Friday).toString());  
142 - Assert.assertEquals("updated:" + encodeURL("Thursday"), Filter.updatedFilter(DateValues.Thursday).toString());  
143 - Assert.assertEquals("updated:" + encodeURL("Wednesday"), Filter.updatedFilter(DateValues.Wednesday).toString());  
144 - Assert.assertEquals("updated:" + encodeURL("Tuesday"), Filter.updatedFilter(DateValues.Tuesday).toString());  
145 - Assert.assertEquals("updated:" + encodeURL("Monday"), Filter.updatedFilter(DateValues.Monday).toString());  
146 - Assert.assertEquals("updated:" + encodeURL("Sunday"), Filter.updatedFilter(DateValues.Sunday).toString()); 117 + Assert.assertEquals("updated:Today", Filter.updatedFilter(DateValues.Today).toString());
  118 + Assert.assertEquals("updated:Yesterday", Filter.updatedFilter(DateValues.Yesterday).toString());
  119 + Assert.assertEquals("updated:2012-01-01", Filter.updatedFilter("2012-01-01").toString());
  120 + Assert.assertEquals("updated:{Last month}", Filter.updatedFilter(DateValues.LastMonth).toString());
  121 + Assert.assertEquals("updated:{This month}", Filter.updatedFilter(DateValues.ThisMonth).toString());
  122 + Assert.assertEquals("updated:{Two weeks ago}", Filter.updatedFilter(DateValues.TwoWeeksAgo).toString());
  123 + Assert.assertEquals("updated:{Three weeks ago}", Filter.updatedFilter(DateValues.ThreeWeeksAgo).toString());
  124 + Assert.assertEquals("updated:{Last week}", Filter.updatedFilter(DateValues.LastWeek).toString());
  125 + Assert.assertEquals("updated:{This week}", Filter.updatedFilter(DateValues.ThisWeek).toString());
  126 + Assert.assertEquals("updated:Saturday", Filter.updatedFilter(DateValues.Saturday).toString());
  127 + Assert.assertEquals("updated:Friday", Filter.updatedFilter(DateValues.Friday).toString());
  128 + Assert.assertEquals("updated:Thursday", Filter.updatedFilter(DateValues.Thursday).toString());
  129 + Assert.assertEquals("updated:Wednesday", Filter.updatedFilter(DateValues.Wednesday).toString());
  130 + Assert.assertEquals("updated:Tuesday", Filter.updatedFilter(DateValues.Tuesday).toString());
  131 + Assert.assertEquals("updated:Monday", Filter.updatedFilter(DateValues.Monday).toString());
  132 + Assert.assertEquals("updated:Sunday", Filter.updatedFilter(DateValues.Sunday).toString());
147 } 133 }
148 134
149 @Test 135 @Test
@@ -13,20 +13,20 @@ import static org.junit.Assert.assertEquals; @@ -13,20 +13,20 @@ import static org.junit.Assert.assertEquals;
13 public class URIUtilsTest { 13 public class URIUtilsTest {
14 // -------------------------- OTHER METHODS -------------------------- 14 // -------------------------- OTHER METHODS --------------------------
15 15
16 - @Test  
17 - public void buildURI() throws URISyntaxException  
18 - { 16 + @Test
  17 + public void buildURI() throws URISyntaxException
  18 + {
19 // Given 19 // Given
20 - final URI base = new URI("http://localhost:8080/youtrack/");  
21 - final String pathA = "/rest/admin/bundle/QA note types";  
22 - final String queryA = "q=" + Filter.stateFilter(StateValues.NotWontFix).maxResults(20); 20 + final URI base = new URI("http://localhost:8080/youtrack/");
  21 + final String pathA = "/rest/admin/bundle/QA note types";
  22 + final String queryA = "q=" + Filter.stateFilter(StateValues.NotWontFix).maxResults(20);
23 23
24 // When 24 // When
25 - final URI uriA = URIUtils.buildURI(base, pathA);  
26 - final URI uriB = URIUtils.buildURI(base, pathA, queryA); 25 + final URI uriA = URIUtils.buildURI(base, pathA);
  26 + final URI uriB = URIUtils.buildURI(base, pathA, queryA);
27 27
28 // Then 28 // Then
29 - assertEquals("http://localhost/youtrack/rest/admin/bundle/QA%20note%20types", uriA.toString());  
30 - assertEquals("http://localhost/youtrack/rest/admin/bundle/QA%20note%20types?q=state:-%257BWon%2527t+fix%257D&max=20", uriB.toString());  
31 - } 29 + assertEquals("http://localhost/youtrack/rest/admin/bundle/QA%20note%20types", uriA.toString());
  30 + assertEquals("http://localhost/youtrack/rest/admin/bundle/QA%20note%20types?q=state:-%7BWon't%20fix%7D&max=20", uriB.toString());
  31 + }
32 } 32 }
Please register or login to post a comment