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.boolex;
015
016import ch.qos.logback.access.dummy.DummyRequest;
017import ch.qos.logback.access.dummy.DummyResponse;
018import ch.qos.logback.access.dummy.DummyServerAdapter;
019import ch.qos.logback.access.spi.AccessEvent;
020import ch.qos.logback.access.spi.IAccessEvent;
021import ch.qos.logback.core.Context;
022import ch.qos.logback.core.ContextBase;
023import ch.qos.logback.core.boolex.EvaluationException;
024import org.junit.Before;
025import org.junit.Test;
026
027import static org.junit.Assert.assertTrue;
028import static org.junit.Assert.fail;
029
030public class JaninoEventEvaluatorTest {
031
032    final String expectedURL1 = "testUrl1";
033    final String expectedURL2 = "testUrl2";
034    Context context = new ContextBase();
035    JaninoEventEvaluator evaluator;
036    DummyRequest request;
037    DummyResponse response;
038    DummyServerAdapter serverAdapter;
039
040    @Before
041    public void setUp() throws Exception {
042        evaluator = new JaninoEventEvaluator();
043        evaluator.setContext(context);
044        request = new DummyRequest();
045        response = new DummyResponse();
046        serverAdapter = new DummyServerAdapter(request, response);
047    }
048
049    @Test
050    public void smoke() throws EvaluationException {
051        evaluator.setExpression("event.getProtocol().equals(\"testProtocol\")");
052        evaluator.start();
053        IAccessEvent ae = new AccessEvent(request, response, serverAdapter);
054        assertTrue(evaluator.evaluate(ae));
055    }
056
057    @Test
058    public void block() throws EvaluationException {
059        evaluator.setExpression("String protocol = event.getProtocol();" + "return protocol.equals(\"testProtocol\");");
060        evaluator.start();
061        IAccessEvent ae = new AccessEvent(request, response, serverAdapter);
062        assertTrue(evaluator.evaluate(ae));
063    }
064
065    @Test
066    public void invalidExpression() throws EvaluationException {
067        evaluator.setExpression("return true");
068        evaluator.start();
069        IAccessEvent ae = new AccessEvent(request, response, serverAdapter);
070        try {
071            evaluator.evaluate(ae);
072            fail("Was expecting an exception");
073        } catch (IllegalStateException e) {
074        }
075    }
076}