changeset 1501:fd84d9b293df

Fixed LockedFile for readonly on windows * netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java: made isUnix deprecated * netx/net/sourceforge/jnlp/util/lockingfile/LockedFile.java: creating file (for windows) only if not readonly * tests/netx/unit/net/sourceforge/jnlp/util/lockingfile/NonWindowsLockedFile.java: tests for os set to no windows * tests/netx/unit/net/sourceforge/jnlp/util/lockingfile/WindowsLockedFileTest.java: tests for os set to windows
author Jiri Vanek <jvanek@redhat.com>
date Fri, 23 Nov 2018 15:33:04 +0100
parents 4453afdafaf9
children dfc800562a29
files ChangeLog netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java netx/net/sourceforge/jnlp/util/lockingfile/LockedFile.java tests/netx/unit/net/sourceforge/jnlp/util/lockingfile/NonWindowsLockedFile.java tests/netx/unit/net/sourceforge/jnlp/util/lockingfile/WindowsLockedFileTest.java
diffstat 5 files changed, 115 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Nov 23 13:41:00 2018 +0100
+++ b/ChangeLog	Fri Nov 23 15:33:04 2018 +0100
@@ -1,3 +1,13 @@
+2018-11-23  Lars Herschke <lhersch@dssgmbh.de>
+            Jiri Vanek <jvanek@redhat.com>
+
+	Fixed LockedFile for readonly on windows
+	* netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java: made isUnix deprecated
+	* netx/net/sourceforge/jnlp/util/lockingfile/LockedFile.java: creating file (for windows) only if not readonly
+	* tests/netx/unit/net/sourceforge/jnlp/util/lockingfile/NonWindowsLockedFile.java: tests for os set to no windows
+	* tests/netx/unit/net/sourceforge/jnlp/util/lockingfile/WindowsLockedFileTest.java: tests for os set to windows
+
+
 2018-11-23  Lars Herschke <lhersch@dssgmbh.de>
 
 	Fix for java.lang.NoClassDefFoundError: Could not initialize class net.sourceforge.jnlp.runtime.JNLPRuntime$DeploymentConfigurationHolder
--- a/netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java	Fri Nov 23 13:41:00 2018 +0100
+++ b/netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java	Fri Nov 23 15:33:04 2018 +0100
@@ -775,6 +775,7 @@
      * @return {@code true} if running on a Unix or Unix-like system (including
      * Linux and *BSD)
      */
+    @Deprecated
     public static boolean isUnix() {
         String sep = System.getProperty("file.separator");
         return (sep != null && sep.equals("/"));
--- a/netx/net/sourceforge/jnlp/util/lockingfile/LockedFile.java	Fri Nov 23 13:41:00 2018 +0100
+++ b/netx/net/sourceforge/jnlp/util/lockingfile/LockedFile.java	Fri Nov 23 15:33:04 2018 +0100
@@ -197,7 +197,9 @@
         /*Comment why itis different*/
         @Override
         public void lock() throws IOException {
-            super.file.createNewFile();
+            if (!isReadOnly()) {
+                super.file.createNewFile();
+            }
             super.threadLock.lock();
         }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/netx/unit/net/sourceforge/jnlp/util/lockingfile/NonWindowsLockedFile.java	Fri Nov 23 15:33:04 2018 +0100
@@ -0,0 +1,30 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package net.sourceforge.jnlp.util.lockingfile;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+
+/**
+ *
+ * @author jvanek
+ */
+public class NonWindowsLockedFile extends WindowsLockedFileTest {
+
+    private static String os;
+
+    @BeforeClass
+    public static void smuggleOs() {
+        os = System.getProperty("os.name");
+        System.setProperty("os.name", "No Windows for itw");
+    }
+
+    @AfterClass
+    public static void restoreOs() {
+        System.setProperty("os.name", os);
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/netx/unit/net/sourceforge/jnlp/util/lockingfile/WindowsLockedFileTest.java	Fri Nov 23 15:33:04 2018 +0100
@@ -0,0 +1,71 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package net.sourceforge.jnlp.util.lockingfile;
+
+import java.io.File;
+import java.io.IOException;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ *
+ * @author jvanek
+ */
+public class WindowsLockedFileTest {
+
+    private static String os;
+
+    @BeforeClass
+    public static void smuggleOs() {
+        os = System.getProperty("os.name");
+        System.setProperty("os.name", "Windows for itw");
+    }
+
+    @AfterClass
+    public static void restoreOs() {
+        System.setProperty("os.name", os);
+    }
+
+    @Test
+    public void testLockUnlockOkExists() throws IOException {
+        File f = File.createTempFile("itw", "lockingFile");
+        f.deleteOnExit();
+        LockedFile lf = LockedFile.getInstance(f);
+        lf.lock();
+        lf.unlock();
+    }
+
+    @Test
+    public void testLockUnlockOkNotExists() throws IOException {
+        File f = File.createTempFile("itw", "lockingFile");
+        f.delete();
+        LockedFile lf = LockedFile.getInstance(f);
+        lf.lock();
+        lf.unlock();
+    }
+
+    @Test
+    public void testLockUnlockNoOkNotExists() throws IOException {
+        File parent = File.createTempFile("itw", "lockingFile");
+        parent.deleteOnExit();
+        File f = new File(parent, "itwLcokingRelict");
+        f.delete();
+        parent.setReadOnly();
+        LockedFile lf = LockedFile.getInstance(f);
+        lf.lock();
+        lf.unlock();;
+    }
+
+    @Test
+    public void testLockUnlockNotOkExists() throws IOException {
+        File f = new File("/some/deffinitley/not/exisitng/file.itw");
+        LockedFile lf = LockedFile.getInstance(f);
+        lf.lock();
+        lf.unlock();
+    }
+
+}