Hi James,

I think I have fixed the problem. The following may answer your
question:

In the onCreate() in my ContentProvider, I was leaving the database
open:

    @Override
public boolean onCreate()
{
    UKMPGDataProvider.init(getContext(), Constants.DATABASE_NAME);
    return (UKMPGDataProvider.getWritableDatabase() == null) ? false :
true;
}
(In my defence, this was copied from a tutorial on the net.)

In my test, the addVehicle() would result in a call the
getWriteableDatabase() on SQLiteOpenHelper, which looks like this
(shortened for clarity):

public synchronized SQLiteDatabase getWritableDatabase() {
    if (mDatabase != null && mDatabase.isOpen() && !
mDatabase.isReadOnly()) {
        return mDatabase;  // The database is already open for
business
    }

        //lots more initialisation here, omitted
        int version = db.getVersion();
        if (version != mNewVersion) {
            db.beginTransaction();
            try {
                if (version == 0) {
                    //DATABASE CREATED HERE
                    onCreate(db);
                    //END
                } else {
                    onUpgrade(db, version, mNewVersion);
                }
                db.setVersion(mNewVersion);
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }
        }
    //omitted code again
}
As I had left the database open, execution would not get passed the
first if statement and the onCreate(db) line would never be reached.

In my code, I would close the database after inserting a vehicle, so
next call to getWriteableDatabase() (getVehicles()) would get passed
the first if statement and execute the onCreate(db).

It doesn't really explain why the first insertion was successful
though. And it doesn't explain why the test passed in 'run' mode. I
added some logging to the onCreate() in my ContentProvider and
SQLiteOpenHelper and in run mode the ContentProvider onCreate() is
called twice.

I don't know if this made the difference or not... If anyone can shed
any light on what the difference is between run and debug when unit
testing, I'd be interested to learn more.

Thanks,
Barry

On Dec 31, 3:28 am, James Black <planiturth...@gmail.com> wrote:
> Where is the database table created?
> On Dec 30, 2011 6:54 PM, "barry" <barry.drinkwa...@gmail.com> wrote:
>
>
>
>
>
>
>
> > I have an app which uses a sqlite database and a ContentProvider to
> > serve up data. The eclipse project has a unit test which adds a record
> > to a database, retrieves it and asserts against the retrieved data:
>
> > public void testAddNewVehicle() { Vehicle vehicle1 = new
> > Vehicle("xyz", "my car", 111f); long result =
> > VehicleProvider.addVehicle(getContext(), vehicle1);
>
> > assertTrue(1 == result);
>
> > ArrayList<Vehicle> vehicles =
> > VehicleProvider.getVehicles(getContext()) ;
>
> > assertEquals(1, vehicles.size()); assertEquals("xyz",
> > vehicles.get(0).getRegistrationNo()); assertEquals("my car",
> > vehicles.get(0).getDescription()); assertEquals(111.0f,
> > vehicles.get(0).getInitialMileage()); }
>
> > My setup method deletes the database:
>
> > @Override protected void setUp() throws Exception { super.setUp();
> > deleteTestDatabase(); }
>
> > The test passes when I choose 'run', but if I choose debug, it fails
> > at theassertEquals(1, vehicles.size()); line. After stepping through
> > the code I have noticed something strange: even though the data
> > insertion succeeds and assertTrue(1 == result); passes, the database
> > does not exist on the file system at this point. It is only created
> > when VehicleProvider.getVehicles(getContext()); is called.
>
> > Both addVehicle() and getVehicles() result in a call
> > togetWritableDatabase(), so I don't see why the first call does not
> > create the database on disk. The addVehicle() method will eventually
> > call insert() (irrelevant code omitted):
>
> > @Override public Uri insert(Uri uri, ContentValues values) { String
> > table = table = Constants.VEHICLE_TABLE_NAME;
>
> > long rowID = UKMPGDataProvider.getWritableDatabase().in sert(table,
> > null, values);
>
> > // ---if added successfully---if (rowID > 0) { Uri insertedRowUri =
> > insertedRowUri = ContentUris.withAppendedId(VEHICLE_CONTENT _URI,
> > rowID);
>
> > getContext().getContentResolver().notifyC hange(insertedRowUri, null);
> > return insertedRowUri; } throw new SQLException("Failed to insert row
> > into " + uri); }
>
> > And the getVehicle() will eventually call query():
>
> > @Override public Cursor query(Uri uri, String[] projection, String
> > selection, String[] selectionArgs, String sortOrder)
> > { SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder();
>
> > sqlBuilder.setTables(uri.getPathSegments( ).get(0));
>
> > if (uriMatcher.match(uri) == VEHICLE_ID) // ---if getting a particular
> > vehicle
>
> > sqlBuilder.appendWhere(BaseColumns._ID + " = " +
> > uri.getPathSegments().get(1));
>
> > if (sortOrder == null || sortOrder == "") { sortOrder =
> > BaseColumns._ID; }
>
> > Cursor c = sqlBuilder.query(UKMPGDataProvider.getWri tableDatabase(),
> > projection, selection, selectionArgs, null, null, sortOrder);
>
> > // ---register to watch a content URI for changes---
>
> > c.setNotificationUri(getContext().getCont entResolver(), uri); return
> > c; }
>
> > As I say, the test passes when not in debug mode.
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to android-developers@googlegroups.com
> > To unsubscribe from this group, send email to
> > android-developers+unsubscr...@googlegroups.com
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to