Home | History | Annotate | Download | only in cts
      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 android.content.ContentUris;
     20 import android.net.Uri;
     21 import android.net.Uri.Builder;
     22 import android.test.AndroidTestCase;
     23 
     24 public class ContentUrisTest extends AndroidTestCase {
     25     private static final String AUTHORITY = "ctstest";
     26     private static final String PATH1 = "testPath1";
     27     private static final String PATH2 = "testPath2";
     28 
     29     private static final int CODE1 = 1;
     30     private static final int CODE2 = 2;
     31 
     32     private Uri uri1 = Uri.parse("content://" + AUTHORITY + "/" + PATH1);
     33     private Uri uri2 = Uri.parse("content://" + AUTHORITY + "/" + PATH2);
     34 
     35     public void testConstructor() {
     36         new ContentUris();
     37     }
     38 
     39     public void testParseId() {
     40         Uri result = ContentUris.withAppendedId(uri1, CODE1);
     41         assertEquals(CODE1, ContentUris.parseId(result));
     42 
     43         result = ContentUris.withAppendedId(uri2, CODE2);
     44         assertEquals(CODE2, ContentUris.parseId(result));
     45 
     46         // no code in Uri
     47         assertEquals(-1, ContentUris.parseId(Uri.parse("")));
     48     }
     49 
     50     public void testParseIdFailure() {
     51         try {
     52             ContentUris.parseId(uri1);
     53             fail("There should be a NumberFormatException thrown out.");
     54         } catch (NumberFormatException e) {
     55             //expected, test success.
     56         }
     57 
     58         Uri uri = Uri.fromParts("abc", "123", null);
     59         ContentUris.parseId(uri);
     60 
     61         try {
     62             ContentUris.parseId(null);
     63             fail("There should be a NullPointerException thrown out.");
     64         } catch (NullPointerException e) {
     65             // expected, test success.
     66         }
     67     }
     68 
     69     public void testWithAppendedId() {
     70         String expected = "content://" + AUTHORITY + "/" + PATH1 + "/" + CODE1;
     71         Uri actually;
     72 
     73         assertNotNull(actually = ContentUris.withAppendedId(uri1, CODE1));
     74         assertEquals(expected, actually.toString());
     75 
     76         expected = "content://" + AUTHORITY + "/" + PATH2 + "/" + CODE2;
     77         assertNotNull(actually = ContentUris.withAppendedId(uri2, CODE2));
     78         assertEquals(expected, actually.toString());
     79     }
     80 
     81     public void testWithAppendedIdFailure() {
     82         try {
     83             ContentUris.withAppendedId(null, -1);
     84             fail("There should be a NullPointerException thrown out.");
     85         } catch (NullPointerException e) {
     86             // expected, test success.
     87         }
     88     }
     89 
     90     public void testAppendId() {
     91         String expected = "content://" + AUTHORITY + "/" + PATH1 + "/" + CODE1;
     92         Builder actually;
     93         Builder b = uri1.buildUpon();
     94 
     95         assertNotNull(actually = ContentUris.appendId(b, CODE1));
     96         assertEquals(expected, actually.toString());
     97 
     98         expected = "content://" + AUTHORITY + "/" + PATH2 + "/" + CODE2;
     99         b = uri2.buildUpon();
    100 
    101         assertNotNull(actually = ContentUris.appendId(b, CODE2));
    102         assertEquals(expected, actually.toString());
    103     }
    104 
    105     public void testAppendIdFailure() {
    106         try {
    107             ContentUris.appendId(null, -1);
    108             fail("There should be a NullPointerException thrown out.");
    109         } catch (NullPointerException e) {
    110             // expected, test success.
    111         }
    112     }
    113 }
    114