public void testCommitChangesError() {
DataContext context = createDataContext();
// setup mockup PK generator that will blow on PK request
// to emulate an exception
PkGenerator newGenerator = new JdbcPkGenerator(new JdbcAdapter()) {
@Override
public Object generatePk(DataNode node, DbAttribute pk) throws Exception {
throw new CayenneRuntimeException("Intentional");
}
};
PkGenerator oldGenerator = getNode().getAdapter().getPkGenerator();
JdbcAdapter adapter = (JdbcAdapter) getNode().getAdapter();
adapter.setPkGenerator(newGenerator);
try {
Artist newArtist = context.newObject(Artist.class);
newArtist.setArtistName("aaa");
context.commitChanges();
fail("Exception expected but not thrown due to missing PK generation routine.");
}
catch (CayenneRuntimeException ex) {
// exception expected
}
finally {
adapter.setPkGenerator(oldGenerator);
}
}