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.net;
015
016import static org.junit.Assert.*;
017
018import ch.qos.logback.access.spi.IAccessEvent;
019import org.junit.After;
020import org.junit.Before;
021import org.junit.Test;
022
023import ch.qos.logback.access.dummy.DummyRequest;
024import ch.qos.logback.access.dummy.DummyResponse;
025import ch.qos.logback.access.dummy.DummyServerAdapter;
026import ch.qos.logback.access.spi.AccessEvent;
027import ch.qos.logback.core.Context;
028import ch.qos.logback.core.ContextBase;
029import ch.qos.logback.core.boolex.EvaluationException;
030
031public class URLEvaluatorTest {
032
033    final String expectedURL1 = "testUrl1";
034    final String expectedURL2 = "testUrl2";
035    Context context = new ContextBase();
036    URLEvaluator evaluator;
037    DummyRequest request;
038    DummyResponse response;
039    DummyServerAdapter serverAdapter;
040
041    @Before
042    public void setUp() throws Exception {
043        evaluator = new URLEvaluator();
044        evaluator.setContext(context);
045        evaluator.addURL(expectedURL1);
046        evaluator.start();
047        request = new DummyRequest();
048        response = new DummyResponse();
049        serverAdapter = new DummyServerAdapter(request, response);
050    }
051
052    @After
053    public void tearDown() throws Exception {
054        evaluator.stop();
055        evaluator = null;
056        request = null;
057        response = null;
058        serverAdapter = null;
059        context = null;
060    }
061
062    @Test
063    public void testExpectFalse() throws EvaluationException {
064        request.setRequestUri("test");
065        IAccessEvent ae = new AccessEvent(request, response, serverAdapter);
066        assertFalse(evaluator.evaluate(ae));
067    }
068
069    @Test
070    public void testExpectTrue() throws EvaluationException {
071        request.setRequestUri(expectedURL1);
072        IAccessEvent ae = new AccessEvent(request, response, serverAdapter);
073        assertTrue(evaluator.evaluate(ae));
074    }
075
076    @Test
077    public void testExpectTrueMultiple() throws EvaluationException {
078        evaluator.addURL(expectedURL2);
079        request.setRequestUri(expectedURL2);
080        IAccessEvent ae = new AccessEvent(request, response, serverAdapter);
081        assertTrue(evaluator.evaluate(ae));
082    }
083}