1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ahat;
18 
19 import java.net.URI;
20 import java.net.URISyntaxException;
21 import org.junit.Test;
22 
23 import static org.junit.Assert.assertEquals;
24 
25 public class QueryTest {
26   @Test
simple()27   public void simple() throws URISyntaxException {
28     String uri = "http://localhost:7100/object?foo=bar&answer=42";
29     Query query = new Query(new URI(uri));
30     assertEquals("bar", query.get("foo", "not found"));
31     assertEquals("42", query.get("answer", "not found"));
32     assertEquals(42, query.getLong("answer", 0));
33     assertEquals(42, query.getInt("answer", 0));
34     assertEquals("not found", query.get("bar", "not found"));
35     assertEquals("really not found", query.get("bar", "really not found"));
36     assertEquals(0, query.getLong("bar", 0));
37     assertEquals(0, query.getInt("bar", 0));
38     assertEquals(42, query.getLong("bar", 42));
39     assertEquals(42, query.getInt("bar", 42));
40     assertEquals("/object?answer=42&foo=sludge", query.with("foo", "sludge").toString());
41     assertEquals("/object?answer=43&foo=bar", query.with("answer", "43").toString());
42     assertEquals("/object?answer=43&foo=bar", query.with("answer", 43).toString());
43     assertEquals("/object?answer=42&bar=finally&foo=bar", query.with("bar", "finally").toString());
44     assertEquals("/object?answer=42", query.with("foo", null).toString());
45   }
46 
47   @Test
multiValue()48   public void multiValue() throws URISyntaxException {
49     String uri = "http://localhost:7100/object?foo=bar&answer=42&foo=sludge";
50     Query query = new Query(new URI(uri));
51     assertEquals("sludge", query.get("foo", "not found"));
52     assertEquals(42, query.getLong("answer", 0));
53     assertEquals(42, query.getInt("answer", 0));
54     assertEquals("not found", query.get("bar", "not found"));
55     assertEquals("/object?answer=42&foo=tar", query.with("foo", "tar").toString());
56     assertEquals("/object?answer=43&foo=sludge", query.with("answer", "43").toString());
57     assertEquals("/object?answer=42&bar=finally&foo=sludge",
58         query.with("bar", "finally").toString());
59     assertEquals("/object?answer=42", query.with("foo", null).toString());
60   }
61 
62   @Test
empty()63   public void empty() throws URISyntaxException {
64     String uri = "http://localhost:7100/object";
65     Query query = new Query(new URI(uri));
66     assertEquals("not found", query.get("foo", "not found"));
67     assertEquals(2, query.getLong("foo", 2));
68     assertEquals(2, query.getInt("foo", 2));
69     assertEquals("/object?foo=sludge", query.with("foo", "sludge").toString());
70     assertEquals("/object?answer=43", query.with("answer", "43").toString());
71     assertEquals("/object?", query.with("foo", null).toString());
72   }
73 }
74