001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.tck;
018
019 import java.util.ArrayList;
020 import java.util.List;
021
022 import javax.jbi.messaging.MessageExchange;
023 import javax.jbi.messaging.MessagingException;
024 import javax.jbi.messaging.NormalizedMessage;
025
026 import junit.framework.Assert;
027
028 import org.apache.commons.logging.Log;
029 import org.apache.commons.logging.LogFactory;
030 import org.apache.servicemix.client.Message;
031 import org.apache.servicemix.client.MessageListener;
032
033 /**
034 * A simple container for performing testing and rendezvous style code.
035 *
036 * @version $Revision: 564607 $
037 */
038 public class MessageList extends Assert implements MessageListener {
039
040 private static final Log LOG = LogFactory.getLog(MessageList.class);
041
042 private List messages = new ArrayList();
043
044 private Object semaphore;
045
046 public MessageList() {
047 this(new Object());
048 }
049
050 public MessageList(Object semaphore) {
051 this.semaphore = semaphore;
052 }
053
054 /**
055 * @return all the messages on the list so far, clearing the buffer
056 */
057 public List flushMessages() {
058 synchronized (semaphore) {
059 List answer = new ArrayList(messages);
060 messages.clear();
061 return answer;
062 }
063 }
064
065 public synchronized List getMessages() {
066 synchronized (semaphore) {
067 return new ArrayList(messages);
068 }
069 }
070
071 public void addMessage(NormalizedMessage message) throws MessagingException {
072 synchronized (semaphore) {
073 messages.add(message);
074 semaphore.notifyAll();
075 }
076 }
077
078 public void addMessage(String message) throws MessagingException {
079 synchronized (semaphore) {
080 messages.add(message);
081 semaphore.notifyAll();
082 }
083 }
084
085 public int getMessageCount() {
086 synchronized (semaphore) {
087 return messages.size();
088 }
089 }
090
091 public void waitForMessagesToArrive(int messageCount) {
092 waitForMessagesToArrive(messageCount, 4000);
093 }
094
095 public void waitForMessagesToArrive(int messageCount, long baseTimeout) {
096 LOG.info("Waiting for message to arrive");
097
098 long start = System.currentTimeMillis();
099
100 while (System.currentTimeMillis() - start < baseTimeout + 100 * messageCount) {
101 try {
102 if (hasReceivedMessages(messageCount)) {
103 break;
104 }
105 synchronized (semaphore) {
106 semaphore.wait(4000);
107 }
108 } catch (InterruptedException e) {
109 LOG.info("Caught: " + e);
110 }
111 }
112 long end = System.currentTimeMillis() - start;
113
114 LOG.info("End of wait for " + end + " millis");
115 }
116
117 /**
118 * Performs a testing assertion that the correct number of messages have
119 * been received
120 *
121 * @param messageCount
122 */
123 public void assertMessagesReceived(int messageCount) {
124 waitForMessagesToArrive(messageCount);
125
126 assertEquals("expected number of messages when received: " + getMessages(), messageCount, getMessageCount());
127 }
128
129 public boolean hasReceivedMessage() {
130 return getMessageCount() > 0;
131 }
132
133 public boolean hasReceivedMessages(int messageCount) {
134 return getMessageCount() >= messageCount;
135 }
136
137 // MessageListener interface
138 // -------------------------------------------------------------------------
139 public void onMessage(MessageExchange exchange, Message message) throws Exception {
140 addMessage(message);
141 }
142 }