Search in sources :

Example 1 with JdbcPkGenerator

use of org.apache.cayenne.dba.JdbcPkGenerator in project cayenne by apache.

the class DataContextExtrasIT method testCommitChangesError.

@Test
public void testCommitChangesError() {
    DataDomain domain = context.getParentDataDomain();
    // setup mockup PK generator that will blow on PK request
    // to emulate an exception
    JdbcAdapter jdbcAdapter = objectFactory.newInstance(JdbcAdapter.class, JdbcAdapter.class.getName());
    PkGenerator newGenerator = new JdbcPkGenerator(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);
    }
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) JdbcPkGenerator(org.apache.cayenne.dba.JdbcPkGenerator) PkGenerator(org.apache.cayenne.dba.PkGenerator) JdbcPkGenerator(org.apache.cayenne.dba.JdbcPkGenerator) Test(org.junit.Test)

Aggregations

CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)1 JdbcPkGenerator (org.apache.cayenne.dba.JdbcPkGenerator)1 PkGenerator (org.apache.cayenne.dba.PkGenerator)1 DbAttribute (org.apache.cayenne.map.DbAttribute)1 Artist (org.apache.cayenne.testdo.testmap.Artist)1 Test (org.junit.Test)1