package net.kencochrane.raven.log4j;

import mockit.Expectations;
import mockit.Injectable;
import mockit.Mocked;
import mockit.Verifications;
import net.kencochrane.raven.Raven;
import net.kencochrane.raven.RavenFactory;
import net.kencochrane.raven.dsn.Dsn;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggingEvent;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

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

public class SentryAppenderDsnTest {
    private SentryAppender sentryAppender;
    private MockUpErrorHandler mockUpErrorHandler;
    @Injectable
    private Raven mockRaven = null;
    @Injectable
    private Logger mockLogger = null;
    @Mocked("ravenInstance")
    private RavenFactory mockRavenFactory;
    @Mocked("dsnLookup")
    private Dsn mockDsn;

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

    @Test
    public void testDsnDetected() throws Exception {
        final String dsnUri = "protocol://public:private@host/1";
        new Expectations() {{
            Dsn.dsnLookup();
            result = dsnUri;
            RavenFactory.ravenInstance(withEqual(new Dsn(dsnUri)), anyString);
            result = mockRaven;
        }};

        sentryAppender.activateOptions();
        sentryAppender.append(new LoggingEvent(null, mockLogger, 0, Level.ERROR, null, null));

        new Verifications() {{
            assertThat(mockUpErrorHandler.getErrorCount(), is(0));
        }};
    }

    @Test
    public void testDsnProvided() throws Exception {
        final String dsnUri = "protocol://public:private@host/2";
        sentryAppender.setDsn(dsnUri);
        new Expectations() {{
            RavenFactory.ravenInstance(withEqual(new Dsn(dsnUri)), anyString);
            result = mockRaven;
        }};

        sentryAppender.activateOptions();
        sentryAppender.append(new LoggingEvent(null, mockLogger, 0, Level.ERROR, null, null));

        new Verifications() {{
            assertThat(mockUpErrorHandler.getErrorCount(), is(0));
        }};
    }
}

@@ -111,37 +111,8 @@
 ed;%0A
-import mockit.Verifications;%0A
 impo
@@ -143,16 +143,16 @@
 .Raven;%0A
+
 import n
@@ -329,16 +329,59 @@
 gEvent;%0A
+import org.testng.annotations.AfterMethod;%0A
 import o
@@ -1147,32 +1147,165 @@
 ance());%0A    %7D%0A%0A
+    @AfterMethod%0A    public void tearDown() throws Exception %7B%0A        assertThat(mockUpErrorHandler.getErrorCount(), is(0));%0A    %7D%0A%0A
     @Test%0A    pu
@@ -1769,119 +1769,8 @@
 l));
-%0A%0A        new Verifications() %7B%7B%0A            assertThat(mockUpErrorHandler.getErrorCount(), is(0));%0A        %7D%7D;
 %0A   
@@ -2129,32 +2129,32 @@
 ivateOptions();%0A
+
         sentryAp
@@ -2235,119 +2235,8 @@
 l));
-%0A%0A        new Verifications() %7B%7B%0A            assertThat(mockUpErrorHandler.getErrorCount(), is(0));%0A        %7D%7D;
 %0A   

Add assert no error as tearDown