MatrixCursorTest.java revision 6574247f775f9a026abf8182b62d0d1bd350c394
1package com.xtremelabs.robolectric.shadows; 2 3import android.database.CursorIndexOutOfBoundsException; 4import android.database.MatrixCursor; 5import com.xtremelabs.robolectric.WithTestDefaultsRunner; 6import org.junit.Before; 7import org.junit.Test; 8import org.junit.runner.RunWith; 9 10import static org.hamcrest.CoreMatchers.equalTo; 11import static org.hamcrest.CoreMatchers.is; 12import static org.hamcrest.CoreMatchers.nullValue; 13import static org.junit.Assert.assertFalse; 14import static org.junit.Assert.assertThat; 15import static org.junit.Assert.assertTrue; 16 17@RunWith(WithTestDefaultsRunner.class) 18public class MatrixCursorTest { 19 20 private MatrixCursor singleColumnSingleNullValueMatrixCursor; 21 22 @Before 23 public void setUp() throws Exception 24 { 25 singleColumnSingleNullValueMatrixCursor = new MatrixCursor(new String[]{"a"}); 26 singleColumnSingleNullValueMatrixCursor.addRow(new Object[] { null }); 27 singleColumnSingleNullValueMatrixCursor.moveToFirst(); 28 } 29 30 @Test 31 public void shouldAddRows() throws Exception { 32 MatrixCursor cursor = new MatrixCursor(new String[] { "a", "b", "c"}); 33 cursor.addRow(new Object[] { "foo", 10L, 0.1f }); 34 cursor.addRow(new Object[] { "baz", 20L, null }); 35 assertThat(cursor.getCount(), equalTo(2)); 36 37 assertTrue(cursor.moveToFirst()); 38 39 assertThat(cursor.getString(0), equalTo("foo")); 40 assertThat(cursor.getLong(1), equalTo(10L)); 41 assertThat(cursor.getFloat(2), equalTo(0.1f)); 42 43 assertTrue(cursor.moveToNext()); 44 45 assertThat(cursor.getString(0), equalTo("baz")); 46 assertThat(cursor.getLong(1), equalTo(20L)); 47 assertTrue(cursor.isNull(2)); 48 49 assertFalse(cursor.moveToNext()); 50 } 51 52 @Test 53 public void shouldDefineColumnNames() throws Exception { 54 MatrixCursor cursor = new MatrixCursor(new String[] { "a", "b", "c"}); 55 56 assertThat(cursor.getColumnCount(), equalTo(3)); 57 58 assertThat(cursor.getColumnName(0), equalTo("a")); 59 assertThat(cursor.getColumnName(1), equalTo("b")); 60 assertThat(cursor.getColumnName(2), equalTo("c")); 61 62 assertThat(cursor.getColumnNames(), equalTo(new String[] { "a", "b", "c"} )); 63 64 assertThat(cursor.getColumnIndex("b"), equalTo(1)); 65 assertThat(cursor.getColumnIndex("z"), equalTo(-1)); 66 } 67 68 @Test 69 public void shouldDefineGetBlob() throws Exception { 70 byte[] blob = { 1, 2, 3, 4 }; 71 72 MatrixCursor cursor = new MatrixCursor(new String[] { "a" }); 73 cursor.addRow(new Object[] { blob }); 74 assertTrue(cursor.moveToFirst()); 75 76 assertThat(cursor.getBlob(0), equalTo(blob)); 77 } 78 79 @Test 80 public void shouldAllowTypeFlexibility() throws Exception { 81 MatrixCursor cursor = new MatrixCursor(new String[] { "a", "b", "c" }); 82 cursor.addRow(new Object[] { 42, 3.3 }); 83 assertTrue(cursor.moveToFirst()); 84 85 assertThat(cursor.getString(0), equalTo("42")); 86 assertThat(cursor.getShort(0), equalTo((short) 42)); 87 assertThat(cursor.getInt(0), equalTo(42)); 88 assertThat(cursor.getLong(0), equalTo(42L)); 89 assertThat(cursor.getFloat(0), equalTo(42.0F)); 90 assertThat(cursor.getDouble(0), equalTo(42.0)); 91 92 assertThat(cursor.getString(1), equalTo("3.3")); 93 assertThat(cursor.getShort(1), equalTo((short) 3)); 94 assertThat(cursor.getInt(1), equalTo(3)); 95 assertThat(cursor.getLong(1), equalTo(3L)); 96 assertThat(cursor.getFloat(1), equalTo(3.3F)); 97 assertThat(cursor.getDouble(1), equalTo(3.3)); 98 } 99 100 @Test(expected = IllegalArgumentException.class) 101 public void shouldDefineGetColumnNameOrThrow() throws Exception { 102 MatrixCursor cursor = new MatrixCursor(new String[] { "a", "b", "c"}); 103 cursor.getColumnIndexOrThrow("z"); 104 } 105 106 @Test(expected = CursorIndexOutOfBoundsException.class) 107 public void shouldThrowIndexOutOfBoundsExceptionWithoutData() throws Exception { 108 MatrixCursor cursor = new MatrixCursor(new String[] { "a", "b", "c"}); 109 cursor.getString(0); 110 } 111 112 @Test(expected = CursorIndexOutOfBoundsException.class) 113 public void shouldThrowIndexOutOfBoundsExceptionForInvalidColumn() throws Exception { 114 MatrixCursor cursor = new MatrixCursor(new String[] { "a", "b", "c"}); 115 cursor.addRow(new Object[] { "foo", 10L, 0.1f }); 116 cursor.getString(3); 117 } 118 119 @Test(expected = CursorIndexOutOfBoundsException.class) 120 public void shouldThrowIndexOutOfBoundsExceptionForInvalidColumnLastRow() throws Exception { 121 MatrixCursor cursor = new MatrixCursor(new String[] { "a", "b", "c"}); 122 cursor.addRow(new Object[] { "foo", 10L, 0.1f }); 123 cursor.moveToFirst(); 124 cursor.moveToNext(); 125 cursor.getString(0); 126 } 127 128 @Test 129 public void returnsNullWhenGettingStringFromNullColumn() 130 { 131 assertThat(singleColumnSingleNullValueMatrixCursor.getString(0), is(nullValue())); 132 } 133 134 @Test 135 public void returnsZeroWhenGettingIntFromNullColumn() 136 { 137 assertThat(singleColumnSingleNullValueMatrixCursor.getInt(0), is(equalTo(0))); 138 } 139 140 @Test 141 public void returnsZeroWhenGettingLongFromNullColumn() 142 { 143 assertThat(singleColumnSingleNullValueMatrixCursor.getLong(0), is(equalTo(0L))); 144 } 145 146 @Test 147 public void returnsZeroWhenGettingShortFromNullColumn() 148 { 149 assertThat(singleColumnSingleNullValueMatrixCursor.getShort(0), is(equalTo((short)0))); 150 } 151 152 @Test 153 public void returnsZeroWhenGettingFloatFromNullColumn() 154 { 155 assertThat(singleColumnSingleNullValueMatrixCursor.getFloat(0), is(equalTo(0.0f))); 156 } 157 158 @Test 159 public void returnsZeroWhenGettingDoubleFromNullColumn() 160 { 161 assertThat(singleColumnSingleNullValueMatrixCursor.getDouble(0), is(equalTo(0.0))); 162 } 163} 164