1 /* 2 * Copyright (C) 2007 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 android.test.MoreAsserts; 20 21 import junit.framework.TestCase; 22 23 import java.util.*; 24 25 public class MatrixCursorTest extends TestCase { 26 testEmptyCursor()27 public void testEmptyCursor() { 28 Cursor cursor = new MatrixCursor(new String[] { "a" }); 29 assertEquals(0, cursor.getCount()); 30 } 31 testNullValue()32 public void testNullValue() { 33 MatrixCursor cursor = new MatrixCursor(new String[] { "a" }); 34 cursor.newRow().add(null); 35 cursor.moveToNext(); 36 assertTrue(cursor.isNull(0)); 37 assertNull(cursor.getString(0)); 38 assertNull(cursor.getBlob(0)); 39 assertEquals(0, cursor.getShort(0)); 40 assertEquals(0, cursor.getInt(0)); 41 assertEquals(0L, cursor.getLong(0)); 42 assertEquals(0.0f, cursor.getFloat(0)); 43 assertEquals(0.0d, cursor.getDouble(0)); 44 } 45 testMatrixCursor()46 public void testMatrixCursor() { 47 MatrixCursor cursor = newMatrixCursor(); 48 49 cursor.newRow() 50 .add("a") 51 .add(1) 52 .add(2) 53 .add(3) 54 .add(4) 55 .add(5) 56 .add(new byte[] {(byte) 0xaa, (byte) 0x55}); 57 58 cursor.moveToNext(); 59 60 checkValues(cursor); 61 62 cursor.newRow() 63 .add("a") 64 .add("1") 65 .add("2") 66 .add("3") 67 .add("4") 68 .add("5") 69 .add(new byte[] {(byte) 0xaa, (byte) 0x55}); 70 71 cursor.moveToNext(); 72 checkValues(cursor); 73 74 cursor.moveToPrevious(); 75 checkValues(cursor); 76 } 77 testAddArray()78 public void testAddArray() { 79 MatrixCursor cursor = newMatrixCursor(); 80 81 cursor.addRow(new Object[] { "a", 1, 2, 3, 4, 5, new byte[] {(byte) 0xaa, (byte) 0x55} }); 82 cursor.moveToNext(); 83 checkValues(cursor); 84 85 try { 86 cursor.addRow(new Object[0]); 87 fail(); 88 } catch (IllegalArgumentException e) { /* expected */ } 89 } 90 testAddIterable()91 public void testAddIterable() { 92 MatrixCursor cursor = newMatrixCursor(); 93 94 cursor.addRow(Arrays.asList("a", 1, 2, 3, 4, 5, new byte[] {(byte) 0xaa, (byte) 0x55})); 95 cursor.moveToNext(); 96 checkValues(cursor); 97 98 try { 99 cursor.addRow(Collections.emptyList()); 100 fail(); 101 } catch (IllegalArgumentException e) { /* expected */ } 102 103 try { 104 cursor.addRow(Arrays.asList("a", 1, 2, 3, 4, 5, 105 new byte[] {(byte) 0xaa, (byte) 0x55}, "Too many!")); 106 fail(); 107 } catch (IllegalArgumentException e) { /* expected */ } 108 } 109 testAddArrayList()110 public void testAddArrayList() { 111 MatrixCursor cursor = newMatrixCursor(); 112 113 cursor.addRow(new NonIterableArrayList<Object>( 114 Arrays.asList("a", 1, 2, 3, 4, 5, new byte[] {(byte) 0xaa, (byte) 0x55}))); 115 cursor.moveToNext(); 116 checkValues(cursor); 117 118 try { 119 cursor.addRow(new NonIterableArrayList<Object>()); 120 fail(); 121 } catch (IllegalArgumentException e) { /* expected */ } 122 123 try { 124 cursor.addRow(new NonIterableArrayList<Object>( 125 Arrays.asList("a", 1, 2, 3, 4, 5, 126 new byte[] {(byte) 0xaa, (byte) 0x55}, "Too many!"))); 127 fail(); 128 } catch (IllegalArgumentException e) { /* expected */ } 129 } 130 testRowBuilderOffer()131 public void testRowBuilderOffer() { 132 MatrixCursor cursor = newMatrixCursor(); 133 134 cursor.newRow() 135 .add("float", 4.2f) 136 .add("string", "foobar") 137 .add("blob", new byte[] {(byte) 0xaa, (byte) 0x55}) 138 .add("lolwat", "kittens"); 139 140 cursor.newRow(); 141 142 cursor.newRow() 143 .add("string", "zero") 144 .add("string", "one") 145 .add("string", "two") 146 .add("lolwat", "kittens"); 147 148 assertTrue(cursor.moveToFirst()); 149 assertEquals("foobar", cursor.getString(0)); 150 assertEquals(null, cursor.getString(1)); 151 assertEquals(0, cursor.getShort(1)); 152 assertEquals(0, cursor.getInt(2)); 153 assertEquals(0, cursor.getLong(3)); 154 assertEquals(4.2f, cursor.getFloat(4)); 155 assertEquals(0.0d, cursor.getDouble(5)); 156 MoreAsserts.assertEquals(new byte[] {(byte) 0xaa, (byte) 0x55}, cursor.getBlob(6)); 157 158 assertTrue(cursor.moveToNext()); 159 assertEquals(null, cursor.getString(0)); 160 assertEquals(0, cursor.getShort(1)); 161 assertEquals(0, cursor.getInt(2)); 162 assertEquals(0, cursor.getLong(3)); 163 assertEquals(0.0f, cursor.getFloat(4)); 164 assertEquals(0.0d, cursor.getDouble(5)); 165 assertEquals(null, cursor.getBlob(6)); 166 167 assertTrue(cursor.moveToNext()); 168 assertEquals("two", cursor.getString(0)); 169 assertEquals(0, cursor.getShort(1)); 170 assertEquals(0, cursor.getInt(2)); 171 assertEquals(0, cursor.getLong(3)); 172 assertEquals(0.0f, cursor.getFloat(4)); 173 assertEquals(0.0d, cursor.getDouble(5)); 174 assertEquals(null, cursor.getBlob(6)); 175 176 assertTrue(cursor.isLast()); 177 assertFalse(cursor.moveToNext()); 178 assertTrue(cursor.isAfterLast()); 179 } 180 181 static class NonIterableArrayList<T> extends ArrayList<T> { 182 NonIterableArrayList()183 NonIterableArrayList() {} 184 NonIterableArrayList(Collection<? extends T> ts)185 NonIterableArrayList(Collection<? extends T> ts) { 186 super(ts); 187 } 188 189 @Override iterator()190 public Iterator<T> iterator() { 191 throw new AssertionError(); 192 } 193 } 194 newMatrixCursor()195 private MatrixCursor newMatrixCursor() { 196 return new MatrixCursor(new String[] { 197 "string", "short", "int", "long", "float", "double", "blob" }); 198 } 199 checkValues(MatrixCursor cursor)200 private void checkValues(MatrixCursor cursor) { 201 assertEquals("a", cursor.getString(0)); 202 assertEquals(1, cursor.getShort(1)); 203 assertEquals(2, cursor.getInt(2)); 204 assertEquals(3, cursor.getLong(3)); 205 assertEquals(4.0f, cursor.getFloat(4)); 206 assertEquals(5.0D, cursor.getDouble(5)); 207 MoreAsserts.assertEquals(new byte[] {(byte) 0xaa, (byte) 0x55}, cursor.getBlob(6)); 208 } 209 210 } 211