/** Test for issue #39 */
  @Test
  public void testIsMapKeyLocked() throws InterruptedException {
    HazelcastClient hClient = getHazelcastClient();
    final IMap map = hClient.getMap("testIsMapKeyLocked");
    assertFalse(map.isLocked("key"));
    map.lock("key");
    assertTrue(map.isLocked("key"));

    final CountDownLatch latch = new CountDownLatch(1);
    Thread thread =
        new Thread(
            new Runnable() {
              public void run() {
                assertTrue(map.isLocked("key"));
                try {
                  while (map.isLocked("key")) {
                    Thread.sleep(100);
                  }
                } catch (InterruptedException e) {
                  throw new RuntimeException(e);
                }
                latch.countDown();
              }
            });
    thread.start();
    Thread.sleep(100);
    map.unlock("key");
    assertTrue(latch.await(3, TimeUnit.SECONDS));
  }
 @Test
 public void valuesToArray() {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("valuesToArray");
   assertEquals(0, map.size());
   map.put("a", "1");
   map.put("b", "2");
   map.put("c", "3");
   assertEquals(3, map.size());
   {
     final Object[] values = map.values().toArray();
     Arrays.sort(values);
     assertArrayEquals(new Object[] {"1", "2", "3"}, values);
   }
   {
     final String[] values = (String[]) map.values().toArray(new String[3]);
     Arrays.sort(values);
     assertArrayEquals(new String[] {"1", "2", "3"}, values);
   }
   {
     final String[] values = (String[]) map.values().toArray(new String[2]);
     Arrays.sort(values);
     assertArrayEquals(new String[] {"1", "2", "3"}, values);
   }
   {
     final String[] values = (String[]) map.values().toArray(new String[5]);
     Arrays.sort(values, 0, 3);
     assertArrayEquals(new String[] {"1", "2", "3", null, null}, values);
   }
 }
 @Test
 public void iterateOverMapEntries() {
   HazelcastClient hClient = getHazelcastClient();
   IMap<String, String> map = hClient.getMap("iterateOverMapEntries");
   map.put("1", "A");
   map.put("2", "B");
   map.put("3", "C");
   Set<Entry<String, String>> entrySet = map.entrySet();
   assertEquals(3, entrySet.size());
   Set<String> keySet = map.keySet();
   for (Entry<String, String> entry : entrySet) {
     assertTrue(keySet.contains(entry.getKey()));
     assertEquals(entry.getValue(), map.get(entry.getKey()));
   }
   Iterator<Entry<String, String>> it = entrySet.iterator();
   for (String key : keySet) {
     MapEntry mapEntry = map.getMapEntry(key);
     assertEquals(1, mapEntry.getHits());
   }
   while (it.hasNext()) {
     it.next();
     it.remove();
   }
   assertTrue(map.isEmpty());
 }
 @Test
 public void removeListener() throws InterruptedException, IOException {
   HazelcastClient hClient = getHazelcastClient();
   final IMap<String, String> map = hClient.getMap("removeListener");
   final CountDownLatch entryAddLatch = new CountDownLatch(5);
   final CountDownLatch entryUpdatedLatch = new CountDownLatch(5);
   final CountDownLatch entryRemovedLatch = new CountDownLatch(5);
   CountDownLatchEntryListener<String, String> listener1 =
       new CountDownLatchEntryListener<String, String>(
           entryAddLatch, entryUpdatedLatch, entryRemovedLatch);
   CountDownLatchEntryListener<String, String> listener2 =
       new CountDownLatchEntryListener<String, String>(
           entryAddLatch, entryUpdatedLatch, entryRemovedLatch);
   map.addEntryListener(listener1, true);
   map.put("hello", "world");
   map.put("hello", "new world");
   map.remove("hello");
   Thread.sleep(100);
   assertEquals(4, entryAddLatch.getCount());
   assertEquals(4, entryRemovedLatch.getCount());
   assertEquals(4, entryUpdatedLatch.getCount());
   map.removeEntryListener(listener1);
   map.put("hello", "world");
   map.put("hello", "new world");
   map.remove("hello");
   Thread.sleep(100);
   assertEquals(4, entryAddLatch.getCount());
   assertEquals(4, entryRemovedLatch.getCount());
   assertEquals(4, entryUpdatedLatch.getCount());
 }
  @Test
  public void testConfigLifecycleListener() throws InterruptedException {
    ClientConfig config = new ClientConfig();
    final BlockingQueue<LifecycleEvent> q = new LinkedBlockingQueue<LifecycleEvent>();
    config.addListener(
        new LifecycleListener() {
          public void stateChanged(final LifecycleEvent event) {
            q.offer(event);
            System.out.println(event);
          }
        });
    Hazelcast.getDefaultInstance();
    HazelcastClient client = HazelcastClient.newHazelcastClient(config);

    Assert.assertEquals(new LifecycleEvent(LifecycleState.STARTING), q.poll(3, TimeUnit.SECONDS));
    Assert.assertEquals(
        new LifecycleEvent(LifecycleState.CLIENT_CONNECTION_OPENING), q.poll(3, TimeUnit.SECONDS));
    Assert.assertEquals(
        new LifecycleEvent(LifecycleState.CLIENT_CONNECTION_OPENED), q.poll(3, TimeUnit.SECONDS));
    Assert.assertEquals(new LifecycleEvent(LifecycleState.STARTED), q.poll(3, TimeUnit.SECONDS));
    client.shutdown();
    //        Assert.assertEquals(new LifecycleEvent(LifecycleState.CLIENT_CONNECTION_LOST),
    // q.poll(3, TimeUnit.SECONDS));
    Assert.assertEquals(
        new LifecycleEvent(LifecycleState.SHUTTING_DOWN), q.poll(3, TimeUnit.SECONDS));
    Assert.assertEquals(new LifecycleEvent(LifecycleState.SHUTDOWN), q.poll(3, TimeUnit.SECONDS));
  }
 @Test
 public void addIndex() {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("addIndex");
   int size = 1000;
   for (int i = 0; i < size; i++) {
     map.put(String.valueOf(i), new Employee("name" + i, i, true, 0));
   }
   EntryObject e = new PredicateBuilder().getEntryObject();
   Predicate predicate = e.get("age").equal(23);
   long begin = Clock.currentTimeMillis();
   Set<Entry<Object, Object>> set = map.entrySet(predicate);
   long timeWithoutIndex = Clock.currentTimeMillis() - begin;
   assertEquals(1, set.size());
   assertEquals(size, map.size());
   map.destroy();
   map = hClient.getMap("addIndex");
   map.addIndex("age", true);
   for (int i = 0; i < size; i++) {
     map.put(String.valueOf(i), new Employee("name" + i, i, true, 0));
   }
   begin = Clock.currentTimeMillis();
   set = map.entrySet(predicate);
   long timeWithIndex = Clock.currentTimeMillis() - begin;
   assertEquals(1, set.size());
   assertEquals(size, map.size());
   assertTrue(timeWithoutIndex > 2 * timeWithIndex);
   //    	map.addIndex("age", true);
 }
 @Test
 public void lockMap() throws InterruptedException {
   HazelcastClient hClient = getHazelcastClient();
   final IMap<String, String> map = hClient.getMap("lockMap");
   final CountDownLatch unlockLatch = new CountDownLatch(1);
   final CountDownLatch latch = new CountDownLatch(1);
   map.put("a", "b");
   map.lockMap(1, TimeUnit.SECONDS);
   assertTrue(map.tryPut("a", "c", 10, TimeUnit.MILLISECONDS));
   new Thread(
           new Runnable() {
             public void run() {
               assertFalse(map.lockMap(10, TimeUnit.MILLISECONDS));
               unlockLatch.countDown();
               assertTrue(map.lockMap(Long.MAX_VALUE, TimeUnit.SECONDS));
               latch.countDown();
               // map.unlockMap();
             }
           })
       .start();
   assertTrue(unlockLatch.await(10, TimeUnit.SECONDS));
   Thread.sleep(2000);
   map.unlockMap();
   assertEquals("c", map.getMapEntry("a").getValue());
   assertTrue(latch.await(10, TimeUnit.SECONDS));
 }
 @Test
 public void testIssue321() throws Exception {
   HazelcastClient hClient = getHazelcastClient();
   final IMap<Integer, Integer> imap = hClient.getMap("testIssue321_1");
   final BlockingQueue<EntryEvent<Integer, Integer>> events1 =
       new LinkedBlockingQueue<EntryEvent<Integer, Integer>>();
   final BlockingQueue<EntryEvent<Integer, Integer>> events2 =
       new LinkedBlockingQueue<EntryEvent<Integer, Integer>>();
   imap.addEntryListener(
       new EntryAdapter<Integer, Integer>() {
         @Override
         public void entryAdded(EntryEvent event) {
           events2.add(event);
         }
       },
       false);
   imap.addEntryListener(
       new EntryAdapter<Integer, Integer>() {
         @Override
         public void entryAdded(EntryEvent event) {
           events1.add(event);
         }
       },
       true);
   imap.put(1, 1);
   final EntryEvent<Integer, Integer> event1 = events1.poll(10, TimeUnit.MILLISECONDS);
   final EntryEvent<Integer, Integer> event2 = events2.poll(10, TimeUnit.MILLISECONDS);
   assertNotNull(event1);
   assertNotNull(event2);
   assertNotNull(event1.getValue());
   assertNull(event2.getValue());
 }
 @Test
 public void testGetNullMapEntry() {
   HazelcastClient hClient = getHazelcastClient();
   final IMap imap = hClient.getMap("testGetNullMapEntry");
   String key = "key";
   MapEntry mapEntry = imap.getMapEntry(key);
   assertNull(mapEntry);
 }
 @Test
 public void evictFromMap() {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("evictFromMap");
   assertNull(map.put("a", "b"));
   assertEquals("b", map.get("a"));
   assertTrue(map.evict("a"));
   assertNull(map.get("a"));
 }
 @Test
 public void tryPut() throws InterruptedException {
   HazelcastClient hClient = getHazelcastClient();
   IMap<String, String> map = hClient.getMap("tryPut");
   assertEquals(0, map.size());
   Boolean result = map.tryPut("1", "CBDEF", 1, TimeUnit.SECONDS);
   assertTrue(result);
   assertEquals(1, map.size());
 }
 @Test
 public void putIfAbsent() {
   HazelcastClient hClient = getHazelcastClient();
   IMap<String, String> map = hClient.getMap("putIfAbsent");
   String result = map.put("1", "CBDEF");
   assertNull(result);
   assertNull(map.putIfAbsent("2", "C"));
   assertEquals("C", map.putIfAbsent("2", "D"));
 }
 @Test
 public void testTwoMembersWithIndexes() {
   HazelcastClient hClient = getHazelcastClient();
   final IMap imap = hClient.getMap("testTwoMembersWithIndexes");
   imap.addIndex("name", false);
   imap.addIndex("age", true);
   imap.addIndex("active", false);
   doFunctionalQueryTest(imap);
 }
 @Test
 public void removeFromMap() {
   HazelcastClient hClient = getHazelcastClient();
   Map map = hClient.getMap("removeFromMap");
   assertNull(map.put("a", "b"));
   assertEquals("b", map.get("a"));
   assertEquals("b", map.remove("a"));
   assertNull(map.remove("a"));
   assertNull(map.get("a"));
 }
 @Test
 public void getPuttedValueFromTheMap() {
   HazelcastClient hClient = getHazelcastClient();
   Map<String, String> clientMap = hClient.getMap("getPuttedValueFromTheMap");
   int size = clientMap.size();
   clientMap.put("1", "Z");
   String value = clientMap.get("1");
   assertEquals("Z", value);
   assertEquals(size + 1, clientMap.size());
 }
 @Test
 public void putWithTTL() throws InterruptedException {
   HazelcastClient hClient = getHazelcastClient();
   IMap<String, String> map = hClient.getMap("putWithTTL");
   assertEquals(0, map.size());
   map.put("1", "CBDEF", 100, TimeUnit.MILLISECONDS);
   assertEquals(1, map.size());
   Thread.sleep(200);
   assertEquals(0, map.size());
 }
 @Test
 public void removeIfSame() {
   HazelcastClient hClient = getHazelcastClient();
   IMap<String, String> map = hClient.getMap("remove");
   String result = map.put("1", "CBDEF");
   assertNull(result);
   assertFalse(map.remove("1", "CBD"));
   assertEquals("CBDEF", map.get("1"));
   assertTrue(map.remove("1", "CBDEF"));
 }
 @Test
 public void testGetAsync() throws Exception {
   HazelcastClient hClient = getHazelcastClient();
   String key = "key";
   String value1 = "value1";
   IMap<String, String> map = hClient.getMap("map:test:getAsync");
   map.put(key, value1);
   Future<String> f1 = map.getAsync(key);
   assertEquals(value1, f1.get());
 }
 @Test
 public void putIfAbsentWithTtl() throws InterruptedException {
   HazelcastClient hClient = getHazelcastClient();
   IMap<String, String> map = hClient.getMap("putIfAbsentWithTtl");
   String result = map.put("1", "CBDEF");
   assertNull(result);
   assertNull(map.putIfAbsent("2", "C", 100, TimeUnit.MILLISECONDS));
   assertEquals(2, map.size());
   assertEquals("C", map.putIfAbsent("2", "D", 100, TimeUnit.MILLISECONDS));
   Thread.sleep(100);
   assertEquals(1, map.size());
 }
 @Test
 public void isEmpty() {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("isEmpty");
   int counter = 100;
   assertTrue(map.isEmpty());
   for (int i = 0; i < counter; i++) {
     assertNull(map.put(i, i));
     assertEquals(i, map.get(i));
   }
   assertFalse(map.isEmpty());
 }
 @Test
 public void containsValue() {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("containsValue");
   int counter = 100;
   for (int i = 0; i < counter; i++) {
     assertNull(map.put(i, i));
     assertEquals(i, map.get(i));
   }
   for (int i = 0; i < counter; i++) {
     assertTrue(map.containsValue(i));
   }
 }
 @Test
 public void clear() {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("clear");
   for (int i = 0; i < 100; i++) {
     assertNull(map.put(i, i));
     assertEquals(i, map.get(i));
   }
   map.clear();
   for (int i = 0; i < 100; i++) {
     assertNull(map.get(i));
   }
 }
 @Test
 public void testSqlPredicate() {
   HazelcastInstance h = getHazelcastInstance();
   HazelcastClient hClient = getHazelcastClient();
   IMap<Integer, Employee> map = hClient.getMap("testSqlPredicate");
   for (int i = 0; i < 100; i++) {
     h.getMap("testSqlPredicate").put(i, new Employee("" + i, i, i % 2 == 0, i));
   }
   Set<Entry<Integer, Employee>> set = map.entrySet(new SqlPredicate("active AND age < 30"));
   for (Entry<Integer, Employee> entry : set) {
     System.out.println(entry.getValue());
     assertTrue(entry.getValue().age < 30);
     assertTrue(entry.getValue().active);
   }
 }
 @Test
 public void putAllMany() {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("putAllMany");
   int counter = 100;
   for (int j = 0; j < 4; j++, counter *= 10) {
     Map tempMap = new HashMap();
     for (int i = 0; i < counter; i++) {
       tempMap.put(i, i);
     }
     map.putAll(tempMap);
     assertEquals(1, map.get(1));
   }
   map.destroy();
 }
 @Test
 public void putToTheMap() throws InterruptedException {
   HazelcastClient hClient = getHazelcastClient();
   Map<String, String> clientMap = hClient.getMap("putToTheMap");
   assertEquals(0, clientMap.size());
   String result = clientMap.put("1", "CBDEF");
   assertNull(result);
   assertEquals("CBDEF", clientMap.get("1"));
   assertEquals("CBDEF", clientMap.get("1"));
   assertEquals("CBDEF", clientMap.get("1"));
   assertEquals(1, clientMap.size());
   result = clientMap.put("1", "B");
   assertEquals("CBDEF", result);
   assertEquals("B", clientMap.get("1"));
   assertEquals("B", clientMap.get("1"));
 }
 @Test
 public void getMapEntry() {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("getMapEntry");
   assertNull(map.put("a", "b"));
   map.get("a");
   map.get("a");
   MapEntry<String, String> entry = map.getMapEntry("a");
   assertEquals("a", entry.getKey());
   assertEquals("b", entry.getValue());
   assertEquals(2, entry.getHits());
   assertEquals("b", entry.getValue());
   assertEquals("b", entry.setValue("c"));
   assertEquals("c", map.get("a"));
   assertEquals("c", entry.getValue());
 }
  /* github issue #183 */
  @Test
  public void testKeyBasedListeners() throws InterruptedException {
    try {
      Config config = new Config();
      HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
      IMap<String, String> map = instance.getMap("map");
      map.put("key1", "value1");
      map.put("key2", "value2");
      map.put("key3", "value3");

      ClientConfig clientConfig = new ClientConfig();
      HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);

      final AtomicInteger count = new AtomicInteger(0);
      IMap<String, String> clientMap = client.getMap("map");

      clientMap.addEntryListener(
          new EntryListener<String, String>() {
            public void entryAdded(EntryEvent<String, String> entryEvent) {
              count.incrementAndGet();
            }

            public void entryRemoved(EntryEvent<String, String> entryEvent) {}

            public void entryUpdated(EntryEvent<String, String> entryEvent) {
              count.incrementAndGet();
            }

            public void entryEvicted(EntryEvent<String, String> entryEvent) {}
          },
          "key1",
          true);

      clientMap.addEntryListener(
          new EntryListener<String, String>() {
            public void entryAdded(EntryEvent<String, String> entryEvent) {
              count.incrementAndGet();
            }

            public void entryRemoved(EntryEvent<String, String> entryEvent) {}

            public void entryUpdated(EntryEvent<String, String> entryEvent) {
              System.out.println("event map");
              count.incrementAndGet();
            }

            public void entryEvicted(EntryEvent<String, String> entryEvent) {}
          },
          "key2",
          true);

      map.put("key1", "new-value1");
      Thread.sleep(100);
      Assert.assertEquals(count.get(), 1);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
 @Test
 public void destroyMap() throws InterruptedException {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("destroy");
   for (int i = 0; i < 100; i++) {
     assertNull(map.put(i, i));
     assertEquals(i, map.get(i));
   }
   IMap<Integer, Integer> map2 = hClient.getMap("destroy");
   assertTrue(map == map2);
   assertTrue(map.getId().equals(map2.getId()));
   map.destroy();
   //        map2 = hClient.getMap("destroy");
   //        assertFalse(map == map2);
   for (int i = 0; i < 100; i++) {
     //            assertNull(map2.get(i));
   }
 }
 @Test
 public void lockMapKey() throws InterruptedException {
   HazelcastClient hClient = getHazelcastClient();
   final IMap<String, String> map = hClient.getMap("lockMapKey");
   final CountDownLatch latch = new CountDownLatch(1);
   map.put("a", "b");
   Thread.sleep(10);
   map.lock("a");
   new Thread(
           new Runnable() {
             public void run() {
               map.lock("a");
               latch.countDown();
             }
           })
       .start();
   Thread.sleep(10);
   assertEquals(1, latch.getCount());
   map.unlock("a");
   assertTrue(latch.await(10, TimeUnit.SECONDS));
 }
 @Test
 public void putAll() {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("putAll");
   int counter = 100;
   Set keys = new HashSet(counter);
   for (int i = 0; i < counter; i++) {
     keys.add(i);
   }
   Map all = map.getAll(keys);
   assertEquals(0, all.size());
   Map tempMap = new HashMap();
   for (int i = 0; i < counter; i++) {
     tempMap.put(i, i);
   }
   map.putAll(tempMap);
   for (int i = 0; i < counter; i++) {
     assertEquals(i, map.get(i));
   }
   all = map.getAll(keys);
   assertEquals(counter, all.size());
 }