/*
 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
 *
 * 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 com.hazelcast.queue;

import com.hazelcast.config.Config;
import com.hazelcast.config.ItemListenerConfig;
import com.hazelcast.config.QueueConfig;
import com.hazelcast.core.*;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static junit.framework.Assert.assertTrue;


@RunWith(HazelcastParallelClassRunner.class)
@Category(QuickTest.class)
public class QueueListenerTest extends HazelcastTestSupport {

    @Test
    public void testItemListener_addedToQueueConfig_Issue366() throws InterruptedException {

        int count=2000;

        ItemListenerConfig itemListenerConfig = new ItemListenerConfig();
        itemListenerConfig.setImplementation(new SimpleItemListener(count));
        itemListenerConfig.setIncludeValue(true);

        QueueConfig qConfig = new QueueConfig();
        qConfig.setName("Q");
        qConfig.addItemListenerConfig(itemListenerConfig);

        Config cfg = new Config();
        cfg.addQueueConfig(qConfig);

        HazelcastInstance node1 =  Hazelcast.newHazelcastInstance(cfg);

        for(int i=0; i<count/2; i++) {
            node1.getQueue("Q").put(i);
        }

        HazelcastInstance node2 =  Hazelcast.newHazelcastInstance(cfg);

        for(int i=0; i<count/4; i++) {
            node1.getQueue("Q").put(i);
        }

        List<ItemListenerConfig> configs = qConfig.getItemListenerConfigs();
        SimpleItemListener simpleItemListener = (SimpleItemListener) configs.get(0).getImplementation();

        assertTrue(simpleItemListener.added.await(10, TimeUnit.SECONDS));
    }


    private static class SimpleItemListener implements ItemListener {

        public CountDownLatch added;

        public SimpleItemListener(int CountDown){
            added = new CountDownLatch(CountDown);
        }

        public void itemAdded(ItemEvent itemEvent) {
            added.countDown();
        }

        public void itemRemoved(ItemEvent itemEvent) {
        }
    }

}

@@ -1359,16 +1359,339 @@
  %7B%0A%0A    
+private String Qname = %22Q%22;%0A    private int totalQput=2000;%0A%0A    private Config cfg = new Config();%0A    private SimpleItemListener simpleItemListener = new SimpleItemListener(totalQput);%0A    private ItemListenerConfig itemListenerConfig = new ItemListenerConfig();%0A    private QueueConfig qConfig = new QueueConfig();%0A%0A    
 @Test%0A  
@@ -1785,107 +1785,8 @@
 n %7B%0A
-%0A        int count=2000;%0A%0A        ItemListenerConfig itemListenerConfig = new ItemListenerConfig();
 %0A   
@@ -1827,21 +1827,17 @@
 ntation(
-new S
+s
 impleIte
@@ -1849,15 +1849,8 @@
 ener
-(count)
 );%0A 
@@ -1903,57 +1903,8 @@
 );%0A%0A
-        QueueConfig qConfig = new QueueConfig();%0A
     
@@ -1923,19 +1923,21 @@
 setName(
-%22Q%22
+Qname
 );%0A     
@@ -1993,44 +1993,8 @@
 ig);
-%0A%0A        Config cfg = new Config();
 %0A   
@@ -2092,32 +2092,77 @@
 tInstance(cfg);%0A
+        IQueue queue = node1.getQueue(Qname);
 %0A        for(int
@@ -2169,20 +2169,24 @@
  i=0; i%3C
-coun
+totalQpu
 t/2; i++
@@ -2201,35 +2201,21 @@
         
-node1.getQueue(%22Q%22)
+queue
 .put(i);
@@ -2322,20 +2322,24 @@
  i=0; i%3C
-coun
+totalQpu
 t/4; i++
@@ -2358,27 +2358,13 @@
     
-node1.getQueue(%22Q%22)
+queue
 .put
@@ -2383,191 +2383,8 @@
  %7D%0A%0A
-        List%3CItemListenerConfig%3E configs = qConfig.getItemListenerConfigs();%0A        SimpleItemListener simpleItemListener = (SimpleItemListener) configs.get(0).getImplementation();%0A%0A
     
@@ -2433,10 +2433,9 @@
 ait(
-10
+3
 , Ti
@@ -2459,17 +2459,16 @@
 %0A    %7D%0A%0A
-%0A
     priv
@@ -2529,17 +2529,16 @@
 tener %7B%0A
-%0A
         
@@ -2845,11 +2845,10 @@
 %7D%0A    %7D%0A
-%0A
 %7D%0A

test clean up