Search in sources :

Example 6 with MockInterceptor

use of com.opensymphony.xwork2.mock.MockInterceptor in project struts by apache.

the class ConfigurationTest method testInterceptorParamInheritance.

public void testInterceptorParamInheritance() {
    try {
        ActionProxy proxy = actionProxyFactory.createActionProxy("/foo/bar", "TestInterceptorParamInheritance", null, null);
        assertEquals(1, proxy.getConfig().getInterceptors().size());
        MockInterceptor testInterceptor = (MockInterceptor) proxy.getConfig().getInterceptors().get(0).getInterceptor();
        assertEquals("expectedFoo", testInterceptor.getExpectedFoo());
        proxy.execute();
        assertTrue(testInterceptor.isExecuted());
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) MockInterceptor(com.opensymphony.xwork2.mock.MockInterceptor)

Example 7 with MockInterceptor

use of com.opensymphony.xwork2.mock.MockInterceptor in project struts by apache.

the class ConfigurationTest method testInterceptorParams.

public void testInterceptorParams() {
    try {
        ActionProxy proxy = actionProxyFactory.createActionProxy("", "TestInterceptorParam", null, null);
        assertEquals(1, proxy.getConfig().getInterceptors().size());
        MockInterceptor testInterceptor = (MockInterceptor) proxy.getConfig().getInterceptors().get(0).getInterceptor();
        assertEquals("expectedFoo", testInterceptor.getExpectedFoo());
        proxy.execute();
        assertTrue(testInterceptor.isExecuted());
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) MockInterceptor(com.opensymphony.xwork2.mock.MockInterceptor)

Example 8 with MockInterceptor

use of com.opensymphony.xwork2.mock.MockInterceptor in project struts by apache.

the class DefaultActionInvocationTester method testInvokeWithAsyncManager.

public void testInvokeWithAsyncManager() throws Exception {
    DefaultActionInvocation dai = new DefaultActionInvocation(new HashMap<String, Object>(), false);
    dai.stack = container.getInstance(ValueStackFactory.class).createValueStack();
    final Semaphore lock = new Semaphore(1);
    lock.acquire();
    dai.setAsyncManager(new AsyncManager() {

        Object asyncActionResult;

        @Override
        public boolean hasAsyncActionResult() {
            return asyncActionResult != null;
        }

        @Override
        public Object getAsyncActionResult() {
            return asyncActionResult;
        }

        @Override
        public void invokeAsyncAction(Callable asyncAction) {
            try {
                asyncActionResult = asyncAction.call();
            } catch (Exception e) {
                asyncActionResult = e;
            }
            lock.release();
        }
    });
    dai.action = new Callable<Callable<String>>() {

        @Override
        public Callable<String> call() throws Exception {
            return new Callable<String>() {

                @Override
                public String call() throws Exception {
                    return "success";
                }
            };
        }
    };
    MockActionProxy actionProxy = new MockActionProxy();
    actionProxy.setMethod("call");
    dai.proxy = actionProxy;
    final boolean[] preResultExecuted = new boolean[1];
    dai.addPreResultListener(new PreResultListener() {

        @Override
        public void beforeResult(ActionInvocation invocation, String resultCode) {
            preResultExecuted[0] = true;
        }
    });
    List<InterceptorMapping> interceptorMappings = new ArrayList<>();
    MockInterceptor mockInterceptor1 = new MockInterceptor();
    mockInterceptor1.setFoo("test1");
    mockInterceptor1.setExpectedFoo("test1");
    interceptorMappings.add(new InterceptorMapping("test1", mockInterceptor1));
    dai.interceptors = interceptorMappings.iterator();
    dai.ognlUtil = new OgnlUtil();
    dai.invoke();
    assertTrue("interceptor1 should be executed", mockInterceptor1.isExecuted());
    assertFalse("preResultListener should no be executed", preResultExecuted[0]);
    assertNotNull("an async action should be saved", dai.asyncAction);
    assertFalse("invocation should not be executed", dai.executed);
    assertNull("a null result should be passed to upper and wait for the async result", dai.resultCode);
    if (lock.tryAcquire(1500L, TimeUnit.MILLISECONDS)) {
        try {
            dai.invoke();
            assertTrue("preResultListener should be executed", preResultExecuted[0]);
            assertNull("async action should be cleared", dai.asyncAction);
            assertTrue("invocation should be executed", dai.executed);
            assertEquals("success", dai.resultCode);
        } finally {
            lock.release();
        }
    } else {
        lock.release();
        fail("async result did not received on timeout!");
    }
}
Also used : ArrayList(java.util.ArrayList) Semaphore(java.util.concurrent.Semaphore) PreResultListener(com.opensymphony.xwork2.interceptor.PreResultListener) Callable(java.util.concurrent.Callable) MockInterceptor(com.opensymphony.xwork2.mock.MockInterceptor) OgnlUtil(com.opensymphony.xwork2.ognl.OgnlUtil) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) MockActionProxy(com.opensymphony.xwork2.mock.MockActionProxy)

Example 9 with MockInterceptor

use of com.opensymphony.xwork2.mock.MockInterceptor in project struts by apache.

the class DispatcherTest method testInterceptorDestroy.

public void testInterceptorDestroy() throws Exception {
    Mock mockInterceptor = new Mock(Interceptor.class);
    mockInterceptor.matchAndReturn("hashCode", 0);
    mockInterceptor.expect("destroy");
    InterceptorMapping interceptorMapping = new InterceptorMapping("test", (Interceptor) mockInterceptor.proxy());
    InterceptorStackConfig isc = new InterceptorStackConfig.Builder("test").addInterceptor(interceptorMapping).build();
    PackageConfig packageConfig = new PackageConfig.Builder("test").addInterceptorStackConfig(isc).build();
    Map<String, PackageConfig> packageConfigs = new HashMap<String, PackageConfig>();
    packageConfigs.put("test", packageConfig);
    Mock mockContainer = new Mock(Container.class);
    mockContainer.matchAndReturn("getInstance", C.args(C.eq(ObjectFactory.class)), new ObjectFactory());
    String reloadConfigs = container.getInstance(String.class, StrutsConstants.STRUTS_CONFIGURATION_XML_RELOAD);
    mockContainer.expectAndReturn("getInstance", C.args(C.eq(String.class), C.eq(StrutsConstants.STRUTS_CONFIGURATION_XML_RELOAD)), reloadConfigs);
    Mock mockConfiguration = new Mock(Configuration.class);
    mockConfiguration.matchAndReturn("getPackageConfigs", packageConfigs);
    mockConfiguration.matchAndReturn("getContainer", mockContainer.proxy());
    mockConfiguration.expect("destroy");
    ConfigurationManager configurationManager = new ConfigurationManager(Container.DEFAULT_NAME);
    configurationManager.setConfiguration((Configuration) mockConfiguration.proxy());
    Dispatcher dispatcher = new MockDispatcher(new MockServletContext(), new HashMap<String, String>(), configurationManager);
    dispatcher.init();
    dispatcher.cleanup();
    mockInterceptor.verify();
    mockContainer.verify();
    mockConfiguration.verify();
}
Also used : HashMap(java.util.HashMap) Mock(com.mockobjects.dynamic.Mock) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) MockServletContext(org.springframework.mock.web.MockServletContext) InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) ObjectFactory(com.opensymphony.xwork2.ObjectFactory) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) ConfigurationManager(com.opensymphony.xwork2.config.ConfigurationManager)

Example 10 with MockInterceptor

use of com.opensymphony.xwork2.mock.MockInterceptor in project emfcloud-modelserver by eclipse-emfcloud.

the class ModelServerClientV2Test method before.

@Before
public void before() {
    interceptor = new MockInterceptor();
    jsonCodec = new JsonCodecV2();
    baseHttpUrlBuilder = new HttpUrl.Builder().scheme("http").host("fake-url.com").addPathSegment("api").addPathSegment("v2");
    eClass = EcoreFactory.eINSTANCE.createEClass();
    eClass.setName("AbstractTestClass");
    eClass.setAbstract(true);
}
Also used : Builder(okhttp3.HttpUrl.Builder) MockInterceptor(okhttp3.mock.MockInterceptor) JsonCodecV2(org.eclipse.emfcloud.modelserver.emf.common.codecs.JsonCodecV2) HttpUrl(okhttp3.HttpUrl) Before(org.junit.Before)

Aggregations

MockInterceptor (okhttp3.mock.MockInterceptor)13 Before (org.junit.Before)9 MockInterceptor (com.opensymphony.xwork2.mock.MockInterceptor)7 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)5 OkHttpClient (okhttp3.OkHttpClient)5 ActionProxy (com.opensymphony.xwork2.ActionProxy)4 lombok.val (lombok.val)4 Test (org.junit.jupiter.api.Test)4 InterceptorStackConfig (com.opensymphony.xwork2.config.entities.InterceptorStackConfig)3 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 HttpUrl (okhttp3.HttpUrl)3 Builder (okhttp3.HttpUrl.Builder)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)2 Map (java.util.Map)2 StrutsXmlConfigurationProvider (org.apache.struts2.config.StrutsXmlConfigurationProvider)2 JsonCodecV2 (org.eclipse.emfcloud.modelserver.emf.common.codecs.JsonCodecV2)2 Mock (com.mockobjects.dynamic.Mock)1