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("Test1");
        logger.warn("Test2");

        dummyChannel = (DummyChannel) dummyAppender.getChannel();

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

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

        assertThat(entry1.exchange, is(equalTo("")));
        assertThat(entry1.routingKey, is(equalTo("")));
        assertThat(entry1.properties.getType(), is(equalTo("INFO")));
        assertThat(entry1.body, is(equalTo("Test1\n")));

        DummyChannel.Entry entry2 = dummyChannel.entries.get(1);

        assertThat(entry2.exchange, is(equalTo("")));
        assertThat(entry2.routingKey, is(equalTo("")));
        assertThat(entry2.properties.getType(), is(equalTo("WARN")));
        assertThat(entry2.body, is(equalTo("Test2\n")));
    }
}

@@ -1740,11 +1740,1180 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void routingKey() %7B%0A        setUpDummyAppender();%0A%0A        dummyAppender.setRoutingKeyPattern(%22rk%22);%0A%0A        logger.info(%22Test1%22);%0A        logger.warn(%22Test2%22);%0A%0A        dummyChannel = (DummyChannel) dummyAppender.getChannel();%0A%0A        assertThat(dummyChannel.entries.size(), is(equalTo(2)));%0A%0A        DummyChannel.Entry entry1 = dummyChannel.entries.get(0);%0A%0A        assertThat(entry1.routingKey, is(equalTo(%22rk%22)));%0A%0A        DummyChannel.Entry entry2 = dummyChannel.entries.get(1);%0A%0A        assertThat(entry2.routingKey, is(equalTo(%22rk%22)));%0A    %7D%0A%0A    @Test%0A    public void routingKeyPattern() %7B%0A        setUpDummyAppender();%0A%0A        dummyAppender.setRoutingKeyPattern(%22log.%25p%22);%0A%0A        logger.info(%22Test1%22);%0A        logger.warn(%22Test2%22);%0A%0A        dummyChannel = (DummyChannel) dummyAppender.getChannel();%0A%0A        assertThat(dummyChannel.entries.size(), is(equalTo(2)));%0A%0A        DummyChannel.Entry entry1 = dummyChannel.entries.get(0);%0A%0A        assertThat(entry1.routingKey, is(equalTo(%22log.INFO%22)));%0A%0A        DummyChannel.Entry entry2 = dummyChannel.entries.get(1);%0A%0A        assertThat(entry2.routingKey, is(equalTo(%22log.WARN%22)));%0A    %7D
 %0A%7D%0A

Test that the routing key pattern works as expected.