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 
17 package com.android.providers.contacts.util;
18 
19 import static com.android.providers.contacts.util.DbQueryUtils.checkForSupportedColumns;
20 import static com.android.providers.contacts.util.DbQueryUtils.concatenateClauses;
21 import static com.android.providers.contacts.util.DbQueryUtils.escapeLikeValue;
22 
23 import android.content.ContentValues;
24 import android.test.suitebuilder.annotation.SmallTest;
25 
26 import com.android.common.content.ProjectionMap;
27 import com.android.providers.contacts.EvenMoreAsserts;
28 
29 import junit.framework.TestCase;
30 
31 /**
32  * Unit tests for the {@link DbQueryUtils} class.
33  * Run the test like this:
34  * <code>
35  * runtest -c com.android.providers.contacts.util.DBQueryUtilsTest contactsprov
36  * </code>
37  */
38 @SmallTest
39 public class DBQueryUtilsTest extends TestCase {
testGetEqualityClause()40     public void testGetEqualityClause() {
41         assertEquals("(foo = 'bar')", DbQueryUtils.getEqualityClause("foo", "bar"));
42         assertEquals("(foo = 2)", DbQueryUtils.getEqualityClause("foo", 2));
43     }
44 
testGetInEqualityClause()45     public void testGetInEqualityClause() {
46         assertEquals("(foo != 2)", DbQueryUtils.getInequalityClause("foo", 2));
47     }
48 
testConcatenateClauses()49     public void testConcatenateClauses() {
50         assertEquals("(first)", concatenateClauses("first"));
51         assertEquals("(first) AND (second)", concatenateClauses("first", "second"));
52         assertEquals("(second)", concatenateClauses("second", null));
53         assertEquals("(second)", concatenateClauses(null, "second"));
54         assertEquals("(second)", concatenateClauses(null, "second", null));
55         assertEquals("(a) AND (b) AND (c)", concatenateClauses(null, "a", "b", null, "c"));
56         assertEquals("(WHERE \"a\" = \"b\")", concatenateClauses(null, "WHERE \"a\" = \"b\""));
57     }
58 
testCheckForSupportedColumns()59     public void testCheckForSupportedColumns() {
60         final ProjectionMap projectionMap = new ProjectionMap.Builder()
61                 .add("A").add("B").add("C").build();
62         final ContentValues values = new ContentValues();
63         values.put("A", "?");
64         values.put("C", "?");
65         // No exception expected.
66         checkForSupportedColumns(projectionMap, values);
67         // Expect exception for invalid column.
68         EvenMoreAsserts.assertThrows(IllegalArgumentException.class, new Runnable() {
69             @Override
70             public void run() {
71                 values.put("D", "?");
72                 checkForSupportedColumns(projectionMap, values);
73             }
74         });
75     }
76 
testEscapeLikeValuesEscapesUnderscores()77     public void testEscapeLikeValuesEscapesUnderscores() {
78         StringBuilder sb = new StringBuilder();
79         DbQueryUtils.escapeLikeValue(sb, "my_test_string", '\\');
80         assertEquals("my\\_test\\_string", sb.toString());
81 
82         sb = new StringBuilder();
83         DbQueryUtils.escapeLikeValue(sb, "_test_", '\\');
84         assertEquals("\\_test\\_", sb.toString());
85     }
86 
testEscapeLikeValuesEscapesPercents()87     public void testEscapeLikeValuesEscapesPercents() {
88         StringBuilder sb = new StringBuilder();
89         escapeLikeValue(sb, "my%test%string", '\\');
90         assertEquals("my\\%test\\%string", sb.toString());
91 
92         sb = new StringBuilder();
93         escapeLikeValue(sb, "%test%", '\\');
94         assertEquals("\\%test\\%", sb.toString());
95     }
96 
testEscapeLikeValuesEscapesEscapes()97     public void testEscapeLikeValuesEscapesEscapes() {
98         StringBuilder sb = new StringBuilder();
99         escapeLikeValue(sb, "my\\test\\string", '\\');
100         assertEquals("my\\\\test\\\\string", sb.toString());
101 
102         sb = new StringBuilder();
103         escapeLikeValue(sb, "\\test\\", '\\');
104         assertEquals("\\\\test\\\\", sb.toString());
105     }
106 
testEscapeLikeValuesNoChanges()107     public void testEscapeLikeValuesNoChanges() {
108         StringBuilder sb = new StringBuilder();
109         escapeLikeValue(sb, "my test string", '\\');
110         assertEquals("my test string", sb.toString());
111     }
112 }
113