KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > util > JmsLogAppenderTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.util;
19
20 import junit.framework.TestCase;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Properties JavaDoc;
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 JavaDoc;
33 import javax.jms.Connection JavaDoc;
34 import javax.jms.MessageConsumer JavaDoc;
35 import javax.jms.Session JavaDoc;
36 import javax.jms.TextMessage JavaDoc;
37
38 public class JmsLogAppenderTest extends TestCase {
39     protected BrokerService broker;
40
41     public void testLoggingWithJMS() throws IOException JavaDoc, JMSException JavaDoc {
42         // Setup the consumers
43
MessageConsumer JavaDoc info, debug, warn;
44         ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost");
45         Connection JavaDoc 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         // lets try configure log4j
53
Properties JavaDoc properties = new Properties JavaDoc();
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 JavaDoc msg;
78
79         // Test warn level
80
msg = (TextMessage JavaDoc)warn.receive(1000);
81         assertNotNull(msg);
82         assertEquals("Warn Message", msg.getText());
83
84         msg = (TextMessage JavaDoc)warn.receive(1000);
85         assertNull(msg); // We should not receive anymore message because our level is warning only
86

87         // Test info level
88
msg = (TextMessage JavaDoc)info.receive(1000);
89         assertNotNull(msg);
90         assertEquals("Warn Message", msg.getText());
91
92         msg = (TextMessage JavaDoc)info.receive(1000);
93         assertNotNull(msg);
94         assertEquals("Info Message", msg.getText());
95
96         msg = (TextMessage JavaDoc)info.receive(1000);
97         assertNull(msg); // We should not receive the debug message
98

99         // Test debug level
100
msg = (TextMessage JavaDoc)debug.receive(1000);
101         assertNotNull(msg);
102         assertEquals("Warn Message", msg.getText());
103
104         msg = (TextMessage JavaDoc)debug.receive(1000);
105         assertNotNull(msg);
106         assertEquals("Info Message", msg.getText());
107
108         msg = (TextMessage JavaDoc)debug.receive(1000);
109         assertNotNull(msg);
110     }
111
112 }
113
Popular Tags