Search in sources :

Example 1 with Invocation

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

the class TabPostFormatProcessorTest method doDocumentTest.

private void doDocumentTest(@NotNull String initial, @NotNull String expected, boolean useTabs, boolean smartTabs, int tabWidth) {
    Pair<String, TextRange> pair = parse(initial);
    final StringBuilder text = new StringBuilder(pair.first);
    final TextRange range = pair.second;
    myMockery.checking(new Expectations() {

        {
            allowing(myDocument).getCharsSequence();
            will(returnValue(text.toString()));
            allowing(myDocument).getTextLength();
            will(returnValue(text.length()));
        }
    });
    final LineSet lines = LineSet.createLineSet(myDocument.getCharsSequence());
    myMockery.checking(new Expectations() {

        {
            allowing(myDocument).getLineNumber(with(any(int.class)));
            will(new CustomAction("getLineNumber()") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    return lines.findLineIndex((Integer) invocation.getParameter(0));
                }
            });
            allowing(myDocument).getLineStartOffset(with(any(int.class)));
            will(new CustomAction("getLineStartOffset()") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    return lines.getLineStart((Integer) invocation.getParameter(0));
                }
            });
            allowing(myDocument).getLineEndOffset(with(any(int.class)));
            will(new CustomAction("getLineEndOffset()") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    return lines.getLineEnd((Integer) invocation.getParameter(0));
                }
            });
            allowing(myDocument).replaceString(with(any(int.class)), with(any(int.class)), with(any(String.class)));
            will(new CustomAction("replaceString") {

                @Nullable
                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    int start = (Integer) invocation.getParameter(0);
                    int end = (Integer) invocation.getParameter(1);
                    String newText = (String) invocation.getParameter(2);
                    text.replace(start, end, newText);
                    return null;
                }
            });
        }
    });
    TabPostFormatProcessor.processViaDocument(myDocument, range, useTabs, smartTabs, tabWidth);
    assertEquals(expected, text.toString());
}
Also used : Expectations(org.jmock.Expectations) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) TextRange(com.intellij.openapi.util.TextRange) LineSet(com.intellij.openapi.editor.impl.LineSet)

Example 2 with Invocation

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

the class AutoBalancerJUnitTest method testAuditorInvocation.

@Test
public void testAuditorInvocation() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(3);
    mockContext.checking(new Expectations() {

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

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    latch.countDown();
                    return 990L;
                }
            });
        }
    });
    Properties props = AutoBalancerJUnitTest.getBasicConfig();
    assertEquals(3, latch.getCount());
    AutoBalancer autoR = new AutoBalancer(null, mockAuditor, mockClock, null);
    autoR.init(props);
    assertTrue(latch.await(1, TimeUnit.SECONDS));
}
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 3 with Invocation

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

the class DefaultScriptRunnerFactoryTest method setsUpAndTearsDownWhenNonEmptyScriptIsRun.

@Test
public void setsUpAndTearsDownWhenNonEmptyScriptIsRun() {
    ScriptRunner<?, Void> scriptRunner = factory.create(compiledScriptMock, scriptSourceDummy, classLoaderDummy);
    expectScriptInstantiated();
    context.checking(new Expectations() {

        {
            Sequence sequence = context.sequence("seq");
            allowing(compiledScriptMock).getRunDoesSomething();
            will(returnValue(true));
            one(scriptExecutionListenerMock).scriptClassLoaded(scriptSourceDummy, Script.class);
            inSequence(sequence);
            one(scriptMock).init(target, scriptServices);
            inSequence(sequence);
            one(standardOutputCaptureMock).start();
            inSequence(sequence);
            one(scriptMock).run();
            inSequence(sequence);
            will(doAll(new Action() {

                public void describeTo(Description description) {
                    description.appendValue("check context classloader");
                }

                public Object invoke(Invocation invocation) throws Throwable {
                    assertThat(Thread.currentThread().getContextClassLoader(), sameInstance(classLoaderDummy));
                    return null;
                }
            }));
            one(standardOutputCaptureMock).stop();
            inSequence(sequence);
        }
    });
    ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
    assertThat(originalClassLoader, not(sameInstance(classLoaderDummy)));
    scriptRunner.run(target, scriptServices);
    assertThat(Thread.currentThread().getContextClassLoader(), sameInstance(originalClassLoader));
}
Also used : Expectations(org.jmock.Expectations) Script(org.gradle.groovy.scripts.Script) Action(org.jmock.api.Action) Description(org.hamcrest.Description) Invocation(org.jmock.api.Invocation) Sequence(org.jmock.Sequence) Test(org.junit.Test)

Example 4 with Invocation

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

the class TaskReportTaskTest method task.

private Task task(final String name, final String taskGroup, final Task... dependencies) {
    final Task task = context.mock(Task.class);
    context.checking(new Expectations() {

        {
            allowing(task).getName();
            will(returnValue(name));
            allowing(task).getPath();
            will(returnValue(':' + name));
            allowing(task).getProject();
            will(returnValue(project));
            allowing(project).relativeProjectPath(':' + name);
            will(returnValue(name));
            allowing(task).getGroup();
            will(returnValue(taskGroup));
            allowing(task).compareTo(with(Matchers.notNullValue(Task.class)));
            will(new Action() {

                public Object invoke(Invocation invocation) throws Throwable {
                    Task other = (Task) invocation.getParameter(0);
                    return name.compareTo(other.getName());
                }

                public void describeTo(Description description) {
                    description.appendText("compare to");
                }
            });
            TaskDependency dependency = context.mock(TaskDependency.class);
            allowing(task).getTaskDependencies();
            will(returnValue(dependency));
            allowing(dependency).getDependencies(task);
            will(returnValue(toSet(dependencies)));
        }
    });
    return task;
}
Also used : Expectations(org.jmock.Expectations) TaskDependency(org.gradle.api.tasks.TaskDependency) Task(org.gradle.api.Task) Action(org.jmock.api.Action) Description(org.hamcrest.Description) Invocation(org.jmock.api.Invocation)

Example 5 with Invocation

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

the class AnnotationsMockSetup method setupExpectations.

/**
 * Sets up the expectations for the {@link IOService} and {@link IOTargetService} to return correctly the values in
 * the test files for {@code docName}. Call this function when operating with mocked documents to provide all the
 * information in the test file (document source, rendered contents, annotations).
 *
 * @param docName the name of the document to setup expectations for
 * @throws IOServiceException if something wrong happens while mocking the documents access
 * @throws IOException if something wrong happens while mocking the documents access
 */
public void setupExpectations(final String docName) throws IOServiceException, IOException {
    mockery.checking(new Expectations() {

        {
            MockDocument mDoc = docFactory.getDocument(docName);
            allowing(ioService).getAnnotations(with(docName));
            will(returnValue(mDoc.getAnnotations()));
            allowing(ioService).updateAnnotations(with(docName), with(any(Collection.class)));
            // update the list of document annotations
            will(new Action() {

                @Override
                public void describeTo(Description description) {
                    description.appendText("Updates the annotations");
                }

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    String documentName = (String) invocation.getParameter(0);
                    MockDocument document = docFactory.getDocument(documentName);
                    Collection<Annotation> annList = (Collection<Annotation>) invocation.getParameter(1);
                    for (Annotation ann : annList) {
                        Annotation toUpdate = getAnnotation(ann.getId(), document.getAnnotations());
                        // remove toUpdate and add ann
                        if (toUpdate != null) {
                            document.getAnnotations().remove(toUpdate);
                        }
                        document.getAnnotations().add(ann);
                    }
                    return null;
                }

                private Annotation getAnnotation(String annId, Collection<Annotation> list) {
                    for (Annotation ann : list) {
                        if (ann.getId().equals(annId)) {
                            return ann;
                        }
                    }
                    return null;
                }
            });
            allowing(ioTargetService).getSource(with(docName));
            will(returnValue(mDoc.getSource()));
            allowing(ioTargetService).getSourceSyntax(with(docName));
            will(returnValue(mDoc.getSyntax()));
        }
    });
}
Also used : Expectations(org.jmock.Expectations) Action(org.jmock.api.Action) Description(org.hamcrest.Description) Invocation(org.jmock.api.Invocation) Collection(java.util.Collection)

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