Home | History | Annotate | Download | only in database
      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 junit.framework.TestCase;
     20 
     21 import java.util.*;
     22 
     23 public class MatrixCursorTest extends TestCase {
     24 
     25     public void testEmptyCursor() {
     26         Cursor cursor = new MatrixCursor(new String[] { "a" });
     27         assertEquals(0, cursor.getCount());
     28     }
     29 
     30     public void testNullValue() {
     31         MatrixCursor cursor = new MatrixCursor(new String[] { "a" });
     32         cursor.newRow().add(null);
     33         cursor.moveToNext();
     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));
     41     }
     42 
     43     public void testMatrixCursor() {
     44         MatrixCursor cursor = newMatrixCursor();
     45 
     46         cursor.newRow()
     47                 .add("a")
     48                 .add(1)
     49                 .add(2)
     50                 .add(3)
     51                 .add(4)
     52                 .add(5);
     53 
     54         cursor.moveToNext();
     55 
     56         checkValues(cursor);
     57 
     58         cursor.newRow()
     59                 .add("a")
     60                 .add("1")
     61                 .add("2")
     62                 .add("3")
     63                 .add("4")
     64                 .add("5");
     65 
     66         cursor.moveToNext();
     67         checkValues(cursor);
     68 
     69         cursor.moveToPrevious();
     70         checkValues(cursor);
     71     }
     72 
     73     public void testAddArray() {
     74         MatrixCursor cursor = newMatrixCursor();
     75 
     76         cursor.addRow(new Object[] { "a", 1, 2, 3, 4, 5 });
     77         cursor.moveToNext();
     78         checkValues(cursor);
     79 
     80         try {
     81             cursor.addRow(new Object[0]);
     82             fail();
     83         } catch (IllegalArgumentException e) { /* expected */ }
     84     }
     85 
     86     public void testAddIterable() {
     87         MatrixCursor cursor = newMatrixCursor();
     88 
     89         cursor.addRow(Arrays.asList("a", 1, 2, 3, 4, 5));
     90         cursor.moveToNext();
     91         checkValues(cursor);
     92 
     93         try {
     94             cursor.addRow(Collections.emptyList());
     95             fail();
     96         } catch (IllegalArgumentException e) { /* expected */ }
     97 
     98         try {
     99             cursor.addRow(Arrays.asList("a", 1, 2, 3, 4, 5, "Too many!"));
    100             fail();
    101         } catch (IllegalArgumentException e) { /* expected */ }
    102     }
    103 
    104     public void testAddArrayList() {
    105         MatrixCursor cursor = newMatrixCursor();
    106 
    107         cursor.addRow(new NonIterableArrayList<Object>(
    108                 Arrays.asList("a", 1, 2, 3, 4, 5)));
    109         cursor.moveToNext();
    110         checkValues(cursor);
    111 
    112         try {
    113             cursor.addRow(new NonIterableArrayList<Object>());
    114             fail();
    115         } catch (IllegalArgumentException e) { /* expected */ }
    116 
    117         try {
    118             cursor.addRow(new NonIterableArrayList<Object>(
    119                     Arrays.asList("a", 1, 2, 3, 4, 5, "Too many!")));
    120             fail();
    121         } catch (IllegalArgumentException e) { /* expected */ }
    122     }
    123 
    124     static class NonIterableArrayList<T> extends ArrayList<T> {
    125 
    126         NonIterableArrayList() {}
    127 
    128         NonIterableArrayList(Collection<? extends T> ts) {
    129             super(ts);
    130         }
    131 
    132         @Override
    133         public Iterator<T> iterator() {
    134             throw new AssertionError();
    135         }
    136     }
    137 
    138     private MatrixCursor newMatrixCursor() {
    139         return new MatrixCursor(new String[] {
    140                 "string", "short", "int", "long", "float", "double" });
    141     }
    142 
    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));
    150     }
    151 
    152 }
    153