package com.codahale.metrics.log4j2;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.SharedMetricRegistries;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.Configurator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class InstrumentedAppenderConfigTest {
  public static final String METRIC_NAME_PREFIX = "metrics";
  public static final String REGISTRY_NAME = "shared-metrics-registry";

  private final MetricRegistry registry = SharedMetricRegistries.getOrCreate(REGISTRY_NAME);
  private ConfigurationSource source;
  private LoggerContext context;

  @Before
  public void setUp() throws Exception {
    source = new ConfigurationSource(this.getClass().getClassLoader().getResourceAsStream("log4j2-testconfig.xml"));
    context = Configurator.initialize(null, source);
  }
  @After
  public void tearDown() throws Exception {
    context.stop();
  }

  // The biggest test is that we can initialize the log4j2 config at all.

  @Test
  public void canRecordAll() throws Exception {
    Logger logger = context.getLogger(this.getClass().getName());

    long initialAllCount = registry.meter(METRIC_NAME_PREFIX + ".all").getCount();
    logger.error("an error message");
    assertThat(registry.meter(METRIC_NAME_PREFIX + ".all").getCount())
            .isEqualTo(initialAllCount + 1);
  }

  @Test
  public void canRecordError() throws Exception {
    Logger logger = context.getLogger(this.getClass().getName());

    long initialErrorCount = registry.meter(METRIC_NAME_PREFIX + ".error").getCount();
    logger.error("an error message");
    assertThat(registry.meter(METRIC_NAME_PREFIX + ".all").getCount())
            .isEqualTo(initialErrorCount + 1);
  }

  @Test
  public void noInvalidRecording() throws Exception {
    Logger logger = context.getLogger(this.getClass().getName());

    long initialInfoCount = registry.meter(METRIC_NAME_PREFIX + ".info").getCount();
    logger.error("an error message");
    assertThat(registry.meter(METRIC_NAME_PREFIX + ".info").getCount())
            .isEqualTo(initialInfoCount);
  }

}

@@ -525,16 +525,18 @@
 gTest %7B%0A
+  
   public
@@ -588,16 +588,18 @@
 trics%22;%0A
+  
   public
@@ -663,16 +663,18 @@
 stry%22;%0A%0A
+  
   privat
@@ -758,16 +758,18 @@
 _NAME);%0A
+  
   privat
@@ -798,16 +798,18 @@
 source;%0A
+  
   privat
@@ -834,16 +834,18 @@
 ntext;%0A%0A
+  
   @Befor
@@ -846,16 +846,18 @@
 @Before%0A
+  
   public
@@ -893,16 +893,20 @@
 n %7B%0A    
+ 
+   
 source =
@@ -1010,16 +1010,20 @@
 xml%22));%0A
+    
     cont
@@ -1069,18 +1069,23 @@
 rce);%0A  
-%7D%0A
+  %7D%0A%0A  
   @After
@@ -1085,16 +1085,18 @@
  @After%0A
+  
   public
@@ -1127,24 +1127,28 @@
 Exception %7B%0A
+    
     context.
@@ -1155,21 +1155,25 @@
 stop();%0A
+  
   %7D%0A%0A
+  
   // The
@@ -1239,24 +1239,26 @@
 t all.%0A%0A
+  
   @Test%0A
   public
@@ -1241,32 +1241,34 @@
 all.%0A%0A    @Test%0A
+  
   public void ca
@@ -1295,32 +1295,36 @@
 Exception %7B%0A    
+   
+ 
 Logger logger = 
@@ -1362,32 +1362,36 @@
 ().getName());%0A%0A
+    
     long initial
@@ -1453,32 +1453,36 @@
 getCount();%0A    
+  
+  
 logger.error(%22an
@@ -1491,32 +1491,36 @@
 rror message%22);%0A
+    
     assertThat(r
@@ -1578,32 +1578,36 @@
 ())%0A            
+ 
+   
 .isEqualTo(initi
@@ -1615,37 +1615,41 @@
 lAllCount + 1);%0A
+  
   %7D%0A%0A
+  
   @Test%0A  public
@@ -1634,32 +1634,34 @@
  %7D%0A%0A    @Test%0A  
+ 
+ 
 public void canR
@@ -1684,32 +1684,36 @@
 ows Exception %7B%0A
+    
     Logger logge
@@ -1759,32 +1759,36 @@
 etName());%0A%0A    
+  
+  
 long initialErro
@@ -1846,32 +1846,36 @@
 r%22).getCount();%0A
+    
     logger.error
@@ -1892,32 +1892,36 @@
  message%22);%0A    
+ 
+   
 assertThat(regis
@@ -1963,32 +1963,36 @@
 l%22).getCount())%0A
+    
             .isE
@@ -2026,21 +2026,27 @@
 1);%0A
+  
   %7D%0A%0A  
+ 
+ 
 @Test%0A
+  
   pu
@@ -2095,24 +2095,28 @@
 ption %7B%0A    
+ 
+   
 Logger logge
@@ -2162,24 +2162,28 @@
 etName());%0A%0A
+    
     long ini
@@ -2251,24 +2251,28 @@
 getCount();%0A
+    
     logger.e
@@ -2293,24 +2293,28 @@
  message%22);%0A
+    
     assertTh
@@ -2377,32 +2377,36 @@
 ())%0A            
+    
 .isEqualTo(initi
@@ -2419,15 +2419,17 @@
 Count);%0A
+  
   %7D%0A%0A%7D%0A

Use correct indentation