1 18 package org.apache.activemq.util; 19 20 import junit.framework.TestCase; 21 22 import java.io.IOException ; 23 import java.util.Properties ; 24 25 import org.apache.log4j.Logger; 26 import org.apache.log4j.PropertyConfigurator; 27 import org.apache.log4j.Level; 28 import org.apache.activemq.ActiveMQConnectionFactory; 29 import org.apache.activemq.broker.BrokerService; 30 import org.apache.activemq.command.ActiveMQTopic; 31 32 import javax.jms.JMSException ; 33 import javax.jms.Connection ; 34 import javax.jms.MessageConsumer ; 35 import javax.jms.Session ; 36 import javax.jms.TextMessage ; 37 38 public class JmsLogAppenderTest extends TestCase { 39 protected BrokerService broker; 40 41 public void testLoggingWithJMS() throws IOException , JMSException { 42 MessageConsumer info, debug, warn; 44 ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost"); 45 Connection conn = factory.createConnection(); 46 conn.start(); 47 48 warn = conn.createSession(false, Session.AUTO_ACKNOWLEDGE).createConsumer(new ActiveMQTopic("log4j.MAIN.WARN")); 49 info = conn.createSession(false, Session.AUTO_ACKNOWLEDGE).createConsumer(new ActiveMQTopic("log4j.MAIN.INFO")); 50 debug = conn.createSession(false, Session.AUTO_ACKNOWLEDGE).createConsumer(new ActiveMQTopic("log4j.MAIN.DEBUG")); 51 52 Properties properties = new Properties (); 54 properties.load(getClass().getResourceAsStream("test-log4j.properties")); 55 PropertyConfigurator.configure(properties); 56 57 Logger warnLog, infoLog, debugLog; 58 59 warnLog = Logger.getLogger("MAIN.WARN"); 60 warnLog.setLevel(Level.WARN); 61 warnLog.warn("Warn Message"); 62 warnLog.info("Info Message"); 63 warnLog.debug("Debug Message"); 64 65 infoLog = Logger.getLogger("MAIN.INFO"); 66 infoLog.setLevel(Level.INFO); 67 infoLog.warn("Warn Message"); 68 infoLog.info("Info Message"); 69 infoLog.debug("Debug Message"); 70 71 debugLog = Logger.getLogger("MAIN.DEBUG"); 72 debugLog.setLevel(Level.DEBUG); 73 debugLog.warn("Warn Message"); 74 debugLog.info("Info Message"); 75 debugLog.debug("Debug Message"); 76 77 TextMessage msg; 78 79 msg = (TextMessage )warn.receive(1000); 81 assertNotNull(msg); 82 assertEquals("Warn Message", msg.getText()); 83 84 msg = (TextMessage )warn.receive(1000); 85 assertNull(msg); 87 msg = (TextMessage )info.receive(1000); 89 assertNotNull(msg); 90 assertEquals("Warn Message", msg.getText()); 91 92 msg = (TextMessage )info.receive(1000); 93 assertNotNull(msg); 94 assertEquals("Info Message", msg.getText()); 95 96 msg = (TextMessage )info.receive(1000); 97 assertNull(msg); 99 msg = (TextMessage )debug.receive(1000); 101 assertNotNull(msg); 102 assertEquals("Warn Message", msg.getText()); 103 104 msg = (TextMessage )debug.receive(1000); 105 assertNotNull(msg); 106 assertEquals("Info Message", msg.getText()); 107 108 msg = (TextMessage )debug.receive(1000); 109 assertNotNull(msg); 110 } 111 112 } 113 | Popular Tags |