    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.20 $
    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         persistenceAdapter.beginTransaction();
    134
    135         transactionTemplate = new TimedTransactionTemplate(persistenceAdapter, messageCount);
    136
    137         container = createContainer();
    138         assertTrue("Should have created a container: " + container != null);
    139         container.start();
    140     }
    141
    142     protected void tearDown() throws Exception {
    143         super.tearDown();
    144
    145         if (container != null) {
    146             container.stop();
    147         }
    148
    149         persistenceAdapter.commitTransaction();
    150         persistenceAdapter.stop();
    151     }
    152
    153     protected MessageContainer createContainer() throws JMSException {
    154         if (topic) {
    155             return persistenceAdapter.createTopicMessageContainer(getDestinationName());
    156         }
    157         else {
    158             return persistenceAdapter.createQueueMessageContainer(getDestinationName());
    159
    160         }
    161     }
    162
    163     protected PersistenceAdapter createPersistenceAdapter() throws Exception {
    164         assertTrue("Must overload this method to create a PersistenceAdapater", false);
    165         return null;
    166     }
    167
    168
    169     protected void writeMessage(int i) throws JMSException {
    170         ActiveMQMessage message = createMessage(i);
    171         String id = idGenerator.generateId();
    172         messageIds[i] = id;
    173         message.setJMSMessageID(id);
    174         messageIdenties[i] = container.addMessage(message);
    175     }
    176
    177     protected void readMessage(int i) throws JMSException {
    178         ActiveMQTextMessage message = (ActiveMQTextMessage) container.getMessage(messageIdenties[i]);
    179         assertTrue("Message should not be null", message != null);
    180
    181         String text = message.getText();
    182         assertEquals("Message text should be equal", messageTexts[i], text);
    183     }
    184
    185     protected MessageAck createMessageAck(MessageIdentity messageIdentity) {
    186         MessageAck answer = new MessageAck();
    187         answer.setConsumerId(idGenerator.generateId());
    188         answer.setMessageID(messageIdentity.getMessageID());
    189         answer.setMessageRead(true);
    190         return answer;
    191     }
    192
    193     protected void deleteMessage(int i) throws JMSException {
    194         container.delete(messageIdenties[i], createMessageAck(messageIdenties[i]));
    195     }
    196
    197     protected void assertNoMessage(int i) throws JMSException {
    198         ActiveMQMessage message = container.getMessage(messageIdenties[i]);
    199         assertTrue("Message should be null", message == null);
    200     }
    201
    202     protected ActiveMQMessage createMessage(int i) throws JMSException {
    203         ActiveMQTextMessage answer = new ActiveMQTextMessage();
    204         String text = "Message: " + i;
    205         messageTexts[i] = text;
    206         answer.setText(text);
    207         return answer;
    208     }
    209 }