/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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;

import javax.jms.Connection;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @version $Revision: 744947 $
 */
public class ZeroPrefetchConsumerTest extends EmbeddedBrokerTestSupport {

    private static final Log LOG = LogFactory.getLog(ZeroPrefetchConsumerTest.class);

    protected Connection connection;
    protected Queue queue;


    public void testIdleConsumer() throws Exception {
        doTestIdleConsumer(true);
    }

    private void doTestIdleConsumer(boolean transacted) throws Exception {
        Session session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);

        MessageProducer producer = session.createProducer(queue);
        producer.send(session.createTextMessage("Msg1"));
        producer.send(session.createTextMessage("Msg2"));
        producer.send(session.createTextMessage("Msg3"));
        producer.send(session.createTextMessage("Msg4"));
        producer.send(session.createTextMessage("Msg5"));
        producer.send(session.createTextMessage("Msg6"));
        producer.send(session.createTextMessage("Msg7"));
        producer.send(session.createTextMessage("Msg8"));
        producer.send(session.createTextMessage("Msg9"));
        if (transacted) {
            session.commit();
        }
        // now lets receive it
        MessageConsumer consumer = session.createConsumer(queue);
        
        MessageConsumer consumer2  =session.createConsumer(queue);
        TextMessage answer = (TextMessage)consumer.receive(5000);
        assertEquals("Should have received a message!", answer.getText(), "Msg1");
        if (transacted) {
            session.commit();
        }
         answer = (TextMessage)consumer.receive(5000);
        assertEquals("Should have received a message!", answer.getText(), "Msg2");
        if (transacted) {
            session.commit();
        }
         answer = (TextMessage)consumer.receive(5000);
        assertEquals("Should have received a message!", answer.getText(), "Msg3");
        if (transacted) {
            session.commit();
        }
        // this call would return null if prefetchSize > 0
        answer = (TextMessage)consumer.receive(5000);
        assertEquals("Should have received a message!", answer.getText(), "Msg4");
        if (transacted) {
            session.commit();
        }
        // Now using other consumer 
        // this call should return the next message (Msg5) still left on the queue
        answer = (TextMessage)consumer2.receive(5000);
        assertEquals("Should have received a message!", answer.getText(), "Msg5");
        if (transacted) {
            session.commit();
        }
        answer = (TextMessage)consumer.receiveNoWait();
        assertNull("Should have not received a message!", answer);
    }

    protected void setUp() throws Exception {
        bindAddress = "tcp://localhost:61616";
        super.setUp();

        connection = createConnection();
        connection.start();
        queue = createQueue();
    }

    protected void tearDown() throws Exception {
        connection.close();
        super.tearDown();
    }

    protected Queue createQueue() {
        return new ActiveMQQueue(getDestinationString() + "?consumer.prefetchSize=0");
    }

}
