/**
 *
 * Copyright 2005-2006 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.activemq.util;

import java.util.ArrayList;
import java.util.List;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;

import junit.framework.Assert;

/**
 * A simple container of messages for performing testing and rendezvous style
 * code. You can use this class a {@link MessageListener} and then make
 * assertions about how many messages it has received allowing a certain maximum
 * amount of time to ensure that the test does not hang forever.
 * 
 * Also you can chain these instances together with the
 * {@link #setParent(MessageListener)} method so that you can aggregate the
 * total number of messages consumed across a number of consumers.
 * 
 * @version $Revision: 1.6 $
 */
public class MessageIdList extends Assert implements MessageListener {
    private List messageIds = new ArrayList();
    private Object semaphore;
    private boolean verbose;
    private MessageListener parent;
    private long maximumDuration = 15000L;

    public MessageIdList() {
        this(new Object());
    }

    public MessageIdList(Object semaphore) {
        this.semaphore = semaphore;
    }

    public boolean equals(Object that) {
        if (that instanceof MessageIdList) {
            MessageIdList thatList = (MessageIdList) that;
            return getMessageIds().equals(thatList.getMessageIds());
        }
        return false;
    }

    public int hashCode() {
        synchronized (semaphore) {
            return messageIds.hashCode() + 1;
        }
    }

    public String toString() {
        synchronized (semaphore) {
            return messageIds.toString();
        }
    }

    /**
     * @return all the messages on the list so far, clearing the buffer
     */
    public List flushMessages() {
        synchronized (semaphore) {
            List answer = new ArrayList(messageIds);
            messageIds.clear();
            return answer;
        }
    }

    public synchronized List getMessageIds() {
        synchronized (semaphore) {
            return new ArrayList(messageIds);
        }
    }

    public void onMessage(Message message) {
        if (parent != null) {
            parent.onMessage(message);
        }
        String id=null;
        try {
            id = message.getJMSMessageID();
            synchronized (semaphore) {
                messageIds.add(id);
                semaphore.notifyAll();
            }
            if (verbose) {
                System.out.println("###received message: " + message);
            }
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    public int getMessageCount() {
        synchronized (semaphore) {
            return messageIds.size();
        }
    }

    public void waitForMessagesToArrive(int messageCount) {
        System.out.println("Waiting for " + messageCount + " message(s) to arrive");

        long start = System.currentTimeMillis();

        for (int i = 0; i < messageCount; i++) {
            try {
                if (hasReceivedMessages(messageCount)) {
                    break;
                }
                long duration = System.currentTimeMillis() - start;
                if (duration > maximumDuration ) {
                    break;
                }
                synchronized (semaphore) {
                    semaphore.wait(4000);
                }
            }
            catch (InterruptedException e) {
                System.out.println("Caught: " + e);
            }
        }
        long end = System.currentTimeMillis() - start;

        System.out.println("End of wait for " + end + " millis and received: " + getMessageCount() + " messages");
    }

    /**
     * Performs a testing assertion that the correct number of messages have
     * been received without waiting
     * 
     * @param messageCount
     */
    public void assertMessagesReceivedNoWait(int messageCount) {
        assertEquals("expected number of messages when received", messageCount, getMessageCount());
    }
    
    /**
     * Performs a testing assertion that the correct number of messages have
     * been received waiting for the messages to arrive up to a fixed amount of time.
     * 
     * @param messageCount
     */
    public void assertMessagesReceived(int messageCount) {
        waitForMessagesToArrive(messageCount);

        assertMessagesReceivedNoWait(messageCount);
    }

    /**
     * Asserts that there are at least the given number of messages received without waiting.
     */
    public void assertAtLeastMessagesReceived(int messageCount) {
        int actual = getMessageCount();
        assertTrue("at least: " + messageCount + " messages received. Actual: " + actual, actual >= messageCount);
    }

    /**
     * Asserts that there are at most the number of messages received without waiting
     * @param messageCount
     */
    public void assertAtMostMessagesReceived(int messageCount) {
        int actual = getMessageCount();
        assertTrue("at most: " + messageCount + " messages received. Actual: " + actual, actual <= messageCount);
    }

    public boolean hasReceivedMessage() {
        return getMessageCount() == 0;
    }

    public boolean hasReceivedMessages(int messageCount) {
        return getMessageCount() >= messageCount;
    }

    public boolean isVerbose() {
        return verbose;
    }

    public void setVerbose(boolean verbose) {
        this.verbose = verbose;
    }

    public MessageListener getParent() {
        return parent;
    }

    /**
     * Allows a parent listener to be specified such as to aggregate messages
     * consumed across consumers
     */
    public void setParent(MessageListener parent) {
        this.parent = parent;
    }

}

@@ -3826,16 +3826,17 @@
 ration %3E
+=
  maximum
@@ -3975,12 +3975,32 @@
 ait(
-4000
+maximumDuration-duration
 );%0A 

use a tighter wait() based on the remaining time