2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.database;
19 import junit.framework.TestCase;
23 public class MatrixCursorTest extends TestCase {
25 public void testEmptyCursor() {
26 Cursor cursor = new MatrixCursor(new String[] { "a" });
27 assertEquals(0, cursor.getCount());
30 public void testNullValue() {
31 MatrixCursor cursor = new MatrixCursor(new String[] { "a" });
32 cursor.newRow().add(null);
34 assertTrue(cursor.isNull(0));
35 assertNull(cursor.getString(0));
36 assertEquals(0, cursor.getShort(0));
37 assertEquals(0, cursor.getInt(0));
38 assertEquals(0L, cursor.getLong(0));
39 assertEquals(0.0f, cursor.getFloat(0));
40 assertEquals(0.0d, cursor.getDouble(0));
43 public void testMatrixCursor() {
44 MatrixCursor cursor = newMatrixCursor();
69 cursor.moveToPrevious();
73 public void testAddArray() {
74 MatrixCursor cursor = newMatrixCursor();
76 cursor.addRow(new Object[] { "a", 1, 2, 3, 4, 5 });
81 cursor.addRow(new Object[0]);
83 } catch (IllegalArgumentException e) { /* expected */ }
86 public void testAddIterable() {
87 MatrixCursor cursor = newMatrixCursor();
89 cursor.addRow(Arrays.asList("a", 1, 2, 3, 4, 5));
94 cursor.addRow(Collections.emptyList());
96 } catch (IllegalArgumentException e) { /* expected */ }
99 cursor.addRow(Arrays.asList("a", 1, 2, 3, 4, 5, "Too many!"));
101 } catch (IllegalArgumentException e) { /* expected */ }
104 public void testAddArrayList() {
105 MatrixCursor cursor = newMatrixCursor();
107 cursor.addRow(new NonIterableArrayList<Object>(
108 Arrays.asList("a", 1, 2, 3, 4, 5)));
113 cursor.addRow(new NonIterableArrayList<Object>());
115 } catch (IllegalArgumentException e) { /* expected */ }
118 cursor.addRow(new NonIterableArrayList<Object>(
119 Arrays.asList("a", 1, 2, 3, 4, 5, "Too many!")));
121 } catch (IllegalArgumentException e) { /* expected */ }
124 static class NonIterableArrayList<T> extends ArrayList<T> {
126 NonIterableArrayList() {}
128 NonIterableArrayList(Collection<? extends T> ts) {
133 public Iterator<T> iterator() {
134 throw new AssertionError();
138 private MatrixCursor newMatrixCursor() {
139 return new MatrixCursor(new String[] {
140 "string", "short", "int", "long", "float", "double" });
143 private void checkValues(MatrixCursor cursor) {
144 assertEquals("a", cursor.getString(0));
145 assertEquals(1, cursor.getShort(1));
146 assertEquals(2, cursor.getInt(2));
147 assertEquals(3, cursor.getLong(3));
148 assertEquals(4.0f, cursor.getFloat(4));
149 assertEquals(5.0D, cursor.getDouble(5));