package org.gem.log;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

import org.junit.Test;

import static org.junit.Assert.assertThat;

import static org.junit.matchers.JUnitMatchers.*;

import static org.hamcrest.CoreMatchers.*;

public class AMQPAppenderTest {
    private DummyChannel dummyChannel;
    private DummyAppender dummyAppender;
    private Logger logger = Logger.getLogger(AMQPAppenderTest.class);

    void tearDown() {
        dummyChannel = null;
        dummyAppender = null;

        // this closes the RabbitMQ connections
        BasicConfigurator.resetConfiguration();
    }

    private void setUpDummyAppender() {
        dummyAppender = new DummyAppender();
        dummyAppender.setLayout(new PatternLayout());

        BasicConfigurator.configure(dummyAppender);
    }

    @Test
    public void basicLogging() {
        setUpDummyAppender();

        logger.info("Test");

        dummyChannel = (DummyChannel) dummyAppender.getChannel();

        assertThat(dummyChannel.entries.size(), is(equalTo(1)));

        DummyChannel.Entry entry = dummyChannel.entries.get(0);

        assertThat(entry.exchange, is(equalTo("")));
        assertThat(entry.routingKey, is(equalTo("")));
        assertThat(entry.properties.getType(), is(equalTo("INFO")));
        assertThat(entry.body, is(equalTo("Test\n")));
    }
}

@@ -959,16 +959,47 @@
 fo(%22Test
+1%22);%0A        logger.warn(%22Test2
 %22);%0A%0A   
@@ -1121,17 +1121,17 @@
 equalTo(
-1
+2
 )));%0A%0A  
@@ -1160,16 +1160,17 @@
 ry entry
+1
  = dummy
@@ -1218,16 +1218,17 @@
 at(entry
+1
 .exchang
@@ -1272,16 +1272,17 @@
 at(entry
+1
 .routing
@@ -1328,16 +1328,17 @@
 at(entry
+1
 .propert
@@ -1398,16 +1398,17 @@
 at(entry
+1
 .body, i
@@ -1422,16 +1422,321 @@
 To(%22Test
+1%5Cn%22)));%0A%0A        DummyChannel.Entry entry2 = dummyChannel.entries.get(1);%0A%0A        assertThat(entry2.exchange, is(equalTo(%22%22)));%0A        assertThat(entry2.routingKey, is(equalTo(%22%22)));%0A        assertThat(entry2.properties.getType(), is(equalTo(%22WARN%22)));%0A        assertThat(entry2.body, is(equalTo(%22Test2
 %5Cn%22)));%0A

Test that .info() and .warn() set the correct event type.