Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 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; |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 21 | import org.junit.Test; |
| 22 | |
Richard Uhler | cda4f2e | 2016-09-09 09:56:20 +0100 | [diff] [blame] | 23 | import static org.junit.Assert.assertEquals; |
| 24 | |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 25 | public class QueryTest { |
| 26 | @Test |
| 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 | } |
| 45 | |
| 46 | @Test |
| 47 | public void multiValue() throws URISyntaxException { |
| 48 | String uri = "http://localhost:7100/object?foo=bar&answer=42&foo=sludge"; |
| 49 | Query query = new Query(new URI(uri)); |
| 50 | assertEquals("sludge", query.get("foo", "not found")); |
| 51 | assertEquals(42, query.getLong("answer", 0)); |
| 52 | assertEquals(42, query.getInt("answer", 0)); |
| 53 | assertEquals("not found", query.get("bar", "not found")); |
| 54 | assertEquals("/object?answer=42&foo=tar", query.with("foo", "tar").toString()); |
| 55 | assertEquals("/object?answer=43&foo=sludge", query.with("answer", "43").toString()); |
| 56 | assertEquals("/object?answer=42&bar=finally&foo=sludge", |
| 57 | query.with("bar", "finally").toString()); |
| 58 | } |
| 59 | |
| 60 | @Test |
| 61 | public void empty() throws URISyntaxException { |
| 62 | String uri = "http://localhost:7100/object"; |
| 63 | Query query = new Query(new URI(uri)); |
| 64 | assertEquals("not found", query.get("foo", "not found")); |
| 65 | assertEquals(2, query.getLong("foo", 2)); |
| 66 | assertEquals(2, query.getInt("foo", 2)); |
| 67 | assertEquals("/object?foo=sludge", query.with("foo", "sludge").toString()); |
| 68 | assertEquals("/object?answer=43", query.with("answer", "43").toString()); |
| 69 | } |
| 70 | } |