File: test_jvmfinder.py

package info (click to toggle)
python-jpype 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,984 kB
  • sloc: python: 18,767; cpp: 17,931; java: 8,448; xml: 1,305; makefile: 154; sh: 35
file content (188 lines) | stat: -rw-r--r-- 8,056 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# *****************************************************************************
#
#   Licensed 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.
#
#   See NOTICE file for details.
#
# *****************************************************************************
# part of JPype1; author Martin K. Scherer; 2014


import unittest
from unittest import mock
import common
import os

import jpype._jvmfinder
from jpype._jvmfinder import *


class JVMFinderTest(unittest.TestCase):
    """
    test some methods to obtain a jvm.
    TODO: add windows (need to mock registry)
    """

    def test_find_libjvm(self):
        """
        test JVMFinder.find_libjvm does not return broken jvm implementation.
        """
        walk_fake = [('jre', ('lib',), ()),
                     ('jre/lib', ('amd64',), ()),
                     ('jre/lib/amd64',
                      ('cacao', 'jamvm', 'server'), ()),
                     ('jre/lib/amd64/cacao',
                      ('',), ('libjvm.so',)),
                     ('jre/lib/amd64/jamvm',
                      ('',), ('libjvm.so',)),
                     ('jre/lib/amd64/server',
                      ('',), ('libjvm.so',)),
                     ]
        with mock.patch('os.walk') as mockwalk:
            # contains broken and working jvms
            mockwalk.return_value = walk_fake

            finder = jpype._jvmfinder.LinuxJVMFinder()
            p = finder.find_libjvm('arbitrary java home')
            self.assertEqual(
                p, os.path.join('jre/lib/amd64/server', 'libjvm.so'), 'wrong jvm returned')

        with mock.patch('os.walk') as mockwalk:
            # contains only broken jvms, since server impl is removed
            walk_fake[-1] = ((), (), (),)
            mockwalk.return_value = walk_fake

            finder = jpype._jvmfinder.LinuxJVMFinder()
            with self.assertRaises(JVMNotSupportedException) as context:
                finder.find_libjvm('arbitrary java home')

    @mock.patch('os.walk')
    @mock.patch('os.path.exists')
    @mock.patch('os.path.realpath')
    def test_get_from_bin(self, mock_real_path, mock_path_exists, mock_os_walk):
        """
        test _get_from_bin method (used in linux and darwin)
        '/usr/bin/java' => some jre/jdk path
        """
        java_path = '/usr/lib/jvm/java-6-openjdk-amd64/bin/java'

        mock_os_walk.return_value = [
            ('/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/amd64/server', ('',), ('libjvm.so',))]
        mock_path_exists.return_value = True
        mock_real_path.return_value = '/usr/lib/jvm/java-6-openjdk-amd64/bin/java'

        finder = jpype._jvmfinder.LinuxJVMFinder()
        p = finder._get_from_bin()

        self.assertEqual(
            p, os.path.join('/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/amd64/server', 'libjvm.so'))

    @mock.patch('platform.mac_ver')
    def testDarwinBinary(self, mock_mac_ver):
        # this version has java_home binary
        mock_mac_ver.return_value = ('10.6.8', '', '')

        expected = '/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home\n'

        finder = jpype._jvmfinder.DarwinJVMFinder()

        # fake check_output
        with mock.patch('subprocess.check_output') as mock_checkoutput:
            mock_checkoutput.return_value = expected
            p = finder._javahome_binary()
            self.assertEqual(p, expected.strip())

        # this version has no java_home binary
        mock_mac_ver.return_value = ('10.5', '', '')
        p = finder._javahome_binary()
        self.assertEqual(p, None)

    # FIXME this is testing the details of the implementation rather than the results.
    # it is included only for coverage purposes.  Revise this to be a more meaningful test
    # next time it breaks.
    # FIXME this test does passes locally but not in the CI.  Disabling for now.
    @common.unittest.skip  # type: ignore
    def testPlatform(self):
        with mock.patch('jpype._jvmfinder.sys') as mocksys, mock.patch('jpype._jvmfinder.WindowsJVMFinder') as finder:
            mocksys.platform = 'win32'
            jpype._jvmfinder.getDefaultJVMPath()
            self.assertIn(finder().get_jvm_path, finder.mock_calls)

        with mock.patch('jpype._jvmfinder.sys') as mocksys, mock.patch('jpype._jvmfinder.LinuxJVMFinder') as finder:
            mocksys.platform = 'linux'
            getDefaultJVMPath()
            self.assertIn(finder().get_jvm_path, finder.mock_calls)

        with mock.patch('jpype._jvmfinder.sys') as mocksys, mock.patch('jpype._jvmfinder.DarwinJVMFinder') as finder:
            mocksys.platform = 'darwin'
            getDefaultJVMPath()
            self.assertIn(finder().get_jvm_path, finder.mock_calls)

    def testLinuxGetFromBin(self):
        finder = jpype._jvmfinder.LinuxJVMFinder()

        def f(s):
            return s
        with mock.patch('os.path') as pathmock, \
                mock.patch('jpype._jvmfinder.JVMFinder.find_libjvm') as mockfind:
            pathmock.exists.return_value = True
            pathmock.realpath = f
            pathmock.abspath = f
            pathmock.dirname = os.path.dirname
            pathmock.join = os.path.join
            finder._get_from_bin()
            self.assertEqual(
                pathmock.dirname.mock_calls[0][1], (finder._java,))

    # FIXME this test is faking files using the mock system.  Replace it with stub
    # files so that we have a more accurate test rather than just testing the implementation.
    # FIXME this fails in the CI but works locally.   Disabling this for now.
    @common.unittest.skip  # type: ignore
    def testCheckArch(self):
        import struct
        with mock.patch("builtins.open", mock.mock_open(read_data="data")) as mock_file, \
                self.assertRaises(JVMNotSupportedException):
            jpype._jvmfinder._checkJVMArch('path', 2**32)

        data = struct.pack('<ccIH', b'M', b'Z', 0, 332)
        with mock.patch("builtins.open", mock.mock_open(read_data=data)) as mock_file:
            jpype._jvmfinder._checkJVMArch('path', 2**32)
        with mock.patch("builtins.open", mock.mock_open(read_data=data)) as mock_file, \
                self.assertRaises(JVMNotSupportedException):
            jpype._jvmfinder._checkJVMArch('path', 2**64)

        data = struct.pack('<ccIH', b'M', b'Z', 0, 512)
        with mock.patch("builtins.open", mock.mock_open(read_data=data)) as mock_file:
            jpype._jvmfinder._checkJVMArch('path', 2**64)
        with mock.patch("builtins.open", mock.mock_open(read_data=data)) as mock_file, \
                self.assertRaises(JVMNotSupportedException):
            jpype._jvmfinder._checkJVMArch('path', 2**32)

        data = struct.pack('<ccIH', b'M', b'Z', 0, 34404)
        with mock.patch("builtins.open", mock.mock_open(read_data=data)) as mock_file:
            jpype._jvmfinder._checkJVMArch('path', 2**64)
        with mock.patch("builtins.open", mock.mock_open(read_data=data)) as mock_file, \
                self.assertRaises(JVMNotSupportedException):
            jpype._jvmfinder._checkJVMArch('path', 2**32)

    def testWindowsRegistry(self):
        finder = jpype._jvmfinder.WindowsJVMFinder()
        with mock.patch("jpype._jvmfinder.winreg") as winregmock:
            winregmock.QueryValueEx.return_value = ('success', '')
            self.assertEqual(finder._get_from_registry(), 'success')
            winregmock.OpenKey.side_effect = OSError()
            self.assertEqual(finder._get_from_registry(), None)


if __name__ == '__main__':
    unittest.main()