0

I have a class with the following method:

/**
 * @param contact
 * @return
 */
public long create(Contact contact) {
    ContentValues contactValues = buildContentValues(contact);

    try {
        return getDb().insert(CONTACTS_TABLE_NAME, null, contactValues);
    } catch (SQLiteConstraintException sce) {
        sce.printStackTrace(); // line 1
        log.debug("Error inserting:" + contact, sce); //line2
        return getDb().update(CONTACTS_TABLE_NAME, contactValues, COLUMN_ID + "=" + contact.userInfo.id, null); //line 3
    }
}

I am running this using an AndroidTestCase as an Android Junit Test from Eclipse Android SDK. When I first started running it, my test was failing. When I stepped through it, it was handling the exception and running the update (marked as line 3).

Hmm...wonder why that is. So I added logging information and re-ran the unit test. When I step through the code, it goes directly to line 3 in the debugger without printing the stack trace. Strange, I think, but this is probably just some code stuck in the bin directory. So I run a project clean and run it again. EXACT same results.

It's a trick, I think. I set a breakpoint on line 3 AND line 1 and re-run the test. Test skips directly to line 3 without hitting the line 1 breakpoint.

Hmmm....Maybe the codes stuck in the bin directory and eclipse can't delete it with a clean. I've seen this happen before. So, I stop eclipse, go to the bin directory, and manually delete all of the files there. I then restart eclipse refresh the file system, run a project clean and then re-run my test. No change.

WTF?

Aha! I think, the code is stuck on the phone. I uninstall all of our software, including the test apps from the phone. Project->Clean and re-run the test. Still no change.

WTFF?

So, I think, I've tested on this phone so much, some code fragments stuck somewhere in there. I use the Android tool and build a fresh emulator. Project->Clean, run the test on the emulator. Exact same results.

FWTFF?

So, I wonder around aimlessly for a bit and have no ideas what to try next, so I'm posting here.

After this was posted, I spoke with a colleague and showed him the problem I was having. He has an eclipse setup slightly different than mine. He used my source and re-created the issue exactly.

FFFFF.

Thom
  • 14,013
  • 25
  • 105
  • 185
  • Do you use Proguard? Maybe these lines have been removed by Proguard? – Simon Sep 02 '14 at 13:52
  • Since I don't know what Proguard is, I'm going to say no. – Thom Sep 02 '14 at 14:07
  • Proguard obfuscates and optimizes java code and is automatically included in Android projects created with the Android SDK. http://developer.android.com/tools/help/proguard.html . I'm not saying I'm sure that this is your problem but it might be. – Simon Sep 02 '14 at 14:35
  • @Simon It appears from http://stackoverflow.com/questions/10642030/turning-proguard-on-off-using-properties that proguard must be enabled to run. I have the proguard lines commented out in my project.properties so that must not be it. – Thom Sep 02 '14 at 19:17

1 Answers1

0

I think the defect was an optical illusion due to some bug. I changed the code to:

public long create(Contact contact) {
    ContentValues contactValues = buildContentValues(contact);

    long result;    
    try {
        result = getDb().insert(CONTACTS_TABLE_NAME, null, contactValues);
    } catch (SQLiteConstraintException sce) {
        sce.printStackTrace();
        log.debug("Error inserting:" + contact, sce);
        result = getDb().update(CONTACTS_TABLE_NAME, contactValues, COLUMN_ID + "=" + contact.userInfo.id, null);
    }

    return result;
}

And the catch statement never got hit.

Thom
  • 14,013
  • 25
  • 105
  • 185