/** Query .../models URI */
 public void testQueryAllModels() {
   final ContentResolver cr = getContext().getContentResolver();
   // Query all available models
   final Cursor allModels = cr.query(FlashCardsContract.Model.CONTENT_URI, null, null, null, null);
   assertNotNull(allModels);
   try {
     assertTrue("Check that there is at least one result", allModels.getCount() > 0);
     while (allModels.moveToNext()) {
       long modelId = allModels.getLong(allModels.getColumnIndex(FlashCardsContract.Model._ID));
       Uri modelUri =
           Uri.withAppendedPath(FlashCardsContract.Model.CONTENT_URI, Long.toString(modelId));
       final Cursor singleModel = cr.query(modelUri, null, null, null, null);
       assertNotNull(singleModel);
       try {
         assertEquals("Check that there is exactly one result", 1, singleModel.getCount());
         assertTrue("Move to beginning of cursor", singleModel.moveToFirst());
         String nameFromModels =
             allModels.getString(allModels.getColumnIndex(FlashCardsContract.Model.NAME));
         String nameFromModel =
             singleModel.getString(allModels.getColumnIndex(FlashCardsContract.Model.NAME));
         assertEquals("Check that model names are the same", nameFromModel, nameFromModels);
         String flds =
             allModels.getString(allModels.getColumnIndex(FlashCardsContract.Model.FIELD_NAMES));
         assertTrue("Check that valid number of fields", Utils.splitFields(flds).length >= 1);
         Integer numCards =
             allModels.getInt(allModels.getColumnIndex(FlashCardsContract.Model.NUM_CARDS));
         assertTrue("Check that valid number of cards", numCards >= 1);
       } finally {
         singleModel.close();
       }
     }
   } finally {
     allModels.close();
   }
 }
 /** Check that updating the flds column works as expected */
 public void testUpdateNoteFields() {
   final ContentResolver cr = getContext().getContentResolver();
   ContentValues cv = new ContentValues();
   // Change the fields so that the first field is now "newTestValue"
   String[] dummyFields2 = mDummyFields.clone();
   dummyFields2[0] = TEST_FIELD_VALUE;
   for (Uri uri : mCreatedNotes) {
     // Update the flds
     cv.put(FlashCardsContract.Note.FLDS, Utils.joinFields(dummyFields2));
     cr.update(uri, cv, null, null);
     // Query the table again
     Cursor noteCursor =
         cr.query(uri, FlashCardsContract.Note.DEFAULT_PROJECTION, null, null, null);
     try {
       assertNotNull(
           "Check that there is a valid cursor for detail data after update", noteCursor);
       assertEquals(
           "Check that there is one and only one entry after update", 1, noteCursor.getCount());
       assertTrue("Move to first item in cursor", noteCursor.moveToFirst());
       String[] newFlds =
           Utils.splitFields(
               noteCursor.getString(noteCursor.getColumnIndex(FlashCardsContract.Note.FLDS)));
       assertTrue(
           "Check that the flds have been updated correctly",
           Arrays.equals(newFlds, dummyFields2));
     } finally {
       noteCursor.close();
     }
   }
 }
Example #3
0
  public static ArrayList<HashMap<String, String>> getIntentInformation(Context context) {
    openDBIfClosed(context);
    Cursor cursor = null;
    ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    try {
      cursor =
          mMetaDb.query(
              "intentInformation", new String[] {"id", "fields"}, null, null, null, null, "id");
      while (cursor.moveToNext()) {
        HashMap<String, String> item = new HashMap<String, String>();
        item.put("id", Integer.toString(cursor.getInt(0)));
        String fields = cursor.getString(1);
        String[] split = Utils.splitFields(fields);
        String source = null;
        String target = null;
        for (int i = 0; i < split.length; i++) {
          if (source == null || source.length() == 0) {
            source = split[i];
          } else if (target == null || target.length() == 0) {
            target = split[i];
          } else {
            break;
          }
        }
        item.put("source", source);
        item.put("target", target);
        item.put("fields", fields);
        list.add(item);
      }
    } catch (SQLiteException e) {
      upgradeDB(mMetaDb, DATABASE_VERSION);

      Timber.e(e, "Error while querying intentInformation");
    } finally {
      if (cursor != null && !cursor.isClosed()) {
        cursor.close();
      }
    }
    return list;
  }