1/*
2 * Copyright (C) 2011 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
17package com.android.providers.contacts.util;
18
19import android.net.Uri;
20import android.test.AndroidTestCase;
21import android.test.suitebuilder.annotation.SmallTest;
22
23/**
24 * Unit tests for {@link TypedUriMatcherImpl}.
25 * Run the test like this:
26 * <code>
27 * runtest -c com.android.providers.contacts.util.TypedUriMatcherImplTest contactsprov
28 * </code>
29 */
30@SmallTest
31public class TypedUriMatcherImplTest extends AndroidTestCase {
32    /** URI type used for testing. */
33    private static enum TestUriType implements UriType {
34        NO_MATCH(null),
35        SIMPLE_URI("build"),
36        URI_WITH_ID("build/#"),
37        URI_WITH_TWO_IDS("project/*/build/#");
38
39        private String path;
40
41        private TestUriType(String path) {
42            this.path = path;
43        }
44
45        @Override
46        public String path() {
47            return path;
48        }
49    }
50
51    private final static String AUTHORITY = "authority";
52    private final static String BASE_URI = "scheme://" + AUTHORITY + "/";
53
54    /** The object under test. */
55    TypedUriMatcherImpl<TestUriType> mTypedUriMatcherImpl;
56
57    @Override
58    protected void setUp() throws Exception {
59        super.setUp();
60        mTypedUriMatcherImpl =
61                new TypedUriMatcherImpl<TestUriType>(AUTHORITY, TestUriType.values());
62    }
63
64    public void testMatch_NoMatch() {
65        // Incorrect authority.
66        assertUriTypeMatch(TestUriType.NO_MATCH, "scheme://authority1/build");
67        // Incorrect path.
68        assertUriTypeMatch(TestUriType.NO_MATCH, BASE_URI + "test");
69    }
70
71    public void testMatch_SimpleUri() {
72        assertUriTypeMatch(TestUriType.SIMPLE_URI, BASE_URI + "build");
73    }
74
75    public void testMatch_UriWithId() {
76        assertUriTypeMatch(TestUriType.URI_WITH_ID, BASE_URI + "build/2");
77        // Argument must be a number.
78        assertUriTypeMatch(TestUriType.NO_MATCH, BASE_URI + "build/a");
79        // Additional arguments not allowed.
80        assertUriTypeMatch(TestUriType.NO_MATCH, BASE_URI + "build/2/more");
81    }
82
83    public void testMatch_UriWithTwoIds() {
84        assertUriTypeMatch(TestUriType.URI_WITH_TWO_IDS, BASE_URI + "project/vm/build/3");
85        // Missing argument.
86        assertUriTypeMatch(TestUriType.NO_MATCH, BASE_URI + "project/vm/build/");
87        // Argument cannot contain / itself
88        assertUriTypeMatch(TestUriType.NO_MATCH, BASE_URI + "project/vm/x/build/3");
89    }
90
91    private void assertUriTypeMatch(UriType expectedType, String uri) {
92        assertEquals(expectedType, mTypedUriMatcherImpl.match(Uri.parse(uri)));
93    }
94}
95