/**
 * 
 * Copyright 2004 Protique Ltd
 * 
 * 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.activemq.test;

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

import javax.jms.Destination;
import javax.jms.JMSException;

import org.activemq.message.ActiveMQDestination;
import org.activemq.message.ActiveMQMessage;
import org.activemq.message.ActiveMQQueue;
import org.activemq.message.MessageAck;
import org.activemq.service.MessageIdentity;
import org.activemq.service.QueueMessageContainer;

/**
 * @version $Revision: 1.1.1.1 $
 */
public abstract class QueueMessageStoreTestSupport extends MessageStoreTestSupport {

    protected void acknowledgeMessage(int i) throws JMSException {
        MessageIdentity identity = getMessage(i).getJMSMessageIdentity();
        getQueueMessageContainer().delete(identity, createMessageAck(identity, getMessage(i).getJMSDestination()));
    }

    protected QueueMessageContainer getQueueMessageContainer() {
        return (QueueMessageContainer) container;
    }

    protected ActiveMQDestination createDestination() {
        return new ActiveMQQueue(getSubject());
    }

    protected void recover() throws JMSException {
    }

    protected MessageAck createMessageAck(MessageIdentity messageIdentity, Destination destination) {
        MessageAck answer = new MessageAck();
        answer.setConsumerId("James");
        answer.setMessageID(messageIdentity.getMessageID());
        answer.setMessageRead(true);
        answer.setDestination(this.destination);
        return answer;
    }

    protected ActiveMQMessage[] getMessagesToDispatch() throws JMSException {
        List list = new ArrayList();
        while (true) {
            ActiveMQMessage message = getQueueMessageContainer().poll();
            if (message == null) {
                break;
            }
            list.add(message);
        }
        ActiveMQMessage[] answer = new ActiveMQMessage[list.size()];
        list.toArray(answer);
        return answer;
    }
}
