PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/hornetq-2.2.13/HornetQ_2_2_13_AS7_Final_Pending/tests/src/org/hornetq/tests/integration/stomp/StompTestBase.java

#
Java | 293 lines | 221 code | 46 blank | 26 comment | 12 complexity | afea14e6f5bf3eb96cda80bbabd503db MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  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.hornetq.tests.integration.stomp;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.OutputStream;
  23. import java.net.Socket;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. import javax.jms.BytesMessage;
  27. import javax.jms.Connection;
  28. import javax.jms.ConnectionFactory;
  29. import javax.jms.Destination;
  30. import javax.jms.MessageProducer;
  31. import javax.jms.Queue;
  32. import javax.jms.Session;
  33. import javax.jms.TextMessage;
  34. import javax.jms.Topic;
  35. import junit.framework.Assert;
  36. import org.hornetq.api.core.TransportConfiguration;
  37. import org.hornetq.core.config.Configuration;
  38. import org.hornetq.core.config.impl.ConfigurationImpl;
  39. import org.hornetq.core.logging.Logger;
  40. import org.hornetq.core.remoting.impl.invm.InVMAcceptorFactory;
  41. import org.hornetq.core.remoting.impl.invm.InVMConnectorFactory;
  42. import org.hornetq.core.remoting.impl.netty.NettyAcceptorFactory;
  43. import org.hornetq.core.remoting.impl.netty.TransportConstants;
  44. import org.hornetq.core.server.HornetQServer;
  45. import org.hornetq.core.server.HornetQServers;
  46. import org.hornetq.jms.client.HornetQJMSConnectionFactory;
  47. import org.hornetq.jms.server.JMSServerManager;
  48. import org.hornetq.jms.server.config.JMSConfiguration;
  49. import org.hornetq.jms.server.config.impl.JMSConfigurationImpl;
  50. import org.hornetq.jms.server.config.impl.JMSQueueConfigurationImpl;
  51. import org.hornetq.jms.server.config.impl.TopicConfigurationImpl;
  52. import org.hornetq.jms.server.impl.JMSServerManagerImpl;
  53. import org.hornetq.spi.core.protocol.ProtocolType;
  54. import org.hornetq.tests.unit.util.InVMContext;
  55. import org.hornetq.tests.util.UnitTestCase;
  56. public abstract class StompTestBase extends UnitTestCase
  57. {
  58. private static final transient Logger log = Logger.getLogger(StompTestBase.class);
  59. private int port = 61613;
  60. private Socket stompSocket;
  61. private ByteArrayOutputStream inputBuffer;
  62. private ConnectionFactory connectionFactory;
  63. private Connection connection;
  64. protected Session session;
  65. protected Queue queue;
  66. protected Topic topic;
  67. protected JMSServerManager server;
  68. // Implementation methods
  69. // -------------------------------------------------------------------------
  70. protected void setUp() throws Exception
  71. {
  72. super.setUp();
  73. forceGC();
  74. server = createServer();
  75. server.start();
  76. connectionFactory = createConnectionFactory();
  77. stompSocket = createSocket();
  78. inputBuffer = new ByteArrayOutputStream();
  79. connection = connectionFactory.createConnection();
  80. session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  81. queue = session.createQueue(getQueueName());
  82. topic = session.createTopic(getTopicName());
  83. connection.start();
  84. }
  85. /**
  86. * @return
  87. * @throws Exception
  88. */
  89. protected JMSServerManager createServer() throws Exception
  90. {
  91. Configuration config = createBasicConfig();
  92. config.setSecurityEnabled(false);
  93. config.setPersistenceEnabled(false);
  94. Map<String, Object> params = new HashMap<String, Object>();
  95. params.put(TransportConstants.PROTOCOL_PROP_NAME, ProtocolType.STOMP.toString());
  96. params.put(TransportConstants.PORT_PROP_NAME, TransportConstants.DEFAULT_STOMP_PORT);
  97. params.put(TransportConstants.STOMP_CONSUMERS_CREDIT, "-1");
  98. TransportConfiguration stompTransport = new TransportConfiguration(NettyAcceptorFactory.class.getName(), params);
  99. config.getAcceptorConfigurations().add(stompTransport);
  100. config.getAcceptorConfigurations().add(new TransportConfiguration(InVMAcceptorFactory.class.getName()));
  101. HornetQServer hornetQServer = HornetQServers.newHornetQServer(config);
  102. JMSConfiguration jmsConfig = new JMSConfigurationImpl();
  103. jmsConfig.getQueueConfigurations()
  104. .add(new JMSQueueConfigurationImpl(getQueueName(), null, false, getQueueName()));
  105. jmsConfig.getTopicConfigurations().add(new TopicConfigurationImpl(getTopicName(), getTopicName()));
  106. server = new JMSServerManagerImpl(hornetQServer, jmsConfig);
  107. server.setContext(new InVMContext());
  108. return server;
  109. }
  110. protected void tearDown() throws Exception
  111. {
  112. connection.close();
  113. if (stompSocket != null)
  114. {
  115. stompSocket.close();
  116. }
  117. server.stop();
  118. super.tearDown();
  119. }
  120. protected void reconnect() throws Exception
  121. {
  122. reconnect(0);
  123. }
  124. protected void reconnect(long sleep) throws Exception
  125. {
  126. stompSocket.close();
  127. if (sleep > 0)
  128. {
  129. Thread.sleep(sleep);
  130. }
  131. stompSocket = createSocket();
  132. inputBuffer = new ByteArrayOutputStream();
  133. }
  134. protected ConnectionFactory createConnectionFactory()
  135. {
  136. return new HornetQJMSConnectionFactory(false, new TransportConfiguration(InVMConnectorFactory.class.getName()));
  137. }
  138. protected Socket createSocket() throws IOException
  139. {
  140. return new Socket("localhost", port);
  141. }
  142. protected String getQueueName()
  143. {
  144. return "test";
  145. }
  146. protected String getQueuePrefix()
  147. {
  148. return "jms.queue.";
  149. }
  150. protected String getTopicName()
  151. {
  152. return "testtopic";
  153. }
  154. protected String getTopicPrefix()
  155. {
  156. return "jms.topic.";
  157. }
  158. public void sendFrame(String data) throws Exception
  159. {
  160. byte[] bytes = data.getBytes("UTF-8");
  161. OutputStream outputStream = stompSocket.getOutputStream();
  162. for (int i = 0; i < bytes.length; i++)
  163. {
  164. outputStream.write(bytes[i]);
  165. }
  166. outputStream.flush();
  167. }
  168. public void sendFrame(byte[] data) throws Exception
  169. {
  170. OutputStream outputStream = stompSocket.getOutputStream();
  171. for (int i = 0; i < data.length; i++)
  172. {
  173. outputStream.write(data[i]);
  174. }
  175. outputStream.flush();
  176. }
  177. public String receiveFrame(long timeOut) throws Exception
  178. {
  179. stompSocket.setSoTimeout((int)timeOut);
  180. InputStream is = stompSocket.getInputStream();
  181. int c = 0;
  182. for (;;)
  183. {
  184. c = is.read();
  185. if (c < 0)
  186. {
  187. throw new IOException("socket closed.");
  188. }
  189. else if (c == 0)
  190. {
  191. c = is.read();
  192. if (c != '\n')
  193. {
  194. byte[] ba = inputBuffer.toByteArray();
  195. System.out.println(new String(ba, "UTF-8"));
  196. }
  197. Assert.assertEquals("Expecting stomp frame to terminate with \0\n", c, '\n');
  198. byte[] ba = inputBuffer.toByteArray();
  199. inputBuffer.reset();
  200. return new String(ba, "UTF-8");
  201. }
  202. else
  203. {
  204. inputBuffer.write(c);
  205. }
  206. }
  207. }
  208. public void sendMessage(String msg) throws Exception
  209. {
  210. sendMessage(msg, queue);
  211. }
  212. public void sendMessage(String msg, Destination destination) throws Exception
  213. {
  214. MessageProducer producer = session.createProducer(destination);
  215. TextMessage message = session.createTextMessage(msg);
  216. producer.send(message);
  217. }
  218. public void sendMessage(byte[] data, Destination destination) throws Exception
  219. {
  220. sendMessage(data, "foo", "xyz", destination);
  221. }
  222. public void sendMessage(String msg, String propertyName, String propertyValue) throws Exception
  223. {
  224. sendMessage(msg.getBytes("UTF-8"), propertyName, propertyValue, queue);
  225. }
  226. public void sendMessage(byte[] data, String propertyName, String propertyValue, Destination destination) throws Exception
  227. {
  228. MessageProducer producer = session.createProducer(destination);
  229. BytesMessage message = session.createBytesMessage();
  230. message.setStringProperty(propertyName, propertyValue);
  231. message.writeBytes(data);
  232. producer.send(message);
  233. }
  234. protected void waitForReceipt() throws Exception
  235. {
  236. String frame = receiveFrame(50000);
  237. assertNotNull(frame);
  238. assertTrue(frame.indexOf("RECEIPT") > -1);
  239. }
  240. protected void waitForFrameToTakeEffect() throws InterruptedException
  241. {
  242. // bit of a dirty hack :)
  243. // another option would be to force some kind of receipt to be returned
  244. // from the frame
  245. Thread.sleep(2000);
  246. }
  247. }