package com.codahale.metrics.httpclient;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.MetricRegistryListener;
import com.codahale.metrics.Timer;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Matchers.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class InstrumentedHttpClientTest {

    @Mock
    public HttpClientMetricNameStrategy metricNameStrategy;
    @Mock
    public MetricRegistryListener registryListener;

    private MetricRegistry metricRegistry;
    private HttpClient client;

    @Before
    public void init() {
        metricRegistry = new MetricRegistry();
        metricRegistry.addListener(registryListener);
        client = new InstrumentedHttpClient(metricRegistry, metricNameStrategy);
    }

    @Test
    public void registersExpectedMetricsGivenNameStrategy() throws Exception {
        HttpGet get = new HttpGet("http://google.com?q=anything");
        String metricName = "some.made.up.metric.name";

        when(metricNameStrategy.getNameFor(anyString(), eq(get))).thenReturn(metricName);

        try {
            client.execute(get);
        } catch (Exception e) {
        }

        verify(registryListener).onTimerAdded(eq(metricName), any(Timer.class));
    }
}

@@ -307,113 +307,48 @@
 st;%0A
+%0A
 import 
-org.junit.runner.RunWith;%0Aimport org.mockito.Mock;%0Aimport org.mockito.runners.MockitoJUnitRunner;%0A
+static org.mockito.Matchers.any;
 %0Aimp
@@ -383,9 +383,17 @@
 ers.
-*
+anyString
 ;%0Aim
@@ -421,21 +421,18 @@
 to.M
-ockito.verify
+atchers.eq
 ;%0Aim
@@ -467,49 +467,11 @@
 ito.
-when;%0A%0A@RunWith(MockitoJUnitRunner.class)
+*;%0A
 %0Apub
@@ -509,37 +509,33 @@
 tTest %7B%0A
-%0A    @Mock%0A    public
+    private final
  HttpCli
@@ -578,30 +578,82 @@
 tegy
-;
+ =
 %0A    
-@Mock%0A    public
+        mock(HttpClientMetricNameStrategy.class);%0A    private final
  Met
@@ -692,23 +692,28 @@
 ener
-;%0A
+ =
 %0A    
-private 
+        mock(
 Metr
@@ -726,101 +726,57 @@
 stry
- metricRegistry;%0A    private HttpClient client;%0A%0A    @Before%0A    public void init() %7B%0A       
+Listener.class);%0A    private final MetricRegistry
  met
@@ -819,57 +819,41 @@
     
-  
-  metricRegistry.addListener(registryListener);
+private final HttpClient client =
 %0A   
@@ -857,24 +857,19 @@
         
-client =
+   
  new Ins
@@ -924,16 +924,126 @@
 rategy);
+%0A%0A    @Before%0A    public void setUp() throws Exception %7B%0A        metricRegistry.addListener(registryListener);
 %0A    %7D%0A%0A
@@ -1139,16 +1139,22 @@
         
+final 
 HttpGet 
@@ -1183,12 +1183,13 @@
 p://
-goog
+examp
 le.c
@@ -1213,16 +1213,22 @@
         
+final 
 String m
@@ -1333,16 +1333,33 @@
 q(get)))
+%0A                
 .thenRet
@@ -1380,26 +1380,8 @@
 );%0A%0A
-        try %7B%0A    
     
@@ -1408,50 +1408,8 @@
 et);
-%0A        %7D catch (Exception e) %7B%0A        %7D
 %0A%0A  

Clean up InstrumentedHttpClientTest a bit.