public void insertProducts() { if (server.find(Product.class).findCount() > 0) { return; } server.execute( () -> { Product p = new Product(); p.setName("Chair"); p.setSku("C001"); server.save(p); p = new Product(); p.setName("Desk"); p.setSku("DSK1"); server.save(p); p = new Product(); p.setName("Computer"); p.setSku("C002"); server.save(p); p = new Product(); p.setName("Printer"); p.setSku("C003"); server.save(p); }); }
public void insertCountries() { if (server.find(Country.class).findCount() > 0) { return; } server.execute( () -> { Country c = new Country(); c.setCode("NZ"); c.setName("New Zealand"); server.save(c); Country au = new Country(); au.setCode("AU"); au.setName("Australia"); server.save(au); }); }
public static synchronized void reset() { if (runOnce) { return; } final ResetBasicData me = new ResetBasicData(); server.execute( () -> { if (server.find(Product.class).findCount() > 0) { // we can't really delete this base data as // the test rely on the products being in there return; } // me.deleteAll(); me.insertCountries(); me.insertProducts(); me.insertTestCustAndOrders(); }); runOnce = true; }
@Test public void test() throws SQLException { Properties properties = PropertyMap.defaultProperties(); DataSourceConfig dsConfig = new DataSourceConfig(); dsConfig.loadSettings(properties, "h2autocommit"); // "h2autocommit","pg" dsConfig.setAutoCommit(true); DataSourcePool pool = new ConnectionPool("h2autocommit", dsConfig); Connection connection = pool.getConnection(); assertTrue(connection.getAutoCommit()); connection.close(); System.setProperty("ebean.ignoreExtraDdl", "true"); ServerConfig config = new ServerConfig(); config.setName("h2autocommit"); config.loadFromProperties(); config.setDataSource(pool); config.setDefaultServer(false); config.setRegister(false); config.setExplicitTransactionBeginMode(true); config.addClass(UTMaster.class); config.addClass(UTDetail.class); config.setDdlGenerate(true); config.setDdlRun(true); EbeanServer ebeanServer = EbeanServerFactory.create(config); Query<UTMaster> query = ebeanServer.find(UTMaster.class); List<UTMaster> details = ebeanServer.findList(query, null); assertEquals(0, details.size()); UTMaster bean0 = new UTMaster("one0"); Transaction txn0 = ebeanServer.beginTransaction(); try { ebeanServer.save(bean0); txn0.rollback(); } finally { txn0.end(); } // rollback as expected assertEquals(0, ebeanServer.find(UTMaster.class).findCount()); UTMaster bean1 = new UTMaster("one1"); UTMaster bean2 = new UTMaster("two2"); UTMaster bean3 = new UTMaster("three3"); // use a different transaction to do final query check Transaction otherTxn = ebeanServer.createTransaction(); Transaction txn = ebeanServer.beginTransaction(); try { ebeanServer.save(bean1); ebeanServer.save(bean2); // not visible in other transaction Query<UTMaster> query2 = ebeanServer.find(UTMaster.class); details = ebeanServer.findList(query2, otherTxn); assertEquals(0, details.size()); ebeanServer.save(bean3); txn.commit(); } finally { txn.end(); } // commit as expected Query<UTMaster> query3 = ebeanServer.find(UTMaster.class); details = ebeanServer.findList(query3, otherTxn); assertEquals(3, details.size()); }