View Javadoc
1   package com.atlassian.sal.core.search.parameter;
2   
3   import org.junit.Test;
4   import org.junit.runner.RunWith;
5   import org.junit.runners.Parameterized;
6   import org.junit.runners.Parameterized.Parameters;
7   
8   import java.util.Arrays;
9   
10  import static org.hamcrest.MatcherAssert.assertThat;
11  import static org.hamcrest.Matchers.equalTo;
12  import static org.hamcrest.Matchers.is;
13  
14  @RunWith(value = Parameterized.class)
15  public class TestBasicSearchParameter {
16  
17      private String queryKey;
18      private String queryValue;
19      private String expectedQueryString;
20  
21      public TestBasicSearchParameter(final String queryKey, final String queryValue, final String expectedQueryString) {
22          this.queryKey = queryKey;
23          this.queryValue = queryValue;
24          this.expectedQueryString = expectedQueryString;
25      }
26  
27      @Parameters
28      public static Iterable<Object[]> testData() {
29          /*
30              According to RFC 3986 (http://www.ietf.org/rfc/rfc3986.txt), the ABNF for a query is:
31  
32                  query = *( pchar / "/" / "?" )
33  
34              Where:
35  
36                  pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
37  
38              And:
39  
40                  unreserved    = ALPHA / DIGIT / "-" / "." / "_" / "~"
41                  pct-encoded   = "%" HEXDIG HEXDIG
42                  sub-delims  = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
43  
44              Thus all characters other than unreserved or /, ?, :, @ must be encoded, as we can't know whether unencoded
45              sub-delims characters would conflict with the broader query parameter segement of a URL.
46          */
47          return Arrays.asList(new Object[][]{
48                          {"fixfor", "3.12", "fixfor=3.12"},
49                          {"fix?for", "3.12!%", "fix%3Ffor=3.12%21%25"},
50                          {"key!@#$%^&*()", "value!@#$%^&*()", "key%21%40%23%24%25%5E%26*%28%29=value%21%40%23%24%25%5E%26*%28%29"},
51                          {"key&name", "value&name=", "key%26name=value%26name%3D"},
52                          {"key", "value with spaces", "key=value+with+spaces"},
53                          {"key", "value+with+pluses", "key=value%2Bwith%2Bpluses"},
54                  }
55          );
56      }
57  
58      @Test
59      public void assertThatQueryParametersAreCorrectlyEncodedAndDecoded() {
60          BasicSearchParameter basicSearchParameter = new BasicSearchParameter(queryKey, queryValue);
61          String queryString = basicSearchParameter.buildQueryString();
62          assertThat(queryString, is(equalTo(expectedQueryString)));
63  
64          BasicSearchParameter searchParameter = new BasicSearchParameter(queryString);
65          assertThat(searchParameter.getName(), is(equalTo(queryKey)));
66          assertThat(searchParameter.getValue(), is(equalTo(queryValue)));
67      }
68  
69  }