package net.kencochrane.raven.log4j2;

import mockit.Expectations;
import mockit.Injectable;
import mockit.NonStrict;
import mockit.Verifications;
import net.kencochrane.raven.Raven;
import net.kencochrane.raven.event.Event;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.message.SimpleMessage;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

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

public class SentryAppenderFailuresTest {
    private SentryAppender sentryAppender;
    private MockUpErrorHandler mockUpErrorHandler;
    @Injectable
    @NonStrict
    private Raven mockRaven = null;

    @BeforeMethod
    public void setUp() throws Exception {
        sentryAppender = new SentryAppender(mockRaven);
        mockUpErrorHandler = new MockUpErrorHandler();
        sentryAppender.setHandler(mockUpErrorHandler.getMockInstance());
    }

    @Test
    public void testRavenFailureDoesNotPropagate() throws Exception {
        new Expectations() {{
            mockRaven.sendEvent((Event) any);
            result = new UnsupportedOperationException();
        }};

        sentryAppender.append(new Log4jLogEvent(null, null, null, Level.INFO, new SimpleMessage(""), null));

        assertThat(mockUpErrorHandler.getErrorCount(), is(1));
    }

    @Test
    public void testAppendFailIfCurrentThreadSpawnedByRaven() throws Exception {
        try {
            Raven.RAVEN_THREAD.set(true);

            sentryAppender.append(new Log4jLogEvent(null, null, null, Level.INFO, new SimpleMessage(""), null));

            new Verifications() {{
                mockRaven.sendEvent((Event) any);
                times = 0;
                assertThat(mockUpErrorHandler.getErrorCount(), is(0));
            }};
        } finally {
            Raven.RAVEN_THREAD.remove();
        }
    }
}

@@ -50,100 +50,88 @@
 kit.
-Expectations;%0Aimport mockit.Injectable;%0Aimport mockit.NonStrict;%0Aimport mockit.Verifications
+*;%0Aimport net.kencochrane.raven.Raven;%0Aimport net.kencochrane.raven.RavenFactory
 ;%0Aim
@@ -149,36 +149,38 @@
 ncochrane.raven.
-Rave
+dsn.Ds
 n;%0Aimport net.ke
@@ -736,16 +736,88 @@
  = null;
+%0A    @Mocked(%22ravenInstance%22)%0A    private RavenFactory mockRavenFactory;
 %0A%0A    @B
@@ -1461,32 +1461,591 @@
  is(1));%0A    %7D%0A%0A
+    @Test%0A    public void testRavenFactoryFailureDoesNotPropagate() throws Exception %7B%0A        new Expectations() %7B%7B%0A            RavenFactory.ravenInstance((Dsn) any, anyString);%0A            result = new UnsupportedOperationException();%0A        %7D%7D;%0A        SentryAppender sentryAppender = new SentryAppender();%0A        sentryAppender.setHandler(mockUpErrorHandler.getMockInstance());%0A        sentryAppender.setDsn(%22protocol://public:private@host/1%22);%0A%0A        sentryAppender.initRaven();%0A%0A        assertThat(mockUpErrorHandler.getErrorCount(), is(1));%0A    %7D%0A%0A
     @Test%0A    pu

Add test to check that RavenFactory exception do not propagate