1 /* 2 * Copyright (C) 2018 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.database; 18 19 import static android.database.DatabaseUtils.bindSelection; 20 21 import static org.junit.Assert.assertEquals; 22 23 import androidx.test.runner.AndroidJUnit4; 24 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 28 @RunWith(AndroidJUnit4.class) 29 public class DatabaseUtilsTest { 30 private static final Object[] ARGS = { "baz", 4, null }; 31 32 @Test testBindSelection_none()33 public void testBindSelection_none() throws Exception { 34 assertEquals(null, 35 bindSelection(null, ARGS)); 36 assertEquals("", 37 bindSelection("", ARGS)); 38 assertEquals("foo=bar", 39 bindSelection("foo=bar", ARGS)); 40 } 41 42 @Test testBindSelection_normal()43 public void testBindSelection_normal() throws Exception { 44 assertEquals("foo='baz'", 45 bindSelection("foo=?", ARGS)); 46 assertEquals("foo='baz' AND bar=4", 47 bindSelection("foo=? AND bar=?", ARGS)); 48 assertEquals("foo='baz' AND bar=4 AND meow=NULL", 49 bindSelection("foo=? AND bar=? AND meow=?", ARGS)); 50 } 51 52 @Test testBindSelection_whitespace()53 public void testBindSelection_whitespace() throws Exception { 54 assertEquals("BETWEEN 5 AND 10", 55 bindSelection("BETWEEN? AND ?", 5, 10)); 56 assertEquals("IN 'foo'", 57 bindSelection("IN?", "foo")); 58 } 59 60 @Test testBindSelection_indexed()61 public void testBindSelection_indexed() throws Exception { 62 assertEquals("foo=10 AND bar=11 AND meow=1", 63 bindSelection("foo=?10 AND bar=? AND meow=?1", 64 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)); 65 } 66 } 67