package com.wizzardo.servlet;

import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.junit.Assert;
import org.junit.Test;

import javax.servlet.DispatcherType;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.EnumSet;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by wizzardo on 01.01.15.
 */
public class FilterTest extends ServerTest {

    CustomFilter filter;

    @Override
    protected void init() {
        super.init();

        filter = new CustomFilter();
        filter.onInit = it -> {
            System.out.println("onInit");
            return null;
        };
        filter.onDestroy = () -> {
            System.out.println("onDestroy");
        };
        ((ServletContextHandler) jetty.getHandler()).addFilter(new FilterHolder(filter), "/*", EnumSet.of(DispatcherType.REQUEST));
    }

    @Test
    public void filterTest() throws IOException {
        AtomicInteger counter = new AtomicInteger();
        filter.handler = (req, resp, chain) -> {
            Assert.assertEquals("/foo/bar", ((HttpServletRequest) req).getServletPath());
            counter.incrementAndGet();
        };
        jettyRequest(CONTEXT_PATH + "/foo/bar").get().asString();
        Assert.assertEquals(1, counter.get());
    }
}

@@ -478,16 +478,114 @@
  filter;
+%0A    AtomicInteger inits = new AtomicInteger();%0A    AtomicInteger destoryes = new AtomicInteger();
 %0A%0A    @O
@@ -728,35 +728,30 @@
     
-System.out.println(%22onInit%22
+inits.incrementAndGet(
 );%0A 
@@ -836,38 +836,34 @@
     
-System.out.println(%22onDestroy%22
+destoryes.incrementAndGet(
 );%0A 
@@ -1011,16 +1011,386 @@
 %0A    %7D%0A%0A
+    @Override%0A    public void setUp() throws Exception %7B%0A        super.setUp();%0A%0A        Assert.assertEquals(1, inits.get());%0A        Assert.assertEquals(0, destoryes.get());%0A    %7D%0A%0A    @Override%0A    public void tearDown() throws Exception %7B%0A        super.tearDown();%0A%0A        Assert.assertEquals(1, inits.get());%0A        Assert.assertEquals(1, destoryes.get());%0A    %7D%0A%0A
     @Tes

improve test for jetty filter