DataContext context = (DataContext) this.context;
DataDomain domain = context.getParentDataDomain();
// 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 = domain
.getDataNodes()
.iterator()
.next()
.getAdapter()
.getPkGenerator();
JdbcAdapter adapter = (JdbcAdapter) domain
.getDataNodes()
.iterator()
.next()
.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);
}
}