001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 *
020 */
021 package org.apache.qpid.server.queue;
022
023 import org.apache.log4j.Logger;
024 import org.apache.qpid.AMQChannelClosedException;
025 import org.apache.qpid.AMQConnectionClosedException;
026 import org.apache.qpid.AMQException;
027 import org.apache.qpid.client.AMQConnection;
028 import org.apache.qpid.client.AMQSession;
029 import org.apache.qpid.url.URLSyntaxException;
030 import org.apache.qpid.util.CommandLineParser;
031
032 import javax.jms.JMSException;
033 import javax.jms.MessageProducer;
034 import javax.jms.Queue;
035 import javax.jms.Session;
036 import javax.jms.TextMessage;
037 import java.io.IOException;
038 import java.util.Properties;
039
040 public class PersistentTestManual
041 {
042 private static final Logger _logger = Logger.getLogger(PersistentTestManual.class);
043
044
045 private static final String QUEUE = "direct://amq.direct//PersistentTest-Queue2?durable='true',exclusive='true'";
046
047 protected AMQConnection _connection;
048
049 protected Session _session;
050
051 protected Queue _queue;
052 private Properties properties;
053
054 private String _brokerDetails;
055 private String _username;
056 private String _password;
057 private String _virtualpath;
058
059 public PersistentTestManual(Properties overrides)
060 {
061 properties = new Properties(defaults);
062 properties.putAll(overrides);
063
064 _brokerDetails = properties.getProperty(BROKER_PROPNAME);
065 _username = properties.getProperty(USERNAME_PROPNAME);
066 _password = properties.getProperty(PASSWORD_PROPNAME);
067 _virtualpath = properties.getProperty(VIRTUAL_HOST_PROPNAME);
068
069 createConnection();
070 }
071
072 protected void createConnection()
073 {
074 try
075 {
076 _connection = new AMQConnection(_brokerDetails, _username, _password, "PersistentTest", _virtualpath);
077
078 _session = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
079
080 _connection.start();
081 }
082 catch (Exception e)
083 {
084 _logger.error("Unable to create test class due to:" + e.getMessage(), e);
085 System.exit(0);
086 }
087 }
088
089 public void test() throws AMQException, URLSyntaxException
090 {
091
092 //Create the Durable Queue
093 try
094 {
095 _session.createConsumer(_session.createQueue(QUEUE)).close();
096 }
097 catch (JMSException e)
098 {
099 _logger.error("Unable to create Queue due to:" + e.getMessage(), e);
100 System.exit(0);
101 }
102
103 try
104 {
105 if (testQueue())
106 {
107 // close connection
108 _connection.close();
109 // wait
110 System.out.println("Restart Broker Now");
111 try
112 {
113 System.in.read();
114 }
115 catch (IOException e)
116 {
117 //
118 }
119 finally
120 {
121 System.out.println("Continuing....");
122 }
123
124 //Test queue is still there.
125 AMQConnection connection = new AMQConnection(_brokerDetails, _username, _password, "DifferentClientID", _virtualpath);
126
127 AMQSession session = (AMQSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
128
129 try
130 {
131 session.createConsumer(session.createQueue(QUEUE));
132 _logger.error("Create consumer succeeded." +
133 " This shouldn't be allowed as this means the queue didn't exist when it should");
134
135 connection.close();
136
137 exit();
138 }
139 catch (JMSException e)
140 {
141 try
142 {
143 connection.close();
144 }
145 catch (JMSException cce)
146 {
147 if (cce.getLinkedException() instanceof AMQConnectionClosedException)
148 {
149 _logger.error("Channel Close Bug still present QPID-432, should see an 'Error closing session'");
150 }
151 else
152 {
153 exit(cce);
154 }
155 }
156
157 if (e.getLinkedException() instanceof AMQChannelClosedException)
158 {
159 _logger.info("AMQChannelClosedException received as expected");
160 }
161 else
162 {
163 exit(e);
164 }
165 }
166 }
167 }
168 catch (JMSException e)
169 {
170 _logger.error("Unable to test Queue due to:" + e.getMessage(), e);
171 System.exit(0);
172 }
173 }
174
175 private void exit(JMSException e)
176 {
177 _logger.error("JMSException received:" + e.getMessage());
178 e.printStackTrace();
179 exit();
180 }
181
182 private void exit()
183 {
184 try
185 {
186 _connection.close();
187 }
188 catch (JMSException e)
189 {
190 //
191 }
192 System.exit(0);
193 }
194
195 private boolean testQueue() throws JMSException
196 {
197 String TEST_TEXT = "init";
198
199 //Create a new session to send producer
200 Session session = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
201
202 Queue q = session.createQueue(QUEUE);
203 MessageProducer producer = session.createProducer(q);
204
205 producer.send(session.createTextMessage(TEST_TEXT));
206
207 //create a new consumer on the original session
208 TextMessage m = (TextMessage) _session.createConsumer(q).receive();
209
210
211 if ((m != null) && m.getText().equals(TEST_TEXT))
212 {
213 return true;
214 }
215 else
216 {
217 _logger.error("Incorrect values returned from Queue Test:" + m);
218 System.exit(0);
219 return false;
220 }
221 }
222
223 /** Holds the name of the property to get the test broker url from. */
224 public static final String BROKER_PROPNAME = "broker";
225
226 /** Holds the default broker url for the test. */
227 public static final String BROKER_DEFAULT = "tcp://localhost:5672";
228
229 /** Holds the name of the property to get the test broker virtual path. */
230 public static final String VIRTUAL_HOST_PROPNAME = "virtualHost";
231
232 /** Holds the default virtual path for the test. */
233 public static final String VIRTUAL_HOST_DEFAULT = "";
234
235 /** Holds the name of the property to get the broker access username from. */
236 public static final String USERNAME_PROPNAME = "username";
237
238 /** Holds the default broker log on username. */
239 public static final String USERNAME_DEFAULT = "guest";
240
241 /** Holds the name of the property to get the broker access password from. */
242 public static final String PASSWORD_PROPNAME = "password";
243
244 /** Holds the default broker log on password. */
245 public static final String PASSWORD_DEFAULT = "guest";
246
247 /** Holds the default configuration properties. */
248 public static Properties defaults = new Properties();
249
250 static
251 {
252 defaults.setProperty(BROKER_PROPNAME, BROKER_DEFAULT);
253 defaults.setProperty(USERNAME_PROPNAME, USERNAME_DEFAULT);
254 defaults.setProperty(PASSWORD_PROPNAME, PASSWORD_DEFAULT);
255 defaults.setProperty(VIRTUAL_HOST_PROPNAME, VIRTUAL_HOST_DEFAULT);
256 }
257
258 public static void main(String[] args)
259 {
260 PersistentTestManual test;
261
262 Properties options =
263 CommandLineParser.processCommandLine(args, new CommandLineParser(new String[][]{}), System.getProperties());
264
265 test = new PersistentTestManual(options);
266 try
267 {
268 test.test();
269 System.out.println("Test was successfull.");
270 }
271 catch (Exception e)
272 {
273 _logger.error("Unable to test due to:" + e.getMessage(), e);
274 }
275 }
276 }
|