How to use hasCaptured method of org.easymock.Capture class

Best Easymock code snippet using org.easymock.Capture.hasCaptured

Source:TransparentReplayRendererTest.java Github

copy

Full Screen

...231 TransparentReplayRenderer cut2 = new TransparentReplayRenderer(new IdentityHttpHeaderProcessor());232 cut2.renderResource(request, response, wbRequest, result,233 headersResource, payloadResource, uriConverter, results);234 EasyMock.verify(response);235 assertFalse("Transfer-Encoding header must not be set", transferEncodingCapture.hasCaptured());236 // content is the original gzip-compressed bytes for PAYLOAD_GIF.237 String output = new String(servletOutput.getBytes(), "UTF-8");238 assertEquals(payload, output);239 }240 protected byte[] buildTestBytes(int length) {241 byte[] bytes = new byte[length];242 for (int i = 0; i < length; i++) {243 bytes[i] = (byte)(i & 0xff);244 }245 return bytes;246 }247 public void testRenderResource_Range_From200() throws Exception {248 final String ct = "application/octet-stream";249 final byte[] payload = buildTestBytes(1024);250 final byte[] recordBytes = TestWARCRecordInfo.buildHttpResponseBlock(251 "200 OK", ct, payload, false);252 WARCRecordInfo recinfo = new TestWARCRecordInfo(recordBytes);253 TestWARCReader ar = new TestWARCReader(recinfo);254 WARCRecord rec = ar.get(0);255 Resource payloadResource = new WarcResource(rec, ar);256 payloadResource.parseHeaders();257 final String range = "bytes=10-19";258 // range request259 EasyMock.expect(request.getHeader(EasyMock.matches("(?i)range$")))260 .andStubReturn(range);261 TestServletOutputStream servletOutput = new TestServletOutputStream();262 // expectations263 response.setStatus(206);264 EasyMock.expect(response.getOutputStream()).andReturn(servletOutput);265 Capture<String> contentRange = new Capture<String>(CaptureType.FIRST);266 response.setHeader(EasyMock.matches("(?i)content-range$"),267 EasyMock.capture(contentRange));268 EasyMock.expectLastCall().once();269 Capture<String> contentLength = new Capture<String>(CaptureType.FIRST);270 response.setHeader(EasyMock.matches("(?i)content-length$"),271 EasyMock.capture(contentLength));272 EasyMock.expectLastCall().once();273 response.setHeader(EasyMock.<String>anyObject(),274 EasyMock.<String>anyObject());275 EasyMock.expectLastCall().anyTimes();276 EasyMock.replay(request, response, uriConverter);277 cut.renderResource(request, response, wbRequest, result,278 payloadResource, uriConverter, results);279 EasyMock.verify(response);280 assertTrue(contentRange.hasCaptured());281 String contentRangeValue = contentRange.getValue();282 assertEquals("bytes 10-19/1024", contentRangeValue);283 assertTrue(contentLength.hasCaptured());284 assertEquals("10", contentLength.getValue());285 byte[] outputBytes = servletOutput.getBytes();286 assertEquals(10, outputBytes.length);287 assertEquals(10, outputBytes[0]);288 assertEquals(19, outputBytes[9]);289 }290 public void testRenderResource_Range_From200_All() throws Exception {291 final String ct = "application/octet-stream";292 final byte[] payload = buildTestBytes(1024);293 final byte[] recordBytes = TestWARCRecordInfo.buildHttpResponseBlock(294 "200 OK", ct, payload, false);295 WARCRecordInfo recinfo = new TestWARCRecordInfo(recordBytes);296 TestWARCReader ar = new TestWARCReader(recinfo);297 WARCRecord rec = ar.get(0);298 Resource payloadResource = new WarcResource(rec, ar);299 payloadResource.parseHeaders();300 final String range = "bytes=0-";301 // range request302 EasyMock.expect(request.getHeader(EasyMock.matches("(?i)range$")))303 .andStubReturn(range);304 TestServletOutputStream servletOutput = new TestServletOutputStream();305 // expectations306 response.setStatus(206);307 EasyMock.expect(response.getOutputStream()).andReturn(servletOutput);308 Capture<String> contentRange = new Capture<String>(CaptureType.FIRST);309 response.setHeader(EasyMock.matches("(?i)content-range$"),310 EasyMock.capture(contentRange));311 EasyMock.expectLastCall().once();312 Capture<String> contentLength = new Capture<String>(CaptureType.FIRST);313 response.setHeader(EasyMock.matches("(?i)content-length$"),314 EasyMock.capture(contentLength));315 EasyMock.expectLastCall().once();316 response.setHeader(EasyMock.<String>anyObject(),317 EasyMock.<String>anyObject());318 EasyMock.expectLastCall().anyTimes();319 EasyMock.replay(request, response, uriConverter);320 cut.renderResource(request, response, wbRequest, result,321 payloadResource, uriConverter, results);322 EasyMock.verify(response);323 assertTrue(contentRange.hasCaptured());324 String contentRangeValue = contentRange.getValue();325 assertEquals("bytes 0-1023/1024", contentRangeValue);326 assertTrue(contentLength.hasCaptured());327 assertEquals("1024", contentLength.getValue());328 byte[] outputBytes = servletOutput.getBytes();329 assertEquals(1024, outputBytes.length);330 assertEquals(0, outputBytes[0]);331 assertEquals(-1, outputBytes[1023]);332 }333 public void testRenderResource_Range_From200_Tail() throws Exception {334 final String ct = "application/octet-stream";335 final byte[] payload = buildTestBytes(1024);336 final byte[] recordBytes = TestWARCRecordInfo.buildHttpResponseBlock(337 "200 OK", ct, payload, false);338 WARCRecordInfo recinfo = new TestWARCRecordInfo(recordBytes);339 TestWARCReader ar = new TestWARCReader(recinfo);340 WARCRecord rec = ar.get(0);341 Resource payloadResource = new WarcResource(rec, ar);342 payloadResource.parseHeaders();343 final String range = "bytes=-10";344 // range request345 EasyMock.expect(request.getHeader(EasyMock.matches("(?i)range$")))346 .andStubReturn(range);347 TestServletOutputStream servletOutput = new TestServletOutputStream();348 // expectations349 response.setStatus(206);350 EasyMock.expect(response.getOutputStream()).andReturn(servletOutput);351 Capture<String> contentRange = new Capture<String>(CaptureType.FIRST);352 response.setHeader(EasyMock.matches("(?i)content-range$"),353 EasyMock.capture(contentRange));354 EasyMock.expectLastCall().once();355 Capture<String> contentLength = new Capture<String>(CaptureType.FIRST);356 response.setHeader(EasyMock.matches("(?i)content-length$"),357 EasyMock.capture(contentLength));358 EasyMock.expectLastCall().once();359 response.setHeader(EasyMock.<String>anyObject(),360 EasyMock.<String>anyObject());361 EasyMock.expectLastCall().anyTimes();362 EasyMock.replay(request, response, uriConverter);363 cut.renderResource(request, response, wbRequest, result,364 payloadResource, uriConverter, results);365 EasyMock.verify(response);366 assertTrue(contentRange.hasCaptured());367 String contentRangeValue = contentRange.getValue();368 assertEquals("bytes 1014-1023/1024", contentRangeValue);369 assertTrue(contentLength.hasCaptured());370 assertEquals("10", contentLength.getValue());371 byte[] outputBytes = servletOutput.getBytes();372 assertEquals(10, outputBytes.length);373 assertEquals(-10, outputBytes[0]);374 assertEquals(-1, outputBytes[9]);375 }376 protected static byte[] buildPartialContentResponseBlock(String ctype,377 byte[] payloadBytes, long startPos, long instanceSize)378 throws IOException {379 assertTrue(startPos + payloadBytes.length <= instanceSize);380 final String CRLF = "\r\n";381 final String contentRange = String.format("bytes %d-%d/%d", startPos, startPos + payloadBytes.length - 1, instanceSize);382 ByteArrayOutputStream blockbuf = new ByteArrayOutputStream();383 Writer bw = new OutputStreamWriter(blockbuf);384 bw.write("HTTP/1.0 206 Partial Content" + CRLF);385 bw.write("Content-Type: " + ctype + CRLF);386 bw.write("Content-Range: " + contentRange + CRLF);387 bw.write("Content-Length: " + payloadBytes.length + CRLF);388 bw.write(CRLF);389 bw.close();390 blockbuf.write(payloadBytes);391 return blockbuf.toByteArray();392 }393 public void testRenderResource_Range_From206() throws Exception {394 final String ct = "application/octet-stream";395 final byte[] payload = buildTestBytes(1024);396 final byte[] recordBytes = buildPartialContentResponseBlock(ct,397 payload, 10, 1040);398 WARCRecordInfo recinfo = new TestWARCRecordInfo(recordBytes);399 TestWARCReader ar = new TestWARCReader(recinfo);400 WARCRecord rec = ar.get(0);401 Resource payloadResource = new WarcResource(rec, ar);402 payloadResource.parseHeaders();403 final String range = "bytes=12-1032";404 // range request405 EasyMock.expect(request.getHeader(EasyMock.matches("(?i)range$")))406 .andStubReturn(range);407 TestServletOutputStream servletOutput = new TestServletOutputStream();408 // expectations409 response.setStatus(206);410 EasyMock.expect(response.getOutputStream()).andReturn(servletOutput);411 Capture<String> contentRange = new Capture<String>(CaptureType.FIRST);412 response.setHeader(EasyMock.matches("(?i)content-range$"),413 EasyMock.capture(contentRange));414 EasyMock.expectLastCall().once();415 Capture<String> contentLength = new Capture<String>(CaptureType.FIRST);416 response.setHeader(EasyMock.matches("(?i)content-length$"),417 EasyMock.capture(contentLength));418 EasyMock.expectLastCall().once();419 response.setHeader(EasyMock.<String>anyObject(),420 EasyMock.<String>anyObject());421 EasyMock.expectLastCall().anyTimes();422 EasyMock.replay(request, response, uriConverter);423 cut.renderResource(request, response, wbRequest, result,424 payloadResource, uriConverter, results);425 EasyMock.verify(response);426 assertTrue(contentRange.hasCaptured());427 String contentRangeValue = contentRange.getValue();428 assertEquals("bytes 12-1032/1040", contentRangeValue);429 assertTrue(contentLength.hasCaptured());430 assertEquals("1021", contentLength.getValue());431 byte[] outputBytes = servletOutput.getBytes();432 assertEquals(1021, outputBytes.length);433 assertEquals(2, outputBytes[0]);434 assertEquals(-2, outputBytes[1020]);435 }436}...

Full Screen

Full Screen

Source:MongoDbAuthenticatorTest.java Github

copy

Full Screen

...73 final MongoDbAuthenticator authenticator = new MongoDbAuthenticator();74 // Start the process with a getnonce command.75 authenticator.startAuthentication(credential, mockConnection);76 // Should have sent the getnonce command.77 assertThat(getNonceCapture.hasCaptured(), is(true));78 assertThat(getNonceCapture.getValue(), instanceOf(Command.class));79 final Command nonceCommand = (Command) getNonceCapture.getValue();80 assertThat(nonceCommand.getCommand(),81 is(BuilderFactory.start().add("getnonce", 1).build()));82 assertThat(getNonceReplyCapture.hasCaptured(), is(true));83 // Trigger the next stage of processing.84 Document reply = BuilderFactory.start()85 .add("nonce", "0123456789abcdef").build();86 getNonceReplyCapture.getValue().callback(87 new Reply(1, 0, 0, Collections.singletonList(reply), true,88 false, false, false));89 // And the auth command is sent.90 assertThat(authCapture.hasCaptured(), is(true));91 assertThat(authCapture.getValue(), instanceOf(Command.class));92 final Command authCommand = (Command) authCapture.getValue();93 assertThat(94 authCommand.getCommand(),95 is(BuilderFactory.start().addInteger("authenticate", 1)96 .add(reply.get("nonce"))97 .addString("user", credential.getUserName())98 .addString("key", "54d8cd8954719ec33c7166807c164969")99 .build()));100 assertThat(authReplyCapture.hasCaptured(), is(true));101 // and trigger the auth.102 reply = BuilderFactory.start().add("ok", 1).build();103 authReplyCapture.getValue().callback(104 new Reply(1, 0, 0, Collections.singletonList(reply), true,105 false, false, false));106 assertThat(authenticator.result(), is(true));107 verify(mockConnection);108 }109 /**110 * Test method for111 * {@link MongoDbAuthenticator#startAuthentication(Credential, Connection)}112 * .113 */114 @Test115 public void testAuthenticationFails() {116 final Credential credential = Credential117 .builder()118 .userName("allanbank")119 .password(120 new char[] { 's', 'u', 'p', 'e', 'r', 's', 'e', 'c',121 'r', 'e', 't' }).build();122 final Capture<Message> getNonceCapture = new Capture<Message>();123 final Capture<ReplyCallback> getNonceReplyCapture = new Capture<ReplyCallback>();124 final Capture<Message> authCapture = new Capture<Message>();125 final Capture<ReplyCallback> authReplyCapture = new Capture<ReplyCallback>();126 final Connection mockConnection = createMock(Connection.class);127 mockConnection.send(capture(getNonceCapture),128 capture(getNonceReplyCapture));129 expectLastCall();130 mockConnection.send(capture(authCapture), capture(authReplyCapture));131 expectLastCall();132 replay(mockConnection);133 final MongoDbAuthenticator authenticator = new MongoDbAuthenticator();134 // Start the process with a getnonce command.135 authenticator.startAuthentication(credential, mockConnection);136 // Should have sent the getnonce command.137 assertThat(getNonceCapture.hasCaptured(), is(true));138 assertThat(getNonceCapture.getValue(), instanceOf(Command.class));139 final Command nonceCommand = (Command) getNonceCapture.getValue();140 assertThat(nonceCommand.getCommand(),141 is(BuilderFactory.start().add("getnonce", 1).build()));142 assertThat(getNonceReplyCapture.hasCaptured(), is(true));143 // Trigger the next stage of processing.144 Document reply = BuilderFactory.start()145 .add("nonce", "0123456789abcdef").build();146 getNonceReplyCapture.getValue().callback(147 new Reply(1, 0, 0, Collections.singletonList(reply), true,148 false, false, false));149 // And the auth command is sent.150 assertThat(authCapture.hasCaptured(), is(true));151 assertThat(authCapture.getValue(), instanceOf(Command.class));152 final Command authCommand = (Command) authCapture.getValue();153 assertThat(154 authCommand.getCommand(),155 is(BuilderFactory.start().addInteger("authenticate", 1)156 .add(reply.get("nonce"))157 .addString("user", credential.getUserName())158 .addString("key", "54d8cd8954719ec33c7166807c164969")159 .build()));160 assertThat(authReplyCapture.hasCaptured(), is(true));161 // and trigger the auth failure.162 reply = BuilderFactory.start().build();163 authReplyCapture.getValue().callback(164 new Reply(1, 0, 0, Collections.singletonList(reply), true,165 false, false, false));166 assertThat(authenticator.result(), is(false));167 verify(mockConnection);168 }169 /**170 * Test method for171 * {@link MongoDbAuthenticator#startAuthentication(Credential, Connection)}172 * .173 */174 @Test175 public void testAuthenticationNonceReplyMissingNonce() {176 final Credential credential = Credential177 .builder()178 .userName("allanbank")179 .password(180 new char[] { 's', 'u', 'p', 'e', 'r', 's', 'e', 'c',181 'r', 'e', 't' }).build();182 final Capture<Message> getNonceCapture = new Capture<Message>();183 final Capture<ReplyCallback> getNonceReplyCapture = new Capture<ReplyCallback>();184 final Connection mockConnection = createMock(Connection.class);185 mockConnection.send(capture(getNonceCapture),186 capture(getNonceReplyCapture));187 expectLastCall();188 replay(mockConnection);189 final MongoDbAuthenticator authenticator = new MongoDbAuthenticator();190 // Start the process with a getnonce command.191 authenticator.startAuthentication(credential, mockConnection);192 // Should have sent the getnonce command.193 assertThat(getNonceCapture.hasCaptured(), is(true));194 assertThat(getNonceCapture.getValue(), instanceOf(Command.class));195 final Command nonceCommand = (Command) getNonceCapture.getValue();196 assertThat(nonceCommand.getCommand(),197 is(BuilderFactory.start().add("getnonce", 1).build()));198 assertThat(getNonceReplyCapture.hasCaptured(), is(true));199 // Trigger the next stage of processing.200 final Document reply = BuilderFactory.start().build();201 getNonceReplyCapture.getValue().callback(202 new Reply(1, 0, 0, Collections.singletonList(reply), true,203 false, false, false));204 try {205 authenticator.result();206 fail("Should have thrown a MongoDbAuthenticationException");207 }208 catch (final MongoDbAuthenticationException error) {209 // Good.210 }211 verify(mockConnection);212 }213 /**214 * Test method for215 * {@link MongoDbAuthenticator#startAuthentication(Credential, Connection)}216 * .217 */218 @Test219 public void testAuthenticationNonceReplyMissingResults() {220 final Credential credential = Credential221 .builder()222 .userName("allanbank")223 .password(224 new char[] { 's', 'u', 'p', 'e', 'r', 's', 'e', 'c',225 'r', 'e', 't' }).build();226 final Capture<Message> getNonceCapture = new Capture<Message>();227 final Capture<ReplyCallback> getNonceReplyCapture = new Capture<ReplyCallback>();228 final Connection mockConnection = createMock(Connection.class);229 mockConnection.send(capture(getNonceCapture),230 capture(getNonceReplyCapture));231 expectLastCall();232 replay(mockConnection);233 final MongoDbAuthenticator authenticator = new MongoDbAuthenticator();234 // Start the process with a getnonce command.235 authenticator.startAuthentication(credential, mockConnection);236 // Should have sent the getnonce command.237 assertThat(getNonceCapture.hasCaptured(), is(true));238 assertThat(getNonceCapture.getValue(), instanceOf(Command.class));239 final Command nonceCommand = (Command) getNonceCapture.getValue();240 assertThat(nonceCommand.getCommand(),241 is(BuilderFactory.start().add("getnonce", 1).build()));242 assertThat(getNonceReplyCapture.hasCaptured(), is(true));243 // Trigger the next stage of processing.244 final List<Document> reply = Collections.emptyList();245 getNonceReplyCapture.getValue().callback(246 new Reply(1, 0, 0, reply, true, false, false, false));247 try {248 authenticator.result();249 fail("Should have thrown a MongoDbAuthenticationException");250 }251 catch (final MongoDbAuthenticationException error) {252 // Good.253 }254 verify(mockConnection);255 }256 /**257 * Test method for258 * {@link MongoDbAuthenticator#startAuthentication(Credential, Connection)}259 * .260 */261 @Test262 public void testAuthenticationNonceReplyNotOk() {263 final Credential credential = Credential264 .builder()265 .userName("allanbank")266 .password(267 new char[] { 's', 'u', 'p', 'e', 'r', 's', 'e', 'c',268 'r', 'e', 't' }).build();269 final Capture<Message> getNonceCapture = new Capture<Message>();270 final Capture<ReplyCallback> getNonceReplyCapture = new Capture<ReplyCallback>();271 final Connection mockConnection = createMock(Connection.class);272 mockConnection.send(capture(getNonceCapture),273 capture(getNonceReplyCapture));274 expectLastCall();275 replay(mockConnection);276 final MongoDbAuthenticator authenticator = new MongoDbAuthenticator();277 // Start the process with a getnonce command.278 authenticator.startAuthentication(credential, mockConnection);279 // Should have sent the getnonce command.280 assertThat(getNonceCapture.hasCaptured(), is(true));281 assertThat(getNonceCapture.getValue(), instanceOf(Command.class));282 final Command nonceCommand = (Command) getNonceCapture.getValue();283 assertThat(nonceCommand.getCommand(),284 is(BuilderFactory.start().add("getnonce", 1).build()));285 assertThat(getNonceReplyCapture.hasCaptured(), is(true));286 // Trigger the next stage of processing.287 final Document reply = BuilderFactory.start().add("ok", 0)288 .add("nonce", "0123456789abcdef").build();289 getNonceReplyCapture.getValue().callback(290 new Reply(1, 0, 0, Collections.singletonList(reply), true,291 false, false, false));292 try {293 authenticator.result();294 fail("Should have thrown a MongoDbAuthenticationException");295 }296 catch (final MongoDbAuthenticationException error) {297 // Good.298 }299 verify(mockConnection);300 }301 /**302 * Test method for {@link MongoDbAuthenticator#result()} .303 */304 @Test305 public void testResultWhenInterrupted() {306 final Credential credential = Credential307 .builder()308 .userName("allanbank")309 .password(310 new char[] { 's', 'u', 'p', 'e', 'r', 's', 'e', 'c',311 'r', 'e', 't' }).build();312 final Capture<Message> getNonceCapture = new Capture<Message>();313 final Capture<ReplyCallback> getNonceReplyCapture = new Capture<ReplyCallback>();314 final Capture<Message> authCapture = new Capture<Message>();315 final Capture<ReplyCallback> authReplyCapture = new Capture<ReplyCallback>();316 final Connection mockConnection = createMock(Connection.class);317 mockConnection.send(capture(getNonceCapture),318 capture(getNonceReplyCapture));319 expectLastCall();320 mockConnection.send(capture(authCapture), capture(authReplyCapture));321 expectLastCall();322 replay(mockConnection);323 final MongoDbAuthenticator authenticator = new MongoDbAuthenticator();324 // Start the process with a getnonce command.325 authenticator.startAuthentication(credential, mockConnection);326 // Should have sent the getnonce command.327 assertThat(getNonceCapture.hasCaptured(), is(true));328 assertThat(getNonceCapture.getValue(), instanceOf(Command.class));329 final Command nonceCommand = (Command) getNonceCapture.getValue();330 assertThat(nonceCommand.getCommand(),331 is(BuilderFactory.start().add("getnonce", 1).build()));332 assertThat(getNonceReplyCapture.hasCaptured(), is(true));333 // Trigger the next stage of processing.334 Document reply = BuilderFactory.start()335 .add("nonce", "0123456789abcdef").build();336 getNonceReplyCapture.getValue().callback(337 new Reply(1, 0, 0, Collections.singletonList(reply), true,338 false, false, false));339 // And the auth command is sent.340 assertThat(authCapture.hasCaptured(), is(true));341 assertThat(authCapture.getValue(), instanceOf(Command.class));342 final Command authCommand = (Command) authCapture.getValue();343 assertThat(344 authCommand.getCommand(),345 is(BuilderFactory.start().addInteger("authenticate", 1)346 .add(reply.get("nonce"))347 .addString("user", credential.getUserName())348 .addString("key", "54d8cd8954719ec33c7166807c164969")349 .build()));350 assertThat(authReplyCapture.hasCaptured(), is(true));351 // and trigger the auth.352 reply = BuilderFactory.start().add("ok", 1).build();353 authReplyCapture.getValue().callback(354 new Reply(1, 0, 0, Collections.singletonList(reply), true,355 false, false, false));356 // Should not clear the interrupted state.357 Thread.currentThread().interrupt();358 assertThat(authenticator.result(), is(true));359 assertThat(Thread.interrupted(), is(true));360 verify(mockConnection);361 }362}...

Full Screen

Full Screen

Source:HistoryManagerTest.java Github

copy

Full Screen

...76 EasyMock.expectLastCall();77 Set<ServiceDefinition> items = Collections.emptySet();78 EasyMock.replay(delegate, serviceLoader);79 manager.processUrl("p/");80 assertTrue(cbCapture.hasCaptured());81 cbCapture.getValue().onSuccess(items);82 assertTrue(contextCapture.hasCaptured());83 ExplorerContext ctxt = contextCapture.getValue();84 assertTrue(ctxt.isEntryListVisible());85 assertEquals(Collections.emptySet(), ctxt.getServicesList());86 assertEquals(RootNavigationItem.PREFERRED_SERVICES, ctxt.getRootNavigationItem());87 EasyMock.verify(delegate, serviceLoader);88 }89 /** Test routing to a specific service. */90 public void testServiceRouting() {91 Capture<Callback<ApiService, String>> cbCapture = new Capture<Callback<ApiService, String>>();92 serviceLoader.loadService(EasyMock.eq("plus"), EasyMock.eq("v1"), EasyMock.capture(cbCapture));93 EasyMock.expectLastCall().times(2);94 Capture<ExplorerContext> contextCapture = new Capture<ExplorerContext>();95 delegate.setContext(EasyMock.capture(contextCapture));96 EasyMock.expectLastCall();97 ApiService service = EasyMock.createMock(ApiService.class);98 EasyMock.expect(service.displayTitle()).andReturn("Plus API");99 EasyMock.expect(service.getVersion()).andReturn("v1");100 EasyMock.replay(serviceLoader, delegate, service);101 manager.processUrl("s/plus/v1/");102 assertTrue(cbCapture.hasCaptured());103 // This call completes the title for breadcrumbs.104 cbCapture.getValue().onSuccess(service);105 // This call completes the service load to generate the context.106 cbCapture.getValue().onSuccess(service);107 assertTrue(contextCapture.hasCaptured());108 ExplorerContext ctxt = contextCapture.getValue();109 assertEquals(service, ctxt.getService());110 assertTrue(ctxt.isEntryListVisible());111 assertEquals(RootNavigationItem.ALL_VERSIONS, ctxt.getRootNavigationItem());112 EasyMock.verify(serviceLoader, delegate, service);113 }114 public void testMethodRouting() {115 Capture<Callback<ApiService, String>> cbCapture = new Capture<Callback<ApiService, String>>();116 serviceLoader.loadService(EasyMock.eq("plus"), EasyMock.eq("v1"), EasyMock.capture(cbCapture));117 EasyMock.expectLastCall().times(2);118 Capture<ExplorerContext> contextCapture = new Capture<ExplorerContext>();119 delegate.setContext(EasyMock.capture(contextCapture));120 EasyMock.expectLastCall();121 ApiService service = EasyMock.createMock(ApiService.class);122 ApiMethod method = EasyMock.createMock(ApiMethod.class);123 Map<String, ApiMethod> allMethods = ImmutableMap.of("plus.method.name", method);124 EasyMock.expect(service.allMethods()).andReturn(allMethods);125 EasyMock.expect(service.displayTitle()).andReturn("Plus API");126 EasyMock.expect(service.getVersion()).andReturn("v1");127 EasyMock.replay(serviceLoader, delegate, service);128 manager.processUrl("s/plus/v1/plus.method.name");129 assertTrue(cbCapture.hasCaptured());130 // This call fills in the breadcrumbs.131 cbCapture.getValue().onSuccess(service);132 // This call generated the context.133 cbCapture.getValue().onSuccess(service);134 assertTrue(contextCapture.hasCaptured());135 ExplorerContext ctxt = contextCapture.getValue();136 assertEquals(service, ctxt.getService());137 assertEquals(method, ctxt.getMethod());138 assertTrue(ctxt.isMethodFormVisible());139 assertFalse(ctxt.isEntryListVisible());140 assertEquals(RootNavigationItem.ALL_VERSIONS, ctxt.getRootNavigationItem());141 EasyMock.verify(serviceLoader, delegate, service);142 }143 /** Test routing when there is a search term, including premature routing. */144 public void testSearchRouting() {145 String searchTerm = "searchTerm";146 EasyMock.expect(resultIndex.search(searchTerm))147 .andReturn(Collections.<SearchResult>emptySet()).times(2);148 Capture<ExplorerContext> contextCapture = new Capture<ExplorerContext>();149 delegate.setContext(EasyMock.capture(contextCapture));150 EasyMock.expectLastCall().times(2);151 EasyMock.replay(resultIndex, delegate);152 manager.processUrl("search/" + searchTerm + "/");153 assertTrue(contextCapture.hasCaptured());154 ExplorerContext ctxt = contextCapture.getValue();155 assertTrue(Iterables.isEmpty(ctxt.getSearchResults()));156 assertTrue(ctxt.isSearchResultsVisible());157 assertEquals(RootNavigationItem.NONE, ctxt.getRootNavigationItem());158 // This will invoke the second calls of search and setContext.159 manager.searchReady();160 EasyMock.verify(resultIndex, delegate);161 }162 public void testNestedSearchRouting() {163 Capture<Callback<ApiService, String>> cbCapture = new Capture<Callback<ApiService, String>>();164 serviceLoader.loadService(EasyMock.eq("plus"), EasyMock.eq("v1"), EasyMock.capture(cbCapture));165 EasyMock.expectLastCall();166 EasyMock.replay(serviceLoader);167 manager.processUrl("search/searchterm/plus/v1/");168 assertTrue(cbCapture.hasCaptured());169 EasyMock.verify(serviceLoader);170 }171 public void testHistoryItemWithAnalytics() {172 String fragment = "h/1";173 analyticsManager.trackEvent(AnalyticsEvent.SHOW_HISTORY);174 EasyMock.expectLastCall();175 @SuppressWarnings("unchecked")176 ValueChangeEvent<String> event = EasyMock.createMock(ValueChangeEvent.class);177 EasyMock.expect(event.getValue()).andReturn(fragment);178 HistoryItem item = createEmptyHistoryItem();179 EasyMock.expect(historyCache.getHistoryItem("1")).andReturn(item);180 Capture<ExplorerContext> contextCapture = new Capture<ExplorerContext>();181 delegate.setContext(EasyMock.capture(contextCapture));182 EasyMock.expectLastCall();183 EasyMock.replay(event, analyticsManager, historyCache, delegate);184 manager.onValueChange(event);185 assertTrue(contextCapture.hasCaptured());186 ExplorerContext ctxt = contextCapture.getValue();187 assertEquals(item, Iterables.getOnlyElement(ctxt.getHistoryItems()));188 assertTrue(ctxt.isHistoryItemVisible());189 assertEquals(RootNavigationItem.REQUEST_HISTORY, ctxt.getRootNavigationItem());190 EasyMock.verify(event, analyticsManager, historyCache, delegate);191 }192 /** This is necessary because history item cannot be subclassed. */193 private HistoryItem createEmptyHistoryItem() {194 ApiRequest mockRequest = EasyMock.createMock(ApiRequest.class);195 ApiResponse mockResponse = EasyMock.createMock(ApiResponse.class);196 return new HistoryItem("1", mockRequest, mockResponse, 0, 0);197 }198}...

Full Screen

Full Screen

hasCaptured

Using AI Code Generation

copy

Full Screen

1package org.easymock.examples;2import org.easymock.Capture;3import org.easymock.EasyMock;4import org.easymock.IAnswer;5import org.junit.Test;6import static org.easymock.EasyMock.*;7import static org.junit.Assert.*;8public class CaptureExample {9 public void testHasCaptured() {10 Capture<String> capture = new Capture<String>();11 ExampleInterface mock = EasyMock.createMock(ExampleInterface.class);12 mock.method1(capture(capture));13 assertTrue(capture.hasCaptured());14 }15}16java -cp .;easymock-3.1.jar CaptureExample17The above error is because the method1() is not called. Hence, the capture object does not have any value. To fix this, we need to call the method1() of the mock object. The following code will fix this error:18package org.easymock.examples;19import org.easymock.Capture;20import org.easymock.EasyMock;21import org.easymock.IAnswer;22import org.junit.Test;23import static org.easymock.EasyMock.*;24import static org.junit.Assert.*;25public class CaptureExample {26 public void testHasCaptured() {27 Capture<String> capture = new Capture<String>();28 ExampleInterface mock = EasyMock.createMock(ExampleInterface.class);29 mock.method1(capture(capture));30 mock.method1("Hello");31 assertTrue(capture.hasCaptured());32 }33}34java -cp .;easymock-3.1.jar CaptureExample35The above output shows that the test case was successful. We can also use the hasCaptured() method to verify the number of times a method was called with a specific argument. The following code will demonstrate this:

Full Screen

Full Screen

hasCaptured

Using AI Code Generation

copy

Full Screen

1import org.easymock.Capture;2import org.easymock.EasyMock;3import org.junit.Test;4import static org.junit.Assert.*;5import static org.easymock.EasyMock.*;6public class 1 {7 public void test1() {8 Capture<java.lang.String> capture = EasyMock.newCapture();9 I1 i1 = createMock(I1.class);10 expect(i1.m1(capture(capture))).andReturn(0);11 replay(i1);12 i1.m1("abc");13 assertTrue(capture.hasCaptured());14 assertEquals("abc", capture.getValue());15 }16}17interface I1 {18 int m1(String s);19}20org.easymock.internal.MocksControl.verifyState(MocksControl.java:66)21 org.easymock.internal.MocksControl.verifyState(MocksControl.java:52)22 org.easymock.internal.MocksControl.verify(MocksControl.java:46)23 org.easymock.EasyMock.verify(EasyMock.java:114)24 1.test1(1.java:21)25 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)26 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)27 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)28 java.lang.reflect.Method.invoke(Method.java:597)29 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)30 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)31 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)32 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)33 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)34 org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)35 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)36 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)37 org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)38 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)39 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:

Full Screen

Full Screen

hasCaptured

Using AI Code Generation

copy

Full Screen

1package org.easymock;2import java.util.List;3import org.easymock.Capture;4import org.easymock.EasyMock;5import org.easymock.IArgumentMatcher;6import org.junit.Test;7public class CaptureTest {8 public static class TestBean {9 public String testMethod(String arg1, int arg2) {10 return arg1;11 }12 }13 public void testCapture() {14 TestBean testBean = EasyMock.createMock(TestBean.class);15 Capture<String> arg1 = EasyMock.newCapture();16 Capture<Integer> arg2 = EasyMock.newCapture();17 EasyMock.expect(testBean.testMethod(EasyMock.capture(arg1),18 EasyMock.capture(arg2))).andReturn("test").anyTimes();19 EasyMock.replay(testBean);20 testBean.testMethod("test", 1);21 testBean.testMethod("test2", 2);22 EasyMock.verify(testBean);23 assert arg1.hasCaptured();24 assert arg2.hasCaptured();25 assert arg1.getValue().equals("test2");26 assert arg2.getValue().equals(2);27 }28 public void testCaptureList() {29 TestBean testBean = EasyMock.createMock(TestBean.class);30 Capture<String> arg1 = EasyMock.newCapture();31 Capture<Integer> arg2 = EasyMock.newCapture();32 EasyMock.expect(testBean.testMethod(EasyMock.capture(arg1),33 EasyMock.capture(arg2))).andReturn("test").anyTimes();34 EasyMock.replay(testBean);35 testBean.testMethod("test", 1);36 testBean.testMethod("test2", 2);37 EasyMock.verify(testBean);38 List<String> arg1Values = arg1.getValues();39 List<Integer> arg2Values = arg2.getValues();40 assert arg1Values.size() == 2;41 assert arg2Values.size() == 2;42 assert arg1Values.get(0).equals("test");43 assert arg1Values.get(1).equals("test2");44 assert arg2Values.get(0) == 1;45 assert arg2Values.get(1) == 2;46 }47 public void testCaptureListWithCustomMatcher() {48 TestBean testBean = EasyMock.createMock(TestBean.class);49 Capture<String> arg1 = EasyMock.newCapture();

Full Screen

Full Screen

hasCaptured

Using AI Code Generation

copy

Full Screen

1package org.easymock.examples;2import org.easymock.Capture;3import org.easymock.EasyMock;4import org.easymock.IAnswer;5import org.junit.Test;6import static org.easymock.EasyMock.*;7import static org.junit.Assert.*;8public class CaptureTest {9 public interface IMethods {10 void voidMethod(String s);11 }12 public void testCapture() {13 IMethods mock = createMock(IMethods.class);14 Capture<String> capturedArg = new Capture<String>();15 mock.voidMethod(capture(capturedArg));16 expectLastCall().andAnswer(new IAnswer<Object>() {17 public Object answer() throws Throwable {18 assertEquals("foo", capturedArg.getValue());19 return null;20 }21 });22 replay(mock);23 mock.voidMethod("foo");24 verify(mock);25 }26}

Full Screen

Full Screen

hasCaptured

Using AI Code Generation

copy

Full Screen

1import org.easymock.Capture;2import org.easymock.EasyMock;3import org.easymock.EasyMockSupport;4import org.junit.Test;5public class TestCapture extends EasyMockSupport {6 public void testCapture() {7 Capture<String> captured = Capture.newInstance();8 Capture<String> captured2 = Capture.newInstance();9 Capture<String> captured3 = Capture.newInstance();10 Capture<String> captured4 = Capture.newInstance();11 Capture<String> captured5 = Capture.newInstance();12 Capture<String> captured6 = Capture.newInstance();13 ITest mock = createMock(ITest.class);14 mock.testMethod(capture(captured), capture(captured2));15 mock.testMethod(capture(captured3), capture(captured4));16 mock.testMethod(capture(captured5), capture(captured6));17 replayAll();18 mock.testMethod("a", "b");19 mock.testMethod("c", "d");20 mock.testMethod("e", "f");21 verifyAll();22 System.out.println("captured.hasCaptured(): " + captured.hasCaptured());23 System.out.println("captured2.hasCaptured(): " + captured2.hasCaptured());24 System.out.println("captured3.hasCaptured(): " + captured3.hasCaptured());25 System.out.println("captured4.hasCaptured(): " + captured4.hasCaptured());26 System.out.println("captured5.hasCaptured(): " + captured5.hasCaptured());27 System.out.println("captured6.hasCaptured(): " + captured6.hasCaptured());28 }29}30public interface ITest {31 public void testMethod(String a, String b);32}33captured.hasCaptured(): true34captured2.hasCaptured(): true35captured3.hasCaptured(): true36captured4.hasCaptured(): true37captured5.hasCaptured(): true38captured6.hasCaptured(): true

Full Screen

Full Screen

hasCaptured

Using AI Code Generation

copy

Full Screen

1import org.easymock.Capture;2import org.easymock.EasyMock;3import org.junit.Test;4import static org.junit.Assert.*;5import static org.easymock.EasyMock.*;6public class CaptureTest {7public void testCapture() {8Capture<String> capture = new Capture<String>();9List<String> mockList = createMock(List.class);10mockList.add(capture(capture));11replay(mockList);12mockList.add("test");13verify(mockList);14assertTrue(capture.hasCaptured());15}16}17T getValue() : Returns the value captu

Full Screen

Full Screen

hasCaptured

Using AI Code Generation

copy

Full Screen

1import org.easymock.Capture;2import org.easymock.EasyMock;3import static org.easymock.EasyMock.*;4import java.util.ArrayList;5public class 1 {6 public static void main(String[] args) {7 ArrayList mock = EasyMock.createMock(ArrayList.class);8 Capture capture = new Capture();9 mock.add(capture);10 EasyMock.expectLastCall().anyTimes();11 EasyMock.replay(mock);12 mock.add("Hello");13 mock.add("World");14 EasyMock.verify(mock);15 System.out.println(capture.hasCaptured());16 System.out.println(capture.hasCaptured("Hello"));17 System.out.println(capture.hasCaptured("World"));18 System.out.println(capture.hasCaptured("Hi"));19 }20}

Full Screen

Full Screen

hasCaptured

Using AI Code Generation

copy

Full Screen

1import org.easymock.Capture;2import org.easymock.EasyMock;3import org.easymock.IAnswer;4import org.junit.Test;5import static org.junit.Assert.*;6public class CaptureTest {7 public void captureTest() throws Exception {8 TestInterface mock = EasyMock.createMock(TestInterface.class);9 Capture<String> captured = new Capture<String>();10 EasyMock.expect(mock.testMethod(EasyMock.capture(captured))).andReturn("test");11 EasyMock.replay(mock);12 mock.testMethod("test");13 assertTrue(captured.hasCaptured());14 assertEquals("test", captured.getValue());15 }16}17import org.easymock.Capture;18import org.easymock.EasyMock;19import org.easymock.IAnswer;20import org.junit.Test;21import static org.junit.Assert.*;22public class CaptureTest {23 public void captureTest() throws Exception {24 TestInterface mock = EasyMock.createMock(TestInterface.class);25 Capture<String> captured = new Capture<String>();26 EasyMock.expect(mock.testMethod(EasyMock.capture(captured))).andReturn("test");27 EasyMock.replay(mock);28 mock.testMethod("test");29 assertTrue(captured.hasCaptured());30 assertEquals("test", captured.getValue());31 }32}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Easymock automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful