/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.jena.dboe.base.file;

import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;

import java.io.File;
import java.io.IOException;

import org.apache.jena.atlas.lib.FileOps;
import org.apache.jena.dboe.base.file.AlreadyLocked;
import org.apache.jena.dboe.base.file.ProcessFileLock;
import org.apache.jena.dboe.sys.Names;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class TestProcessFileLock {
    // TestLocationLockStoreConnection
    
    private static final String DIR  = "target/locktest";
    private static final String lockfile = DIR+"/"+Names.TDB_LOCK_FILE;
    
    
    @BeforeClass public static void beforeClass() {
        FileOps.ensureDir(DIR);
    }
    
    @Before public void beforeTest() {
        File f = new File(lockfile);
        try {
            f.delete();
            f.createNewFile();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test public void process_lock_1() {
        ProcessFileLock lock = ProcessFileLock.create(lockfile);
        String fn = new File(lockfile).getAbsolutePath();
        assertEquals(fn, lock.getPath().toString());
    }
    
    @Test public void process_lock_2() {
        ProcessFileLock lock1 = ProcessFileLock.create(lockfile);
        ProcessFileLock lock2 = ProcessFileLock.create(lockfile);
        assertSame(lock1, lock2);
    }

    @Test public void process_lock_3() {
        ProcessFileLock lock1 = ProcessFileLock.create(lockfile);
        ProcessFileLock.release(lock1);
        ProcessFileLock lock2 = ProcessFileLock.create(lockfile);
        assertNotSame(lock1, lock2);
    }
    
    @Test public void process_lock_4() {
        ProcessFileLock lock = ProcessFileLock.create(lockfile);
        assertFalse(lock.isLockedHere());
        lock.lockEx();
        assertTrue(lock.isLockedHere());
        lock.unlock();
        assertFalse(lock.isLockedHere());
    }

    @Test(expected=AlreadyLocked.class)
    public void process_lock_5() {
        ProcessFileLock lock = ProcessFileLock.create(lockfile);
        lock.lockEx();
        lock.lockEx();
    }
    
    @Test(expected=AlreadyLocked.class)
    public void process_lock_6() {
        ProcessFileLock lock = ProcessFileLock.create(lockfile);
        lock.lockEx();
        boolean b = lock.tryLock();
        assertFalse(b);
    }

    @Test(expected=AlreadyLocked.class)
    public void process_lock_7() {
        ProcessFileLock lock = ProcessFileLock.create(lockfile);
        lock.tryLock();
        lock.tryLock();
    }
}

@@ -872,17 +872,28 @@
 .Assert.
-*
+assertEquals
 ;%0Aimport
@@ -1018,16 +1018,59 @@
 ertSame;
+%0Aimport static org.junit.Assert.assertTrue;
 %0A%0Aimport
@@ -1146,19 +1146,13 @@
 las.
-lib.FileOps
+io.IO
 ;%0Aim
@@ -1181,125 +1181,65 @@
 boe.
-base.file.AlreadyLocked;%0Aimport org.apache.jena.dboe.base.file.ProcessFileLock;%0Aimport org.apache.jena.dboe.sys.Names
+sys.Names;%0Aimport org.junit.Before;%0Aimport org.junit.Rule
 ;%0Aim
@@ -1245,38 +1245,36 @@
 mport org.junit.
-Before
+Test
 ;%0Aimport org.jun
@@ -1280,42 +1280,29 @@
 nit.
-BeforeClass;%0Aimport org.junit.Test
+rules.TemporaryFolder
 ;%0A%0Ap
@@ -1343,424 +1343,358 @@
     
-// TestLocationLockStoreConnection%0A    %0A    private static final String DIR  = %22target/locktest%22;%0A    priva
+%0A    private String lockfile;%0A    %0A    //Using a per-
 te
- 
 st
-atic final String lockfile = DIR+%22/%22+Names.TDB_LOCK_FILE;%0A    %0A    %0A    @BeforeClass public static void beforeClass() %7B%0A        FileOps.ensureDir(DIR);%0A    %7D%0A    %0A    @Before public void beforeTest() %7B%0A        File f = new File(lockfile);%0A        try %7B%0A            f.delete();%0A            f.createNewFile
+ rule is %22doubly-safe%22 because we clear the process state.%0A    @Rule%0A    public TemporaryFolder tempFolder = new TemporaryFolder();%0A    %0A    @Before public void beforeTest() %7B%0A        try %7B%0A            File f = tempFolder.newFile(Names.TDB_LOCK_FILE);%0A            lockfile = f.getCanonicalPath
 ();%0A
@@ -1751,26 +1751,22 @@
     
-e.printStackTrace(
+IO.exception(e
 );%0A 
@@ -1776,24 +1776,28 @@
     %7D%0A    %7D%0A
+    
 %0A    @Test p

Isolate tests after observed problems on MSWindows.