@Before public void setup() { memcachedClient = mock(MemcachedClient.class); stub(memcachedClient.get("key1")).toReturn("value1"); stub(memcachedClient.get("key2")).toReturn("value2"); OperationFuture<Boolean> mockedFuture = mock(OperationFuture.class); stub(memcachedClient.add("keyAdd", 3600, "valueadd")).toReturn(mockedFuture); }
DQP dqp = mock(DQP.class); try { stub(dqp.start((XidImpl)Mockito.anyObject(), Mockito.anyInt(), Mockito.anyInt())).toAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { stub(dqp.rollback((XidImpl)Mockito.anyObject())).toAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { stub(dqp.rollback()).toAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { stub(mock.getService(DQP.class)).toReturn(dqp); stub(mock.getLogonResult()).toReturn(new LogonResult(new SessionToken(1, "admin"), STD_DATABASE_NAME, "fake")); //$NON-NLS-1$ return new ConnectionImpl(mock, props, url);
@Test public void testFindRequiresSource() throws Exception { String modelName = "model"; //$NON-NLS-1$ String functionName = "fakeFunction"; //$NON-NLS-1$ ArrayList<String> bindings = new ArrayList<String>(); bindings.add(modelName); VDBMetaData vdb = Mockito.mock(VDBMetaData.class); ModelMetaData model = Mockito.mock(ModelMetaData.class); Mockito.stub(vdb.getModel(modelName)).toReturn(model); Mockito.stub(model.getSourceNames()).toReturn(bindings); BasicSourceCapabilities basicSourceCapabilities = new BasicSourceCapabilities(); basicSourceCapabilities.setFunctionSupport(functionName, true); ConnectorManagerRepository repo = Mockito.mock(ConnectorManagerRepository.class); ConnectorManager cm = Mockito.mock(ConnectorManager.class); Mockito.stub(cm.getCapabilities()).toThrow(new TranslatorException()); Mockito.stub(repo.getConnectorManager(Mockito.anyString())).toReturn(cm); CachedFinder finder = new CachedFinder(repo, vdb); // Test SourceCapabilities actual = finder.findCapabilities(modelName); assertNotNull(actual); //$NON-NLS-1$ assertFalse(finder.isValid(modelName)); }
@Test public void testExceptionResultsInNormalError() throws Exception { // In this test, we ensure that all handlers are called even if we throw an exception in one RefreshHandler exceptionalHandler = Mockito.mock(RefreshHandler.class); Mockito.stub(exceptionalHandler.handleRefresh(Mockito.anyString(), Mockito.any(String[].class))) .toThrow(new RuntimeException("Exceptional Handler Throws Exception")); RefreshHandler otherExceptionalHandler = Mockito.mock(RefreshHandler.class); Mockito.stub(otherExceptionalHandler.handleRefresh(Mockito.anyString(), Mockito.any(String[].class))) .toThrow(new RuntimeException("More Exceptions")); RefreshRegistry.defaultRegistry().register("exceptional", exceptionalHandler); RefreshRegistry.defaultRegistry().register("exceptional", otherExceptionalHandler); DFSAdmin admin = new DFSAdmin(config); String[] args = new String[]{"-refresh", "localhost:" + cluster.getNameNodePort(), "exceptional"}; int exitCode = admin.run(args); assertEquals(-1, exitCode); // Exceptions result in a -1 Mockito.verify(exceptionalHandler).handleRefresh("exceptional", new String[]{}); Mockito.verify(otherExceptionalHandler).handleRefresh("exceptional", new String[]{}); RefreshRegistry.defaultRegistry().unregisterAll("exceptional"); } }
when(resourceResolver.resolveModule(any(Path.class))).thenReturn(kieModule); stub(ioService.createDirectory(any(org.uberfire.java.nio.file.Path.class))).toAnswer(new Answer<org.uberfire.java.nio.file.Path>() { @Override public org.uberfire.java.nio.file.Path answer(final InvocationOnMock invocationOnMock) throws Throwable {
@Test public void testAsynchTimeout() throws Exception { ConnectionImpl conn = Mockito.mock(ConnectionImpl.class); Mockito.stub(conn.getConnectionProps()).toReturn(new Properties()); final StatementImpl statement = new StatementImpl(conn, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); statement.setQueryTimeoutMS(1); DQP dqp = Mockito.mock(DQP.class); Mockito.stub(statement.getDQP()).toReturn(dqp); final AtomicInteger counter = new AtomicInteger(); Mockito.stub(dqp.cancelRequest(0)).toAnswer(new Answer<Boolean>() { @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { Mockito.stub(dqp.executeRequest(Mockito.anyLong(), (RequestMessage) Mockito.anyObject())).toReturn(future); statement.submitExecute("select 'hello world'", null); synchronized (statement) {
Mockito.stub(scktChnl.write((ByteBuffer)Mockito.anyObject())).toThrow(new IOException());
@Test public void shrinkingChoosesAComponentCapableOfShrinkingTheValue() { stub(first.canShrink(7)).toReturn(true); stub(second.canShrink(7)).toReturn(false); stub(third.canShrink(7)).toReturn(true); when(first.types()).thenReturn(singletonList(Integer.class)); when(first.doShrink(random, 7)).thenReturn(asList(3, 6)); when(random.nextInt(9)).thenReturn(1); assertEquals(asList(3, 6), composite.shrink(random, 7)); verify(first, atLeastOnce()).doShrink(random, 7); } }
static ResultSetImpl helpTestBatching(StatementImpl statement, final int fetchSize, final int batchLength, final int totalLength, final boolean partial) throws TeiidProcessingException, SQLException { DQP dqp = statement.getDQP(); if (dqp == null) { dqp = mock(DQP.class); stub(statement.getDQP()).toReturn(dqp); } stub(statement.getFetchSize()).toReturn(fetchSize); stub(dqp.processCursorRequest(Matchers.eq(REQUEST_ID), Matchers.anyInt(), Matchers.eq(fetchSize))).toAnswer(new Answer<ResultsFuture<ResultsMessage>>() { @Override public ResultsFuture<ResultsMessage> answer( InvocationOnMock invocation) throws Throwable { ResultsFuture<ResultsMessage> nextBatch = new ResultsFuture<ResultsMessage>(); int begin = Math.min(totalLength, (Integer)invocation.getArguments()[1]); if (partial && begin == fetchSize + 1) { begin = begin -5; } int length = Math.min(fetchSize, Math.min(totalLength - begin + 1, batchLength)); nextBatch.getResultsReceiver().receiveResults(exampleResultsMsg4(begin, length, begin + length - 1>= totalLength)); return nextBatch; } }); int initial = Math.min(fetchSize, batchLength); ResultsMessage msg = exampleResultsMsg4(1, initial, initial == totalLength); return new ResultSetImpl(msg, statement, new ResultSetMetaDataImpl(new MetadataProvider(DeferredMetadataProvider.loadPartialMetadata(msg.getColumnNames(), msg.getDataTypes())), null), 0); }
@Test public void testLogonFailsWithMultipleHosts() throws Exception { Properties p = new Properties(); SocketServerInstanceFactory instanceFactory = Mockito.mock(SocketServerInstanceFactory.class); Mockito.stub(instanceFactory.getServerInstance((HostInfo)Mockito.anyObject())).toThrow(new SingleInstanceCommunicationException()); UrlServerDiscovery discovery = new UrlServerDiscovery(new TeiidURL("mm://host1:1,host2:2")); //$NON-NLS-1$ try { new SocketServerConnection(instanceFactory, false, discovery, p); fail("exception expected"); //$NON-NLS-1$ } catch (CommunicationException e) { assertEquals("TEIID20021 No valid host available. Attempted connections to: [host1:1, host2:2]", e.getMessage()); //$NON-NLS-1$ } }
@Test(expected = org.openmrs.api.ProgramNameDuplicatedException.class) public void getProgramByName_shouldFailWhenTwoProgramsFoundWithSameName() { ProgramWorkflowDAO mockDao = Mockito.mock(ProgramWorkflowDAO.class); List<Program> programsWithGivenName = new ArrayList<>(); Program program1 = new Program("A name"); Program program2 = new Program("A name"); programsWithGivenName.add(program1); programsWithGivenName.add(program2); Mockito.stub(mockDao.getProgramsByName("A name", false)).toReturn(programsWithGivenName); Mockito.stub(mockDao.getProgramsByName("A name", true)).toReturn(programsWithGivenName); pws.setProgramWorkflowDAO(mockDao); pws.getProgramByName("A name"); }
Mockito.stub(conn.getConnectionProps()).toReturn(new Properties()); DQP dqp = Mockito.mock(DQP.class); ServerConnection serverConn = Mockito.mock(ServerConnection.class); Mockito.stub(conn.getServerConnection()).toReturn(serverConn); Mockito.stub(serverConn.getLogonResult()).toReturn(logonResult); Mockito.stub(logonResult.getTimeZone()).toReturn(TimeZone.getDefault()); 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) rm.setUpdateResult(true); results.getResultsReceiver().receiveResults(rm); Mockito.stub(conn.getDQP()).toReturn(dqp);
Mockito.stub(scktChnl.write((ByteBuffer[])Mockito.anyObject(), Mockito.anyInt(), Mockito.anyInt())).toThrow(new IOException());
@Test public void getProgramByName_shouldReturnNullWhenThereIsNoProgramForGivenName() { ProgramWorkflowDAO mockDao = Mockito.mock(ProgramWorkflowDAO.class); List<Program> noProgramWithGivenName = new ArrayList<>(); Mockito.stub(mockDao.getProgramsByName("A name", false)).toReturn(noProgramWithGivenName); Mockito.stub(mockDao.getProgramsByName("A name", true)).toReturn(noProgramWithGivenName); pws.setProgramWorkflowDAO(mockDao); Assert.assertNull(pws.getProgramByName("A name")); }
Mockito.stub(scktChnl.write((ByteBuffer[])Mockito.anyObject(), Mockito.anyInt(), Mockito.anyInt())).toThrow(new IOException());
@Before public void setup() { this.server = Mockito.mock(TeiidServer.class); this.context = Mockito.mock(ApplicationContext.class); Environment env = Mockito.mock(Environment.class); Mockito.stub(env.getProperty(Mockito.anyString())).toReturn(null); Mockito.stub(context.getEnvironment()).toReturn(env); }
Mockito.stub(scktChnl.write((ByteBuffer[])Mockito.anyObject(), Mockito.anyInt(), Mockito.anyInt())).toThrow(new IOException());
@Override protected TupleBuffer getBufferDirect(int maxRows) throws BlockedException, TeiidComponentException, TeiidProcessingException { TupleBuffer tb = Mockito.mock(TupleBuffer.class); Mockito.stub(tb.getRowCount()).toReturn((long)(maxRows != -1 ? Math.min(maxRows, data.length) : data.length)); return tb; }
static StatementImpl createMockStatement(int cursorType, MockSettings mockSetting) throws SQLException { StatementImpl statement = mock(StatementImpl.class, mockSetting); DQP dqp = mock(DQP.class); stub(statement.getDQP()).toReturn(dqp); stub(statement.getResultSetType()).toReturn(cursorType); TimeZone tz = TimeZone.getTimeZone("GMT-06:00"); //$NON-NLS-1$ TimeZone serverTz = TimeZone.getTimeZone("GMT-05:00"); //$NON-NLS-1$ stub(statement.getDefaultCalendar()).toReturn(Calendar.getInstance(tz)); stub(statement.getServerTimeZone()).toReturn(serverTz); return statement; }
public Metadata findOrCreateMetadata (final DataObject dataObject) { final Metadata metadata = Mockito.mock(Metadata.class); Mockito.stub(metadata.getDataObject()).toReturn(dataObject); creationCount++; return metadata; } }