/*
 * 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,26 @@
 las.
-lib.FileOps
+RuntimeIOException
 ;%0Aim
@@ -1189,91 +1189,61 @@
 ena.
-dboe.base.file.AlreadyLocked;%0Aimport org.apache.jena.dboe.base.file.ProcessFileLock
+atlas.io.IO;%0Aimport org.apache.jena.atlas.lib.FileOps
 ;%0Aim
@@ -1572,21 +1572,16 @@
 E;%0A    %0A
-    %0A
     @Bef
@@ -1773,19 +1773,202 @@
     
-f.delete();
+if ( FileOps.exists(lockfile) ) %7B %0A                boolean b = f.delete();%0A                if ( !b )%0A                    throw new RuntimeIOException(%22Failed to delete %22+lockfile);%0A            %7D
 %0A   
@@ -2040,49 +2040,25 @@
 e) %7B
-%0A            e.printStackTrace();%0A       
+ IO.exception(e);
  %7D%0A 

Check .delete() in beforeTest