Search in sources :

Example 21 with Invocation

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

the class StartupFilterImplTest method setProvider.

private void setProvider(final TestProvider provider) throws Exception {
    final BundleContext bundleContext = mockery.mock(BundleContext.class);
    final Action storeStatus = new Action() {

        @Override
        public void describeTo(Description d) {
            d.appendText("Store HTTP response values");
        }

        @Override
        public Object invoke(Invocation invocation) throws Throwable {
            lastReturnedStatus = (Integer) invocation.getParameter(0);
            return null;
        }
    };
    messageWriter = new StringWriter();
    final PrintWriter responseWriter = new PrintWriter(messageWriter);
    final Map<String, Object> props = new HashMap<String, Object>();
    props.put(StartupFilterImpl.ACTIVE_BY_DEFAULT_PROP, Boolean.TRUE);
    final ServiceReference[] providerRefs = provider == null ? null : new ServiceReference[] { provider };
    mockery.checking(new Expectations() {

        {
            allowing(bundleContext).createFilter(with(any(String.class)));
            allowing(bundleContext).addServiceListener(with(any(ServiceListener.class)));
            allowing(bundleContext).addServiceListener(with(any(ServiceListener.class)), with(any(String.class)));
            allowing(bundleContext).getServiceReferences(StartupInfoProvider.class.getName(), null);
            will(returnValue(providerRefs));
            allowing(bundleContext).getService(with(any(ServiceReference.class)));
            will(returnValue(provider));
            allowing(bundleContext).getProperty(with("felix.webconsole.manager.root"));
            will(returnValue(CONSOLE_ROOT));
            allowing(bundleContext).registerService(with(equal(Filter.class)), with(any(Filter.class)), with(any(Dictionary.class)));
            will(new DoAllAction(new ChangeInteger(activeFilterCount, true), returnValue(serviceRegistration)));
            allowing(response).setStatus((with(any(Integer.class))));
            will(storeStatus);
            allowing(response).setContentType("text/plain");
            allowing(response).getWriter();
            will(returnValue(responseWriter));
            allowing(response).setCharacterEncoding(with(any(String.class)));
            allowing(serviceRegistration).unregister();
            will(new ChangeInteger(activeFilterCount, false));
            allowing(request).getServletPath();
            will(returnValue(""));
            allowing(request).getPathInfo();
            will(returnValue(getPathInfo()));
            allowing(chain).doFilter(with(any(ServletRequest.class)), with(any(ServletResponse.class)));
        }
    });
    filter.setup(bundleContext, props);
}
Also used : Expectations(org.jmock.Expectations) DoAllAction(org.jmock.lib.action.DoAllAction) Dictionary(java.util.Dictionary) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) DoAllAction(org.jmock.lib.action.DoAllAction) Action(org.jmock.api.Action) Description(org.hamcrest.Description) ServiceListener(org.osgi.framework.ServiceListener) Invocation(org.jmock.api.Invocation) HashMap(java.util.HashMap) ServiceReference(org.osgi.framework.ServiceReference) StringWriter(java.io.StringWriter) Filter(javax.servlet.Filter) BundleContext(org.osgi.framework.BundleContext) PrintWriter(java.io.PrintWriter)

Example 22 with Invocation

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

the class LeaseTerm_Test method returnLeaseTerm.

private Action returnLeaseTerm() {
    return new Action() {

        @Override
        public Object invoke(Invocation invocation) throws Throwable {
            LeaseItem leaseItem = (LeaseItem) invocation.getParameter(0);
            LeaseTerm leaseTerm = (LeaseTerm) invocation.getParameter(1);
            LocalDate startDate = (LocalDate) invocation.getParameter(2);
            LocalDate endDate = (LocalDate) invocation.getParameter(3);
            LeaseTermForTesting ltt = new LeaseTermForTesting();
            // relationships
            ltt.setLeaseItem(leaseItem);
            leaseItem.getTerms().add(ltt);
            ltt.setPrevious(leaseTerm);
            leaseTerm.setNext(ltt);
            // set values
            ltt.modifyStartDate(startDate);
            ltt.modifyEndDate(endDate);
            ltt.clockService = mockClockService;
            return ltt;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("new Lease Term under item and with previous term");
        }
    };
}
Also used : Action(org.jmock.api.Action) Description(org.hamcrest.Description) Invocation(org.jmock.api.Invocation) LocalDate(org.joda.time.LocalDate)

Example 23 with Invocation

use of org.jmock.api.Invocation in project activemq-artemis by apache.

the class DiscoveryNetworkReconnectTest method setUp.

@Before
public void setUp() throws Exception {
    context = new JUnit4Mockery() {

        {
            setImposteriser(ClassImposteriser.INSTANCE);
        }
    };
    brokerA = new BrokerService();
    brokerA.setBrokerName("BrokerA");
    configure(brokerA);
    brokerA.addConnector("tcp://localhost:0");
    brokerA.start();
    brokerA.waitUntilStarted();
    proxy = new SocketProxy(brokerA.getTransportConnectors().get(0).getConnectUri());
    managementContext = context.mock(ManagementContext.class);
    context.checking(new Expectations() {

        {
            allowing(managementContext).getJmxDomainName();
            will(returnValue("Test"));
            allowing(managementContext).setBrokerName("BrokerNC");
            allowing(managementContext).start();
            allowing(managementContext).isCreateConnector();
            allowing(managementContext).stop();
            allowing(managementContext).isConnectorStarted();
            // expected MBeans
            allowing(managementContext).registerMBean(with(any(Object.class)), with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC"))));
            allowing(managementContext).registerMBean(with(any(Object.class)), with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,service=Health"))));
            allowing(managementContext).registerMBean(with(any(Object.class)), with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,connector=networkConnectors,networkConnectorName=NC"))));
            allowing(managementContext).registerMBean(with(any(Object.class)), with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,service=Log4JConfiguration"))));
            allowing(managementContext).registerMBean(with(any(Object.class)), with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,destinationType=Topic,destinationName=ActiveMQ.Advisory.Connection"))));
            allowing(managementContext).registerMBean(with(any(Object.class)), with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,destinationType=Topic,destinationName=ActiveMQ.Advisory.NetworkBridge"))));
            allowing(managementContext).registerMBean(with(any(Object.class)), with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,destinationType=Topic,destinationName=ActiveMQ.Advisory.MasterBroker"))));
            allowing(managementContext).registerMBean(with(any(Object.class)), with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,service=jobScheduler,jobSchedulerName=JMS"))));
            atLeast(maxReconnects - 1).of(managementContext).registerMBean(with(any(Object.class)), with(new NetworkBridgeObjectNameMatcher<>(new ObjectName("Test:type=Broker,brokerName=BrokerNC,connector=networkConnectors,networkConnectorName=NC,networkBridge=localhost/127.0.0.1_" + proxy.getUrl().getPort()))));
            will(new CustomAction("signal register network mbean") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    LOG.info("Mbean Registered: " + invocation.getParameter(0));
                    mbeanRegistered.release();
                    return new ObjectInstance((ObjectName) invocation.getParameter(1), "discription");
                }
            });
            atLeast(maxReconnects - 1).of(managementContext).unregisterMBean(with(new NetworkBridgeObjectNameMatcher<>(new ObjectName("Test:type=Broker,brokerName=BrokerNC,connector=networkConnectors,networkConnectorName=NC,networkBridge=localhost/127.0.0.1_" + proxy.getUrl().getPort()))));
            will(new CustomAction("signal unregister network mbean") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    LOG.info("Mbean Unregistered: " + invocation.getParameter(0));
                    mbeanUnregistered.release();
                    return null;
                }
            });
            allowing(managementContext).unregisterMBean(with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC"))));
            allowing(managementContext).unregisterMBean(with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,service=Health"))));
            allowing(managementContext).unregisterMBean(with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,connector=networkConnectors,networkConnectorName=NC"))));
            allowing(managementContext).unregisterMBean(with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,service=Log4JConfiguration"))));
            allowing(managementContext).unregisterMBean(with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,destinationType=Topic,destinationName=ActiveMQ.Advisory.Connection"))));
            allowing(managementContext).unregisterMBean(with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,destinationType=Topic,destinationName=ActiveMQ.Advisory.NetworkBridge"))));
            allowing(managementContext).unregisterMBean(with(equal(new ObjectName("Test:type=Broker,brokerName=BrokerNC,destinationType=Topic,destinationName=ActiveMQ.Advisory.MasterBroker"))));
        }
    });
    brokerB = new BrokerService();
    brokerB.setManagementContext(managementContext);
    brokerB.setBrokerName("BrokerNC");
    configure(brokerB);
}
Also used : Expectations(org.jmock.Expectations) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) ObjectInstance(javax.management.ObjectInstance) ManagementContext(org.apache.activemq.broker.jmx.ManagementContext) SocketProxy(org.apache.activemq.util.SocketProxy) BrokerService(org.apache.activemq.broker.BrokerService) ObjectName(javax.management.ObjectName) Before(org.junit.Before)

Example 24 with Invocation

use of org.jmock.api.Invocation in project xwiki-platform by xwiki.

the class IntegrationTest method initialize.

@RenderingTestSuite.Initialized
@SuppressWarnings("unchecked")
public void initialize(final MockingComponentManager componentManager) throws Exception {
    Mockery mockery = new JUnit4Mockery();
    // Since we have a dependency on XWiki Platform Oldcore the Context Component Manager will be found and the
    // test will try to look up the Dashboard macro in the User and Wiki Component Manager and thus need a Current
    // User and a Current Wiki. It's easier for this test to simply unregister the Context Component Manager rather
    // than have to provide mocks for them.
    componentManager.unregisterComponent(ComponentManager.class, "context");
    final SkinExtension mockSsfx = componentManager.registerMockComponent(mockery, SkinExtension.class, "ssfx", "ssfxMock");
    final SkinExtension mockJsfx = componentManager.registerMockComponent(mockery, SkinExtension.class, "jsfx", "jsfxMock");
    mockery.checking(new Expectations() {

        {
            allowing(mockSsfx).use(with("uicomponents/container/columns.css"), with(any(Map.class)));
            allowing(mockSsfx).use(with("uicomponents/dashboard/dashboard.css"), with(any(Map.class)));
            allowing(mockJsfx).use(with("js/scriptaculous/dragdrop.js"));
            allowing(mockJsfx).use(with("js/scriptaculous/effects.js"));
            allowing(mockJsfx).use(with("uicomponents/dashboard/dashboard.js"), with(any(Map.class)));
        }
    });
    final GadgetSource mockGadgetSource = componentManager.registerMockComponent(mockery, GadgetSource.class);
    mockery.checking(new Expectations() {

        {
            // Mock gadget for macrodashboard_nested_velocity.test
            allowing(mockGadgetSource).getGadgets(with("nested_velocity"), with(any(MacroTransformationContext.class)));
            will(returnValue(Arrays.asList(new Gadget("0", Arrays.asList(new WordBlock("title")), Arrays.asList(new MacroMarkerBlock("velocity", Collections.emptyMap(), "someVelocityCodeHere", Collections.singletonList(new WordBlock("someVelocityOutputHere")), true)), "1,1"))));
            // Mock gadget for macrodashboard1.test
            allowing(mockGadgetSource).getGadgets(with(aNull(String.class)), with(any(MacroTransformationContext.class)));
            will(returnValue(Arrays.asList(new Gadget("0", Arrays.<Block>asList(new WordBlock("title")), Arrays.<Block>asList(new WordBlock("content")), "1,1"))));
            allowing(mockGadgetSource).getDashboardSourceMetadata(with(AnyOf.anyOf(aNull(String.class), any(String.class))), with(any(MacroTransformationContext.class)));
            will(returnValue(Collections.<Block>emptyList()));
            allowing(mockGadgetSource).isEditing();
            // return true on is editing, to take as many paths possible
            will(returnValue(true));
        }
    });
    // Mock VelocityManager used in macrodashboard_nested_velocity.test because we do not have an XWikiContext
    // instance in the ExecutionContext.
    final VelocityManager mockVelocityManager = componentManager.registerMockComponentWithId(mockery, VelocityManager.class, "velocityManagerMock");
    mockery.checking(new Expectations() {

        {
            allowing(mockVelocityManager).getVelocityContext();
            will(returnValue(new VelocityContext()));
            allowing(mockVelocityManager).getCurrentVelocityContext();
            will(returnValue(new VelocityContext()));
            allowing(mockVelocityManager).getVelocityEngine();
            will(doAll(new CustomAction("mockGetVelocityEngine") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    VelocityEngine velocityEngine = componentManager.getInstance(VelocityEngine.class);
                    Properties properties = new Properties();
                    properties.setProperty("resource.loader", "file");
                    velocityEngine.initialize(properties);
                    return velocityEngine;
                }
            }));
            allowing(mockVelocityManager).evaluate(with(any(Writer.class)), with(any(String.class)), with(any(Reader.class)));
            will(doAll(new CustomAction("mockEvaluate") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    VelocityEngine velocityEngine = mockVelocityManager.getVelocityEngine();
                    return velocityEngine.evaluate(mockVelocityManager.getVelocityContext(), (Writer) invocation.getParameter(0), (String) invocation.getParameter(1), (Reader) invocation.getParameter(2));
                }
            }));
        }
    });
    componentManager.registerMockComponent(mockery, ContextualAuthorizationManager.class);
}
Also used : Expectations(org.jmock.Expectations) VelocityEngine(org.xwiki.velocity.VelocityEngine) SkinExtension(org.xwiki.skinx.SkinExtension) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) VelocityContext(org.apache.velocity.VelocityContext) WordBlock(org.xwiki.rendering.block.WordBlock) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) Reader(java.io.Reader) Properties(java.util.Properties) Mockery(org.jmock.Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) VelocityManager(org.xwiki.velocity.VelocityManager) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) Map(java.util.Map) Writer(java.io.Writer)

Example 25 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(any(Object.class)));
            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) 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) Test(org.junit.Test)

Aggregations

Invocation (org.jmock.api.Invocation)25 Expectations (org.jmock.Expectations)24 Description (org.hamcrest.Description)13 Test (org.junit.Test)13 Action (org.jmock.api.Action)12 CustomAction (org.jmock.lib.action.CustomAction)12 HashMap (java.util.HashMap)5 Properties (java.util.Properties)5 JUnit4Mockery (org.jmock.integration.junit4.JUnit4Mockery)4 Before (org.junit.Before)4 Reader (java.io.Reader)3 Writer (java.io.Writer)3 Map (java.util.Map)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 Channel (com.weibo.api.motan.transport.Channel)2 MessageHandler (com.weibo.api.motan.transport.MessageHandler)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 StringReader (java.io.StringReader)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2