/** * Initializes the DataVerifier. Needs to be called from the testing * framework before the initialize method is invoked. * @param em EntityManager instance of the current persistence context */ public final void init(EntityManager em) { this.em = em; this.emUtil = new JuEmUtil(em); }
/** * Gets a list of the primery key columns for the specified table * <p> * Column names are kept the way the driver returns them (may be upper, lower or mixed case) * @param tableName Table name * @return List of all columns that make up the primary key. If no primary key is applied, an empty list is returned. */ public List<String> getPrimaryKeyColumns(final String tableName) { final String actualTableName = this.getDbType().getDbSpecificHandler(this.asConnUtil()).convertTableNameCasing(tableName); List<String> columnNames = this.extractDatabaseMetaData(new DatabaseMetaDataCallback<List<String>>() { @Override public List<String> processMetaData(DatabaseMetaData dbmd) throws SQLException { ResultSet rs = dbmd.getPrimaryKeys(null, null, actualTableName); List<String> columnNames = new ArrayList<>(); while (rs.next()) { columnNames.add(rs.getString("COLUMN_NAME")); } rs.close(); return columnNames; } }); return columnNames; }
@Override public void doWork(final DbWork work) { this.doWork(new Work() { @Override public void execute(Connection connection) throws SQLException { work.execute(connection); } }); }
/** * Gets a list of all sequence names of the DB. Sequences names are returned the * way the DB driver returns them, which is usually upper case. * @return List of Sequences names * @throws JuDbException If the list cannot be evaluated */ public List<String> getSequenceNames() throws JuDbException { // There doesn't seem to be a generic way to retrieve sequences using JDBC meta data, so // we have to use native queries. The queries are stored in the DB specific handlers. return this.getDbType().getDbSpecificHandler(this.asConnUtil()).getSequenceNames(); }
@Override public List<String> processMetaData(DatabaseMetaData dbmd) throws SQLException { // TODO: Consider Schema names for other DBs; refactor String schemaName = null; if (getDbType() == DbType.ORACLE) { schemaName = getMetaDataUserName(); } ResultSet rs = dbmd.getTables(schemaName, schemaName, null, new String[]{"TABLE"}); List<String> tableNames = new ArrayList<>(); while (rs.next()) { String tableName = rs.getString("TABLE_NAME"); // We check if the TableName already exists in the list as // Oracle seems to return the same table names multiple times on some // Schemas... if (!tableNames.contains(tableName)) { tableNames.add(tableName); } } rs.close(); Collections.sort(tableNames); return tableNames; } });
/** * Assumes that the DB represented by JuEmUtil is none of the * DbTypes specified. * @param emUtil JuEmUtil * @param types Types we assume the DB is not * @deprecated Use dbIsNot(JuConnUtil, DbType...) instead */ @Deprecated public static void dbIsNot(JuEmUtil emUtil, DbType... types) { DbType actualType = emUtil.getDbType(); for (DbType type : types) { Assume.assumeFalse("Assumed DB was not " + type, type == actualType); } }
@Test public void liquibaseScript_isExecuted() { Assert.assertTrue(JuCollectionUtils.collectionContainsIgnoreCase(this.emUtil.getTableNames(), "TestingEntity")); }
/** * Gets the Connection URL of this EntityManager from the MetaData. * @return Connection URL */ public String getMetaDataUrl() { return this.extractDatabaseMetaData(new DatabaseMetaDataCallback<String>() { @Override public String processMetaData(DatabaseMetaData dbmd) throws SQLException { return dbmd.getURL(); } }); }
@Test public void canResetIdentityGeneration_forSequences() { JuAssumeUtils.dbIsNot(this.emUtil, DbType.MYSQL); DbSchemaUtil su = new DbSchemaUtil(this.emUtil); su.prepareDefaultSchemaAndTestData(); su.runLiquibaseChangeLog("ch/inftec/ju/dbutil/test/JuEmUtilTest_canListSequences.xml"); this.emUtil.resetIdentityGenerationOrSequences(1); // Is done by prepareDefaultSchemaAndTestData, but the testSequence might just have been created //CREATE SEQUENCE PUBLIC.testSequence Assert.assertEquals(new Long(1L), this.emUtil.getNextValueFromSequence("testSequence")); this.emUtil.resetIdentityGenerationOrSequences(10); Assert.assertEquals(new Long(10L), this.emUtil.getNextValueFromSequence("testSequence")); }
/** * Creates a DbDataUtil that will use the internal "raw" DB connection * of the EntityManager to perform data export and import. * @param emUtil Wrapper around an EntityManager instance */ public DbDataUtil(JuEmUtil emUtil) { this(emUtil.asConnUtil()); }
this.runInitializer(dataSetInfo.getAnnotation().preInitializer(), emUtil.getEm()); this.runInitializer(dataSetInfo.getAnnotation().postInitializer(), emUtil.getEm()); emUtil.resetIdentityGenerationOrSequences(sequenceValue);
@Test public void getMetaDataUserName_returnsNotNull() { Assert.assertNotNull(this.emUtil.getMetaDataUserName()); }
@Test public void canResetIdentityGeneration_forPrimeryKeys() { new DbSchemaUtil(this.emUtil).prepareDefaultSchemaAndTestData(); // Try to set identity generation to 10 this.emUtil.resetIdentityGenerationOrSequences(10); TestingEntity te1 = new TestingEntity(); this.em.persist(te1); Assert.assertEquals(new Long(10L), te1.getId()); // Delete all TestingEntities and set identity generation to 1 this.em.createQuery("delete from TestingEntity t").executeUpdate(); this.emUtil.resetIdentityGenerationOrSequences(1); TestingEntity te2 = new TestingEntity(); this.em.persist(te2); Assert.assertEquals(new Long(1L), te2.getId()); }
/** * Resets identity generation of all tables or sequences to allow for predictable * and repeatable entity generation. * <p> * The exact behaviour of identity generation may vary from DB to DB. * @param val Value for the next primary key */ public void resetIdentityGenerationOrSequences(int val) { this.getDbType().getDbSpecificHandler(this.asConnUtil()).resetIdentityGenerationOrSequences(val); }
@Test public void getDriverName_returnsNotNull() { Assert.assertNotNull(this.emUtil.getDbType()); }
@Test public void tablesAreCreated_usingLiquibaseExplicitly() { DbSchemaUtil su = new DbSchemaUtil(this.em); su.clearSchema(); Assert.assertFalse(JuCollectionUtils.collectionContainsIgnoreCase(this.emUtil.getTableNames(), "TestingEntity_LB")); su.runLiquibaseChangeLog("ch/inftec/ju/dbutil/test/LiquibaseTestDataTest_testingEntity.xml"); Assert.assertTrue(JuCollectionUtils.collectionContainsIgnoreCase(this.emUtil.getTableNames(), "TestingEntity_LB")); }
/** * Gets the UserName from the connection of this EntityManager from the MetaData. * @return Connection UserName */ public String getMetaDataUserName() { return this.extractDatabaseMetaData(new DatabaseMetaDataCallback<String>() { @Override public String processMetaData(DatabaseMetaData dbmd) throws SQLException { return dbmd.getUserName(); } }); }
public DbSchemaUtil(JuEmUtil emUtil) { this(emUtil.asConnUtil()); }
/** * Create a new DbDataUtil that will use the specified EntityManager to get * a raw connection to the DB and execute SQL queries. * @param em EntityManager instance to execute SQL in a JDBC connection */ public DbDataUtil(EntityManager em) { this(new JuEmUtil(em)); }
public Long getNextValueFromSequence(String sequenceName) throws JuDbException { return this.getDbType().getDbSpecificHandler(this.asConnUtil()).getNextValueFromSequence(sequenceName); }