/** * Create a new capture instance with a specific {@link org.easymock.CaptureType} * * @param type capture type wanted * @param <T> type of the class to be captured * @return the new capture object */ public static <T> Capture<T> newInstance(CaptureType type) { return new Capture<>(type); }
/** * Create a new capture instance that will keep only the last captured value * * @param <T> type of the class to be captured * @return the new capture object */ public static <T> Capture<T> newInstance() { return new Capture<>(); }
Capture capturedArgument = new Capture(); EasyMock.expect(mockObject.someMethod(EasyMock.capture(capturedArgument)).andReturn(1.5); Assert.assertEquals(expectedValue, capturedArgument.getValue());
public <T> T capture(final Class<T> type) { Capture<Object> capture = new Capture<>(); List<Capture<Object>> captures = this.captures.get(type); if (captures == null) { captures = new ArrayList<>(); this.captures.put(type, captures); } captures.add(capture); return (T) EasyMock.capture(capture); }
txConfigCapture = new Capture<BaseTransactionConfig>(CaptureType.ALL); inconsistentTx = ctrl.createMock(StoreTransaction.class); consistentTx = ctrl.createMock(StoreTransaction.class);
@Test public void testPropertySetting() throws Exception { IMocksControl control = createControl(); TypeEncounter<SomeInjectableBean> encounter = control.createMock(TypeEncounter.class); Provider<Injector> injectorProvider = control.createMock(Provider.class); Injector injector = control.createMock(Injector.class); expect(encounter.getProvider(Injector.class)).andReturn(injectorProvider); expect(injectorProvider.get()).andReturn(injector).anyTimes(); Capture<MembersInjector<SomeInjectableBean>> capture = new Capture<MembersInjector<SomeInjectableBean>>(); encounter.register(and(anyObject(MembersInjector.class), capture(capture))); SecurityManager securityManager = control.createMock(SecurityManager.class); String property = "myPropertyValue"; expect(injector.getInstance(Key.get(SecurityManager.class))).andReturn(securityManager); expect(injector.getInstance(Key.get(String.class, Names.named("shiro.myProperty")))).andReturn(property); expect(injector.getInstance(Key.get(String.class, Names.named("shiro.unavailableProperty")))) .andThrow(new ConfigurationException(Collections.singleton(new Message("Not Available!")))); expect((Map)injector.getInstance(BeanTypeListener.MAP_KEY)).andReturn(Collections.EMPTY_MAP).anyTimes(); control.replay(); BeanTypeListener underTest = new BeanTypeListener(); underTest.hear(TypeLiteral.get(SomeInjectableBean.class), encounter); SomeInjectableBean bean = new SomeInjectableBean(); capture.getValue().injectMembers(bean); assertSame(securityManager, bean.securityManager); assertSame(property, bean.myProperty); assertNull(bean.unavailableProperty); control.verify(); }
@Test public void testConstructor() { WebSecurityManager securityManager = createMock(WebSecurityManager.class); FilterChainResolver filterChainResolver = createMock(FilterChainResolver.class); ServletContext servletContext = createMock(ServletContext.class); Capture<WebGuiceEnvironment> capture = new Capture<WebGuiceEnvironment>(); servletContext.setAttribute(eq(EnvironmentLoaderListener.ENVIRONMENT_ATTRIBUTE_KEY), and(anyObject(WebGuiceEnvironment.class), capture(capture))); replay(servletContext, securityManager, filterChainResolver); WebGuiceEnvironment underTest = new WebGuiceEnvironment(filterChainResolver, servletContext, securityManager); assertSame(securityManager, underTest.getSecurityManager()); assertSame(filterChainResolver, underTest.getFilterChainResolver()); assertSame(securityManager, underTest.getWebSecurityManager()); assertSame(servletContext, underTest.getServletContext()); assertSame(underTest, capture.getValue()); verify(servletContext); } }
@Test public void testDoFilter() throws Exception { IMocksControl ctrl = createStrictControl(); FilterChain originalChain = ctrl.createMock(FilterChain.class); Filter filter1 = ctrl.createMock("filter1", Filter.class); Filter filter2 = ctrl.createMock("filter2", Filter.class); ServletRequest request = ctrl.createMock(ServletRequest.class); ServletResponse response = ctrl.createMock(ServletResponse.class); Capture<FilterChain> fc1 = new Capture<FilterChain>(); Capture<FilterChain> fc2 = new Capture<FilterChain>(); filter1.doFilter(same(request), same(response), and(anyObject(FilterChain.class), capture(fc1))); filter2.doFilter(same(request), same(response), and(anyObject(FilterChain.class), capture(fc2))); originalChain.doFilter(request, response); ctrl.replay(); SimpleFilterChain underTest = new SimpleFilterChain(originalChain, Arrays.asList(filter1, filter2).iterator()); // all we actually care about is that, if we keep calling the filter chain, everything is called in the right // order - we don't care what fc actually contains underTest.doFilter(request, response); fc1.getValue().doFilter(request, response); fc2.getValue().doFilter(request, response); ctrl.verify(); } }
final CountDownLatch queryIsRegistered = new CountDownLatch(1); Capture<ListenableFuture> capturedFuture = new Capture<>(); QueryWatcher watcher = EasyMock.createStrictMock(QueryWatcher.class); watcher.registerQuery(
replay(reader); AbstractGridFormat format = createNiceMock("theFormat", AbstractGridFormat.class); Capture<Hints> capturedHints = new Capture<>(); expect(format.getReader(EasyMock.eq(url), capture(capturedHints))) .andReturn(reader)
log.setLevel(Level.FINE); expectLastCall().once(); final Capture<Handler> handlerCap = new Capture<>(); log.addHandler(capture(handlerCap)); expectLastCall().once();
log.setLevel(Level.FINE); expectLastCall().once(); final Capture<Handler> handlerCap = new Capture<>(); final Exception ex = new IllegalArgumentException();
log.setLevel(Level.FINE); expectLastCall().once(); final Capture<Handler> handlerCap = new Capture<>(); final Exception ex = new IllegalArgumentException(); log.addHandler(capture(handlerCap));
log.setLevel(Level.FINE); expectLastCall().once(); final Capture<Handler> handlerCap = new Capture<>();
log.setLevel(Level.FINE); expectLastCall().once(); final Capture<Handler> handlerCap = new Capture<>();
log.setLevel(Level.FINE); expectLastCall().once(); final Capture<Handler> handlerCap = new Capture<>();
@Test public void testUploadFromByteArray() throws IOException { EasyMock.reset(mockS3); EasyMock.expect(mockS3.getRegion()).andReturn(Region.US_Standard); Capture<PutObjectRequest> por = new Capture<PutObjectRequest>(); EasyMock.expect(mockS3.putObject(EasyMock.capture(por))).andReturn(null); EasyMock.replay(mockS3); mapper.getS3ClientCache().useClient(mockS3); S3Link link = mapper.createS3Link(bucket, key); link.uploadFrom("Test".getBytes(StringUtils.UTF8)); ByteArrayOutputStream bos = new ByteArrayOutputStream(); InputStream is = por.getValue().getInputStream(); int currByte = -1; while ((currByte = is.read()) != -1) { bos.write(currByte); } assertArrayEquals(bos.toByteArray(), "Test".getBytes(StringUtils.UTF8)); EasyMock.verify(); }
@Test public void testUploadFromFile() throws IOException { EasyMock.reset(mockS3); EasyMock.expect(mockS3.getRegion()).andReturn(Region.US_Standard); Capture<PutObjectRequest> por = new Capture<PutObjectRequest>(); EasyMock.expect(mockS3.putObject(EasyMock.capture(por))).andReturn(null); EasyMock.replay(mockS3); mapper.getS3ClientCache().useClient(mockS3); S3Link link = mapper.createS3Link(bucket, key); File temp = new File("testUpload"); if (temp.exists()) { assertTrue(temp.delete()); } temp.createNewFile(); FileOutputStream fos = new FileOutputStream(temp); fos.write("Test".getBytes(StringUtils.UTF8)); fos.close(); link.uploadFrom(temp); File sent = por.getValue().getFile(); assertEquals(temp, sent); assertTrue(temp.delete()); EasyMock.verify(); }