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.net;
15  
16  import static org.junit.Assert.*;
17  
18  import ch.qos.logback.access.spi.IAccessEvent;
19  import org.junit.After;
20  import org.junit.Before;
21  import org.junit.Test;
22  
23  import ch.qos.logback.access.dummy.DummyRequest;
24  import ch.qos.logback.access.dummy.DummyResponse;
25  import ch.qos.logback.access.dummy.DummyServerAdapter;
26  import ch.qos.logback.access.spi.AccessEvent;
27  import ch.qos.logback.core.Context;
28  import ch.qos.logback.core.ContextBase;
29  import ch.qos.logback.core.boolex.EvaluationException;
30  
31  public class URLEvaluatorTest {
32  
33      final String expectedURL1 = "testUrl1";
34      final String expectedURL2 = "testUrl2";
35      Context context = new ContextBase();
36      URLEvaluator evaluator;
37      DummyRequest request;
38      DummyResponse response;
39      DummyServerAdapter serverAdapter;
40  
41      @Before
42      public void setUp() throws Exception {
43          evaluator = new URLEvaluator();
44          evaluator.setContext(context);
45          evaluator.addURL(expectedURL1);
46          evaluator.start();
47          request = new DummyRequest();
48          response = new DummyResponse();
49          serverAdapter = new DummyServerAdapter(request, response);
50      }
51  
52      @After
53      public void tearDown() throws Exception {
54          evaluator.stop();
55          evaluator = null;
56          request = null;
57          response = null;
58          serverAdapter = null;
59          context = null;
60      }
61  
62      @Test
63      public void testExpectFalse() throws EvaluationException {
64          request.setRequestUri("test");
65          IAccessEvent ae = new AccessEvent(request, response, serverAdapter);
66          assertFalse(evaluator.evaluate(ae));
67      }
68  
69      @Test
70      public void testExpectTrue() throws EvaluationException {
71          request.setRequestUri(expectedURL1);
72          IAccessEvent ae = new AccessEvent(request, response, serverAdapter);
73          assertTrue(evaluator.evaluate(ae));
74      }
75  
76      @Test
77      public void testExpectTrueMultiple() throws EvaluationException {
78          evaluator.addURL(expectedURL2);
79          request.setRequestUri(expectedURL2);
80          IAccessEvent ae = new AccessEvent(request, response, serverAdapter);
81          assertTrue(evaluator.evaluate(ae));
82      }
83  }