1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.backsource.alert.agent;
21 import java.util.Hashtable;
22 import java.util.ArrayList;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29
30 import javax.jms.*;
31
32 import org.backsource.alert.AlertError;
33 import org.backsource.alert.PropertyUtil;
34 import org.backsource.alert.PropertyConstants;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 import org.backsource.adaptor.jms.MessageDriven;
40
41 /***
42 * Test that makes a durable subscription against a topic/logging channel in amsterdam.
43 *
44 * @author <a href="mailto:pra@tim.se">Peter Antman</a>
45 * @version $Revision: 1.1.1.1 $
46 */
47
48 public class JmsLogTest extends BaseCase {
49 /*** java.naming.provider.url */
50 public static final String JDNI_URL = "java.naming.provider.url";
51 /*** jms.logging.selector */
52 public static final String SELECTOR = "jms.logging.selector";
53
54 public static final String FACTORYREF = "ConnectionFactory";
55
56 /*** The initial context factory to use. */
57 public static final String INITIAL_CONTEXT_FACTORY =
58 "org.jnp.interfaces.NamingContextFactory";
59
60 /**/package-summary/html">The url package prefixes/ *//package-summary.html">m>* The url package prefixes. */
61 public static final String URL_PKG_PREFIXES =
62 "org.jboss.naming";
63
64 static final Log log = LogFactory.getLog(JmsLogTest.class);
65 public JmsLogTest (String name){
66 super(name);
67 }
68 protected void setUp() throws Exception{
69 super.setUp();
70 String dirsP = prop.getProperty(PropertyConstants.DIR_DIRS);
71
72
73 }
74 /***
75 * Check if there are any logmessages that fits the selector that has
76 * arrived since last check: send em all as an error.
77 */
78 public void testLoggmessages() throws Exception {
79 LogFetcher fetcher = new LogFetcher();
80 fetcher.setDestinationFactory(FACTORYREF);
81 fetcher.setUsername("dynsub");
82 fetcher.setPassword("dynsub");
83 fetcher.setDestinationType("javax.jms.Topic");
84 fetcher.setDestinationJndiName("topic/logging");
85 fetcher.setClientId("MySub");
86 fetcher.setFailsafe(false);
87 fetcher.setDurable(true);
88 fetcher.setMessageSelector( prop.getProperty(SELECTOR));
89 fetcher.setSubscriptionId("MySubId");
90 fetcher.setPullSubscription(true);
91 fetcher.setPullTimeout(3000);
92
93 List messages = fetcher.getMessages();
94 if ( messages.size() > 0) {
95 StringBuffer buf = new StringBuffer();
96 Iterator ms = messages.iterator();
97 while (ms.hasNext()) {
98 TextMessage m = (TextMessage)ms.next();
99 buf.append("\nLOGRECORD:\n").append( m.getText() ).append("\n");
100 }
101 log.error("Found logrecords");
102 throw new AlertError("Found logrecords:\n"+buf.toString());
103 } else {
104 log.info("No logrecords found");
105 }
106
107
108 }
109
110 class LogFetcher extends MessageDriven {
111 ArrayList messages = new ArrayList();
112
113 public void onMessage(final Message message){
114 this.log.debug("Got message " + message);
115
116 }
117 public List getMessages() throws Exception {
118 startService();
119 stopService();
120 destroyService();
121 JmsLogTest.this.log.debug("Destroyed");
122 return messages;
123 }
124
125 protected void receive( MessageConsumer consumer) throws JMSException {
126
127
128 connection.start();
129 JmsLogTest.this.log.debug("Trying to receive");
130 Message msg = consumer.receive( getPullTimeout() );
131 JmsLogTest.this.log.debug("Got message "+msg);
132 while(msg != null){
133 JmsLogTest.this.log.debug("Got message "+msg);
134 messages.add(msg);
135 msg = consumer.receive( getPullTimeout() );
136 }
137 }
138
139 protected Context getContext() throws JMSException {
140 try {
141 Hashtable env = new Hashtable();
142 env.put(Context.INITIAL_CONTEXT_FACTORY,
143 INITIAL_CONTEXT_FACTORY);
144 env.put(Context.URL_PKG_PREFIXES, URL_PKG_PREFIXES);
145 env.put(JDNI_URL, prop.getProperty(JDNI_URL) );
146 return new InitialContext(env);
147 } catch ( NamingException e) {
148 throw new JMSException("Could get an initial context: " + e);
149 }
150 }
151 }
152 }