Package org.jmock

Examples of org.jmock.States


        final BrokerService brokerService = context.mock(BrokerService.class);
        final JDBCPersistenceAdapter jdbcPersistenceAdapter = context.mock(JDBCPersistenceAdapter.class);
        final Locker locker = context.mock(Locker.class);

        final States jdbcConn = context.states("jdbc").startsAs("down");
        final States broker = context.states("broker").startsAs("started");

        // simulate jdbc up between hasLock and checkpoint, so hasLock fails to verify
        context.checking(new Expectations() {{
            allowing(brokerService).isRestartAllowed();
            will(returnValue(false));
            allowing(brokerService).stopAllConnectors(with(any(ServiceStopper.class)));
            allowing(brokerService).getPersistenceAdapter();
            will(returnValue(jdbcPersistenceAdapter));
            allowing(jdbcPersistenceAdapter).getLocker();
            will(returnValue(locker));
            allowing(locker).keepAlive();
            when(jdbcConn.is("down"));
            will(returnValue(true));
            allowing(locker).keepAlive();
            when(jdbcConn.is("up"));
            will(returnValue(false));

            allowing(jdbcPersistenceAdapter).checkpoint(with(true));
            then(jdbcConn.is("up"));
            allowing(brokerService).stop();
            then(broker.is("stopped"));

        }});

        JDBCIOExceptionHandler underTest = new JDBCIOExceptionHandler();
        underTest.setBrokerService(brokerService);

        try {
            underTest.handle(new IOException());
            fail("except suppress reply ex");
        } catch (SuppressReplyException expected) {
        }

        assertTrue("broker stopped state triggered", Wait.waitFor(new Wait.Condition() {
            @Override
            public boolean isSatisified() throws Exception {
                LOG.info("broker state {}", broker);
                return broker.is("stopped").isActive();
            }
        }));
        context.assertIsSatisfied();

        assertTrue("no exceptions: " + exceptions, exceptions.isEmpty());
View Full Code Here


        final Event<String> event5 = new EventImpl<String>( 1550, "five" );
        final Event<String> event6 = new EventImpl<String>( 1700, "six" );
       
        // define a jmock sequence and state machine
        final Sequence seq = context.sequence( "call sequence" );
        final States ts = context.states( "timestamp" ).startsAs( String.valueOf( event1.getTimestamp() ) );
       
        // create mock objects for the source and the receiver
        final EventSource source = context.mock( EventSource.class );
        final EventReceiver receiver = context.mock( EventReceiver.class );
       
        // create the scheduler used by drools and the feeder to be tested
        PseudoClockScheduler clock = new PseudoClockScheduler();
        EventFeeder feeder = new EventFeeder( clock, source, receiver );
       
        // create the expectations
        context.checking( new Expectations() {{
            // there is an event1, so, read and feed
            oneOf( source ).hasNext(); inSequence( seq ); will( returnValue( true ) );
            oneOf( source ).getNext(); inSequence( seq ); will( returnValue( event1 ) )
            oneOf( receiver ).receive( event1 ); inSequence( seq ); when( ts.is( String.valueOf( event1.getTimestamp() ) ) );
            then( ts.is( String.valueOf( event2.getTimestamp() ) ) );
           
            // there is an event 2, so read and feed
            oneOf( source ).hasNext(); inSequence( seq ); will( returnValue( true ) );
            oneOf( source ).getNext(); inSequence( seq ); will( returnValue( event2 ) )
            oneOf( receiver ).receive( event2 ); inSequence( seq ); when( ts.is( String.valueOf( event2.getTimestamp() ) ) );
            then( ts.is( String.valueOf( event3.getTimestamp() ) ) );
           
            // there is an event 3, so read and feed
            oneOf( source ).hasNext(); inSequence( seq ); will( returnValue( true ) );
            oneOf( source ).getNext(); inSequence( seq ); will( returnValue( event3 ) )
            oneOf( receiver ).receive( event3 ); inSequence( seq ); when( ts.is( String.valueOf( event3.getTimestamp() ) ) );
            then( ts.is( String.valueOf( event4.getTimestamp() ) ) );
           
            // there is an event 4, so read and feed
            oneOf( source ).hasNext(); inSequence( seq ); will( returnValue( true ) );
            oneOf( source ).getNext(); inSequence( seq ); will( returnValue( event4 ) )
            oneOf( receiver ).receive( event4 ); inSequence( seq ); when( ts.is( String.valueOf( event4.getTimestamp() ) ) );
            then( ts.is( String.valueOf( event5.getTimestamp() ) ) );
           
            // there is an event 5, so read and feed
            oneOf( source ).hasNext(); inSequence( seq ); will( returnValue( true ) );
            oneOf( source ).getNext(); inSequence( seq ); will( returnValue( event5 ) )
            oneOf( receiver ).receive( event5 ); inSequence( seq ); when( ts.is( String.valueOf( event5.getTimestamp() ) ) );
            then( ts.is( String.valueOf( event6.getTimestamp() ) ) );
           
            // there is an event 6, so read and feed
            oneOf( source ).hasNext(); inSequence( seq ); will( returnValue( true ) );
            oneOf( source ).getNext(); inSequence( seq ); will( returnValue( event6 ) )
            oneOf( receiver ).receive( event6 ); inSequence( seq ); when( ts.is( String.valueOf( event6.getTimestamp() ) ) );
           
            // there are no more events
            oneOf( source ).hasNext(); inSequence( seq ); will( returnValue( false ) );
        }});
       
View Full Code Here

        final DataProvider<Object> provider = mockery.mock(DataProvider.class);
        final EventPoller<Object> poller = EventPoller.newInstance(provider, sequencer, pollSequence, bufferSequence,
                                                                   gatingSequence);
        final Object event = new Object();

        final States states = mockery.states("polling");

        mockery.checking(new Expectations()
        {
            {
                allowing(sequencer).getCursor();
                will(returnValue(-1L));
                when(states.is("idle"));

                allowing(sequencer).getCursor();
                will(returnValue(0L));
                when(states.is("gating"));

                allowing(sequencer).getCursor();
                will(returnValue(0L));
                when(states.is("processing"));

                allowing(sequencer).getHighestPublishedSequence(0L, -1L);
                will(returnValue(-1L));

                allowing(sequencer).getHighestPublishedSequence(0L, 0L);
                will(returnValue(0L));

                allowing(provider).get(0);
                will(returnValue(event));
                when(states.is("processing"));

                one(handler).onEvent(event, 0, true);
                when(states.is("processing"));
            }
        });

        // Initial State - nothing published.
        states.become("idle");
        assertThat(poller.poll(handler), is(PollState.IDLE));

        // Publish Event.
        states.become("gating");
        bufferSequence.incrementAndGet();
        assertThat(poller.poll(handler), is(PollState.GATING));

        states.become("processing");
        gatingSequence.incrementAndGet();
        assertThat(poller.poll(handler), is(PollState.PROCESSING));
    }
View Full Code Here

        new DefaultTimeoutableCompletionService(completionService).submit(asList(task1, task2, task3));
    }

    @Test (expected = TimeoutException.class, timeout = 5000)
    public void tasksSubmittedButNeverCompleteTimeout() throws Exception {
        final States taken = context.states("taken").startsAs("none");
        context.checking(new Expectations() {{
            oneOf(completionService).submit(task1);
            oneOf(completionService).submit(task2);

            oneOf(completionService).take(); will(returnValue(new StubFuture(TASK1_RESULT))); then(taken.is("one"));
            oneOf(completionService).take(); will(waitForever()); when(taken.is("one"));
        }});

        new DefaultTimeoutableCompletionService(completionService, TIMEOUT, time).submit(asList(task1, task2));
    }
View Full Code Here

     *             Won't.
     */
    @Test
    public void testNotifyBasic() throws Exception
    {
        final States state = context.states("main");
        state.startsAs("none");
        context.checking(new Expectations()
        {
            {
                oneOf(placeholderPersonMapper).execute(RECIPIENT1);
                will(returnValue(person1));
                then(state.is("person1"));

                exactly(recipients.size()).of(velocityEngine).evaluate(with(any(VelocityContext.class)),
                        with(any(StringWriter.class)), with(equal("InAppNotification-POST_TO_PERSONAL_STREAM")),
                        with(equal(TEMPLATE)));
                will(new AppendRenderedAction());

                oneOf(insertMapper).execute(with(new EasyMatcher<PersistenceRequest>()
                {
                    @Override
                    protected boolean isMatch(final PersistenceRequest testObject)
                    {
                        InAppNotificationEntity notif = (InAppNotificationEntity) testObject.getDomainEnity();
                        return person1 == notif.getRecipient() && RENDERED.equals(notif.getMessage())
                                && OK_TYPE == notif.getNotificationType() && notif.getUrl() == null
                                && !notif.isHighPriority() && notif.getSourceType() == EntityType.NOTSET
                                && notif.getSourceUniqueId() == null && notif.getSourceName() == null
                                && notif.getAvatarOwnerType() == EntityType.NOTSET
                                && notif.getAvatarOwnerUniqueId() == null;
                    }
                }));
                when(state.is("person1"));

                oneOf(syncMapper).execute(RECIPIENT1);
                when(state.is("person1"));

                oneOf(placeholderPersonMapper).execute(RECIPIENT2);
                will(returnValue(person2));
                then(state.is("person2"));

                oneOf(person1).getId();
                will(returnValue(RECIPIENT1));
                when(state.is("person1"));

                oneOf(person2).getId();
                will(returnValue(RECIPIENT2));
                when(state.is("person2"));

                oneOf(insertMapper).execute(with(new EasyMatcher<PersistenceRequest>()
                {
                    @Override
                    protected boolean isMatch(final PersistenceRequest testObject)
                    {
                        InAppNotificationEntity notif = (InAppNotificationEntity) testObject.getDomainEnity();
                        return person2 == notif.getRecipient() && RENDERED.equals(notif.getMessage())
                                && OK_TYPE == notif.getNotificationType() && notif.getUrl() == null
                                && !notif.isHighPriority() && notif.getSourceType() == EntityType.NOTSET
                                && notif.getSourceUniqueId() == null && notif.getSourceName() == null
                                && notif.getAvatarOwnerType() == EntityType.NOTSET
                                && notif.getAvatarOwnerUniqueId() == null;
                    }
                }));
                when(state.is("person2"));

                oneOf(syncMapper).execute(RECIPIENT2);
                when(state.is("person2"));
            }
        });

        Collection<UserActionRequest> result = sut.notify(NotificationType.POST_TO_PERSONAL_STREAM, recipients,
                Collections.EMPTY_MAP, recipientIndex);
View Full Code Here

    {
        final Gadget g = context.mock(Gadget.class, "gadget#" + id);
        context.checking(new Expectations()
        {
            {
                States znState = context.states(id + ".zoneNumber").startsAs("initial");
                gadgetStates.put(id + ".zoneNumber", znState);

                States ziState = context.states(id + ".zoneNumber").startsAs("initial");
                gadgetStates.put(id + ".zoneIndex", ziState);

                allowing(g).getId();
                will(returnValue(id));

                allowing(g).getZoneNumber();
                will(returnValue(zoneNumber));
                when(znState.isNot("changed"));

                allowing(g).getZoneIndex();
                will(returnValue(zoneIndex));
                when(ziState.isNot("changed"));

                allowing(g).getGadgetDefinition();
                will(returnValue(new GadgetDefinition()));

                allowing(g).getOwner();
View Full Code Here

    @Test
    public void testAssertStates()
    {
        final Gadget g = context.mock(Gadget.class, "foo");

        final States znState = context.states("500.zoneNumber").startsAs("initial");

        context.checking(new Expectations()
        {
            {
                allowing(g).getZoneIndex();
                will(returnValue(1));
                when(znState.isNot("changed"));

                one(g).setZoneIndex(2);
                then(znState.is("changed"));

                allowing(g).getZoneIndex();
                will(returnValue(2));
                when(znState.is("changed"));
            }
        });

        assertEquals(1, g.getZoneIndex());
        assertEquals(1, g.getZoneIndex());
View Full Code Here

    @Test
    public void testExecute() throws MessagingException, IOException
    {
        final Map data = mockery.mock(Map.class);
        final TransactionStatus transaction = mockery.mock(TransactionStatus.class);
        final States state = mockery.states("trans").startsAs("none");
        final String content = "This is the content";
        final Serializable params = mockery.mock(Serializable.class, "params");
        final UserActionRequest selection = new UserActionRequest(ACTION_NAME, null, params);

        mockery.checking(new Expectations()
        {
            {
                allowing(message).getFrom();
                will(returnValue(new Address[] { new InternetAddress(SENDER_ADDRESS) }));

                allowing(message).getRecipients(RecipientType.TO);
                will(returnValue(new Address[] { new InternetAddress("system+" + TOKEN + AT_DOMAIN) }));
                allowing(tokenEncoder).couldBeToken(TOKEN);
                will(returnValue(true));

                oneOf(transactionMgr).getTransaction(with(any(TransactionDefinition.class)));
                when(state.is("none"));
                will(returnValue(transaction));
                then(state.is("in"));

                allowing(personIdByEmailDao).execute(SENDER_ADDRESS);
                when(state.is("in"));
                will(returnValue(PERSON_ID));

                allowing(userKeyByIdDao).execute(PERSON_ID);
                when(state.is("in"));
                will(returnValue(KEY));

                allowing(personDao).execute(PERSON_ID);
                when(state.is("in"));
                will(returnValue(person));

                oneOf(transactionMgr).commit(transaction);
                when(state.is("in"));
                then(state.is("none"));

                oneOf(tokenEncoder).decode(TOKEN, KEY);
                will(returnValue(TOKEN_CONTENT));
                oneOf(tokenContentFormatter).parse(TOKEN_CONTENT);
                will(returnValue(data));
View Full Code Here

    @Test
    public void testDownloadFileEmpty() throws IOException
    {
        setupForDownloadFileTests();

        final States state = context.states("readStep").startsAs("1");
        context.checking(new Expectations()
        {
            {
                oneOf(httpReader).read(with(any(char[].class)), with(any(int.class)), with(any(int.class)));
                when(state.is("1"));
                then(state.is("2"));
                will(returnValue(-1));

                oneOf(httpReader).close();
                when(state.is("2"));
                then(state.is("E"));
            }
        });

        assertEquals("", sut.downloadFile(URL, ACCOUNT_ID));
View Full Code Here

    @Test
    public void testDownloadFileSmall() throws IOException
    {
        setupForDownloadFileTests();

        final States state = context.states("readStep").startsAs("1");
        context.checking(new Expectations()
        {
            {
                oneOf(httpReader).read(with(any(char[].class)), with(any(int.class)), with(any(int.class)));
                when(state.is("1"));
                then(state.is("2"));
                will(new ReadAction("ABCDEFGHIJ"));

                oneOf(httpReader).read(with(any(char[].class)), with(any(int.class)), with(any(int.class)));
                when(state.is("2"));
                then(state.is("3"));
                will(new ReadAction("WXYZ"));

                oneOf(httpReader).read(with(any(char[].class)), with(any(int.class)), with(any(int.class)));
                when(state.is("3"));
                then(state.is("4"));
                will(returnValue(-1));

                oneOf(httpReader).close();
                when(state.is("4"));
                then(state.is("E"));
            }
        });

        assertEquals("ABCDEFGHIJWXYZ", sut.downloadFile(URL, ACCOUNT_ID));
View Full Code Here

TOP

Related Classes of org.jmock.States

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.