|
| 1 | +# |
| 2 | +# GeoAPI - Programming interfaces for OGC/ISO standards |
| 3 | +# Copyright © 2026 Open Geospatial Consortium, Inc. |
| 4 | +# http://www.geoapi.org |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | + |
| 19 | +import jpy |
| 20 | + |
| 21 | +import unittest |
| 22 | +import logging |
| 23 | + |
| 24 | +logger = logging.getLogger(__name__) |
| 25 | + |
| 26 | +class TestMetadata(unittest.TestCase): |
| 27 | + |
| 28 | + def test_jvm(self): |
| 29 | + """ |
| 30 | + Test that the JVM is well initialized and that classes can be loaded. |
| 31 | + :return: |
| 32 | + """ |
| 33 | + java_system = jpy.get_type("java.lang.System") |
| 34 | + self.assertIsNotNone(java_system, "Java system not loaded") |
| 35 | + jRuntime = jpy.get_type("java.lang.Runtime") |
| 36 | + self.assertIsNotNone(jRuntime, "Java Runtime class not loaded") |
| 37 | + java_version = jRuntime.version() |
| 38 | + self.assertIsNotNone(java_version, "Java version of initialized jvm is None.") |
| 39 | + |
| 40 | + # Load java standard Math class |
| 41 | + jMath = jpy.get_type("java.lang.Math") |
| 42 | + self.assertIsNotNone(jMath, "Java Math class not loaded") |
| 43 | + self.assertEqual(jMath.cos(0), 1, "Java Math cos fails to compute cos(0)") |
| 44 | + |
| 45 | + def test_metadata(self): |
| 46 | + |
| 47 | + # Get java ClassLoader to check tested java classes are well configured in running JVM |
| 48 | + class_loader = jpy.get_type("java.lang.ClassLoader") |
| 49 | + self.assertIsNotNone(class_loader, "Java class loader not loaded.") |
| 50 | + # Get the system class loader (the one with your classpath) |
| 51 | + system_loader = class_loader.getSystemClassLoader() |
| 52 | + self.assertIsNotNone(system_loader, "Java class loader not loaded.") |
| 53 | + |
| 54 | + # Check access to project's java source ; If it fails ensure classpath is well set in ./init.py |
| 55 | + cls_url = system_loader.getResource("org/opengis/bridge/python/PythonHelper.class") |
| 56 | + self.assertIsNotNone(cls_url, "PythonHelper class not accessible in jvm classpath.") |
| 57 | + cls = system_loader.loadClass("org.opengis.bridge.python.PythonHelper") |
| 58 | + self.assertIsNotNone(cls, "PythonHelper class could not be loaded.") |
| 59 | + logger.info("PythonHelper loaded successfully : " +str(cls)) |
| 60 | + |
| 61 | + # Check access to project's java test source ; If it fails ensure classpath is well set in ./init.py |
| 62 | + cls_url = system_loader.getResource("org/opengis/bridge/python/PythonBridgeHelper.class") |
| 63 | + self.assertIsNotNone(cls_url, "PythonBridgeTestHelper class not accessible in jvm classpath.") |
| 64 | + cls = system_loader.loadClass("org.opengis.bridge.python.PythonBridgeHelper") |
| 65 | + self.assertIsNotNone(cls, "PythonBridgeTestHelper class could not be loaded.") |
| 66 | + logger.info("PythonBridgeTestHelper loaded successfully : " +str(cls)) |
| 67 | + |
| 68 | + # Check access to geoapi core module |
| 69 | + metadata_class = jpy.get_type('org.opengis.metadata.Metadata') |
| 70 | + logger.info("Metadata loaded successfully from geoapi dependency : " +str(metadata_class)) |
| 71 | + |
| 72 | + self._test_helper = jpy.get_type('org.opengis.bridge.python.PythonBridgeHelper') |
| 73 | + self.assertIsNotNone(self._test_helper, "Failed to get python binding for PythonBridgeHelper class.") |
| 74 | + logger.info("Java PythonBridgeTestHelper helper class accessed from python.") |
| 75 | + |
| 76 | + |
| 77 | + metadata_ = self._test_helper.metadata() |
| 78 | + self.assertIsNotNone(metadata_, "Java PythonBridgeHelper#metadata run but returned None.") |
| 79 | + logger.info("Python binding instantiate for java metadata :"+str(metadata_)) |
| 80 | + responsibilities_ = metadata_.getContacts() |
| 81 | + self.assertIsNotNone(responsibilities_, "Failed to obtain from python metadata") |
| 82 | + logger.info("Contacts obtained from python metadata : "+str(responsibilities_)) |
0 commit comments