/**
 * 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.bugs;

import java.net.URI;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import org.apache.activemq.JmsMultipleBrokersTestSupport;
import org.apache.activemq.broker.jmx.BrokerView;

public class AMQ2439Test extends JmsMultipleBrokersTestSupport {
    Destination dest;

    
    public void testDuplicatesThroughNetwork() throws Exception {
        assertEquals("received expected amount", 500, receiveExactMessages("BrokerB", 500));
        assertEquals("received expected amount", 500, receiveExactMessages("BrokerB", 500));
        validateQueueStats();
    }
    
    private void validateQueueStats() throws Exception {
       BrokerView brokerView = brokers.get("BrokerA").broker.getAdminView();
       assertEquals("enequeue is correct", 1000, brokerView.getTotalEnqueueCount());
       assertEquals("dequeue is correct", 1000, brokerView.getTotalDequeueCount());
    }

    protected int receiveExactMessages(String brokerName, int msgCount) throws Exception {
        
        BrokerItem brokerItem = brokers.get(brokerName);
        Connection connection = brokerItem.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);       
        MessageConsumer consumer = session.createConsumer(dest);
        
        Message msg;
        int i;
        for (i = 0; i < msgCount; i++) {
            msg = consumer.receive(1000);
            if (msg == null) {
                break;
            }
        }

        connection.close();
        brokerItem.connections.remove(connection);
        
        return i;
    }
    
    public void setUp() throws Exception {
        super.setUp();
        createBroker(new URI("broker:(tcp://localhost:61616)/BrokerA?persistent=true&deleteAllMessagesOnStartup=true&advisorySupport=false"));
        createBroker(new URI("broker:(tcp://localhost:61617)/BrokerB?persistent=true&deleteAllMessagesOnStartup=true&useJmx=false"));
        bridgeBrokers("BrokerA", "BrokerB");
        
        startAllBrokers();
        
        // Create queue
        dest = createDestination("TEST.FOO", false);
        sendMessages("BrokerA", dest, 1000);
    }   
}

@@ -1108,16 +1108,54 @@
 kerView;
+%0Aimport org.apache.activemq.util.Wait;
 %0A%0Apublic
@@ -1594,24 +1594,30 @@
 on %7B%0A       
+final 
 BrokerView b
@@ -1670,24 +1670,24 @@
 dminView();%0A
-
        asser
@@ -1755,24 +1755,32 @@
 ueCount());%0A
+       %0A
        asser
@@ -1780,22 +1780,20 @@
   assert
-Equals
+True
 (%22dequeu
@@ -1798,38 +1798,158 @@
 eue is correct%22,
+ Wait.waitFor(new Wait.Condition() %7B%0A           public boolean isSatisified() throws Exception %7B%0A               return
  1000
-,
+ ==
  brokerView.getT
@@ -1962,24 +1962,48 @@
 queueCount()
+;%0A           %7D%0A       %7D)
 );%0A    %7D%0A%0A  

fix race condition in test that validates dequeue count, thanks Al on irc