Tidy up ddl-generator BaseTestCase

This commit is contained in:
rbygrave committed 2021-09-08 17:31:14 +12:00
1 parent cee879d2f3
commit 1049435f86
1 file changed
-166
@@ -28,20 +28,6 @@ public abstract class BaseTestCase {
protected static Logger logger = LoggerFactory.getLogger(BaseTestCase.class);
// @Rule public TestName name = new TestName();
//
// @After
// public void checkForLeak() {
// TransactionScopeManager scope = spiEbeanServer().getTransactionManager().scope();
// SpiTransaction trans = scope.getInScope();
// if (trans != null) {
// String msg = getClass().getSimpleName() + "." + name.getMethodName() + " did not clear threadScope:" + trans;
// scope.clearExternal(); // clear for next test
// fail(msg);
// }
// }
/**
* this is the clock delta that may occur between testing machine and db server.
* If the clock delta of DB server is in future, an "asOf" query may not find the
@@ -68,99 +54,6 @@ public abstract class BaseTestCase {
}
}
protected void clearAllL2Cache() {
server().cacheManager().clearAll();
}
protected void resetAllMetrics() {
server().metaInfo().resetAllMetrics();
}
protected ServerMetrics collectMetrics() {
return server().metaInfo().collectMetrics();
}
protected List<MetaTimedMetric> visitTimedMetrics() {
return collectMetrics().timedMetrics();
}
protected List<MetaTimedMetric> sqlMetrics() {
List<MetaTimedMetric> timedMetrics = visitTimedMetrics();
return timedMetrics.stream()
.filter((it) -> it.name().startsWith("sql.") || it.name().startsWith("orm."))
.collect(Collectors.toList());
}
protected SpiTransaction getInScopeTransaction() {
return spiEbeanServer().transactionManager().scope().getInScope();
}
/**
* Return the generated sql trimming column alias if required.
*/
protected String sqlOf(Query<?> query) {
return trimSql(query.getGeneratedSql());
}
/**
* Return the generated sql trimming column alias if required.
*/
protected String sqlOf(Query<?> query, int columns) {
return trimSql(query.getGeneratedSql(), columns);
}
protected void assertSqlBind(String sql) {
assertThat(sql).contains("-- bind");
}
protected void assertSqlBind(List<String> sql, int i) {
assertThat(sql.get(i)).contains("-- bind");
}
protected void assertSqlBind(List<String> sql, int from, int to) {
for (int i = from; i <= to; i++) {
assertThat(sql.get(i)).contains("-- bind");
}
}
protected AbstractCharSequenceAssert<?, String> assertSql(Query<?> query, int count) {
return org.assertj.core.api.Assertions.assertThat(sqlOf(query, count));
}
protected AbstractCharSequenceAssert<?, String> assertSql(Query<?> query) {
return org.assertj.core.api.Assertions.assertThat(sqlOf(query));
}
protected AbstractCharSequenceAssert<?, String> assertSql(String sql) {
return org.assertj.core.api.Assertions.assertThat(trimSql(sql));
}
protected String trimSql(String sql) {
if (sql.contains(" c0,") || sql.contains(" c0 ") || sql.contains(" c1,") || sql.contains(" c1 ")) {
// for oracle we include column alias so lets remove those
return trimSql(sql, 10);
}
return sql;//trimSql(sql, 0);
}
/**
* Trim out column alias if required from the generated sql.
*/
protected String trimSql(String sql, int columns) {
for (int i = 0; i <= columns; i++) {
sql = StringHelper.replace(sql, " c" + i + ",", ",");
}
for (int i = 0; i <= columns; i++) {
sql = StringHelper.replace(sql, " c" + i + " ", " ");
}
return sql;
}
public boolean isPlatformCaseSensitive() {
return spiEbeanServer().databasePlatform().isCaseSensitiveCollation();
}
/**
* MS SQL Server does not allow setting explicit values on identity columns
* so tests that do this need to be skipped for SQL Server.
@@ -173,22 +66,10 @@ public abstract class BaseTestCase {
return Platform.H2 == platform();
}
public boolean isHSqlDb() {
return Platform.HSQLDB == platform();
}
public boolean isOracle() {
return Platform.ORACLE == platform();
}
public boolean isNuoDb() {
return Platform.NUODB == platform();
}
public boolean isDb2() {
return Platform.DB2 == platform();
}
public boolean isPostgres() {
return Platform.POSTGRES == platform().base();
}
@@ -228,22 +109,10 @@ public abstract class BaseTestCase {
// do nothing, used to thread sleep
}
protected <T> BeanDescriptor<T> getBeanDescriptor(Class<T> cls) {
return spiEbeanServer().descriptor(cls);
}
protected <T> ServerCacheStatistics getBeanCacheStats(Class<T> cls, boolean reset) {
return server().cacheManager().beanCache(cls).statistics(reset);
}
protected Platform platform() {
return spiEbeanServer().databasePlatform().getPlatform().base();
}
protected IdType idType() {
return spiEbeanServer().databasePlatform().getDbIdentity().getIdType();
}
protected SpiEbeanServer spiEbeanServer() {
return (SpiEbeanServer) DB.getDefault();
}
@@ -252,39 +121,4 @@ public abstract class BaseTestCase {
return DB.getDefault();
}
/**
* Platform specific IN clause assert.
*/
protected void platformAssertIn(String sql, String containsIn) {
if (isPostgres()) {
assertThat(sql).contains(containsIn+" = any(");
} else {
assertThat(sql).contains(containsIn+" in ");
}
// H2 contains("where t0.name in (select * from table(x varchar = ?)");
}
/**
* Platform specific NOT IN clause assert.
*/
protected void platformAssertNotIn(String sql, String containsIn) {
if (isPostgres()) {
assertThat(sql).contains(containsIn+" != all(");
} else {
assertThat(sql).contains(containsIn+" not in ");
}
}
/**
* Platform specific CONCAT clause.
*/
protected String concat(String property0, String separator, String property1) {
return concat(property0, separator, property1, null);
}
protected String concat(String property0, String separator, String property1, String suffix) {
DbExpressionHandler dbExpressionHandler = DbExpressionHandlerFactory.from(spiEbeanServer().databasePlatform());
return dbExpressionHandler.concat(property0, separator, property1, suffix);
}
}