/*
 * Copyright (c) 2002-2015 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.neo4j.kernel.ha.lock;

import org.junit.Test;

import org.neo4j.graphdb.DependencyResolver;
import org.neo4j.helpers.Clock;
import org.neo4j.kernel.AvailabilityGuard;
import org.neo4j.kernel.ha.com.RequestContextFactory;
import org.neo4j.kernel.ha.com.master.Master;
import org.neo4j.kernel.impl.locking.Locks;
import org.neo4j.kernel.impl.locking.community.CommunityLockManger;

import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class SlaveLockManagerTest
{
    @Test
    public void shutsDownLocalLocks() throws Throwable
    {
        Locks localLocks = mock( Locks.class );
        SlaveLockManager slaveLockManager = newSlaveLockManager( localLocks );

        slaveLockManager.shutdown();

        verify( localLocks ).shutdown();
    }

    @Test
    public void doesNotCreateClientsAfterShutdown() throws Throwable
    {
        SlaveLockManager slaveLockManager = newSlaveLockManager( new CommunityLockManger() );

        assertNotNull( slaveLockManager.newClient() );

        slaveLockManager.shutdown();

        try
        {
            slaveLockManager.newClient();
            fail( "Exception expected" );
        }
        catch ( Exception e )
        {
            assertThat( e, instanceOf( IllegalStateException.class ) );
        }
    }

    private static SlaveLockManager newSlaveLockManager( Locks localLocks )
    {
        return new SlaveLockManager( localLocks, new RequestContextFactory( 1, mock( DependencyResolver.class ) ),
                mock( Master.class ), new AvailabilityGuard( Clock.SYSTEM_CLOCK ),
                mock( SlaveLockManager.Configuration.class ) );
    }
}

@@ -844,16 +844,41 @@
 .lock;%0A%0A
+import org.junit.Before;%0A
 import o
@@ -1519,16 +1519,523 @@
 rTest%0A%7B%0A
+    private RequestContextFactory requestContextFactory;%0A    private Master master;%0A    private AvailabilityGuard availabilityGuard;%0A    private SlaveLockManager.Configuration config;%0A%0A    @Before%0A    public void setUp()%0A    %7B%0A        requestContextFactory = new RequestContextFactory( 1, mock( DependencyResolver.class ) );%0A        master = mock( Master.class );%0A        availabilityGuard = new AvailabilityGuard( Clock.SYSTEM_CLOCK );%0A        config = mock( SlaveLockManager.Configuration.class );%0A    %7D%0A%0A
     @Tes
@@ -2842,23 +2842,16 @@
 private 
-static 
 SlaveLoc
@@ -2954,21 +2954,17 @@
 lLocks, 
-new R
+r
 equestCo
@@ -2979,192 +2979,75 @@
 tory
-( 1, mock( DependencyResolver.class ) ),%0A                mock( Master.class ), new AvailabilityGuard( Clock.SYSTEM_CLOCK ),%0A                mock( SlaveLockManager.Configuration.class )
+,%0A                master, availabilityGuard,%0A                config
  );%0A

Prepare SlaveLockManagerTest for more complicated setups