use of org.teiid.client.DQP in project teiid by teiid.
the class TestPreparedStatement method testBatchedUpdateExecution.
/**
* Verify that the <code>executeBatch()</code> method of <code>
* MMPreparedStatement</code> is resulting in the correct command,
* parameter values for each command of the batch, and the request type
* are being set in the request message that would normally be sent to the
* server.
*
* @throws Exception
*/
@Test
public void testBatchedUpdateExecution() throws Exception {
// Build up a fake connection instance for use with the prepared statement
ConnectionImpl conn = Mockito.mock(ConnectionImpl.class);
Mockito.stub(conn.getConnectionProps()).toReturn(new Properties());
DQP dqp = Mockito.mock(DQP.class);
ServerConnection serverConn = Mockito.mock(ServerConnection.class);
LogonResult logonResult = Mockito.mock(LogonResult.class);
// stub methods
Mockito.stub(conn.getServerConnection()).toReturn(serverConn);
Mockito.stub(serverConn.getLogonResult()).toReturn(logonResult);
Mockito.stub(logonResult.getTimeZone()).toReturn(TimeZone.getDefault());
// a dummy result message that is specific to this test case
final ResultsFuture<ResultsMessage> results = new ResultsFuture<ResultsMessage>();
final int[] count = new int[1];
final ResultsMessage rm = new ResultsMessage();
Mockito.stub(dqp.executeRequest(Matchers.anyLong(), (RequestMessage) Matchers.anyObject())).toAnswer(new Answer<ResultsFuture<ResultsMessage>>() {
@Override
public ResultsFuture<ResultsMessage> answer(InvocationOnMock invocation) throws Throwable {
RequestMessage requestMessage = (RequestMessage) invocation.getArguments()[1];
count[0] += requestMessage.getParameterValues().size();
if (count[0] == 100000) {
rm.setException(new TeiidException());
rm.setResults(new List<?>[] { Arrays.asList(Statement.EXECUTE_FAILED) });
} else {
List<?>[] vals = new List<?>[requestMessage.getParameterValues().size()];
Arrays.fill(vals, Arrays.asList(0));
rm.setResults(Arrays.asList(vals));
}
return results;
}
});
rm.setUpdateResult(true);
results.getResultsReceiver().receiveResults(rm);
Mockito.stub(conn.getDQP()).toReturn(dqp);
// some update SQL
// $NON-NLS-1$
String sqlCommand = "delete from table where col=?";
TestableMMPreparedStatement statement = (TestableMMPreparedStatement) getMMPreparedStatement(conn, sqlCommand);
ArrayList<ArrayList<Object>> expectedParameterValues = new ArrayList<ArrayList<Object>>(3);
// Add some batches and their parameter values
expectedParameterValues.add(new ArrayList<Object>(Arrays.asList(new Object[] { new Integer(1) })));
statement.setInt(1, new Integer(1));
statement.addBatch();
expectedParameterValues.add(new ArrayList<Object>(Arrays.asList(new Object[] { new Integer(2) })));
statement.setInt(1, new Integer(2));
statement.addBatch();
expectedParameterValues.add(new ArrayList<Object>(Arrays.asList(new Object[] { new Integer(3) })));
statement.setInt(1, new Integer(3));
statement.addBatch();
// execute the batch and verify that it matches our dummy results
// message set earlier
assertTrue(Arrays.equals(new int[] { 0, 0, 0 }, statement.executeBatch()));
// Now verify the statement's RequestMessage is what we expect
// $NON-NLS-1$
assertEquals("Command does not match", sqlCommand, statement.requestMessage.getCommandString());
// $NON-NLS-1$
assertEquals("Parameter values do not match", expectedParameterValues, statement.requestMessage.getParameterValues());
// $NON-NLS-1$
assertTrue("RequestMessage.isBatchedUpdate should be true", statement.requestMessage.isBatchedUpdate());
// $NON-NLS-1$
assertFalse("RequestMessage.isCallableStatement should be false", statement.requestMessage.isCallableStatement());
// $NON-NLS-1$
assertTrue("RequestMessage.isPreparedStatement should be true", statement.requestMessage.isPreparedStatement());
count[0] = 0;
// large batch handling - should split into 5
for (int i = 0; i < 100000; i++) {
statement.setInt(1, new Integer(1));
statement.addBatch();
}
try {
statement.executeBatch();
fail();
} catch (BatchUpdateException e) {
assertEquals(100000, count[0]);
assertEquals(95309, e.getUpdateCounts().length);
assertEquals(Statement.EXECUTE_FAILED, e.getUpdateCounts()[95308]);
}
}
use of org.teiid.client.DQP in project teiid by teiid.
the class TestStatement method testBatchExecution.
@Test
public void testBatchExecution() throws Exception {
ConnectionImpl conn = Mockito.mock(ConnectionImpl.class);
Mockito.stub(conn.getConnectionProps()).toReturn(new Properties());
DQP dqp = Mockito.mock(DQP.class);
ResultsFuture<ResultsMessage> results = new ResultsFuture<ResultsMessage>();
Mockito.stub(dqp.executeRequest(Mockito.anyLong(), (RequestMessage) Mockito.anyObject())).toReturn(results);
ResultsMessage rm = new ResultsMessage();
rm.setResults(new List<?>[] { Arrays.asList(1), Arrays.asList(2) });
rm.setUpdateResult(true);
results.getResultsReceiver().receiveResults(rm);
Mockito.stub(conn.getDQP()).toReturn(dqp);
StatementImpl statement = new StatementImpl(conn, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
// previously caused npe
statement.clearBatch();
// $NON-NLS-1$
statement.addBatch("delete from table");
// $NON-NLS-1$
statement.addBatch("delete from table1");
assertTrue(Arrays.equals(new int[] { 1, 2 }, statement.executeBatch()));
}
use of org.teiid.client.DQP in project teiid by teiid.
the class TestSocketRemoting method testMethodInvocation.
@Test
public void testMethodInvocation() throws Exception {
ClientServiceRegistryImpl csr = new ClientServiceRegistryImpl() {
@Override
public ClassLoader getCallerClassloader() {
return getClass().getClassLoader();
}
};
csr.registerClientService(ILogon.class, new ILogon() {
public ResultsFuture<?> logoff() throws InvalidSessionException {
ResultsFuture<?> result = new ResultsFuture<Void>();
// $NON-NLS-1$
result.getResultsReceiver().exceptionOccurred(new TeiidComponentException("some exception"));
return result;
}
public LogonResult logon(Properties connectionProperties) throws LogonException, TeiidComponentException {
return new LogonResult();
}
// tests asynch where we don't care about the result
public ResultsFuture<?> ping() throws InvalidSessionException, TeiidComponentException {
return null;
}
@Override
public ResultsFuture<?> ping(Collection<String> sessions) throws TeiidComponentException, CommunicationException {
return null;
}
@Override
public void assertIdentity(SessionToken sessionId) throws InvalidSessionException, TeiidComponentException {
}
@Override
public LogonResult neogitiateGssLogin(Properties connectionProperties, byte[] serviceToken, boolean createSession) throws LogonException {
return null;
}
}, // $NON-NLS-1$
"foo");
// $NON-NLS-1$
csr.registerClientService(FakeService.class, new FakeServiceImpl(), "foo");
final FakeClientServerInstance serverInstance = new FakeClientServerInstance(csr);
SocketServerConnection connection = createFakeConnection(serverInstance);
ILogon logon = connection.getService(ILogon.class);
Future<?> result = logon.ping();
assertNull(result.get(0, TimeUnit.MILLISECONDS));
result = logon.logoff();
try {
result.get(0, TimeUnit.MICROSECONDS);
// $NON-NLS-1$
fail("exception expected");
} catch (ExecutionException e) {
assertTrue(e.getCause() instanceof TeiidComponentException);
}
FakeService service = connection.getService(FakeService.class);
Future<Integer> asynchInteger = service.asynchResult();
assertEquals(new Integer(5), asynchInteger.get(0, TimeUnit.MILLISECONDS));
try {
service.exceptionMethod();
// $NON-NLS-1$
fail("exception expected");
} catch (TeiidProcessingException e) {
}
DQP dqp = connection.getService(DQP.class);
try {
ResultsFuture<?> future = dqp.begin();
future.get();
// $NON-NLS-1$
fail("exception expected");
} catch (Exception e) {
// $NON-NLS-1$
assertTrue(e.getMessage().indexOf("Component not found:") != -1);
}
}
use of org.teiid.client.DQP in project teiid by teiid.
the class EmbeddedServer method start.
public synchronized void start(@SuppressWarnings("hiding") EmbeddedConfiguration config) {
if (running != null) {
throw new IllegalStateException();
}
this.dqp.setLocalProfile(this.embeddedProfile);
this.shutdownListener.setBootInProgress(true);
this.config = config;
System.setProperty("jboss.node.name", config.getNodeName() == null ? "localhost" : config.getNodeName());
this.cmr.setProvider(this);
this.eventDistributorFactoryService = new EmbeddedEventDistributorFactoryService();
this.eventDistributorFactoryService.start();
this.dqp.setEventDistributor(this.eventDistributorFactoryService.getReplicatedEventDistributor());
// $NON-NLS-1$
this.scheduler = Executors.newScheduledThreadPool(config.getMaxAsyncThreads(), new NamedThreadFactory("Asynch Worker"));
this.replicator = config.getObjectReplicator();
if (this.replicator == null && config.getJgroupsConfigFile() != null) {
channelFactory = new SimpleChannelFactory(config);
this.replicator = new JGroupsObjectReplicator(channelFactory, this.scheduler);
try {
this.nodeTracker = new NodeTracker(channelFactory.createChannel("teiid-node-tracker"), config.getNodeName()) {
@Override
public ScheduledExecutorService getScheduledExecutorService() {
return scheduler;
}
};
} catch (Exception e) {
LogManager.logError(LogConstants.CTX_RUNTIME, e, RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40089));
}
}
this.eventDistributorFactoryService = new EmbeddedEventDistributorFactoryService();
// must be called after the replicator is set
this.eventDistributorFactoryService.start();
this.dqp.setEventDistributor(this.eventDistributorFactoryService.getReplicatedEventDistributor());
if (config.getTransactionManager() == null) {
LogManager.logInfo(LogConstants.CTX_RUNTIME, RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40089));
this.transactionService.setTransactionManager((TransactionManager) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { TransactionManager.class }, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
throw new UnsupportedOperationException(RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40089));
}
}));
} else {
this.transactionService.setDetectTransactions(true);
this.transactionService.setTransactionManager(config.getTransactionManager());
}
if (config.getSecurityHelper() != null) {
this.sessionService.setSecurityHelper(config.getSecurityHelper());
} else {
this.sessionService.setSecurityHelper(new DoNothingSecurityHelper());
}
if (config.getSecurityDomain() != null) {
this.sessionService.setSecurityDomain(config.getSecurityDomain());
} else {
// $NON-NLS-1$
this.sessionService.setSecurityDomain("teiid-security");
}
this.sessionService.setVDBRepository(repo);
setBufferManagerProperties(config);
BufferService bs = getBufferService();
this.dqp.setBufferManager(bs.getBufferManager());
startVDBRepository();
// $NON-NLS-1$
rs = new SessionAwareCache<CachedResults>("resultset", config.getCacheFactory(), SessionAwareCache.Type.RESULTSET, config.getMaxResultSetCacheStaleness());
// $NON-NLS-1$
ppc = new SessionAwareCache<PreparedPlan>("preparedplan", config.getCacheFactory(), SessionAwareCache.Type.PREPAREDPLAN, 0);
rs.setTupleBufferCache(bs.getTupleBufferCache());
this.dqp.setResultsetCache(rs);
ppc.setTupleBufferCache(bs.getTupleBufferCache());
this.dqp.setPreparedPlanCache(ppc);
this.dqp.setTransactionService((TransactionService) LogManager.createLoggingProxy(LogConstants.CTX_TXN_LOG, this.transactionService, new Class[] { TransactionService.class }, MessageLevel.DETAIL, Thread.currentThread().getContextClassLoader()));
this.dqp.start(config);
this.sessionService.setDqp(this.dqp);
this.services.setSecurityHelper(this.sessionService.getSecurityHelper());
if (this.config.getAuthenticationType() != null) {
this.services.setAuthenticationType(this.config.getAuthenticationType());
this.sessionService.setAuthenticationType(this.config.getAuthenticationType());
}
this.sessionService.start();
this.services.setVDBRepository(this.repo);
this.materializationMgr = getMaterializationManager();
this.repo.addListener(this.materializationMgr);
this.repo.setAllowEnvFunction(this.config.isAllowEnvFunction());
if (this.nodeTracker != null) {
this.nodeTracker.addNodeListener(this.materializationMgr);
}
this.logon = new LogonImpl(sessionService, null);
services.registerClientService(ILogon.class, logon, LogConstants.CTX_SECURITY);
DQP dqpProxy = DQP.class.cast(Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[] { DQP.class }, new SessionCheckingProxy(dqp, LogConstants.CTX_DQP, MessageLevel.TRACE)));
services.registerClientService(DQP.class, dqpProxy, LogConstants.CTX_DQP);
initDriver();
List<SocketConfiguration> transports = config.getTransports();
if (transports != null && !transports.isEmpty()) {
for (SocketConfiguration socketConfig : transports) {
SocketListener socketConnection = startTransport(socketConfig, bs.getBufferManager(), config.getMaxODBCLobSizeAllowed());
if (socketConfig.getSSLConfiguration() != null) {
try {
socketConfig.getSSLConfiguration().getServerSSLEngine();
} catch (Exception e) {
throw new TeiidRuntimeException(e);
}
}
this.transports.add(socketConnection);
}
}
this.shutdownListener.setBootInProgress(false);
this.shutdownListener.started();
running = true;
}
use of org.teiid.client.DQP in project teiid by teiid.
the class TestResultSet method createMockStatement.
static StatementImpl createMockStatement(int cursorType) throws SQLException {
StatementImpl statement = mock(StatementImpl.class);
DQP dqp = mock(DQP.class);
stub(statement.getDQP()).toReturn(dqp);
stub(statement.getResultSetType()).toReturn(cursorType);
// $NON-NLS-1$
TimeZone tz = TimeZone.getTimeZone("GMT-06:00");
// $NON-NLS-1$
TimeZone serverTz = TimeZone.getTimeZone("GMT-05:00");
stub(statement.getDefaultCalendar()).toReturn(Calendar.getInstance(tz));
stub(statement.getServerTimeZone()).toReturn(serverTz);
return statement;
}
Aggregations