MessageContainerTestSupport xref

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.service;
    19
    20  import junit.framework.TestCase;
    21  import org.codehaus.activemq.message.ActiveMQMessage;
    22  import org.codehaus.activemq.message.ActiveMQTextMessage;
    23  import org.codehaus.activemq.message.MessageAck;
    24  import org.codehaus.activemq.store.PersistenceAdapter;
    25  import org.codehaus.activemq.util.Callback;
    26  import org.codehaus.activemq.util.IdGenerator;
    27  import org.codehaus.activemq.util.TimedTransactionTemplate;
    28  import org.codehaus.activemq.util.TransactionTemplate;
    29
    30  import javax.jms.JMSException;
    31
    32  /***
    33   * @version $Revision: 1.21 $
    34   */
    35  public abstract class MessageContainerTestSupport extends TestCase {
    36      protected MessageContainer container;
    37      protected int messageCount = 50;
    38      protected IdGenerator idGenerator = new IdGenerator();
    39      protected String[] messageIds;
    40      protected MessageIdentity[] messageIdenties;
    41      private String[] messageTexts;
    42      protected PersistenceAdapter persistenceAdapter;
    43      protected boolean topic = false;
    44      private String destinationName;
    45      protected TransactionTemplate transactionTemplate;
    46
    47      public void testWriteLoop() throws Exception {
    48          transactionTemplate.run(new Callback() {
    49              public void execute() throws Throwable {
    50                  writeMessage(0);
    51
    52                  // lets delete using a String which should cause a warning
    53                  MessageIdentity messageIdentity = new MessageIdentity(messageIds[0]);
    54                  container.delete(messageIdentity, createMessageAck(messageIdentity));
    55              }
    56          });
    57
    58          System.out.println("About to write: " + messageCount + " messages to the container: " + container);
    59
    60          transactionTemplate.run(new Callback() {
    61              public void execute() throws Throwable {
    62                  for (int i = 0; i < messageCount; i++) {
    63                      writeMessage(i + 1);
    64                  }
    65              }
    66          });
    67
    68
    69          System.out.println("About to read: " + messageCount + " messages");
    70
    71          transactionTemplate.run(new Callback() {
    72              public void execute() throws Throwable {
    73                  for (int i = 0; i < messageCount; i++) {
    74                      readMessage(i + 1);
    75                  }
    76              }
    77          });
    78
    79          System.out.println("About to delete: " + messageCount + " messages");
    80
    81          transactionTemplate.run(new Callback() {
    82              public void execute() throws Throwable {
    83                  for (int i = 0; i < messageCount; i++) {
    84                      deleteMessage(i + 1);
    85                  }
    86              }
    87          });
    88
    89          if (!topic) {
    90              System.out.println("About to check that all the messages are consumed");
    91              transactionTemplate.run(new Callback() {
    92                  public void execute() throws Throwable {
    93                      for (int i = 0; i < messageCount; i++) {
    94                          assertNoMessage(i + 1);
    95                      }
    96                  }
    97              });
    98          }
    99      }
    100
    101     // Implementation methods
    102     //-------------------------------------------------------------------------
    103     public String getDestinationName() {
    104         if (destinationName == null) {
    105             destinationName = getClass().getName() + "." + getName();
    106         }
    107         return destinationName;
    108     }
    109
    110     public void setDestinationName(String destinationName) {
    111         this.destinationName = destinationName;
    112     }
    113
    114     protected long startUnitOfWork() throws Exception {
    115         return System.currentTimeMillis();
    116     }
    117
    118     protected long endUnitOfWork() throws Exception {
    119         return System.currentTimeMillis();
    120     }
    121
    122     protected void setUp() throws Exception {
    123         super.setUp();
    124         String value = System.getProperty("messageCount");
    125         if (value != null) {
    126             messageCount = Integer.parseInt(value);
    127         }
    128         messageIds = new String[messageCount + 1];
    129         messageTexts = new String[messageCount + 1];
    130         messageIdenties = new MessageIdentity[messageCount + 1];
    131         persistenceAdapter = createPersistenceAdapter();
    132         persistenceAdapter.start();
    133
    134         transactionTemplate = new TimedTransactionTemplate(persistenceAdapter, messageCount);
    135
    136         container = createContainer();
    137         assertTrue("Should have created a container: " + container != null);
    138         container.start();
    139     }
    140
    141     protected void tearDown() throws Exception {
    142         super.tearDown();
    143
    144         if (container != null) {
    145             container.stop();
    146         }
    147
    148         persistenceAdapter.stop();
    149     }
    150
    151     protected MessageContainer createContainer() throws JMSException {
    152         if (topic) {
    153             return persistenceAdapter.createTopicMessageContainer(getDestinationName());
    154         }
    155         else {
    156             return persistenceAdapter.createQueueMessageContainer(getDestinationName());
    157
    158         }
    159     }
    160
    161     protected PersistenceAdapter createPersistenceAdapter() throws Exception {
    162         assertTrue("Must overload this method to create a PersistenceAdapater", false);
    163         return null;
    164     }
    165
    166
    167     protected void writeMessage(int i) throws JMSException {
    168         ActiveMQMessage message = createMessage(i);
    169         String id = idGenerator.generateId();
    170         messageIds[i] = id;
    171         message.setJMSMessageID(id);
    172         messageIdenties[i] = container.addMessage(message);
    173     }
    174
    175     protected void readMessage(int i) throws JMSException {
    176         ActiveMQTextMessage message = (ActiveMQTextMessage) container.getMessage(messageIdenties[i]);
    177         assertTrue("Message should not be null", message != null);
    178
    179         String text = message.getText();
    180         assertEquals("Message text should be equal", messageTexts[i], text);
    181     }
    182
    183     protected MessageAck createMessageAck(MessageIdentity messageIdentity) {
    184         MessageAck answer = new MessageAck();
    185         answer.setConsumerId(idGenerator.generateId());
    186         answer.setMessageID(messageIdentity.getMessageID());
    187         answer.setMessageRead(true);
    188         return answer;
    189     }
    190
    191     protected void deleteMessage(int i) throws JMSException {
    192         container.delete(messageIdenties[i], createMessageAck(messageIdenties[i]));
    193     }
    194
    195     protected void assertNoMessage(int i) throws JMSException {
    196         ActiveMQMessage message = container.getMessage(messageIdenties[i]);
    197         assertTrue("Message should be null", message == null);
    198     }
    199
    200     protected ActiveMQMessage createMessage(int i) throws JMSException {
    201         ActiveMQTextMessage answer = new ActiveMQTextMessage();
    202         String text = "Message: " + i;
    203         messageTexts[i] = text;
    204         answer.setText(text);
    205         return answer;
    206     }
    207 }