sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1801760 - in /sis/branches/JDK8: core/sis-utility/src/main/java/org/apache/sis/internal/system/ core/sis-utility/src/main/java/org/apache/sis/util/resources/ ide-project/NetBeans/ storage/sis-gdal/src/main/c/ storage/sis-gdal/src/main/java...
Date Wed, 12 Jul 2017 17:10:00 GMT
Author: desruisseaux
Date: Wed Jul 12 17:10:00 2017
New Revision: 1801760

URL: http://svn.apache.org/viewvc?rev=1801760&view=rev
Log:
Enable PJTest on platforms having a Proj4 library installed.

Added:
    sis/branches/JDK8/storage/sis-gdal/src/test/java/org/apache/sis/test/
    sis/branches/JDK8/storage/sis-gdal/src/test/java/org/apache/sis/test/suite/
    sis/branches/JDK8/storage/sis-gdal/src/test/java/org/apache/sis/test/suite/GDALTestSuite.java
  (with props)
Modified:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/system/OS.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
    sis/branches/JDK8/ide-project/NetBeans/build.xml
    sis/branches/JDK8/storage/sis-gdal/src/main/c/Makefile
    sis/branches/JDK8/storage/sis-gdal/src/main/java/org/apache/sis/storage/gdal/PJ.java
    sis/branches/JDK8/storage/sis-gdal/src/test/java/org/apache/sis/storage/gdal/PJTest.java

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/system/OS.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/system/OS.java?rev=1801760&r1=1801759&r2=1801760&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/system/OS.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/system/OS.java
[UTF-8] Wed Jul 12 17:10:00 2017
@@ -16,12 +16,23 @@
  */
 package org.apache.sis.internal.system;
 
+import java.net.URL;
+import java.net.URISyntaxException;
+import java.io.File;
+import java.io.InputStream;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
+import org.apache.sis.util.logging.Logging;
+import org.apache.sis.util.resources.Errors;
+
 
 /**
  * The operation system on which SIS is running.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 0.3
+ * @version 0.8
  * @since   0.3
  * @module
  */
@@ -29,22 +40,29 @@ public enum OS {
     /**
      * Unknown system.
      */
-    UNKNOWN(false),
+    UNKNOWN(null, false),
 
     /**
      * Windows.
      */
-    WINDOWS(false),
+    WINDOWS("windows", false),
 
     /**
      * Mac OS.
      */
-    MAC_OS(true),
+    MAC_OS("darwin", true),
 
     /**
      * Linux.
      */
-    LINUX(true);
+    LINUX("linux", true);
+
+    /**
+     * The sub-directory where to look for native files ({@code .so} or {@code .dll}).
+     * Those subdirectories are not standard (as far as we know) and could change in
+     * any future Apache SIS version. The directory is {@code null} for unknown OS.
+     */
+    private final String libdir;
 
     /**
      * {@code true} if this OS is a kind of Unix.
@@ -54,8 +72,9 @@ public enum OS {
     /**
      * Creates a new enumeration.
      */
-    private OS(final boolean unix) {
-        this.unix = unix;
+    private OS(final String libdir, final boolean unix) {
+        this.libdir = libdir;
+        this.unix   = unix;
     }
 
     /**
@@ -72,4 +91,59 @@ public enum OS {
         }
         return UNKNOWN;
     }
+
+    /**
+     * Loads the native library of the given name from the JAR file of the given class.
+     * This method searches for a resource in the {@code /native/<os>} directory where
+     * {@code <os>} is {@code windows}, {@code darwin} or {@code linux}.
+     *
+     * @param  caller  a class in the JAR file where to look for native resources.
+     * @param  name    the native library name without {@code ".so"} or {@code ".dll"} extension.
+     * @throws UnsatisfiedLinkError if the native library can not be loaded for the current
OS.
+     *
+     * @see System#load(String)
+     */
+    public static void load(final Class<?> caller, final String name) {
+        try {
+            System.load(current().nativeLibrary(caller.getClassLoader(), name));
+        } catch (IOException | SecurityException e) {
+            throw (UnsatisfiedLinkError) new UnsatisfiedLinkError(e.getMessage()).initCause(e);
+        }
+    }
+
+    /**
+     * Returns an absolute path to the library of the given name in the JAR file.
+     * If the resources can not be accessed by an absolute path, then this method
+     * copies the resource in a temporary file.
+     *
+     * @param  loader  the loader of the JAR file where to look for native resources.
+     * @param  name    the native library name without {@code ".so"} or {@code ".dll"} extension.
+     * @return absolute path to the library (may be a temporary file).
+     * @throws IOException if an error occurred while copying the library to a temporary
file.
+     * @throws SecurityException if the security manager denies loading resource, creating
absolute path, <i>etc</i>.
+     * @throws UnsatisfiedLinkError if no native resource has been found for the current
OS.
+     *
+     * @see System#load(String)
+     */
+    private String nativeLibrary(final ClassLoader loader, String name) throws IOException
{
+        if (libdir != null) {
+            final String ext = unix ? ".so" : ".dll";
+            name = "native/" + libdir + '/' + name + ext;
+            final URL res = loader.getResource(name);
+            if (res != null) {
+                try {
+                    return new File(res.toURI()).getAbsolutePath();
+                } catch (IllegalArgumentException | URISyntaxException e) {
+                    Logging.recoverableException(Logging.getLogger(Loggers.SYSTEM), OS.class,
"nativeLibrary", e);
+                }
+                final Path tmp = Files.createTempFile(name, ext).toAbsolutePath();
+                tmp.toFile().deleteOnExit();
+                try (InputStream in = res.openStream()) {
+                    Files.copy(in, tmp, StandardCopyOption.REPLACE_EXISTING);
+                }
+                return tmp.toString();
+            }
+        }
+        throw new UnsatisfiedLinkError(Errors.format(Errors.Keys.NativeInterfacesNotFound_2,
System.getProperty("os.name"), name));
+    }
 }

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java?rev=1801760&r1=1801759&r2=1801760&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
[UTF-8] Wed Jul 12 17:10:00 2017
@@ -556,6 +556,11 @@ public final class Errors extends Indexe
         public static final short MutuallyExclusiveOptions_2 = 91;
 
         /**
+         * Native interfaces “{1}” not available for the {0} platform.
+         */
+        public static final short NativeInterfacesNotFound_2 = 176;
+
+        /**
          * Argument ‘{0}’ shall not be negative. The given value was {1}.
          */
         public static final short NegativeArgument_2 = 92;

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties?rev=1801760&r1=1801759&r2=1801760&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
[ISO-8859-1] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
[ISO-8859-1] Wed Jul 12 17:10:00 2017
@@ -121,6 +121,7 @@ MissingValueForProperty_1         = Miss
 MissingValueInColumn_1            = Missing value in the \u201c{0}\u201d column.
 MultiOccurenceValueAtIndices_3    = Can not return a single value for \u201c{0}\u201d because
there is at least two occurrences, at indices {1} and {2}.
 MutuallyExclusiveOptions_2        = Options \u201c{0}\u201d and \u201c{1}\u201d are mutually
exclusive.
+NativeInterfacesNotFound_2        = Native interfaces \u201c{1}\u201d not available for the
{0} platform.
 NegativeArgument_2                = Argument \u2018{0}\u2019 shall not be negative. The given
value was {1}.
 NegativeArrayLength_1             = Can not create a \u201c{0}\u201d array of negative length.
 NestedElementNotAllowed_1         = Nested \u201c{0}\u201d elements are not allowed.

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties?rev=1801760&r1=1801759&r2=1801760&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
[ISO-8859-1] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
[ISO-8859-1] Wed Jul 12 17:10:00 2017
@@ -118,6 +118,7 @@ MissingValueForProperty_1         = Aucu
 MissingValueInColumn_1            = Il manque une valeur dans la colonne \u00ab\u202f{0}\u202f\u00bb.
 MultiOccurenceValueAtIndices_3    = Ne peut pas retourner une valeur unique pour \u00ab\u202f{0}\u202f\u00bb
parce qu\u2019il y a au moins deux occurrences, aux index {1} et {2}.
 MutuallyExclusiveOptions_2        = Les options \u00ab\u202f{0}\u202f\u00bb et \u00ab\u202f{1}\u202f\u00bb
sont mutuellement exclusives.
+NativeInterfacesNotFound_2        = Les interfaces natives \u00ab\u202f{1}\u202f\u00bb ne
sont pas disponibles pour la plateforme {0}.
 NegativeArgument_2                = L\u2019argument \u2018{0}\u2019 ne doit pas \u00eatre
n\u00e9gatif. La valeur donn\u00e9e \u00e9tait {1}.
 NegativeArrayLength_1             = Ne peut pas cr\u00e9er un tableau \u00ab\u202f{0}\u202f\u00bb
de longueur n\u00e9gative.
 NestedElementNotAllowed_1         = L\u2019imbrication d\u2019\u00e9l\u00e9ments \u00ab\u202f{0}\u202f\u00bb
n\u2019est pas autoris\u00e9e.

Modified: sis/branches/JDK8/ide-project/NetBeans/build.xml
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/ide-project/NetBeans/build.xml?rev=1801760&r1=1801759&r2=1801760&view=diff
==============================================================================
--- sis/branches/JDK8/ide-project/NetBeans/build.xml (original)
+++ sis/branches/JDK8/ide-project/NetBeans/build.xml Wed Jul 12 17:10:00 2017
@@ -60,7 +60,7 @@
       </fileset>
       -->
 
-      <!-- Other resources (properties files, SQL scripts). -->
+      <!-- Other resources (properties files, SQL scripts, native libraries). -->
       <fileset dir="${project.root}/core/sis-utility/src/main/resources">
         <include name="**/*.properties"/>
       </fileset>
@@ -73,6 +73,10 @@
       <fileset dir="${project.root}/storage/sis-xmlstore/src/main/resources">
         <include name="**/*.properties"/>
       </fileset>
+      <fileset dir="${project.root}/storage/sis-gdal/src/main/resources">
+        <include name="**/*.so"/>
+        <include name="**/*.dll"/>
+      </fileset>
       <fileset dir="${project.root}/application/sis-console/src/main/resources">
         <include name="**/*.properties"/>
       </fileset>

Modified: sis/branches/JDK8/storage/sis-gdal/src/main/c/Makefile
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-gdal/src/main/c/Makefile?rev=1801760&r1=1801759&r2=1801760&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-gdal/src/main/c/Makefile (original)
+++ sis/branches/JDK8/storage/sis-gdal/src/main/c/Makefile Wed Jul 12 17:10:00 2017
@@ -71,5 +71,7 @@ ifndef OUTDIR
     $(error The $(OS) platform is not supported by this makefile.)
 endif
 
+# The $(LIB) parameter should be last (it way work anywhere,
+# but putting it last allows more predictable results).
 all:
-	gcc $(FLAG) $(INCLUDE_DIR) $(LIB) -o ../resources/native/$(OUTDIR)/$(OUT) $(SRC)
+	gcc $(FLAG) $(INCLUDE_DIR) -o ../resources/native/$(OUTDIR)/$(OUT) $(SRC) $(LIB)

Modified: sis/branches/JDK8/storage/sis-gdal/src/main/java/org/apache/sis/storage/gdal/PJ.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-gdal/src/main/java/org/apache/sis/storage/gdal/PJ.java?rev=1801760&r1=1801759&r2=1801760&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-gdal/src/main/java/org/apache/sis/storage/gdal/PJ.java [UTF-8]
(original)
+++ sis/branches/JDK8/storage/sis-gdal/src/main/java/org/apache/sis/storage/gdal/PJ.java [UTF-8]
Wed Jul 12 17:10:00 2017
@@ -19,19 +19,19 @@ package org.apache.sis.storage.gdal;
 import java.util.Objects;
 import java.lang.annotation.Native;
 import org.opengis.referencing.operation.TransformException;
+import org.apache.sis.internal.system.OS;
 
 
 /**
  * Wraps the <a href="http://proj.osgeo.org/">Proj4</a> {@code PJ} native data
structure.
- * Almost every methods defined in this class are native methods delegating the work to the
- * Proj4 library. This class is the only place where such native methods are defined.
+ * Almost every methods defined in this class are native methods delegating the work to the
Proj4 library.
+ * This class is the only place where such native methods are defined.
  *
  * <p>In the Proj4 library, the {@code PJ} structure aggregates in a single place information
usually
  * splitted in many different ISO 19111 interfaces: {@link org.opengis.referencing.datum.Ellipsoid},
  * {@link org.opengis.referencing.datum.Datum}, {@link org.opengis.referencing.datum.PrimeMeridian},
  * {@link org.opengis.referencing.cs.CoordinateSystem}, {@link org.opengis.referencing.crs.CoordinateReferenceSystem}
- * and their sub-interfaces. The relationship with the GeoAPI methods is indicated in the
- * "See" tags when appropriate.</p>
+ * and their sub-interfaces. The relationship with the GeoAPI methods is indicated in the
"See" tags when appropriate.</p>
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @version 0.8
@@ -48,9 +48,12 @@ class PJ {
 
     /**
      * Loads the Proj4 library.
+     * This static initializer may throw a {@link UnsatisfiedLinkError} if the static library
can not be loaded.
+     * In such case, any future attempt to use this {@code PJ} class will cause a {@link
NoClassDefFoundError}
+     * as per Java language specification.
      */
     static {
-        System.load("libproj-binding.so");
+        OS.load(PJ.class, "libproj-binding");
     }
 
     /**

Modified: sis/branches/JDK8/storage/sis-gdal/src/test/java/org/apache/sis/storage/gdal/PJTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-gdal/src/test/java/org/apache/sis/storage/gdal/PJTest.java?rev=1801760&r1=1801759&r2=1801760&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-gdal/src/test/java/org/apache/sis/storage/gdal/PJTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-gdal/src/test/java/org/apache/sis/storage/gdal/PJTest.java
[UTF-8] Wed Jul 12 17:10:00 2017
@@ -17,9 +17,12 @@
 package org.apache.sis.storage.gdal;
 
 import org.opengis.referencing.operation.TransformException;
+import org.apache.sis.test.TestCase;
+import org.junit.BeforeClass;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
+import static org.junit.Assume.*;
 
 
 /**
@@ -30,7 +33,27 @@ import static org.junit.Assert.*;
  * @since   0.8
  * @module
  */
-public class PJTest {
+public final strictfp class PJTest extends TestCase {
+    /**
+     * If the Proj4 library has been successfully initialized, an empty string.
+     * Otherwise, the reason why the library is not available.
+     */
+    private static String status;
+
+    /**
+     * Verifies if the Proj4 library is available.
+     */
+    @BeforeClass
+    public static synchronized void verifyNativeLibraryAvailability() {
+        if (status == null) try {
+            out.println("Proj.4 version: " + PJ.getVersion());
+            status = "";
+        } catch (UnsatisfiedLinkError e) {
+            status = e.toString();
+        }
+        assumeTrue(status, status.isEmpty());
+    }
+
     /**
      * Ensures that the given object is the WGS84 definition.
      */
@@ -72,10 +95,15 @@ public class PJTest {
      *
      * @throws TransformException should never happen.
      */
-    @Test(expected = NullPointerException.class)
+    @Test
     public void testNullPointerException() throws TransformException {
         final PJ pj = new PJ("+proj=latlong +datum=WGS84");
-        pj.transform(null, 2, null, 0, 1);
+        try {
+            pj.transform(null, 2, null, 0, 1);
+            fail("Expected an exception to be thrown.");
+        } catch (NullPointerException e) {
+            // This is the expected exception.
+        }
     }
 
     /**
@@ -84,10 +112,15 @@ public class PJTest {
      *
      * @throws TransformException should never happen.
      */
-    @Test(expected = IndexOutOfBoundsException.class)
+    @Test
     public void testIndexOutOfBoundsException() throws TransformException {
         final PJ pj = new PJ("+proj=latlong +datum=WGS84");
-        pj.transform(pj, 2, new double[5], 2, 2);
+        try {
+            pj.transform(pj, 2, new double[5], 2, 2);
+            fail("Expected an exception to be thrown.");
+        } catch (IndexOutOfBoundsException e) {
+            // This is the expected exception.
+        }
     }
 
     /**

Added: sis/branches/JDK8/storage/sis-gdal/src/test/java/org/apache/sis/test/suite/GDALTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-gdal/src/test/java/org/apache/sis/test/suite/GDALTestSuite.java?rev=1801760&view=auto
==============================================================================
--- sis/branches/JDK8/storage/sis-gdal/src/test/java/org/apache/sis/test/suite/GDALTestSuite.java
(added)
+++ sis/branches/JDK8/storage/sis-gdal/src/test/java/org/apache/sis/test/suite/GDALTestSuite.java
[UTF-8] Wed Jul 12 17:10:00 2017
@@ -0,0 +1,45 @@
+/*
+ * 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.sis.test.suite;
+
+import org.apache.sis.test.TestSuite;
+import org.junit.BeforeClass;
+import org.junit.runners.Suite;
+
+
+/**
+ * All tests from the {@code sis-gdal} module, in approximative dependency order.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @version 0.8
+ * @since   0.8
+ * @module
+ */
+@Suite.SuiteClasses({
+    org.apache.sis.storage.gdal.PJTest.class
+})
+public final strictfp class GDALTestSuite extends TestSuite {
+    /**
+     * Verifies the list of tests before to run the suite.
+     * See {@link #verifyTestList(Class, Class[])} for more information.
+     */
+    @BeforeClass
+    public static void verifyTestList() {
+        assertNoMissingTest(GDALTestSuite.class);
+        verifyTestList(GDALTestSuite.class);
+    }
+}

Propchange: sis/branches/JDK8/storage/sis-gdal/src/test/java/org/apache/sis/test/suite/GDALTestSuite.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK8/storage/sis-gdal/src/test/java/org/apache/sis/test/suite/GDALTestSuite.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8



Mime
View raw message