package com.vladmihalcea.flexypool.metric.codahale;

import com.vladmihalcea.flexypool.metric.MetricsFactory;
import com.vladmihalcea.flexypool.util.ClassLoaderUtils;
import org.junit.Test;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;

/**
 * CodahaleMetricsFactoryServiceTest - CodahaleMetricsFactoryService Test
 *
 * @author Vlad Mihalcea
 */
public class CodahaleMetricsFactoryServiceTest {

    @Test
    public void testLoadSuccess() {
        MetricsFactory metricsFactory = new CodahaleMetricsFactoryService().load();
        assertNotNull(metricsFactory);
        assertSame(CodahaleMetrics.FACTORY, metricsFactory);
    }

    @Test
    public void testLoadFailure() {
        ClassLoader currentClassLoader = ClassLoaderUtils.getClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(new ClassLoader() {
                @Override
                protected Class loadClass(String class_name, boolean resolve) throws ClassNotFoundException {
                    if(class_name.equals("com.codahale.metrics.Metric")) {
                        return null;
                    }
                    return super.loadClass(class_name, resolve);
                }
            });
            MetricsFactory metricsFactory = new CodahaleMetricsFactoryService().load();
            assertNull(metricsFactory);
        } finally {
            Thread.currentThread().setContextClassLoader(currentClassLoader);
        }
    }
}
@@ -46,16 +46,255 @@
 ahale;%0A%0A
+import com.vladmihalcea.flexypool.adaptor.PoolAdapter;%0Aimport com.vladmihalcea.flexypool.adaptor.PoolAdapterFactory;%0Aimport com.vladmihalcea.flexypool.common.ConfigurationProperties;%0Aimport com.vladmihalcea.flexypool.config.Configuration;%0A
 import c
@@ -426,54 +426,66 @@
 st;%0A
-%0A
 import 
-static org.junit.Assert.assertNotNull;
+org.mockito.Mockito;%0A%0Aimport javax.sql.DataSource;%0A
 %0Aimp
@@ -512,26 +512,17 @@
 .Assert.
-assertNull
+*
 ;%0Aimport
@@ -537,31 +537,68 @@
 org.
-junit.Assert.assertSame
+mockito.Matchers.any;%0Aimport static org.mockito.Mockito.when
 ;%0A%0A/
@@ -1816,10 +1816,880 @@
  %7D%0A    %7D
+%0A%0A    @Test%0A    public void testExistingCodahaleMetricsFactory() %7B%0A        DataSource dataSource = Mockito.mock(DataSource.class);%0A        PoolAdapterFactory%3CDataSource%3E poolAdapterFactory = Mockito.mock(PoolAdapterFactory.class);%0A%0A        PoolAdapter poolAdapter = Mockito.mock(PoolAdapter.class);%0A        when(poolAdapterFactory.newInstance(any(ConfigurationProperties.class))).thenReturn(poolAdapter);%0A%0A        ReservoirFactory reservoirFactory = Mockito.mock(ReservoirFactory.class);%0A%0A        Configuration%3CDataSource%3E configuration = new Configuration.Builder%3CDataSource%3E(%0A                %22unique%22, dataSource, poolAdapterFactory)%0A                .setMetricsFactory(new CodahaleMetrics.ReservoirMetricsFactory(reservoirFactory))%0A                .build();%0A%0A        assertEquals(CodahaleHistogram.class, configuration.getMetrics().histogram(%22test%22).getClass());%0A    %7D
 %0A%7D

Add test for custom RegistryFactory