My apologies for the formatting in the original message! On Dec 30, 11:54 pm, barry <[email protected]> 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 [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

