/**
 * Copyright (c) 2002-2013 "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 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.neo4j.kernel.impl.nioneo.store;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;

import java.io.File;
import java.nio.channels.FileChannel;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.helpers.Settings;
import org.neo4j.kernel.DefaultFileSystemAbstraction;
import org.neo4j.kernel.StoreLocker;
import org.neo4j.test.TargetDirectory;

public class TestOsSpecificLocks
{
    private File path;

    @Rule
    public TestName name = new TestName();

    @Before
    public void doBefore()
    {
        path = TargetDirectory.forTest( getClass() ).directory( name.getMethodName(), true );
    }

    @Test
    public void sanityCheck() throws Exception
    {
        assumeTrue( Settings.osIsWindows() );
        FileSystemAbstraction fs = new DefaultFileSystemAbstraction();
        // Must grab locks only on store_lock file
        File fileName = new File( path, StoreLocker.STORE_LOCK_FILENAME );
        FileChannel channel = fs.open( fileName, "rw" );
        // Lock this sucker!
        FileLock lock = fs.tryLock( fileName, channel );
        assertTrue( new File( path, "lock" ).exists() );
        // If we try to lock with the lock held, a null should be served
        assertNull( fs.tryLock( fileName, channel ) );

        // But the rest of the files should return non null (placebos,
        // actually)
        FileChannel tempChannel = fs.open( new File( fileName.getPath() + "1"), "rw" );
        FileLock tempLock = fs.tryLock( new File( fileName.getPath() + "1"), tempChannel );
        assertNotNull( tempLock );
        tempLock.release();
        tempChannel.close();

        // Release and retry, should succeed
        lock.release();
        assertFalse( new File( path, "lock" ).exists() );
        fs.tryLock( fileName, channel ).release(); // NPE on fail here
        assertFalse( new File( path, "lock" ).exists() );
    }

    @Test
    public void testDatabaseLocking()
    {
        assumeTrue( Settings.osIsWindows() );
        GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase( path.getPath() );
        Transaction tx = db.beginTx();
        db.createNode();
        tx.success();
        tx.finish();
        assertTrue( new File( path, "lock" ).exists() );
        try
        {
            new GraphDatabaseFactory().newEmbeddedDatabase( path.getPath() );
            fail("Should not be able to start up another db in the same dir");
        }
        catch ( Exception e )
        {
            // Good
        }
        db.shutdown();
    }
}

@@ -846,277 +846,48 @@
 ort 
-static org.junit.Assert.assertFalse;%0Aimport static org.junit.Assert.assertNotNull;%0Aimport static org.junit.Assert.assertNull;%0Aimport static org.junit.Assert.assertTrue;%0Aimport static org.junit.Assert.fail;%0Aimport static org.junit.Assume.assumeTrue;%0A%0Aimport java.io.File
+java.io.File;%0Aimport java.io.IOException
 ;%0Aim
@@ -1333,16 +1333,94 @@
 ctory;%0A%0A
+import static org.junit.Assert.*;%0Aimport static org.junit.Assume.assumeTrue;%0A%0A
 public c
@@ -2229,22 +2229,18 @@
 ld, 
-a null
+it
  should 
 be s
@@ -2239,72 +2239,209 @@
 uld 
-be served%0A        assertNull( fs.tryLock( fileName, channel ) );
+throw an IOException%0A        try%0A        %7B%0A            fs.tryLock( fileName, channel );%0A            fail(%22Should have thrown IO exception.%22);%0A        %7D%0A        catch(IOException e)%0A        %7B%0A%0A        %7D
 %0A%0A  

Fix for broken windows build.