Search in sources :

Example 21 with IAnswer

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;
}
Also used : IAnswer(org.easymock.IAnswer) DbusEventInfo(com.linkedin.databus.core.DbusEventInfo) DbusEventBufferAppendable(com.linkedin.databus.core.DbusEventBufferAppendable) DbusEventKey(com.linkedin.databus.core.DbusEventKey)

Example 22 with IAnswer

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);
    }
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) URI(java.net.URI) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) IAnswer(org.easymock.IAnswer) RestRequest(com.linkedin.r2.message.rest.RestRequest) SinglePartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback) Callback(com.linkedin.common.callback.Callback) MultiPartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback) FilterResponseContext(com.linkedin.restli.server.filter.FilterResponseContext) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) AsyncStatusCollectionResource(com.linkedin.restli.server.twitter.AsyncStatusCollectionResource) StatusCollectionResource(com.linkedin.restli.server.twitter.StatusCollectionResource) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext)

Example 23 with IAnswer

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());
}
Also used : Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) URL(java.net.URL) ServiceLoader(java.util.ServiceLoader) IAnswer(org.easymock.IAnswer) MySPI(org.apache.aries.mytest.MySPI) URLClassLoader(java.net.URLClassLoader) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 24 with IAnswer

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;
}
Also used : IAnswer(org.easymock.IAnswer) Dictionary(java.util.Dictionary) ServiceFactory(org.osgi.framework.ServiceFactory) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Example 25 with IAnswer

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;
}
Also used : Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) URL(java.net.URL) IAnswer(org.easymock.IAnswer) URLClassLoader(java.net.URLClassLoader) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) File(java.io.File) BundleContext(org.osgi.framework.BundleContext)

Aggregations

IAnswer (org.easymock.IAnswer)31 Test (org.junit.Test)17 Bundle (org.osgi.framework.Bundle)11 BundleContext (org.osgi.framework.BundleContext)11 Hashtable (java.util.Hashtable)8 ArrayList (java.util.ArrayList)6 Dictionary (java.util.Dictionary)6 ServiceReference (org.osgi.framework.ServiceReference)6 BundleWiring (org.osgi.framework.wiring.BundleWiring)6 Firehose (io.druid.data.input.Firehose)5 InputRow (io.druid.data.input.InputRow)5 MapBasedInputRow (io.druid.data.input.MapBasedInputRow)5 URL (java.net.URL)5 URLClassLoader (java.net.URLClassLoader)5 HashMap (java.util.HashMap)5 ServiceRegistrationHolder (org.apache.karaf.service.guard.impl.GuardProxyCatalog.ServiceRegistrationHolder)5 ServiceRegistration (org.osgi.framework.ServiceRegistration)5 CreateProxyRunnable (org.apache.karaf.service.guard.impl.GuardProxyCatalog.CreateProxyRunnable)4 LinkedHashMap (java.util.LinkedHashMap)3 ProxyManager (org.apache.aries.proxy.ProxyManager)3