/**
 * 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.qpid.jms.consumer;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.broker.jmx.QueueViewMBean;
import org.apache.qpid.jms.JmsConnection;
import org.apache.qpid.jms.support.AmqpTestSupport;
import org.apache.qpid.jms.support.Wait;
import org.junit.Ignore;
import org.junit.Test;

/**
 *
 */
public class JmsZeroPrefetchTest extends AmqpTestSupport {

    @Test(timeout=60000, expected=JMSException.class)
    public void testCannotUseMessageListener() throws Exception {
        connection = createAmqpConnection();
        ((JmsConnection)connection).getPrefetchPolicy().setAll(0);
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(name.getMethodName());
        MessageConsumer consumer = session.createConsumer(queue);

        MessageListener listener = new MessageListener() {

            @Override
            public void onMessage(Message message) {
            }
        };

        consumer.setMessageListener(listener);
    }

    @Test(timeout = 60000)
    public void testPullConsumerWorks() throws Exception {
        connection = createAmqpConnection();
        ((JmsConnection)connection).getPrefetchPolicy().setAll(0);
        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(name.getMethodName());
        MessageProducer producer = session.createProducer(queue);
        producer.send(session.createTextMessage("Hello World!"));

        // now lets receive it
        MessageConsumer consumer = session.createConsumer(queue);
        Message answer = consumer.receive(5000);
        assertNotNull("Should have received a message!", answer);
        // check if method will return at all and will return a null
        answer = consumer.receive(1);
        assertNull("Should have not received a message!", answer);
        answer = consumer.receiveNoWait();
        assertNull("Should have not received a message!", answer);
    }

    // TODO - Enable once broker side credit handling is fixed
    @Ignore // ActiveMQ doesn't honor link credit.
    @Test(timeout = 60000)
    public void testPullConsumerOnlyRequestsOneMessage() throws Exception {
        connection = createAmqpConnection();
        ((JmsConnection)connection).getPrefetchPolicy().setAll(0);
        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(name.getMethodName());
        MessageProducer producer = session.createProducer(queue);
        producer.send(session.createTextMessage("Hello World! 1"));
        producer.send(session.createTextMessage("Hello World! 2"));

        final QueueViewMBean queueView = getProxyToQueue(name.getMethodName());

        // Check initial Queue State
        assertEquals(2, queueView.getQueueSize());
        assertEquals(0, queueView.getInFlightCount());

        // now lets receive it
        MessageConsumer consumer = session.createConsumer(queue);
        Message answer = consumer.receive(5000);
        assertNotNull("Should have received a message!", answer);

        // Assert that we only pulled one message and that we didn't cause
        // the other message to be dispatched.
        assertTrue(Wait.waitFor(new Wait.Condition() {

            @Override
            public boolean isSatisified() throws Exception {
                return queueView.getQueueSize() == 1;
            }
        }));

        assertEquals(0, queueView.getInFlightCount());
    }

    // TODO - Enable once broker side credit handling is fixed
    @Ignore // ActiveMQ doesn't honor link credit.
    @Test(timeout = 60000)
    public void testTwoConsumers() throws Exception {
        connection = createAmqpConnection();
        ((JmsConnection)connection).getPrefetchPolicy().setAll(0);
        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(name.getMethodName());

        MessageProducer producer = session.createProducer(queue);
        producer.send(session.createTextMessage("Msg1"));
        producer.send(session.createTextMessage("Msg2"));

        // now lets receive it
        MessageConsumer consumer1 = session.createConsumer(queue);
        MessageConsumer consumer2 = session.createConsumer(queue);
        TextMessage answer = (TextMessage)consumer1.receive(5000);
        assertNotNull(answer);
        assertEquals("Should have received a message!", answer.getText(), "Msg1");
        answer = (TextMessage)consumer2.receive(5000);
        assertNotNull(answer);
        assertEquals("Should have received a message!", answer.getText(), "Msg2");

        answer = (TextMessage)consumer2.receiveNoWait();
        assertNull("Should have not received a message!", answer);
    }
}

@@ -1447,33 +1447,8 @@
 it;%0A
-import org.junit.Ignore;%0A
 impo
@@ -3247,122 +3247,8 @@
  %7D%0A%0A
-    // TODO - Enable once broker side credit handling is fixed%0A    @Ignore // ActiveMQ doesn't honor link credit.%0A
     
@@ -4687,122 +4687,8 @@
  %7D%0A%0A
-    // TODO - Enable once broker side credit handling is fixed%0A    @Ignore // ActiveMQ doesn't honor link credit.%0A
     

enable previously skipped tests, now work with 5.12