Search in sources :

Example 6 with Invocation

use of org.jmock.api.Invocation in project gradle by gradle.

the class DefaultDomainObjectCollectionTest method allCallsActionForEachNewObjectAddedByTheAction.

@Test
public void allCallsActionForEachNewObjectAddedByTheAction() {
    @SuppressWarnings("unchecked") final Action<CharSequence> action = context.mock(Action.class);
    context.checking(new Expectations() {

        {
            oneOf(action).execute("a");
            will(new org.jmock.api.Action() {

                public Object invoke(Invocation invocation) throws Throwable {
                    container.add("c");
                    return null;
                }

                public void describeTo(Description description) {
                    description.appendText("add 'c'");
                }
            });
            oneOf(action).execute("b");
            oneOf(action).execute("c");
        }
    });
    container.add("a");
    container.add("b");
    container.all(action);
}
Also used : Expectations(org.jmock.Expectations) Action(org.gradle.api.Action) Description(org.hamcrest.Description) Invocation(org.jmock.api.Invocation) Test(org.junit.Test)

Example 7 with Invocation

use of org.jmock.api.Invocation in project motan by weibocom.

the class NettyHttpRequestHandlerTest method testServerStatus.

@Test
public void testServerStatus() throws Exception {
    final MessageHandler messageHandler = mockery.mock(MessageHandler.class);
    final ChannelHandlerContext ctx = mockery.mock(ChannelHandlerContext.class);
    mockery.checking(new Expectations() {

        {
            allowing(ctx).write(with(any(FullHttpResponse.class)));
            will(new CustomAction("verify") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    verifyStatus((FullHttpResponse) invocation.getParameter(0));
                    return null;
                }
            });
            allowing(ctx).flush();
            will(returnValue(null));
            allowing(ctx).close();
            will(returnValue(null));
            allowing(messageHandler).handle(with(any(Channel.class)), with(anything()));
            will(returnValue(null));
        }
    });
    FullHttpRequest httpRequest = buildHttpRequest(NettyHttpRequestHandler.ROOT_PATH);
    NettyHttpRequestHandler handler = new NettyHttpRequestHandler(null, messageHandler);
    // 关闭心跳开关
    MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, false);
    handler.channelRead0(ctx, httpRequest);
    // 打开心跳开关
    MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);
    handler.channelRead0(ctx, httpRequest);
}
Also used : Expectations(org.jmock.Expectations) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) MessageHandler(com.weibo.api.motan.transport.MessageHandler) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) Channel(com.weibo.api.motan.transport.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Test(org.junit.Test)

Example 8 with Invocation

use of org.jmock.api.Invocation in project intellij-community by JetBrains.

the class PathMacroManagerTest method setupApplication.

@Before
public final void setupApplication() throws Exception {
    context = new JUnit4Mockery();
    context.setImposteriser(ClassImposteriser.INSTANCE);
    myApplication = context.mock(ApplicationEx.class, "application");
    context.checking(new Expectations() {

        {
            allowing(myApplication).isUnitTestMode();
            will(returnValue(false));
            allowing(myApplication).getName();
            will(returnValue("IDEA"));
            // some tests leave invokeLater()'s after them
            allowing(myApplication).invokeLater(with(any(Runnable.class)), with(any(ModalityState.class)));
            allowing(myApplication).runReadAction(with(any(Runnable.class)));
            will(new Action() {

                @Override
                public void describeTo(final Description description) {
                    description.appendText("runs runnable");
                }

                @Override
                @Nullable
                public Object invoke(final Invocation invocation) throws Throwable {
                    ((Runnable) invocation.getParameter(0)).run();
                    return null;
                }
            });
        }
    });
    final ExtensionsArea area = Extensions.getRootArea();
    final String epName = PathMacrosCollector.MACRO_FILTER_EXTENSION_POINT_NAME.getName();
    if (!area.hasExtensionPoint(epName)) {
        area.registerExtensionPoint(epName, "com.intellij.openapi.application.PathMacroFilter");
        Disposer.register(myRootDisposable, new Disposable() {

            @Override
            public void dispose() {
                area.unregisterExtensionPoint(epName);
            }
        });
    }
}
Also used : Expectations(org.jmock.Expectations) Disposable(com.intellij.openapi.Disposable) ExtensionsArea(com.intellij.openapi.extensions.ExtensionsArea) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) Action(org.jmock.api.Action) Description(org.hamcrest.Description) ApplicationEx(com.intellij.openapi.application.ex.ApplicationEx) Invocation(org.jmock.api.Invocation) ModalityState(com.intellij.openapi.application.ModalityState) Before(org.junit.Before)

Example 9 with Invocation

use of org.jmock.api.Invocation in project geode by apache.

the class AutoBalancerJUnitTest method destroyAutoBalancer.

@Test
public void destroyAutoBalancer() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(2);
    final CountDownLatch timerLatch = new CountDownLatch(1);
    // simulate 20 milliseconds
    final int timer = 20;
    mockContext.checking(new Expectations() {

        {
            oneOf(mockAuditor).init(with(any(Properties.class)));
            allowing(mockAuditor).execute();
            allowing(mockClock).currentTimeMillis();
            will(new CustomAction("returnTime") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    latch.countDown();
                    if (latch.getCount() == 0) {
                        assertTrue(timerLatch.await(1, TimeUnit.SECONDS));
                        // scheduler is destroyed before wait is over
                        fail();
                    }
                    return 1000L - timer;
                }
            });
        }
    });
    Properties props = AutoBalancerJUnitTest.getBasicConfig();
    assertEquals(2, latch.getCount());
    AutoBalancer autoR = new AutoBalancer(null, mockAuditor, mockClock, null);
    autoR.init(props);
    assertTrue(latch.await(1, TimeUnit.SECONDS));
    // after destroy no more execute will be called.
    autoR.destroy();
    timerLatch.countDown();
    TimeUnit.MILLISECONDS.sleep(2 * timer);
}
Also used : Expectations(org.jmock.Expectations) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) CountDownLatch(java.util.concurrent.CountDownLatch) Properties(java.util.Properties) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 10 with Invocation

use of org.jmock.api.Invocation in project sling by apache.

the class RepositoryTestHelper method mockResourceResolverFactory.

public static ResourceResolverFactory mockResourceResolverFactory(final SlingRepository repositoryOrNull) throws Exception {
    Mockery context = new JUnit4Mockery() {

        {
            // @see http://www.jmock.org/threading-synchroniser.html
            setThreadingPolicy(new Synchroniser());
        }
    };
    final ResourceResolverFactory resourceResolverFactory = context.mock(ResourceResolverFactory.class);
    // final ResourceResolver resourceResolver = new MockResourceResolver();
    // final ResourceResolver resourceResolver = new
    // MockedResourceResolver();
    context.checking(new Expectations() {

        {
            allowing(resourceResolverFactory).getServiceResourceResolver(null);
            will(new Action() {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    return new MockedResourceResolver(repositoryOrNull);
                }

                @Override
                public void describeTo(Description arg0) {
                    arg0.appendText("whateva - im going to create a new mockedresourceresolver");
                }
            });
        }
    });
    return resourceResolverFactory;
}
Also used : Expectations(org.jmock.Expectations) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) Action(org.jmock.api.Action) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) Description(org.hamcrest.Description) Invocation(org.jmock.api.Invocation) Synchroniser(org.jmock.lib.concurrent.Synchroniser) Mockery(org.jmock.Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery)

Aggregations

Expectations (org.jmock.Expectations)12 Invocation (org.jmock.api.Invocation)12 Description (org.hamcrest.Description)7 Test (org.junit.Test)7 Action (org.jmock.api.Action)6 CustomAction (org.jmock.lib.action.CustomAction)5 Channel (com.weibo.api.motan.transport.Channel)2 MessageHandler (com.weibo.api.motan.transport.MessageHandler)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)2 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)2 JUnit4Mockery (org.jmock.integration.junit4.JUnit4Mockery)2 Disposable (com.intellij.openapi.Disposable)1