Search in sources :

Example 1 with InterceptorChain

use of com.dtflys.forest.interceptor.InterceptorChain in project forest by dromara.

the class TestInterceptorChain method testInterceptorChain.

@Test
public void testInterceptorChain() {
    final AtomicInteger count = new AtomicInteger(0);
    final AtomicBoolean inter1Before = new AtomicBoolean(false);
    final AtomicBoolean inter2Before = new AtomicBoolean(false);
    final AtomicBoolean inter3Before = new AtomicBoolean(false);
    final AtomicBoolean inter1Success = new AtomicBoolean(false);
    final AtomicBoolean inter2Success = new AtomicBoolean(false);
    final AtomicBoolean inter1Error = new AtomicBoolean(false);
    final AtomicBoolean inter2Error = new AtomicBoolean(false);
    final AtomicBoolean inter1After = new AtomicBoolean(false);
    final AtomicBoolean inter2After = new AtomicBoolean(false);
    final AtomicBoolean only2After = new AtomicBoolean(false);
    Interceptor interceptor1 = new Interceptor() {

        @Override
        public boolean beforeExecute(ForestRequest request) {
            inter1Before.set(true);
            return true;
        }

        @Override
        public void onSuccess(Object data, ForestRequest request, ForestResponse response) {
            inter1Success.set(true);
            count.incrementAndGet();
        }

        @Override
        public void onError(ForestRuntimeException ex, ForestRequest request, ForestResponse response) {
            inter1Error.set(true);
        }

        @Override
        public void afterExecute(ForestRequest request, ForestResponse response) {
            inter1After.set(true);
        }
    };
    Interceptor interceptor2 = new Interceptor() {

        @Override
        public boolean beforeExecute(ForestRequest request) {
            inter2Before.set(true);
            return true;
        }

        @Override
        public void onSuccess(Object data, ForestRequest request, ForestResponse response) {
            inter2Success.set(true);
            count.incrementAndGet();
        }

        @Override
        public void onError(ForestRuntimeException ex, ForestRequest request, ForestResponse response) {
            inter2Error.set(true);
        }

        @Override
        public void afterExecute(ForestRequest request, ForestResponse response) {
            inter2After.set(true);
            only2After.set(true);
        }
    };
    Interceptor interceptor3 = new Interceptor() {

        @Override
        public boolean beforeExecute(ForestRequest request) {
            inter3Before.set(true);
            return false;
        }

        @Override
        public void onSuccess(Object data, ForestRequest request, ForestResponse response) {
        }

        @Override
        public void onError(ForestRuntimeException ex, ForestRequest request, ForestResponse response) {
        }

        @Override
        public void afterExecute(ForestRequest request, ForestResponse response) {
        }
    };
    InterceptorChain chain = new InterceptorChain();
    chain.addInterceptor(interceptor1).addInterceptor(interceptor2);
    assertEquals(2, chain.getInterceptorSize());
    assertTrue(chain.beforeExecute(null));
    assertTrue(inter1Before.get());
    assertTrue(inter2Before.get());
    chain.addInterceptor(interceptor3);
    assertFalse(inter3Before.get());
    chain.onSuccess(null, null, null);
    assertTrue(inter1Success.get());
    assertTrue(inter2Success.get());
    assertEquals(2, count.get());
    chain.onError(null, null, null);
    assertTrue(inter1Error.get());
    assertTrue(inter2Error.get());
    chain.afterExecute(null, null);
    assertTrue(inter1After.get());
    assertTrue(inter2After.get());
    assertTrue(only2After.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InterceptorChain(com.dtflys.forest.interceptor.InterceptorChain) ForestResponse(com.dtflys.forest.http.ForestResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) ForestRequest(com.dtflys.forest.http.ForestRequest) Interceptor(com.dtflys.forest.interceptor.Interceptor) Test(org.junit.Test)

Aggregations

ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)1 ForestRequest (com.dtflys.forest.http.ForestRequest)1 ForestResponse (com.dtflys.forest.http.ForestResponse)1 Interceptor (com.dtflys.forest.interceptor.Interceptor)1 InterceptorChain (com.dtflys.forest.interceptor.InterceptorChain)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1