Skip to content

Instantly share code, notes, and snippets.

Created October 2, 2014 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1f75493bbc2bcc968ea2 to your computer and use it in GitHub Desktop.
Save anonymous/1f75493bbc2bcc968ea2 to your computer and use it in GitHub Desktop.
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import org.h2.jdbcx.JdbcDataSource;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.teiid.adminapi.impl.ModelMetaData;
import org.teiid.deployers.VirtualDatabaseException;
import org.teiid.dqp.internal.datamgr.ConnectorManagerRepository.ConnectorManagerException;
import org.teiid.jdbc.ConnectionImpl;
import org.teiid.runtime.EmbeddedConfiguration;
import org.teiid.runtime.EmbeddedServer;
import org.teiid.runtime.EmbeddedServer.ConnectionFactoryProvider;
import org.teiid.translator.TranslatorException;
import org.teiid.translator.jdbc.h2.H2ExecutionFactory;
public class DbExecutorTestAgainstTeiid
{
private static EmbeddedServer server;
@BeforeClass
static public void init() throws SQLException,
VirtualDatabaseException,
ConnectorManagerException,
TranslatorException
{
final JdbcDataSource jdbcDataSource1 = new JdbcDataSource();
jdbcDataSource1.setURL("jdbc:h2:target/h1");
try (final Connection c = jdbcDataSource1.getConnection())
{
try (Statement s = c.createStatement())
{
s.execute("create table if not exists t (a integer, b integer)");
}
}
server = new EmbeddedServer();
final EmbeddedConfiguration config = new EmbeddedConfiguration();
final H2ExecutionFactory ef = new H2ExecutionFactory();
server.addTranslator("h2", ef);
server.addConnectionFactoryProvider("h2-jndi",
new ConnectionFactoryProvider<DataSource>()
{
@Override
public DataSource getConnectionFactory() throws TranslatorException
{
return jdbcDataSource1;
}
});
server.start(config);
final ModelMetaData m = new ModelMetaData();
m.setName("m");
m.addProperty("useFullSchemaName", "false");
m.addProperty("useCatalogName", "false");
m.addProperty("useQualifiedName", "false");
m.addSourceMapping("hds", "h2", "h2-jndi");
server.deployVDB("test", m);
}
@Test
public void test() throws Exception
{
try (ConnectionImpl connect = server.getDriver()
.connect("jdbc:teiid:test", null))
{
try (Statement s = connect.createStatement())
{
System.out.println("SELECT a FROM t");
s.executeQuery("SELECT a FROM t");
System.out.println("SELECT a FROM public.t");
s.executeQuery("SELECT a FROM public.t");
System.out.println("SELECT a FROM m.t");
s.executeQuery("SELECT a FROM m.t");
}
}
System.out.println("Success!");
}
@AfterClass
static public void stop()
{
server.stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment