use of org.easymock.IAnswer in project databus by linkedin.
the class TestGoldenGateEventProducer method createMockBufMult.
/**
* Creates a mock DbusEventBufMult
*/
private DbusEventBufferAppendable createMockBufMult() {
long scn = 10;
DbusEventBufferAppendable deba = EasyMock.createMock(DbusEventBufferAppendable.class);
deba.startEvents();
EasyMock.expectLastCall().andAnswer(new IAnswer() {
@Override
public Object answer() {
return null;
}
});
DbusEventKey dek = EasyMock.createNiceMock(DbusEventKey.class);
EasyMock.expect(deba.appendEvent(dek, EasyMock.createNiceMock(DbusEventInfo.class), null)).andReturn(Boolean.TRUE);
deba.endEvents(EasyMock.eq(scn), null);
EasyMock.expectLastCall().andAnswer(new IAnswer() {
@Override
public Object answer() {
return null;
}
});
EasyMock.replay(deba);
return deba;
}
use of org.easymock.IAnswer in project rest.li by linkedin.
the class TestRestLiServer method testValidRequest.
private void testValidRequest(RestLiServer restLiServer, final ProtocolVersion clientProtocolVersion, boolean filters, final String headerConstant, final RestOrStream restOrStream) throws URISyntaxException {
RestRequest request = null;
StreamRequest streamRequest = null;
if (clientProtocolVersion != null) {
if (restOrStream == RestOrStream.REST) {
request = new RestRequestBuilder(new URI("/statuses/1")).setHeader(headerConstant, clientProtocolVersion.toString()).build();
} else {
streamRequest = new StreamRequestBuilder(new URI("/statuses/1")).setHeader(headerConstant, clientProtocolVersion.toString()).build(EntityStreams.emptyStream());
}
} else {
if (restOrStream == RestOrStream.REST) {
request = new RestRequestBuilder(new URI("/statuses/1")).build();
} else {
streamRequest = new StreamRequestBuilder(new URI("/statuses/1")).build(EntityStreams.emptyStream());
}
}
final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
EasyMock.expect(statusResource.get(eq(1L))).andReturn(buildStatusRecord()).once();
if (filters) {
_mockFilter.onRequest(EasyMock.anyObject(FilterRequestContext.class));
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
return CompletableFuture.completedFuture(null);
}
}).times(1);
_mockFilter.onResponse(EasyMock.anyObject(FilterRequestContext.class), EasyMock.anyObject(FilterResponseContext.class));
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
return CompletableFuture.completedFuture(null);
}
}).times(1);
EasyMock.replay(_mockFilter);
}
EasyMock.replay(statusResource);
final Callback<RestResponse> restResponseCallback = new Callback<RestResponse>() {
@Override
public void onSuccess(RestResponse restResponse) {
assertEquals(restResponse.getStatus(), 200);
assertTrue(restResponse.getEntity().length() > 0);
EasyMock.verify(statusResource);
EasyMock.reset(statusResource);
if (clientProtocolVersion != null) {
assertEquals(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, headerConstant, "Rest.li protocol header name is unexpected.");
}
}
@Override
public void onError(Throwable e) {
fail();
}
};
if (restOrStream == RestOrStream.REST) {
restLiServer.handleRequest(request, new RequestContext(), restResponseCallback);
} else {
Callback<StreamResponse> streamResponseCallback = new Callback<StreamResponse>() {
@Override
public void onSuccess(StreamResponse streamResponse) {
Messages.toRestResponse(streamResponse, new Callback<RestResponse>() {
@Override
public void onError(Throwable e) {
Assert.fail();
}
@Override
public void onSuccess(RestResponse result) {
restResponseCallback.onSuccess(result);
}
});
}
@Override
public void onError(Throwable e) {
fail();
}
};
restLiServer.handleRequest(streamRequest, new RequestContext(), streamResponseCallback);
}
if (filters) {
EasyMock.verify(_mockFilter, _mockFilter);
}
}
use of org.easymock.IAnswer in project aries by apache.
the class UtilTest method testFixContextClassLoaderSimpleViaEmbeddedJar.
@Test
public void testFixContextClassLoaderSimpleViaEmbeddedJar() throws Exception {
BaseActivator activator = new BaseActivator() {
public void start(BundleContext context) throws Exception {
}
};
BaseActivator.activator = activator;
URL url = getClass().getResource("/embedded3.jar");
assertNotNull("precondition", url);
Bundle providerBundle = EasyMock.createMock(Bundle.class);
final ClassLoader providerCL = new TestBundleClassLoader(new URL[] { url }, getClass().getClassLoader(), providerBundle);
EasyMock.expect(providerBundle.getBundleContext()).andThrow(new IllegalStateException("Disable getBundleClassLoaderViaAdapt"));
EasyMock.expect(providerBundle.getBundleId()).andReturn(42L).anyTimes();
EasyMock.expect(providerBundle.getEntryPaths((String) EasyMock.anyObject())).andReturn(null).anyTimes();
Dictionary<String, String> providerHeaders = new Hashtable<String, String>();
providerHeaders.put(Constants.BUNDLE_CLASSPATH, ".,embedded3.jar");
EasyMock.expect(providerBundle.getHeaders()).andReturn(providerHeaders).anyTimes();
EasyMock.expect(providerBundle.getResource("embedded3.jar")).andReturn(url).anyTimes();
providerBundle.loadClass((String) EasyMock.anyObject());
EasyMock.expectLastCall().andAnswer(new IAnswer<Class<?>>() {
@Override
public Class<?> answer() throws Throwable {
return providerCL.loadClass((String) EasyMock.getCurrentArguments()[0]);
}
}).anyTimes();
EasyMock.replay(providerBundle);
activator.registerProviderBundle(MySPI.class.getName(), providerBundle, new HashMap<String, Object>());
Bundle clientBundle = EasyMock.createMock(Bundle.class);
EasyMock.replay(clientBundle);
ClassLoader clientCL = new TestBundleClassLoader(new URL[] {}, getClass().getClassLoader(), clientBundle);
Thread.currentThread().setContextClassLoader(null);
Util.fixContextClassloader(ServiceLoader.class.getName(), "load", MySPI.class, clientCL);
assertSame(providerCL, Thread.currentThread().getContextClassLoader());
}
use of org.easymock.IAnswer in project aries by apache.
the class ProviderBundleTrackerCustomizerGenericCapabilityTest method mockSPIBundleContext4.
@SuppressWarnings("unchecked")
private BundleContext mockSPIBundleContext4() {
BundleContext implBC = EasyMock.createNiceMock(BundleContext.class);
implBC.registerService(EasyMock.anyString(), EasyMock.anyObject(), (Dictionary<String, ?>) EasyMock.anyObject());
EasyMock.expectLastCall().andAnswer(new IAnswer<ServiceRegistration<Object>>() {
@Override
public ServiceRegistration<Object> answer() throws Throwable {
final String className = (String) EasyMock.getCurrentArguments()[0];
final Object serviceObject = EasyMock.getCurrentArguments()[1];
final Dictionary<String, Object> registrationProps = (Dictionary<String, Object>) EasyMock.getCurrentArguments()[2];
return new ServiceRegistrationImpl(className, serviceObject, registrationProps);
}
}).anyTimes();
implBC.getService(EasyMock.anyObject(ServiceReference.class));
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
ServiceRegistrationImpl reg = (ServiceRegistrationImpl) EasyMock.getCurrentArguments()[0];
Object svc = reg.getServiceObject();
if (svc instanceof ServiceFactory) {
return ((ServiceFactory) svc).getService(null, reg);
} else {
return svc;
}
}
}).anyTimes();
EasyMock.replay(implBC);
return implBC;
}
use of org.easymock.IAnswer in project aries by apache.
the class ClientWeavingHookTest method mockProviderBundle.
private Bundle mockProviderBundle(String subdir, long id, Version version) throws Exception {
URL url = getClass().getResource("/" + getClass().getName().replace('.', '/') + ".class");
File classFile = new File(url.getFile());
File baseDir = new File(classFile.getParentFile(), subdir);
File directory = new File(baseDir, "/META-INF/services");
final List<String> classNames = new ArrayList<String>();
// Do a directory listing of the applicable META-INF/services directory
List<String> resources = new ArrayList<String>();
for (File f : directory.listFiles()) {
String fileName = f.getName();
if (fileName.startsWith(".") || fileName.endsWith("."))
continue;
classNames.addAll(getClassNames(f));
// Needs to be something like: META-INF/services/org.apache.aries.mytest.MySPI
String path = f.getAbsolutePath().substring(baseDir.getAbsolutePath().length());
path = path.replace('\\', '/');
if (path.startsWith("/")) {
path = path.substring(1);
}
resources.add(path);
}
// Set up the classloader that will be used by the ASM-generated code as the TCCL.
// It can load a META-INF/services file
final ClassLoader cl = new TestProviderBundleClassLoader(subdir, resources.toArray(new String[] {}));
final List<String> classResources = new ArrayList<String>();
for (String className : classNames) {
classResources.add("/" + className.replace('.', '/') + ".class");
}
BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
EasyMock.replay(bc);
Bundle providerBundle = EasyMock.createMock(Bundle.class);
String bsn = subdir;
int idx = bsn.indexOf('_');
if (idx > 0) {
bsn = bsn.substring(0, idx);
}
EasyMock.expect(providerBundle.getSymbolicName()).andReturn(bsn).anyTimes();
EasyMock.expect(providerBundle.getBundleId()).andReturn(id).anyTimes();
EasyMock.expect(providerBundle.getBundleContext()).andReturn(bc).anyTimes();
EasyMock.expect(providerBundle.getVersion()).andReturn(version).anyTimes();
EasyMock.expect(providerBundle.getEntryPaths("/")).andAnswer(new IAnswer<Enumeration<String>>() {
@Override
public Enumeration<String> answer() throws Throwable {
return Collections.enumeration(classResources);
}
}).anyTimes();
EasyMock.<Class<?>>expect(providerBundle.loadClass(EasyMock.anyObject(String.class))).andAnswer(new IAnswer<Class<?>>() {
@Override
public Class<?> answer() throws Throwable {
String name = (String) EasyMock.getCurrentArguments()[0];
if (!classNames.contains(name)) {
throw new ClassCastException(name);
}
return cl.loadClass(name);
}
}).anyTimes();
EasyMock.replay(providerBundle);
return providerBundle;
}
Aggregations