package com.codahale.metrics.log4j;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.SharedMetricRegistries;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.fest.assertions.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class InstrumentedAppenderTest {
    private final MetricRegistry registry = new MetricRegistry();
    private final InstrumentedAppender appender = new InstrumentedAppender(registry);
    private final LoggingEvent event = mock(LoggingEvent.class);

    @Before
    public void setUp() throws Exception {
        appender.activateOptions();

        when(event.getLevel()).thenReturn(Level.INFO);
    }

    @After
    public void tearDown() throws Exception {
        SharedMetricRegistries.clear();
    }

    @Test
    public void metersTraceEvents() throws Exception {
        when(event.getLevel()).thenReturn(Level.TRACE);
        appender.doAppend(event);

        assertThat(registry.meter("org.apache.log4j.Appender.all").getCount())
                .isEqualTo(1);

        assertThat(registry.meter("org.apache.log4j.Appender.trace").getCount())
                .isEqualTo(1);
    }

    @Test
    public void metersDebugEvents() throws Exception {
        when(event.getLevel()).thenReturn(Level.DEBUG);
        appender.doAppend(event);

        assertThat(registry.meter("org.apache.log4j.Appender.all").getCount())
                .isEqualTo(1);

        assertThat(registry.meter("org.apache.log4j.Appender.debug").getCount())
                .isEqualTo(1);
    }

    @Test
    public void metersInfoEvents() throws Exception {
        when(event.getLevel()).thenReturn(Level.INFO);
        appender.doAppend(event);

        assertThat(registry.meter("org.apache.log4j.Appender.all").getCount())
                .isEqualTo(1);

        assertThat(registry.meter("org.apache.log4j.Appender.info").getCount())
                .isEqualTo(1);
    }

    @Test
    public void metersWarnEvents() throws Exception {
        when(event.getLevel()).thenReturn(Level.WARN);
        appender.doAppend(event);

        assertThat(registry.meter("org.apache.log4j.Appender.all").getCount())
                .isEqualTo(1);

        assertThat(registry.meter("org.apache.log4j.Appender.warn").getCount())
                .isEqualTo(1);
    }

    @Test
    public void metersErrorEvents() throws Exception {
        when(event.getLevel()).thenReturn(Level.ERROR);
        appender.doAppend(event);

        assertThat(registry.meter("org.apache.log4j.Appender.all").getCount())
                .isEqualTo(1);

        assertThat(registry.meter("org.apache.log4j.Appender.error").getCount())
                .isEqualTo(1);
    }

    @Test
    public void metersFatalEvents() throws Exception {
        when(event.getLevel()).thenReturn(Level.FATAL);
        appender.doAppend(event);

        assertThat(registry.meter("org.apache.log4j.Appender.all").getCount())
                .isEqualTo(1);

        assertThat(registry.meter("org.apache.log4j.Appender.fatal").getCount())
                .isEqualTo(1);
    }

    @Test
    public void usesSharedRegistries() throws Exception {
        SharedMetricRegistries.add("reg", registry);
        final InstrumentedAppender shared = new InstrumentedAppender("reg");
        shared.activateOptions();
        shared.doAppend(event);

        assertThat(registry.meter("org.apache.log4j.Appender.info").getCount())
                .isEqualTo(1);
    }
}

@@ -454,16 +454,99 @@
 rTest %7B%0A
+%0A    public static final String METRIC_NAME_PREFIX = %22org.apache.log4j.Appender%22;%0A%0A
     priv
@@ -849,64 +849,8 @@
 s();
-%0A%0A        when(event.getLevel()).thenReturn(Level.INFO);
 %0A   
@@ -1078,16 +1078,17 @@
 TRACE);%0A
+%0A
         
@@ -1152,34 +1152,30 @@
 ter(
-%22org.apache.log4j.Appender
+METRIC_NAME_PREFIX + %22
 .all
@@ -1259,34 +1259,30 @@
 ter(
-%22org.apache.log4j.Appender
+METRIC_NAME_PREFIX + %22
 .tra
@@ -1457,16 +1457,17 @@
 DEBUG);%0A
+%0A
         
@@ -1531,34 +1531,30 @@
 ter(
-%22org.apache.log4j.Appender
+METRIC_NAME_PREFIX + %22
 .all
@@ -1638,34 +1638,30 @@
 ter(
-%22org.apache.log4j.Appender
+METRIC_NAME_PREFIX + %22
 .deb
@@ -1834,16 +1834,17 @@
 .INFO);%0A
+%0A
         
@@ -1908,34 +1908,30 @@
 ter(
-%22org.apache.log4j.Appender
+METRIC_NAME_PREFIX + %22
 .all
@@ -2015,34 +2015,30 @@
 ter(
-%22org.apache.log4j.Appender
+METRIC_NAME_PREFIX + %22
 .inf
@@ -2210,16 +2210,17 @@
 .WARN);%0A
+%0A
         
@@ -2284,34 +2284,30 @@
 ter(
-%22org.apache.log4j.Appender
+METRIC_NAME_PREFIX + %22
 .all
@@ -2391,34 +2391,30 @@
 ter(
-%22org.apache.log4j.Appender
+METRIC_NAME_PREFIX + %22
 .war
@@ -2588,16 +2588,17 @@
 ERROR);%0A
+%0A
         
@@ -2662,34 +2662,30 @@
 ter(
-%22org.apache.log4j.Appender
+METRIC_NAME_PREFIX + %22
 .all
@@ -2769,34 +2769,30 @@
 ter(
-%22org.apache.log4j.Appender
+METRIC_NAME_PREFIX + %22
 .err
@@ -2967,16 +2967,17 @@
 FATAL);%0A
+%0A
         
@@ -3041,34 +3041,30 @@
 ter(
-%22org.apache.log4j.Appender
+METRIC_NAME_PREFIX + %22
 .all
@@ -3148,34 +3148,30 @@
 ter(
-%22org.apache.log4j.Appender
+METRIC_NAME_PREFIX + %22
 .fat
@@ -3285,32 +3285,75 @@
 ows Exception %7B%0A
+        String registryName = %22registry%22;%0A%0A
         SharedMe
@@ -3371,21 +3371,28 @@
 ies.add(
-%22
 reg
-%22
+istryName
 , regist
@@ -3392,24 +3392,25 @@
  registry);%0A
+%0A
         fina
@@ -3470,13 +3470,20 @@
 der(
-%22
 reg
-%22
+istryName
 );%0A 
@@ -3511,24 +3511,81 @@
 eOptions();%0A
+%0A        when(event.getLevel()).thenReturn(Level.INFO);%0A%0A
         shar
@@ -3643,34 +3643,30 @@
 ter(
-%22org.apache.log4j.Appender
+METRIC_NAME_PREFIX + %22
 .inf

refactor log4j 1.x tests