View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.access.sift;
15  
16  import static org.junit.Assert.assertEquals;
17  
18  import java.net.HttpURLConnection;
19  import java.net.URL;
20  import java.util.LinkedHashSet;
21  import java.util.Set;
22  
23  import ch.qos.logback.access.jetty.JettyFixtureBase;
24  import org.junit.After;
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  import ch.qos.logback.access.jetty.RequestLogImpl;
29  import ch.qos.logback.access.spi.IAccessEvent;
30  import ch.qos.logback.access.spi.Util;
31  import ch.qos.logback.core.read.ListAppender;
32  import ch.qos.logback.core.testUtil.RandomUtil;
33  import ch.qos.logback.core.util.StatusPrinter;
34  
35  public class SiftingAppenderTest {
36      static final String PREFIX = "src/test/input/jetty/";
37      static int RANDOM_SERVER_PORT = RandomUtil.getRandomServerPort();
38  
39      JettyFixtureBase jettyFixture;
40      RequestLogImpl rli = new RequestLogImpl();
41  
42      @Before
43      public void startServer() throws Exception {
44          jettyFixture = new JettyFixtureBase(rli, RANDOM_SERVER_PORT);
45      }
46  
47      @After
48      public void stopServer() throws Exception {
49          if (jettyFixture != null) {
50              jettyFixture.stop();
51          }
52      }
53  
54      @Test
55      public void invokingDifferentPathShouldBeSiftedAccordingly() throws Exception {
56          rli.setFileName(PREFIX + "sifting.xml");
57          jettyFixture.start();
58          invokeServer("/");
59          invokeServer("/x");
60          invokeServer("/x");
61          invokeServer("/y");
62  
63          StatusPrinter.print(rli);
64          SiftingAppender siftingAppender = (SiftingAppender) rli.getAppender("SIFTING");
65          Set<String> keySet = siftingAppender.getAppenderTracker().allKeys();
66          assertEquals(3, keySet.size());
67  
68          Set<String> witnessSet = new LinkedHashSet<String>();
69          witnessSet.add("NA");
70          witnessSet.add("x");
71          witnessSet.add("y");
72          assertEquals(witnessSet, keySet);
73  
74          check(siftingAppender, "NA", 1);
75          check(siftingAppender, "x", 2);
76          check(siftingAppender, "y", 1);
77      }
78  
79      private void check(SiftingAppender siftingAppender, String key, int expectedCount) {
80          ListAppender<IAccessEvent> listAppender = (ListAppender<IAccessEvent>) siftingAppender.getAppenderTracker().find(key);
81          assertEquals(expectedCount, listAppender.list.size());
82      }
83  
84      void invokeServer(String uri) throws Exception {
85          URL url = new URL("http://localhost:" + RANDOM_SERVER_PORT + uri);
86          HttpURLConnection connection = (HttpURLConnection) url.openConnection();
87          connection.setDoInput(true);
88          Util.readToString(connection.getInputStream());
89          Thread.sleep(10);
90      }
91  }