Skip to content

Commit

Permalink
introduce TestContext
Browse files Browse the repository at this point in the history
  • Loading branch information
stbischof committed Jan 21, 2022
1 parent 8204931 commit 4c034d3
Show file tree
Hide file tree
Showing 11 changed files with 2,572 additions and 2,226 deletions.
10 changes: 10 additions & 0 deletions mondrian/src/test/java/mondrian/junit5/DataLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package mondrian.junit5;

public interface DataLoader {
/**
* @param jdbcConnectionUrl - jdbcConnectionUrl
* @return jdbc connection String
*/
boolean loadDataData(String jdbcConnectionUrl);

}
15 changes: 15 additions & 0 deletions mondrian/src/test/java/mondrian/junit5/DatabaseHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package mondrian.junit5;

import java.io.Closeable;
import java.util.Map;

public interface DatabaseHandler extends Closeable {

/**
*
* @param props - properties
* @return jdbc connection String
*/
String setUpDatabase(Map<String,Object> props);

}
24 changes: 24 additions & 0 deletions mondrian/src/test/java/mondrian/junit5/FoodmardDataLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package mondrian.junit5;

import mondrian.test.loader.MondrianFoodMartLoaderX;

public class FoodmardDataLoader implements DataLoader{

@Override
public boolean loadDataData(String jdbcUrl) {
String[] args=new String[]{
"-verbose",
"-tables",
"-data",
"-indexes",
"-outputJdbcURL="+jdbcUrl,
// "-outputJdbcUser="+mySQLContainer.getUsername(),
// "-outputJdbcPassword="+mySQLContainer.getPassword(),
"-outputJdbcBatchSize=50",
"-jdbcDrivers=com.mysql.cl.jdbc.Driver"
};
MondrianFoodMartLoaderX.main(args);
return true;
}

}
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
package mondrian.junit5;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.extension.ExtensionContext.Namespace.GLOBAL;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.function.Consumer;
import java.util.Locale;

import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.containers.output.OutputFrame;

import mondrian.test.loader.MondrianFoodMartLoaderX;
import mondrian.resource.MondrianResource;

public class MondrianRuntimeExtension
implements ExecutionCondition, BeforeAllCallback, ExtensionContext.Store.CloseableResource {
static Consumer<OutputFrame> x = t -> System.out.println(t.getUtf8String());


private static boolean started = false;
private static MySQLContainer<?> mySQLContainer;

@Override
public void beforeAll(ExtensionContext context) {
Expand All @@ -34,65 +26,21 @@ public void beforeAll(ExtensionContext context) {
started = true;
// registers a callback hook when the root test context is shut down
context.getRoot().getStore(GLOBAL).put("MondrianRuntimeExtensionClosableCallbackHook", this);
defineLocale();

try {
initDB();
loadFootMart();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("##############################################################");
}
}

private void loadFootMart() {

String[] args=new String[]{
"-verbose",
"-tables",
"-data",
"-indexes",
"-outputJdbcURL="+mySQLContainer.getJdbcUrl(),
"-outputJdbcUser="+mySQLContainer.getUsername(),
"-outputJdbcPassword="+mySQLContainer.getPassword(),
"-outputJdbcBatchSize=50",
"-jdbcDrivers=com.mysql.cl.jdbc.Driver"
};
MondrianFoodMartLoaderX.main(args);

private void defineLocale() {
MondrianResource.setThreadLocale( Locale.US );
}

private void initDB() throws SQLException {

mySQLContainer = new MySQLContainer<>("mysql:5.7.34").withDatabaseName("TEST")
.withUsername("user")
.withPassword("pass")
.withEnv("MYSQL_ROOT_HOST", "%").withLogConsumer(x);
System.out.println("11##############################################################");

mySQLContainer.start();
System.out.println("21##############################################################");

String url = mySQLContainer.getJdbcUrl();
System.out.println(url);
Connection con = DriverManager.getConnection(url, "user", "pass");
// Statement stmt = con.createStatement();
// ResultSet rs = stmt.executeQuery("SELECT version() ");
// rs.next();
// String resultSetString = rs.getString(1);

//assertTrue(resultSetString.startsWith("8"), "The database version can be set using a container rule parameter");

}

@Override
public void close() {
// Your "after all tests" logic goes here
if(mySQLContainer!=null) {
mySQLContainer.stop();
mySQLContainer.close();
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@
@Documented
@ExtendWith(MondrianRuntimeExtension.class)
public @interface MondrianRuntimeSupport {

Class<? extends DatabaseHandler> database() default MySQLDatabaseHandler.class;
Class<? extends DataLoader> dataLoader() default FoodmardDataLoader.class;

}
45 changes: 45 additions & 0 deletions mondrian/src/test/java/mondrian/junit5/MySQLDatabaseHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package mondrian.junit5;

import java.io.IOException;
import java.sql.DriverManager;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;

import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.containers.output.OutputFrame;

public class MySQLDatabaseHandler implements DatabaseHandler {

private static MySQLContainer<?> mySQLContainer;
static Consumer<OutputFrame> x = t -> System.out.println(t.getUtf8String());

@Override
public void close() throws IOException {
if (mySQLContainer != null) {
mySQLContainer.stop();
mySQLContainer.close();
}
}

@Override
public String setUpDatabase(Map<String, Object> props) {
String user=props.getOrDefault("username", UUID.randomUUID().toString().replace("-","")).toString();
String pass=props.getOrDefault("password", UUID.randomUUID().toString().replace("-","")).toString();


mySQLContainer = new MySQLContainer<>("mysql:5.7.34").withDatabaseName(user).withUsername(pass)
.withPassword("pass").withEnv("MYSQL_ROOT_HOST", "%").withLogConsumer(x);

mySQLContainer.start();

return mySQLContainer.getJdbcUrl()+"?user="+user+"&password="+pass;
// Connection con = DriverManager.getConnection(url, "user", "pass");

}





}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class OracleDialectTest{
private OracleDialect dialect;

@BeforeEach
protected void setUp() throws Exception {
public void setUp() throws Exception {
when( metaData.getDatabaseProductName() ).thenReturn( Dialect.DatabaseProduct.ORACLE.name() );
when( connection.getMetaData() ).thenReturn( metaData );
dialect = new OracleDialect( connection );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
import java.io.PrintWriter;

/**
* Extension of {@link TestContext} which delegates all behavior to
* Extension of {@link FoodmartTestContextImpl} which delegates all behavior to
* a parent test context.
*
* <p>Derived classes can selectively override methods.
*
* @author jhyde
* @since 7 September, 2005
*/
public class DelegatingTestContext extends TestContext {
public class DelegatingTestContext extends FoodmartTestContextImpl {
protected final TestContext context;

protected DelegatingTestContext(TestContext context) {
Expand Down
12 changes: 6 additions & 6 deletions mondrian/src/test/java/mondrian/test/FoodMartTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public class FoodMartTestCase {
* Returns the test context. Override this method if you wish to use a
* different source for your FoodMart connection.
*/
public static TestContext getTestContext() {
return TestContext.instance();
public static FoodmartTestContextImpl getTestContext() {
return FoodmartTestContextImpl.instance();
}

protected static Connection getConnection() {
Expand Down Expand Up @@ -176,9 +176,9 @@ protected static void assertQueriesReturnSimilarResults(
TestContext testContext)
{
String resultString1 =
TestContext.toString(testContext.executeQuery(query1));
FoodmartTestContextImpl.toString(testContext.executeQuery(query1));
String resultString2 =
TestContext.toString(testContext.executeQuery(query2));
FoodmartTestContextImpl.toString(testContext.executeQuery(query2));
assertEquals(
measureValues(resultString1),
measureValues(resultString2));
Expand Down Expand Up @@ -435,8 +435,8 @@ public static void verifySameNativeAndNot(
Result resultNonNative = context.executeQuery(query);

assertEquals(
TestContext.toString(resultNative),
TestContext.toString(resultNonNative),
FoodmartTestContextImpl.toString(resultNative),
FoodmartTestContextImpl.toString(resultNonNative),
message);

propSaver.reset();
Expand Down
Loading

0 comments on commit 4c034d3

Please sign in to comment.