001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.access.sift;
015
016import static org.junit.Assert.assertEquals;
017
018import java.net.HttpURLConnection;
019import java.net.URL;
020import java.util.LinkedHashSet;
021import java.util.Set;
022
023import ch.qos.logback.access.jetty.JettyFixtureBase;
024import org.junit.After;
025import org.junit.Before;
026import org.junit.Test;
027
028import ch.qos.logback.access.jetty.RequestLogImpl;
029import ch.qos.logback.access.spi.IAccessEvent;
030import ch.qos.logback.access.spi.Util;
031import ch.qos.logback.core.read.ListAppender;
032import ch.qos.logback.core.testUtil.RandomUtil;
033import ch.qos.logback.core.util.StatusPrinter;
034
035public class SiftingAppenderTest {
036    static final String PREFIX = "src/test/input/jetty/";
037    static int RANDOM_SERVER_PORT = RandomUtil.getRandomServerPort();
038
039    JettyFixtureBase jettyFixture;
040    RequestLogImpl rli = new RequestLogImpl();
041
042    @Before
043    public void startServer() throws Exception {
044        jettyFixture = new JettyFixtureBase(rli, RANDOM_SERVER_PORT);
045    }
046
047    @After
048    public void stopServer() throws Exception {
049        if (jettyFixture != null) {
050            jettyFixture.stop();
051        }
052    }
053
054    @Test
055    public void invokingDifferentPathShouldBeSiftedAccordingly() throws Exception {
056        rli.setFileName(PREFIX + "sifting.xml");
057        jettyFixture.start();
058        invokeServer("/");
059        invokeServer("/x");
060        invokeServer("/x");
061        invokeServer("/y");
062
063        StatusPrinter.print(rli);
064        SiftingAppender siftingAppender = (SiftingAppender) rli.getAppender("SIFTING");
065        Set<String> keySet = siftingAppender.getAppenderTracker().allKeys();
066        assertEquals(3, keySet.size());
067
068        Set<String> witnessSet = new LinkedHashSet<String>();
069        witnessSet.add("NA");
070        witnessSet.add("x");
071        witnessSet.add("y");
072        assertEquals(witnessSet, keySet);
073
074        check(siftingAppender, "NA", 1);
075        check(siftingAppender, "x", 2);
076        check(siftingAppender, "y", 1);
077    }
078
079    private void check(SiftingAppender siftingAppender, String key, int expectedCount) {
080        ListAppender<IAccessEvent> listAppender = (ListAppender<IAccessEvent>) siftingAppender.getAppenderTracker().find(key);
081        assertEquals(expectedCount, listAppender.list.size());
082    }
083
084    void invokeServer(String uri) throws Exception {
085        URL url = new URL("http://localhost:" + RANDOM_SERVER_PORT + uri);
086        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
087        connection.setDoInput(true);
088        Util.readToString(connection.getInputStream());
089        Thread.sleep(10);
090    }
091}