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 static com.android.providers.contacts.util.DbQueryUtils.checkForSupportedColumns;
20import static com.android.providers.contacts.util.DbQueryUtils.concatenateClauses;
21
22import android.content.ContentValues;
23import android.test.AndroidTestCase;
24import android.test.suitebuilder.annotation.SmallTest;
25
26import com.android.providers.contacts.EvenMoreAsserts;
27import com.android.providers.contacts.ProjectionMap;
28import com.android.providers.contacts.util.DbQueryUtils;
29
30/**
31 * Unit tests for the {@link DbQueryUtils} class.
32 * Run the test like this:
33 * <code>
34 * runtest -c com.android.providers.contacts.util.DBQueryUtilsTest contactsprov
35 * </code>
36 */
37@SmallTest
38public class DBQueryUtilsTest extends AndroidTestCase {
39    public void testGetEqualityClause() {
40        assertEquals("(foo = 'bar')", DbQueryUtils.getEqualityClause("foo", "bar"));
41    }
42
43    public void testGetInEqualityClause() {
44        assertEquals("(foo != 'bar')", DbQueryUtils.getInequalityClause("foo", "bar"));
45    }
46
47    public void testConcatenateClauses() {
48        assertEquals("(first)", concatenateClauses("first"));
49        assertEquals("(first) AND (second)", concatenateClauses("first", "second"));
50        assertEquals("(second)", concatenateClauses("second", null));
51        assertEquals("(second)", concatenateClauses(null, "second"));
52        assertEquals("(second)", concatenateClauses(null, "second", null));
53        assertEquals("(a) AND (b) AND (c)", concatenateClauses(null, "a", "b", null, "c"));
54        assertEquals("(WHERE \"a\" = \"b\")", concatenateClauses(null, "WHERE \"a\" = \"b\""));
55    }
56
57    public void testCheckForSupportedColumns() {
58        final ProjectionMap projectionMap = new ProjectionMap.Builder()
59                .add("A").add("B").add("C").build();
60        final ContentValues values = new ContentValues();
61        values.put("A", "?");
62        values.put("C", "?");
63        // No exception expected.
64        checkForSupportedColumns(projectionMap, values);
65        // Expect exception for invalid column.
66        EvenMoreAsserts.assertThrows(IllegalArgumentException.class, new Runnable() {
67            @Override
68            public void run() {
69                values.put("D", "?");
70                checkForSupportedColumns(projectionMap, values);
71            }
72        });
73    }
74}
75