Search in sources :

Example 1 with Action

use of org.jmock.api.Action 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 2 with Action

use of org.jmock.api.Action 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 3 with Action

use of org.jmock.api.Action 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 4 with Action

use of org.jmock.api.Action 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)

Example 5 with Action

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

the class TopologyRequestValidatorTest method testTrustResponse.

@Test
public void testTrustResponse() throws IOException {
    final HttpServletRequest request = context.mock(HttpServletRequest.class);
    context.checking(new Expectations() {

        {
            allowing(request).getRequestURI();
            will(returnValue("/Test/Uri2"));
        }
    });
    final HttpServletResponse response = context.mock(HttpServletResponse.class);
    final Map<Object, Object> headers = new HashMap<Object, Object>();
    context.checking(new Expectations() {

        {
            allowing(response).setHeader(with(any(String.class)), with(any(String.class)));
            will(new Action() {

                public void describeTo(Description desc) {
                    desc.appendText("Setting header ");
                }

                public Object invoke(Invocation invocation) throws Throwable {
                    headers.put(invocation.getParameter(0), invocation.getParameter(1));
                    return null;
                }
            });
        }
    });
    String clearMessage = "TestMessage2";
    final String message = topologyRequestValidator.encodeMessage(clearMessage);
    topologyRequestValidator.trustMessage(response, request, message);
    final HttpEntity responseEntity = context.mock(HttpEntity.class);
    context.checking(new Expectations() {

        {
            allowing(responseEntity).getContent();
            will(returnValue(new ByteArrayInputStream(message.getBytes())));
        }
    });
    final HttpResponse resp = context.mock(HttpResponse.class);
    context.checking(new Expectations() {

        {
            allowing(resp).getFirstHeader(with(any(String.class)));
            will(new Action() {

                public void describeTo(Description desc) {
                    desc.appendText("Getting (first) header ");
                }

                public Object invoke(Invocation invocation) throws Throwable {
                    return new BasicHeader((String) invocation.getParameter(0), (String) headers.get(invocation.getParameter(0)));
                }
            });
            allowing(resp).getEntity();
            will(returnValue(responseEntity));
        }
    });
    topologyRequestValidator.isTrusted(resp);
    topologyRequestValidator.decodeMessage("/Test/Uri2", resp);
}
Also used : Expectations(org.jmock.Expectations) Action(org.jmock.api.Action) Description(org.hamcrest.Description) Invocation(org.jmock.api.Invocation) HttpEntity(org.apache.http.HttpEntity) HashMap(java.util.HashMap) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpResponse(org.apache.http.HttpResponse) HttpServletRequest(javax.servlet.http.HttpServletRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) BasicHeader(org.apache.http.message.BasicHeader) Test(org.junit.Test)

Aggregations

Description (org.hamcrest.Description)6 Expectations (org.jmock.Expectations)6 Action (org.jmock.api.Action)6 Invocation (org.jmock.api.Invocation)6 HashMap (java.util.HashMap)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 JUnit4Mockery (org.jmock.integration.junit4.JUnit4Mockery)2 Test (org.junit.Test)2 Disposable (com.intellij.openapi.Disposable)1 ModalityState (com.intellij.openapi.application.ModalityState)1 ApplicationEx (com.intellij.openapi.application.ex.ApplicationEx)1 ExtensionsArea (com.intellij.openapi.extensions.ExtensionsArea)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Dictionary (java.util.Dictionary)1 Filter (javax.servlet.Filter)1 ServletRequest (javax.servlet.ServletRequest)1 ServletResponse (javax.servlet.ServletResponse)1