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