View Javadoc

    1   /**
    2    * Copyright (c) 2004-2011 QOS.ch
    3    * All rights reserved.
    4    *
    5    * Permission is hereby granted, free  of charge, to any person obtaining
    6    * a  copy  of this  software  and  associated  documentation files  (the
    7    * "Software"), to  deal in  the Software without  restriction, including
    8    * without limitation  the rights to  use, copy, modify,  merge, publish,
    9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
    10   * permit persons to whom the Software  is furnished to do so, subject to
    11   * the following conditions:
    12   *
    13   * The  above  copyright  notice  and  this permission  notice  shall  be
    14   * included in all copies or substantial portions of the Software.
    15   *
    16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
    17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
    18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
    19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
    22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    23   *
    24   */
    25  package org.slf4j.dummyExt;
    26
    27  import static org.junit.Assert.assertEquals;
    28
    29  import java.util.Date;
    30  import java.util.Locale;
    31  import java.util.TimeZone;
    32
    33  import org.apache.log4j.spi.LocationInfo;
    34  import org.apache.log4j.spi.LoggingEvent;
    35  import org.junit.After;
    36  import org.junit.Before;
    37  import org.junit.Test;
    38  import org.slf4j.MDC;
    39  import org.slf4j.ext.EventData;
    40  import org.slf4j.ext.EventLogger;
    41
    42  public class EventLoggerTest {
    43
    44      ListAppender listAppender;
    45      org.apache.log4j.Logger log4;
    46
    47      final static String EXPECTED_FILE_NAME = "EventLoggerTest.java";
    48
    49      @Before
    50      public void setUp() throws Exception {
    51
    52          // start from a clean slate for each test
    53
    54          listAppender = new ListAppender();
    55          listAppender.extractLocationInfo = true;
    56          org.apache.log4j.Logger eventLogger = org.apache.log4j.Logger.getLogger("EventLogger");
    57          eventLogger.addAppender(listAppender);
    58          eventLogger.setLevel(org.apache.log4j.Level.TRACE);
    59          eventLogger.setAdditivity(false);
    60          // Items that apply to any activity
    61          MDC.put("ipAddress", "192.168.1.110");
    62          MDC.put("login", "TestUSer");
    63          MDC.put("hostname", "localhost");
    64          MDC.put("productName", "SLF4J");
    65          MDC.put("locale", Locale.getDefault().getDisplayName());
    66          MDC.put("timezone", TimeZone.getDefault().getDisplayName());
    67
    68      }
    69
    70      @After
    71      public void tearDown() throws Exception {
    72          MDC.clear();
    73      }
    74
    75      void verify(LoggingEvent le, String expectedMsg) {
    76          assertEquals(expectedMsg, le.getMessage());
    77          assertEquals(EXPECTED_FILE_NAME, le.getLocationInformation().getFileName());
    78      }
    79
    80      @Test
    81      public void testEventLogger() {
    82          EventData data[] = new EventData[2];
    83          data[0] = new EventData();
    84          data[0].setEventType("Login");
    85          data[0].setEventId("1");
    86          data[0].setEventDateTime(new Date());
    87          data[0].put("Userid", "TestUser");
    88          EventLogger.logEvent(data[0]);
    89
    90          data[1] = new EventData();
    91          data[1].setEventType("Update");
    92          data[1].setEventId("2");
    93          data[1].setEventDateTime(new Date());
    94          data[1].put("FileName", "/etc/hosts");
    95          EventLogger.logEvent(data[1]);
    96
    97          assertEquals(2, listAppender.list.size());
    98          for (int i = 0; i < 2; ++i) {
    99              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 }