Home | History | Annotate | Download | only in test
      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 static org.junit.Assert.assertEquals;
     22 import org.junit.Test;
     23 
     24 public class QueryTest {
     25   @Test
     26   public void simple() throws URISyntaxException {
     27     String uri = "http://localhost:7100/object?foo=bar&answer=42";
     28     Query query = new Query(new URI(uri));
     29     assertEquals("bar", query.get("foo", "not found"));
     30     assertEquals("42", query.get("answer", "not found"));
     31     assertEquals(42, query.getLong("answer", 0));
     32     assertEquals(42, query.getInt("answer", 0));
     33     assertEquals("not found", query.get("bar", "not found"));
     34     assertEquals("really not found", query.get("bar", "really not found"));
     35     assertEquals(0, query.getLong("bar", 0));
     36     assertEquals(0, query.getInt("bar", 0));
     37     assertEquals(42, query.getLong("bar", 42));
     38     assertEquals(42, query.getInt("bar", 42));
     39     assertEquals("/object?answer=42&foo=sludge", query.with("foo", "sludge").toString());
     40     assertEquals("/object?answer=43&foo=bar", query.with("answer", "43").toString());
     41     assertEquals("/object?answer=43&foo=bar", query.with("answer", 43).toString());
     42     assertEquals("/object?answer=42&bar=finally&foo=bar", query.with("bar", "finally").toString());
     43   }
     44 
     45   @Test
     46   public void multiValue() throws URISyntaxException {
     47     String uri = "http://localhost:7100/object?foo=bar&answer=42&foo=sludge";
     48     Query query = new Query(new URI(uri));
     49     assertEquals("sludge", query.get("foo", "not found"));
     50     assertEquals(42, query.getLong("answer", 0));
     51     assertEquals(42, query.getInt("answer", 0));
     52     assertEquals("not found", query.get("bar", "not found"));
     53     assertEquals("/object?answer=42&foo=tar", query.with("foo", "tar").toString());
     54     assertEquals("/object?answer=43&foo=sludge", query.with("answer", "43").toString());
     55     assertEquals("/object?answer=42&bar=finally&foo=sludge",
     56         query.with("bar", "finally").toString());
     57   }
     58 
     59   @Test
     60   public void empty() throws URISyntaxException {
     61     String uri = "http://localhost:7100/object";
     62     Query query = new Query(new URI(uri));
     63     assertEquals("not found", query.get("foo", "not found"));
     64     assertEquals(2, query.getLong("foo", 2));
     65     assertEquals(2, query.getInt("foo", 2));
     66     assertEquals("/object?foo=sludge", query.with("foo", "sludge").toString());
     67     assertEquals("/object?answer=43", query.with("answer", "43").toString());
     68   }
     69 }
     70