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