1 /* 2 * Copyright (C) 2008 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.cts; 18 19 import java.util.Arrays; 20 21 import android.database.AbstractWindowedCursor; 22 import android.database.CharArrayBuffer; 23 import android.database.CursorIndexOutOfBoundsException; 24 import android.database.CursorWindow; 25 import android.database.StaleDataException; 26 import android.test.InstrumentationTestCase; 27 28 public class AbstractWindowedCursorTest extends InstrumentationTestCase { 29 private static final String TEST_STRING = "TESTSTRING"; 30 private static final int COLUMN_INDEX0 = 0; 31 private static final int COLUMN_INDEX1 = 1; 32 private static final int ROW_INDEX0 = 0; 33 private static final int TEST_COLUMN_COUNT = 7; 34 private MockAbstractWindowedCursor mCursor; 35 private CursorWindow mWindow; 36 37 @Override setUp()38 protected void setUp() throws Exception { 39 super.setUp(); 40 41 mCursor = new MockAbstractWindowedCursor(); 42 mWindow = new CursorWindow(false); 43 } 44 45 @Override tearDown()46 protected void tearDown() throws Exception { 47 mCursor.close(); 48 mWindow.close(); 49 super.tearDown(); 50 } 51 testIsNull()52 public void testIsNull() { 53 mCursor.setWindow(mWindow); 54 55 assertTrue(mWindow.setNumColumns(TEST_COLUMN_COUNT)); 56 mCursor.moveToFirst(); 57 assertTrue(mCursor.isNull(COLUMN_INDEX0)); 58 assertTrue(mWindow.allocRow()); 59 60 String str = "abcdefg"; 61 assertTrue(mWindow.putString(str, ROW_INDEX0, COLUMN_INDEX0)); 62 assertFalse(mCursor.isNull(COLUMN_INDEX0)); 63 } 64 testIsBlob()65 public void testIsBlob() { 66 mCursor.setWindow(mWindow); 67 assertTrue(mWindow.setNumColumns(TEST_COLUMN_COUNT)); 68 assertTrue(mWindow.allocRow()); 69 70 mCursor.moveToFirst(); 71 assertFalse(mCursor.isBlob(COLUMN_INDEX0)); 72 73 String str = "abcdefg"; 74 assertTrue(mWindow.putString(str, ROW_INDEX0, COLUMN_INDEX0)); 75 assertTrue(mWindow.putBlob(new byte[10], ROW_INDEX0, COLUMN_INDEX1)); 76 assertTrue(mCursor.isBlob(COLUMN_INDEX1)); 77 } 78 testHasWindow()79 public void testHasWindow() { 80 assertFalse(mCursor.hasWindow()); 81 assertNull(mCursor.getWindow()); 82 83 mCursor.setWindow(mWindow); 84 assertTrue(mCursor.hasWindow()); 85 assertSame(mWindow, mCursor.getWindow()); 86 87 mCursor.setWindow(null); 88 assertFalse(mCursor.hasWindow()); 89 assertNull(mCursor.getWindow()); 90 } 91 testGetString()92 public void testGetString() { 93 mCursor.setWindow(mWindow); 94 assertTrue(mWindow.setNumColumns(TEST_COLUMN_COUNT)); 95 assertTrue(mWindow.allocRow()); 96 97 mCursor.moveToFirst(); 98 String str = "abcdefg"; 99 assertTrue(mWindow.putString(str, ROW_INDEX0, COLUMN_INDEX0)); 100 assertEquals(str, mCursor.getString(COLUMN_INDEX0)); 101 } 102 testGetShort()103 public void testGetShort() { 104 mCursor.setWindow(mWindow); 105 assertTrue(mWindow.setNumColumns(TEST_COLUMN_COUNT)); 106 assertTrue(mWindow.allocRow()); 107 108 mCursor.moveToFirst(); 109 short shortNumber = 10; 110 assertTrue(mWindow.putLong((long) shortNumber, ROW_INDEX0, COLUMN_INDEX0)); 111 assertEquals(shortNumber, mCursor.getShort(COLUMN_INDEX0)); 112 } 113 testGetLong()114 public void testGetLong() { 115 mCursor.setWindow(mWindow); 116 assertTrue(mWindow.setNumColumns(TEST_COLUMN_COUNT)); 117 assertTrue(mWindow.allocRow()); 118 119 mCursor.moveToFirst(); 120 long longNumber = 10; 121 assertTrue(mWindow.putLong(longNumber, ROW_INDEX0, COLUMN_INDEX0)); 122 assertEquals(longNumber, mCursor.getLong(COLUMN_INDEX0)); 123 } 124 testGetInt()125 public void testGetInt() { 126 mCursor.setWindow(mWindow); 127 assertTrue(mWindow.setNumColumns(TEST_COLUMN_COUNT)); 128 assertTrue(mWindow.allocRow()); 129 130 mCursor.moveToFirst(); 131 int intNumber = 10; 132 assertTrue(mWindow.putLong((long) intNumber, ROW_INDEX0, COLUMN_INDEX0)); 133 assertEquals(intNumber, mCursor.getInt(COLUMN_INDEX0)); 134 } 135 testGetFloat()136 public void testGetFloat() { 137 mCursor.setWindow(mWindow); 138 assertTrue(mWindow.setNumColumns(TEST_COLUMN_COUNT)); 139 assertTrue(mWindow.allocRow()); 140 141 mCursor.moveToFirst(); 142 float f1oatNumber = 1.26f; 143 assertTrue(mWindow.putDouble((double) f1oatNumber, ROW_INDEX0, COLUMN_INDEX0)); 144 assertEquals(f1oatNumber, mCursor.getFloat(COLUMN_INDEX0)); 145 } 146 testGetDouble()147 public void testGetDouble() { 148 mCursor.setWindow(mWindow); 149 assertTrue(mWindow.setNumColumns(TEST_COLUMN_COUNT)); 150 assertTrue(mWindow.allocRow()); 151 152 double db1 = 1.26; 153 assertTrue(mWindow.putDouble(db1, ROW_INDEX0, COLUMN_INDEX0)); 154 155 double db2 = mWindow.getDouble(ROW_INDEX0, COLUMN_INDEX0); 156 assertEquals(db1, db2); 157 158 mCursor.moveToFirst(); 159 double cd = mCursor.getDouble(COLUMN_INDEX0); 160 assertEquals(db1, cd); 161 } 162 testGetBlob()163 public void testGetBlob() { 164 byte TEST_VALUE = 3; 165 byte BLOB_SIZE = 100; 166 assertTrue(mWindow.setNumColumns(TEST_COLUMN_COUNT)); 167 assertTrue(mWindow.allocRow()); 168 assertTrue(mWindow.putString("", ROW_INDEX0, COLUMN_INDEX0)); 169 170 byte[] blob = new byte[BLOB_SIZE]; 171 Arrays.fill(blob, TEST_VALUE); 172 assertTrue(mWindow.putBlob(blob, ROW_INDEX0, COLUMN_INDEX1)); 173 174 mCursor.setWindow(mWindow); 175 mCursor.moveToFirst(); 176 177 byte[] targetBuffer = mCursor.getBlob(COLUMN_INDEX1); 178 assertEquals(BLOB_SIZE, targetBuffer.length); 179 assertTrue(Arrays.equals(blob, targetBuffer)); 180 } 181 testCopyStringToBuffer()182 public void testCopyStringToBuffer() { 183 assertTrue(mWindow.setNumColumns(TEST_COLUMN_COUNT)); 184 assertTrue(mWindow.allocRow()); 185 assertTrue(mWindow.putString(TEST_STRING, ROW_INDEX0, COLUMN_INDEX0)); 186 assertTrue(mWindow.putString("", ROW_INDEX0, COLUMN_INDEX1)); 187 188 mCursor.setWindow(mWindow); 189 mCursor.moveToFirst(); 190 191 CharArrayBuffer charArrayBuffer = new CharArrayBuffer(TEST_STRING.length()); 192 193 mCursor.copyStringToBuffer(COLUMN_INDEX0, charArrayBuffer); 194 assertEquals(TEST_STRING.length(), charArrayBuffer.sizeCopied); 195 assertTrue(Arrays.equals(TEST_STRING.toCharArray(), charArrayBuffer.data)); 196 197 Arrays.fill(charArrayBuffer.data, '\0'); 198 mCursor.copyStringToBuffer(COLUMN_INDEX1, charArrayBuffer); 199 assertEquals(0, charArrayBuffer.sizeCopied); 200 } 201 testCheckPosition()202 public void testCheckPosition() { 203 try { 204 mCursor.checkPosition(); 205 fail("testCheckPosition failed"); 206 } catch (CursorIndexOutOfBoundsException e) { 207 // expected 208 } 209 210 try { 211 assertTrue(mCursor.moveToPosition(mCursor.getCount() - 1)); 212 mCursor.checkPosition(); 213 fail("testCheckPosition failed"); 214 } catch (StaleDataException e) { 215 // expected 216 } 217 218 assertTrue(mCursor.moveToPosition(mCursor.getCount() - 1)); 219 mCursor.setWindow(mWindow); 220 mCursor.checkPosition(); 221 } 222 223 private class MockAbstractWindowedCursor extends AbstractWindowedCursor { 224 MockAbstractWindowedCursor()225 public MockAbstractWindowedCursor() { 226 } 227 228 @Override getColumnNames()229 public String[] getColumnNames() { 230 return new String[] { 231 "col1", "col2", "col3" 232 }; 233 } 234 235 @Override getCount()236 public int getCount() { 237 return 1; 238 } 239 240 @Override checkPosition()241 protected void checkPosition() { 242 super.checkPosition(); 243 } 244 } 245 } 246