    001/**
    002 * Copyright (c) 2004-2011 QOS.ch
    003 * All rights reserved.
    004 *
    005 * Permission is hereby granted, free  of charge, to any person obtaining
    006 * a  copy  of this  software  and  associated  documentation files  (the
    007 * "Software"), to  deal in  the Software without  restriction, including
    008 * without limitation  the rights to  use, copy, modify,  merge, publish,
    009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
    010 * permit persons to whom the Software  is furnished to do so, subject to
    011 * the following conditions:
    012 *
    013 * The  above  copyright  notice  and  this permission  notice  shall  be
    014 * included in all copies or substantial portions of the Software.
    015 *
    016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
    017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
    018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
    019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
    022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    023 *
    024 */
    025package org.slf4j.dummyExt;
    026
    027import static org.junit.Assert.assertEquals;
    028
    029import java.util.Date;
    030import java.util.Locale;
    031import java.util.TimeZone;
    032
    033import org.apache.log4j.spi.LocationInfo;
    034import org.apache.log4j.spi.LoggingEvent;
    035import org.junit.After;
    036import org.junit.Before;
    037import org.junit.Test;
    038import org.slf4j.MDC;
    039import org.slf4j.ext.EventData;
    040import org.slf4j.ext.EventLogger;
    041
    042public class EventLoggerTest {
    043
    044    ListAppender listAppender;
    045    org.apache.log4j.Logger log4;
    046
    047    final static String EXPECTED_FILE_NAME = "EventLoggerTest.java";
    048
    049    @Before
    050    public void setUp() throws Exception {
    051
    052        // start from a clean slate for each test
    053
    054        listAppender = new ListAppender();
    055        listAppender.extractLocationInfo = true;
    056        org.apache.log4j.Logger eventLogger = org.apache.log4j.Logger.getLogger("EventLogger");
    057        eventLogger.addAppender(listAppender);
    058        eventLogger.setLevel(org.apache.log4j.Level.TRACE);
    059        eventLogger.setAdditivity(false);
    060        // Items that apply to any activity
    061        MDC.put("ipAddress", "192.168.1.110");
    062        MDC.put("login", "TestUSer");
    063        MDC.put("hostname", "localhost");
    064        MDC.put("productName", "SLF4J");
    065        MDC.put("locale", Locale.getDefault().getDisplayName());
    066        MDC.put("timezone", TimeZone.getDefault().getDisplayName());
    067
    068    }
    069
    070    @After
    071    public void tearDown() throws Exception {
    072        MDC.clear();
    073    }
    074
    075    void verify(LoggingEvent le, String expectedMsg) {
    076        assertEquals(expectedMsg, le.getMessage());
    077        assertEquals(EXPECTED_FILE_NAME, le.getLocationInformation().getFileName());
    078    }
    079
    080    @Test
    081    public void testEventLogger() {
    082        EventData data[] = new EventData[2];
    083        data[0] = new EventData();
    084        data[0].setEventType("Login");
    085        data[0].setEventId("1");
    086        data[0].setEventDateTime(new Date());
    087        data[0].put("Userid", "TestUser");
    088        EventLogger.logEvent(data[0]);
    089
    090        data[1] = new EventData();
    091        data[1].setEventType("Update");
    092        data[1].setEventId("2");
    093        data[1].setEventDateTime(new Date());
    094        data[1].put("FileName", "/etc/hosts");
    095        EventLogger.logEvent(data[1]);
    096
    097        assertEquals(2, listAppender.list.size());
    098        for (int i = 0; i < 2; ++i) {
    099            LoggingEvent event = listAppender.list.get(i);
    100            verify(event, data[i].toXML());
    101            LocationInfo li = event.getLocationInformation();
    102            assertEquals(this.getClass().getName(), li.getClassName());
    103            assertEquals(event.getMDC("hostname"), "localhost");
    104        }
    105    }
    106}