org.jmock.api.Action Java Examples

The following examples show how to use org.jmock.api.Action. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JUnit4GroovyMockery.java    From pushfish-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
void will(final Closure cl) {
    will(new Action() {
        public void describeTo(Description description) {
            description.appendText("execute closure");
        }

        public Object invoke(Invocation invocation) throws Throwable {
            List<Object> params = Arrays.asList(invocation.getParametersAsArray());
            Object result;
            try {
                List<Object> subParams = params.subList(0, Math.min(invocation.getParametersAsArray().length,
                        cl.getMaximumNumberOfParameters()));
                result = cl.call(subParams.toArray(new Object[subParams.size()]));
            } catch (InvokerInvocationException e) {
                throw e.getCause();
            }
            if (invocation.getInvokedMethod().getReturnType().isInstance(result)) {
                return result;
            }
            return null;
        }
    });
}
 
Example #2
Source File: JUnit4GroovyMockery.java    From pushfish-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
void will(final Closure cl) {
    will(new Action() {
        public void describeTo(Description description) {
            description.appendText("execute closure");
        }

        public Object invoke(Invocation invocation) throws Throwable {
            List<Object> params = Arrays.asList(invocation.getParametersAsArray());
            Object result;
            try {
                List<Object> subParams = params.subList(0, Math.min(invocation.getParametersAsArray().length,
                        cl.getMaximumNumberOfParameters()));
                result = cl.call(subParams.toArray(new Object[subParams.size()]));
            } catch (InvokerInvocationException e) {
                throw e.getCause();
            }
            if (invocation.getInvokedMethod().getReturnType().isInstance(result)) {
                return result;
            }
            return null;
        }
    });
}
 
Example #3
Source File: JUnit4GroovyMockery.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
void will(final Closure cl) {
    will(new Action() {
        public void describeTo(Description description) {
            description.appendText("execute closure");
        }

        public Object invoke(Invocation invocation) throws Throwable {
            List<Object> params = Arrays.asList(invocation.getParametersAsArray());
            Object result;
            try {
                List<Object> subParams = params.subList(0, Math.min(invocation.getParametersAsArray().length,
                        cl.getMaximumNumberOfParameters()));
                result = cl.call(subParams.toArray(new Object[subParams.size()]));
            } catch (InvokerInvocationException e) {
                throw e.getCause();
            }
            if (invocation.getInvokedMethod().getReturnType().isInstance(result)) {
                return result;
            }
            return null;
        }
    });
}
 
Example #4
Source File: JUnit4GroovyMockery.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
void will(final Closure cl) {
    will(new Action() {
        public void describeTo(Description description) {
            description.appendText("execute closure");
        }

        public Object invoke(Invocation invocation) throws Throwable {
            List<Object> params = Arrays.asList(invocation.getParametersAsArray());
            Object result;
            try {
                List<Object> subParams = params.subList(0, Math.min(invocation.getParametersAsArray().length,
                        cl.getMaximumNumberOfParameters()));
                result = cl.call(subParams.toArray(new Object[subParams.size()]));
            } catch (InvokerInvocationException e) {
                throw e.getCause();
            }
            if (invocation.getInvokedMethod().getReturnType().isInstance(result)) {
                return result;
            }
            return null;
        }
    });
}
 
Example #5
Source File: IndexValueRepository_Test.java    From estatio with Apache License 2.0 5 votes vote down vote up
private static Action executeCallableAndReturn() {
    return new Action() {
        @Override
        public Object invoke(Invocation invocation) throws Throwable {
            Callable<Object> callable = (Callable<Object>) invocation.getParameter(0);
            return callable.call();
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("execute arg 0 as callable and return");
        }
    };
}
 
Example #6
Source File: LeaseTerm_Test.java    From estatio with Apache License 2.0 5 votes vote down vote up
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");
        }
    };
}
 
Example #7
Source File: PathMacroManagerTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Before
public final void setupApplication() throws Exception {
  // in fact the test accesses extension points so it rather should be converted to a platform one
  assumeNotNull(ApplicationManager.getApplication());

  context = new JUnit4Mockery();
  context.setImposteriser(ClassImposteriser.INSTANCE);
  myApplication = context.mock(ApplicationEx.class, "application");

  context.checking(new Expectations() {
    {
      allowing(myApplication).isUnitTestMode();
      will(returnValue(false));

      // 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
        @javax.annotation.Nullable
        public Object invoke(final Invocation invocation) throws Throwable {
          ((Runnable)invocation.getParameter(0)).run();
          return null;
        }
      });
    }
  });
}
 
Example #8
Source File: ListTagTest.java    From uyuni with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Tests normal conditions for ListTag.
 * @throws Exception something bad happened
 */
public void testRegularRun() throws Exception {
    context().checking(new Expectations() { {
        atLeast(1).of(req).getRequestURI();
        will(returnValue("UTF-8"));

        atLeast(1).of(req).getAttribute("session");
        will(returnValue(webSess));

        atLeast(1).of(webSess).getWebUserId();
        will(returnValue(null));

        atLeast(1).of(req).getParameter(with(containsString("list_")));
        will(returnValue(null));

        atLeast(1).of(req).setAttribute(
                with(equal("pageNum")),
                with(any(String.class)));

        atLeast(1).of(req).setAttribute(
                with(equal("dataSize")),
                with(any(String.class)));

        atLeast(1).of(pageContext).getOut();
        will(returnValue(writer));

        atLeast(1).of(pageContext).setAttribute(
                with(containsString("_cmd")),
                with(any(Object.class)));

        atLeast(1).of(pageContext).setAttribute(
                with(equal("current")),
                with(any(Object.class)));

        atLeast(1).of(pageContext).getAttribute("current");
        will(returnValue(null));

        atLeast(1).of(pageContext).pushBody(with(any(Writer.class)));

        atLeast(1).of(pageContext).popBody();

        atLeast(1).of(req)
                .getParameter(with(containsString("PAGE_SIZE_LABEL_SELECTED")));
        will(returnValue(null));
    } });

    final Action[] cmdValues = {
            returnValue(ListCommand.ENUMERATE), // listtag asking
            returnValue(ListCommand.ENUMERATE), // columntag asking
            returnValue(ListCommand.TBL_HEADING), // listtag asking
            returnValue(ListCommand.TBL_HEADING), // columntag asking
            returnValue(ListCommand.TBL_ADDONS), // listtag asking
            returnValue(ListCommand.TBL_ADDONS), // columntag asking
            returnValue(ListCommand.COL_HEADER), // listtag asking
            returnValue(ListCommand.COL_HEADER), // columntag asking
            returnValue(ListCommand.BEFORE_RENDER), // listtag asking
            returnValue(ListCommand.BEFORE_RENDER), // columntag asking
            returnValue(ListCommand.RENDER),    // listtag asking
            returnValue(ListCommand.RENDER),    // columntag asking
            returnValue(ListCommand.AFTER_RENDER), // listtag asking
            returnValue(ListCommand.AFTER_RENDER), // columntag asking
            returnValue(ListCommand.TBL_FOOTER), // listtag asking
            returnValue(ListCommand.TBL_FOOTER) // columntag asking
    };

    context().checking(new Expectations() { {
        atLeast(1).of(pageContext).getAttribute(with(containsString("_cmd")));
        will(onConsecutiveCalls(cmdValues));
    } });

    int tagval = lt.doStartTag();

    assertEquals(BodyTagSupport.EVAL_BODY_INCLUDE, tagval);
    do {
        tagval = lt.doAfterBody();
    } while (tagval == BodyTagSupport.EVAL_BODY_AGAIN);
    tagval = lt.doEndTag();
    assertEquals(BodyTagSupport.EVAL_PAGE, tagval);
}
 
Example #9
Source File: Matchers.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public Action then(Action action) {
    this.action = action;
    return this;
}
 
Example #10
Source File: Matchers.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public Action then(Action action) {
    this.action = action;
    return this;
}
 
Example #11
Source File: Matchers.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public Action then(Action action) {
    this.action = action;
    return this;
}
 
Example #12
Source File: Matchers.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public Action then(Action action) {
    this.action = action;
    return this;
}
 
Example #13
Source File: FutureActions.java    From c5-replicator with Apache License 2.0 4 votes vote down vote up
public static Action returnFutureWithValue(Object futureValue) {
  return new ReturnFutureWithValueAction(futureValue);
}
 
Example #14
Source File: FutureActions.java    From c5-replicator with Apache License 2.0 4 votes vote down vote up
public static Action returnFutureWithException(Throwable exception) {
  return new ReturnFutureWithException(exception);
}
 
Example #15
Source File: C5FuturesTest.java    From c5-replicator with Apache License 2.0 4 votes vote down vote up
public static Action runTheRunnable() {
  return new RunARunnableAction();
}
 
Example #16
Source File: ListTagTest.java    From spacewalk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Tests normal conditions for ListTag.
 * @throws Exception something bad happened
 */
public void testRegularRun() throws Exception {
    context().checking(new Expectations() { {
        atLeast(1).of(req).getRequestURI();
        will(returnValue("UTF-8"));

        atLeast(1).of(req).getAttribute("session");
        will(returnValue(webSess));

        atLeast(1).of(webSess).getWebUserId();
        will(returnValue(null));

        atLeast(1).of(req).getParameter(with(containsString("list_")));
        will(returnValue(null));

        atLeast(1).of(req).setAttribute(
                with(equal("pageNum")),
                with(any(String.class)));

        atLeast(1).of(req).setAttribute(
                with(equal("dataSize")),
                with(any(String.class)));

        atLeast(1).of(pageContext).getOut();
        will(returnValue(writer));

        atLeast(1).of(pageContext).setAttribute(
                with(containsString("_cmd")),
                with(any(Object.class)));

        atLeast(1).of(pageContext).setAttribute(
                with(equal("current")),
                with(any(Object.class)));

        atLeast(1).of(pageContext).getAttribute("current");
        will(returnValue(null));

        atLeast(1).of(pageContext).pushBody(with(any(Writer.class)));

        atLeast(1).of(pageContext).popBody();

        atLeast(1).of(req)
                .getParameter(with(containsString("PAGE_SIZE_LABEL_SELECTED")));
        will(returnValue(null));
    } });

    final Action[] cmdValues = {
            returnValue(ListCommand.ENUMERATE), // listtag asking
            returnValue(ListCommand.ENUMERATE), // columntag asking
            returnValue(ListCommand.TBL_HEADING), // listtag asking
            returnValue(ListCommand.TBL_HEADING), // columntag asking
            returnValue(ListCommand.TBL_ADDONS), // listtag asking
            returnValue(ListCommand.TBL_ADDONS), // columntag asking
            returnValue(ListCommand.COL_HEADER), // listtag asking
            returnValue(ListCommand.COL_HEADER), // columntag asking
            returnValue(ListCommand.BEFORE_RENDER), // listtag asking
            returnValue(ListCommand.BEFORE_RENDER), // columntag asking
            returnValue(ListCommand.RENDER),    // listtag asking
            returnValue(ListCommand.RENDER),    // columntag asking
            returnValue(ListCommand.AFTER_RENDER), // listtag asking
            returnValue(ListCommand.AFTER_RENDER), // columntag asking
            returnValue(ListCommand.TBL_FOOTER), // listtag asking
            returnValue(ListCommand.TBL_FOOTER) // columntag asking
    };

    context().checking(new Expectations() { {
        atLeast(1).of(pageContext).getAttribute(with(containsString("_cmd")));
        will(onConsecutiveCalls(cmdValues));
    } });

    int tagval = lt.doStartTag();

    assertEquals(BodyTagSupport.EVAL_BODY_INCLUDE, tagval);
    do {
        tagval = lt.doAfterBody();
    } while (tagval == BodyTagSupport.EVAL_BODY_AGAIN);
    tagval = lt.doEndTag();
    assertEquals(BodyTagSupport.EVAL_PAGE, tagval);
}