1package com.xtremelabs.robolectric.shadows;
2
3import static org.hamcrest.CoreMatchers.equalTo;
4import static org.hamcrest.CoreMatchers.is;
5import static org.hamcrest.CoreMatchers.nullValue;
6import static org.junit.Assert.assertFalse;
7import static org.junit.Assert.assertThat;
8import static org.junit.Assert.assertTrue;
9
10import com.xtremelabs.robolectric.WithTestDefaultsRunner;
11import org.junit.Before;
12import org.junit.Test;
13import org.junit.runner.RunWith;
14
15import android.database.CursorIndexOutOfBoundsException;
16import android.database.MatrixCursor;
17
18@RunWith(WithTestDefaultsRunner.class)
19public class MatrixCursorTest {
20
21    private MatrixCursor singleColumnSingleNullValueMatrixCursor;
22
23    @Before
24    public void setUp() throws Exception {
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        assertThat(singleColumnSingleNullValueMatrixCursor.getString(0), is(nullValue()));
131    }
132
133    @Test
134    public void returnsZeroWhenGettingIntFromNullColumn() {
135        assertThat(singleColumnSingleNullValueMatrixCursor.getInt(0), is(equalTo(0)));
136    }
137
138    @Test
139    public void returnsZeroWhenGettingLongFromNullColumn() {
140        assertThat(singleColumnSingleNullValueMatrixCursor.getLong(0), is(equalTo(0L)));
141    }
142
143    @Test
144    public void returnsZeroWhenGettingShortFromNullColumn() {
145        assertThat(singleColumnSingleNullValueMatrixCursor.getShort(0), is(equalTo((short) 0)));
146    }
147
148    @Test
149    public void returnsZeroWhenGettingFloatFromNullColumn() {
150        assertThat(singleColumnSingleNullValueMatrixCursor.getFloat(0), is(equalTo(0.0f)));
151    }
152
153    @Test
154    public void returnsZeroWhenGettingDoubleFromNullColumn() {
155        assertThat(singleColumnSingleNullValueMatrixCursor.getDouble(0), is(equalTo(0.0)));
156    }
157}
158