• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.assertNotNull;
21 import static org.junit.Assert.fail;
22 
23 import android.content.ContentUris;
24 import android.net.Uri;
25 import android.net.Uri.Builder;
26 import android.platform.test.annotations.AppModeSdkSandbox;
27 
28 import androidx.test.runner.AndroidJUnit4;
29 
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 @RunWith(AndroidJUnit4.class)
34 @AppModeSdkSandbox(reason = "Allow test in the SDK sandbox (does not prevent other modes).")
35 public class ContentUrisTest {
36     private static final String AUTHORITY = "ctstest";
37     private static final String PATH1 = "testPath1";
38     private static final String PATH2 = "testPath2";
39 
40     private static final int CODE1 = 1;
41     private static final int CODE2 = 2;
42 
43     private Uri uri1 = Uri.parse("content://" + AUTHORITY + "/" + PATH1);
44     private Uri uri2 = Uri.parse("content://" + AUTHORITY + "/" + PATH2);
45 
46     @Test
testConstructor()47     public void testConstructor() {
48         new ContentUris();
49     }
50 
51     @Test
testParseId()52     public void testParseId() {
53         Uri result = ContentUris.withAppendedId(uri1, CODE1);
54         assertEquals(CODE1, ContentUris.parseId(result));
55 
56         result = ContentUris.withAppendedId(uri2, CODE2);
57         assertEquals(CODE2, ContentUris.parseId(result));
58 
59         // no code in Uri
60         assertEquals(-1, ContentUris.parseId(Uri.parse("")));
61     }
62 
63     @Test
testParseIdFailure()64     public void testParseIdFailure() {
65         try {
66             ContentUris.parseId(uri1);
67             fail("There should be a NumberFormatException thrown out.");
68         } catch (NumberFormatException e) {
69             //expected, test success.
70         }
71 
72         Uri uri = Uri.fromParts("abc", "123", null);
73         ContentUris.parseId(uri);
74 
75         try {
76             ContentUris.parseId(null);
77             fail("There should be a NullPointerException thrown out.");
78         } catch (NullPointerException e) {
79             // expected, test success.
80         }
81     }
82 
83     @Test
testWithAppendedId()84     public void testWithAppendedId() {
85         String expected = "content://" + AUTHORITY + "/" + PATH1 + "/" + CODE1;
86         Uri actually;
87 
88         assertNotNull(actually = ContentUris.withAppendedId(uri1, CODE1));
89         assertEquals(expected, actually.toString());
90 
91         expected = "content://" + AUTHORITY + "/" + PATH2 + "/" + CODE2;
92         assertNotNull(actually = ContentUris.withAppendedId(uri2, CODE2));
93         assertEquals(expected, actually.toString());
94     }
95 
96     @Test
testWithAppendedIdFailure()97     public void testWithAppendedIdFailure() {
98         try {
99             ContentUris.withAppendedId(null, -1);
100             fail("There should be a NullPointerException thrown out.");
101         } catch (NullPointerException e) {
102             // expected, test success.
103         }
104     }
105 
106     @Test
testAppendId()107     public void testAppendId() {
108         String expected = "content://" + AUTHORITY + "/" + PATH1 + "/" + CODE1;
109         Builder actually;
110         Builder b = uri1.buildUpon();
111 
112         assertNotNull(actually = ContentUris.appendId(b, CODE1));
113         assertEquals(expected, actually.toString());
114 
115         expected = "content://" + AUTHORITY + "/" + PATH2 + "/" + CODE2;
116         b = uri2.buildUpon();
117 
118         assertNotNull(actually = ContentUris.appendId(b, CODE2));
119         assertEquals(expected, actually.toString());
120     }
121 
122     @Test
testAppendIdFailure()123     public void testAppendIdFailure() {
124         try {
125             ContentUris.appendId(null, -1);
126             fail("There should be a NullPointerException thrown out.");
127         } catch (NullPointerException e) {
128             // expected, test success.
129         }
130     }
131 
132     @Test
testRemoveId()133     public void testRemoveId() {
134         assertEquals(Uri.parse("content://auth"),
135                 ContentUris.removeId(Uri.parse("content://auth/12")));
136         assertEquals(Uri.parse("content://auth/path"),
137                 ContentUris.removeId(Uri.parse("content://auth/path/12")));
138         assertEquals(Uri.parse("content://auth/path/path"),
139                 ContentUris.removeId(Uri.parse("content://auth/path/path/12")));
140     }
141 
142     @Test
testRemoveId_MissingId()143     public void testRemoveId_MissingId() {
144         try {
145             ContentUris.removeId(Uri.parse("content://auth/"));
146             fail("There should be a IllegalArgumentException thrown out.");
147         } catch (IllegalArgumentException expected) {
148         }
149         try {
150             ContentUris.removeId(Uri.parse("content://auth/path/"));
151             fail("There should be a IllegalArgumentException thrown out.");
152         } catch (IllegalArgumentException expected) {
153         }
154         try {
155             ContentUris.removeId(Uri.parse("content://auth/path/path/"));
156             fail("There should be a IllegalArgumentException thrown out.");
157         } catch (IllegalArgumentException expected) {
158         }
159     }
160 }
161