1 /* 2 * Copyright (C) 2008 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 android.content.cts; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.fail; 21 22 import android.content.UriMatcher; 23 import android.net.Uri; 24 import android.platform.test.annotations.AppModeSdkSandbox; 25 26 import androidx.test.runner.AndroidJUnit4; 27 28 import org.junit.Before; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 32 @RunWith(AndroidJUnit4.class) 33 @AppModeSdkSandbox(reason = "Allow test in the SDK sandbox (does not prevent other modes).") 34 public class UriMatcherTest { 35 UriMatcher mUriMatcher; 36 37 private static final String sAuthority = "ctstest"; 38 private static final String sPath1 = "testPath1"; 39 private static final String sPath2 = "testPath2"; 40 private static final String sPath3 = "testPath3"; 41 private static final String sPath4 = "testPath4"; 42 43 private static final int sCode1 = 1; 44 private static final int sCode2 = 2; 45 private static final int sCode3 = 3; 46 private static final int sCode4 = 4; 47 48 private Uri uri1 = Uri.parse("content://" + sAuthority + "/" + sPath1); 49 private Uri uri2 = Uri.parse("content://" + sAuthority + "/" + sPath2); 50 private Uri uri3 = Uri.parse("content://" + sAuthority + "/" + sPath3); 51 private Uri uri4 = Uri.parse("content://" + sAuthority + "/" + sPath4); 52 53 @Before setUp()54 public void setUp() throws Exception { 55 mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); 56 } 57 58 @Test testConstructor()59 public void testConstructor() { 60 new UriMatcher(UriMatcher.NO_MATCH); 61 } 62 63 @Test testMatch()64 public void testMatch() { 65 mUriMatcher.addURI(sAuthority, sPath1, sCode1); 66 mUriMatcher.addURI(sAuthority, sPath2, sCode2); 67 mUriMatcher.addURI(sAuthority, sPath3, sCode3); 68 mUriMatcher.addURI(sAuthority, sPath4, sCode4); 69 70 assertEquals(sCode1, mUriMatcher.match(uri1)); 71 assertEquals(sCode2, mUriMatcher.match(uri2)); 72 assertEquals(sCode3, mUriMatcher.match(uri3)); 73 assertEquals(sCode4, mUriMatcher.match(uri4)); 74 75 //test unknown uri 76 Uri unknown = Uri.parse("abc"); 77 assertEquals(-1, mUriMatcher.match(unknown)); 78 } 79 80 @Test testMatchFailure()81 public void testMatchFailure() { 82 try { 83 mUriMatcher.match(null); 84 fail("There should be a NullPointerException thrown out."); 85 } catch (NullPointerException e) { 86 //expected, test success. 87 } 88 } 89 90 @Test testAddURI()91 public void testAddURI() { 92 assertEquals(-1, mUriMatcher.match(uri1)); 93 assertEquals(-1, mUriMatcher.match(uri2)); 94 assertEquals(-1, mUriMatcher.match(uri3)); 95 assertEquals(-1, mUriMatcher.match(uri4)); 96 97 mUriMatcher.addURI(sAuthority, sPath1, sCode1); 98 mUriMatcher.addURI(sAuthority, sPath2, sCode2); 99 mUriMatcher.addURI(sAuthority, sPath3, sCode3); 100 mUriMatcher.addURI(sAuthority, sPath4, sCode4); 101 102 assertEquals(sCode1, mUriMatcher.match(uri1)); 103 assertEquals(sCode2, mUriMatcher.match(uri2)); 104 assertEquals(sCode3, mUriMatcher.match(uri3)); 105 assertEquals(sCode4, mUriMatcher.match(uri4)); 106 } 107 108 @Test testAddURIFailure()109 public void testAddURIFailure() { 110 try { 111 mUriMatcher.addURI(null, null, -1); 112 fail("There should be an IllegalArgumentException thrown out."); 113 } catch (IllegalArgumentException e) { 114 //expected, test success. 115 } 116 } 117 } 118