package com.hazelcast.jmx;

import com.hazelcast.core.ILock;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.util.concurrent.TimeUnit;

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

@RunWith(HazelcastParallelClassRunner.class)
@Category(QuickTest.class)
public class LockMBeanTest extends HazelcastTestSupport {
    private static JmxTestDataHolder holder;

    @BeforeClass
    public static void setUp() throws Exception {
        holder = new JmxTestDataHolder();
    }

    @Test
    public void testLock() throws Exception {
        ILock lock = holder.getHz().getLock("lock");
        // Lock for some time so we can later read the expiry time
        lock.lock(1000, TimeUnit.MILLISECONDS);

        // --- Check that mbean gets created at all
        holder.assertMBeanExistEventually("ILock", lock.getName());

        // --- Check remaining lease time
        final long remainingLeaseTime = (Long) holder.getMBeanAttribute("ILock", lock.getName(), "remainingLeaseTime");
        // The remaining lease time has to be in the given range from the originally set value
        assertTrue(remainingLeaseTime > 500);
        assertTrue(remainingLeaseTime < 1000);

        // --- Check number of times locked (locked twice now)
        lock.lock(1000, TimeUnit.MILLISECONDS);
        final int lockCount = (Integer) holder.getMBeanAttribute("ILock", lock.getName(), "lockCount");
        assertEquals(2, lockCount);

        // --- Force unlock
        holder.invokeMBeanOperation("ILock", lock.getName(), "forceUnlock", null, null);
        assertTrue(!lock.isLocked());
    }
}
@@ -32,449 +32,420 @@
 ort 
-com.hazelcast.core.ILock;%0Aimport com.hazelcast.test.HazelcastParallelClassRunner;%0Aimport com.hazelcast.test.HazelcastTestSupport;%0Aimport com.hazelcast.test.annotation.QuickTest;%0Aimport org.junit.BeforeClass;%0Aimport org.junit.Test;%0Aimport org.junit.experimental.categories.Category;%0Aimport org.junit.runner.RunWith;%0A%0Aimport java.util.concurrent.TimeUnit;%0A%0Aimport static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertTrue
+static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertTrue;%0A%0Aimport java.util.concurrent.TimeUnit;%0A%0Aimport org.junit.Test;%0Aimport org.junit.experimental.categories.Category;%0Aimport org.junit.runner.RunWith;%0A%0Aimport com.hazelcast.core.ILock;%0Aimport com.hazelcast.test.HazelcastParallelClassRunner;%0Aimport com.hazelcast.test.HazelcastTestSupport;%0Aimport com.hazelcast.test.annotation.QuickTest
 ;%0A%0A@
@@ -573,16 +573,17 @@
 pport %7B%0A
+%0A
     priv
@@ -590,148 +590,313 @@
 ate 
+JmxTe
 st
+D
 at
-ic JmxTestDataHolder holder;%0A%0A    @BeforeClass%0A    public static void setUp() throws Exception %7B%0A        holder = new JmxTestDataHolder(
+aHolder holder = new JmxTestDataHolder();%0A    private ILock lock = holder.getHz().getLock(%22lock%22);%0A%0A    @Test%0A    public void testMbeanExists_whenLockExists() throws Exception %7B%0A        // --- Check that mbean gets created at all%0A        holder.assertMBeanExistEventually(%22ILock%22, lock.getName()
 );%0A 
@@ -935,16 +935,42 @@
 testLock
+CountIsTwo_whenLockedTwice
 () throw
@@ -995,236 +995,201 @@
     
-ILock lock = holder.getHz().getLock(%22lock%22);%0A        // Lock for some time so we can later read the expiry time%0A        lock.lock(1000, TimeUnit.MILLISECONDS);%0A%0A        // --- Check that mbean gets created at all%0A       
+lock.lock(1000, TimeUnit.MILLISECONDS);%0A        lock.lock(1000, TimeUnit.MILLISECONDS);%0A%0A        // --- Check number of times locked (locked twice now)%0A        int lockCount = (Integer)
  holder.
 asse
@@ -1188,34 +1188,25 @@
 der.
-asser
+ge
 tMBean
-ExistEventually
+Attribute
 (%22IL
@@ -1225,16 +1225,214 @@
 etName()
+, %22lockCount%22);%0A%0A        assertEquals(2, lockCount);%0A    %7D%0A%0A    @Test%0A    public void testMBeanHasLeaseTime_whenLockedWithLeaseTime() throws Exception %7B%0A        lock.lock(1000, TimeUnit.MILLISECONDS
 );%0A%0A    
@@ -1477,22 +1477,16 @@
         
-final 
 long rem
@@ -1579,24 +1579,25 @@
 easeTime%22);%0A
+%0A
         // T
@@ -1690,32 +1690,49 @@
      assertTrue(
+%22Lease time: %22 + 
 remainingLeaseTi
@@ -1737,36 +1737,36 @@
 Time
- %3E 500);%0A        assertTrue(
+, remainingLeaseTime %3E 0 && 
 rema
@@ -1793,288 +1793,149 @@
 0);%0A
-%0A
     
+%7D%0A%0A
     
-// --- Check number of times locked (locked twice now)%0A        lock.lock(1000, TimeUnit.MILLISECONDS);%0A        final int lockCount = (Integer) holder.getMBeanAttribute(%22ILock%22, lock.getName(), %22lockCount%22);%0A        assertEquals(2, lockCount);%0A%0A        // --- Force unlock
+@Test%0A    public void testIsNotLocked_whenMBeanForceUnlocked() throws Exception %7B%0A        lock.lock(1000, TimeUnit.MILLISECONDS);%0A
 %0A   
@@ -2020,16 +2020,17 @@
  null);%0A
+%0A
         
@@ -2044,9 +2044,28 @@
 rue(
-!
+%22Lock is locked%22, ! 
 lock
@@ -2075,16 +2075,17 @@
 Locked()
+ 
 );%0A    %7D
@@ -2086,8 +2086,9 @@
 %0A    %7D%0A%7D
+%0A

Test refactoring. Split up, added messages