package net.kencochrane.raven;

import com.google.common.collect.Iterators;
import mockit.Injectable;
import mockit.Mocked;
import mockit.NonStrictExpectations;
import mockit.Tested;
import net.kencochrane.raven.dsn.Dsn;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.ServiceLoader;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class RavenFactoryTest {
    @Tested
    private RavenFactory ravenFactory;
    @Mocked
    private ServiceLoader mockServiceLoader;

    @BeforeMethod
    public void setUp() throws Exception {
        new NonStrictExpectations() {{
            ServiceLoader.load(RavenFactory.class);
            result = mockServiceLoader;
        }};
    }

    @Test
    public void testGetFactoriesFromServiceLoader(@Injectable final Raven mockRaven,
                                                  @Injectable final Dsn mockDsn) throws Exception {
        new NonStrictExpectations() {{
            mockServiceLoader.iterator();
            result = Iterators.singletonIterator(ravenFactory);
            ravenFactory.createRavenInstance(mockDsn);
            result = mockRaven;
        }};

        Raven raven = RavenFactory.ravenInstance(mockDsn);

        assertThat(raven, is(mockRaven));
    }

    @Test
    public void testGetFactoriesManuallyAdded(@Injectable final Raven mockRaven,
                                                  @Injectable final Dsn mockDsn) throws Exception {
        new NonStrictExpectations() {{
            RavenFactory.registerFactory(ravenFactory);
            ravenFactory.createRavenInstance(mockDsn);
            result = mockRaven;
        }};

        Raven raven = RavenFactory.ravenInstance(mockDsn);

        assertThat(raven, is(mockRaven));
    }
}

@@ -759,16 +759,107 @@
 Loader;%0A
+            mockServiceLoader.iterator();%0A            result = Iterators.emptyIterator();%0A%0A
         
@@ -862,16 +862,16 @@
     %7D%7D;%0A
-
     %7D%0A%0A 
@@ -1501,36 +1501,32 @@
 aven mockRaven,%0A
-    
                 
@@ -1597,32 +1597,32 @@
 ows Exception %7B%0A
+
         new NonS
@@ -1852,32 +1852,32 @@
 ance(mockDsn);%0A%0A
-
         assertTh
@@ -1907,11 +1907,1188 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testRavenInstanceForFactoryNameSucceedsIfFactoryFound(@Injectable final Raven mockRaven,%0A                                                                     @Injectable final Dsn mockDsn) throws Exception %7B%0A        String factoryName = ravenFactory.getClass().getName();%0A        new NonStrictExpectations() %7B%7B%0A            RavenFactory.registerFactory(ravenFactory);%0A            ravenFactory.createRavenInstance(mockDsn);%0A            result = mockRaven;%0A        %7D%7D;%0A%0A        Raven raven = RavenFactory.ravenInstance(mockDsn, factoryName);%0A%0A        assertThat(raven, is(mockRaven));%0A    %7D%0A%0A    @Test(expectedExceptions = IllegalStateException.class)%0A    public void testRavenInstanceForFactoryNameFailsIfNoFactoryFound(@Injectable final Raven mockRaven,%0A                                            @Injectable final Dsn mockDsn) throws Exception %7B%0A        String factoryName = %22invalidName%22;%0A        new NonStrictExpectations() %7B%7B%0A            RavenFactory.registerFactory(ravenFactory);%0A            ravenFactory.createRavenInstance(mockDsn);%0A            result = mockRaven;%0A        %7D%7D;%0A%0A        RavenFactory.ravenInstance(mockDsn, factoryName);%0A    %7D
 %0A%7D%0A

Check if the factory required name is used