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));
    }
}

@@ -1278,16 +1278,511 @@
 kDsn);%0A%0A
+        assertThat(raven, is(mockRaven));%0A    %7D%0A%0A    @Test%0A    public void testGetFactoriesManuallyAdded(@Injectable final Raven mockRaven,%0A                                                  @Injectable final Dsn mockDsn) throws Exception %7B%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);%0A%0A
         

Test that factories can be added manually