Debian Codegraph @

test/jpypetest/test_jvmfinder.py

raw symbols SCIP
1 # *****************************************************************************
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 # See NOTICE file for details.
16 #
17 # *****************************************************************************
18 # part of JPype1; author Martin K. Scherer; 2014
19 # from unittest import mock
20 import os
21 import pathlib
22 import sys
23 import unittest
24 from unittest import mock
25
26 from jpype._jvmfinder import (LinuxJVMFinder, JVMNotSupportedException, DarwinJVMFinder,
27 WindowsJVMFinder)
28
29
30 class JVMFinderTest(unittest.TestCase):
31 """
32 test some methods to obtain a jvm.
33 TODO: add windows (need to mock registry)
34 """
35
36 def test_find_libjvm(self):
37 """
38 test JVMFinder.find_libjvm does not return broken jvm implementation.
39 """
40 walk_fake = [('jre', ('lib',), ()),
41 ('jre/lib', ('amd64',), ()),
42 ('jre/lib/amd64',
43 # cacoa and jamvm are not supported.
44 ('cacao', 'jamvm', 'server'), ()),
45 ('jre/lib/amd64/cacao',
46 ('',), ('libjvm.so',)),
47 ('jre/lib/amd64/jamvm',
48 ('',), ('libjvm.so',)),
49 ('jre/lib/amd64/server',
50 ('',), ('libjvm.so',)),
51 ]
52 with mock.patch('os.walk') as mockwalk:
53 # contains broken and working jvms
54 mockwalk.return_value = walk_fake
55
56 finder = LinuxJVMFinder()
57 p = finder.find_libjvm('arbitrary java home')
58 # cacoa and javmvm should not be found
59 self.assertEqual(
60 p, os.path.join('jre/lib/amd64/server', 'libjvm.so'), 'wrong jvm returned')
61
62 def test_find_libjvm_unsupported_jvm(self):
63 """Checks for the case only unsupported JVM impls are being found."""
64 walk_fake = [('jre', ('lib',), ()),
65 ('jre/lib', ('amd64',), ()),
66 ('jre/lib/amd64',
67 # cacoa and jamvm are not supported.
68 ('cacao', 'jamvm', ), ()),
69 ('jre/lib/amd64/cacao',
70 ('',), ('libjvm.so',)),
71 ('jre/lib/amd64/jamvm',
72 ('',), ('libjvm.so',)),
73 ]
74 with mock.patch('os.walk') as mockwalk:
75 # contains broken and working jvms
76 mockwalk.return_value = walk_fake
77
78 finder = LinuxJVMFinder()
79 with self.assertRaises(JVMNotSupportedException):
80 finder.find_libjvm('arbitrary java home')
81
82 @mock.patch('os.walk')
83 @mock.patch('os.path.exists')
84 @mock.patch('os.path.realpath')
85 def test_get_from_bin(self, mock_real_path, mock_path_exists, mock_os_walk):
86 """
87 test _get_from_bin method (used in linux and darwin)
88 '/usr/bin/java' => some jre/jdk path
89 """
90 java_path = '/usr/lib/jvm/java-6-openjdk-amd64/bin/java'
91
92 mock_os_walk.return_value = [
93 ('/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/amd64/server', ('',), ('libjvm.so',))]
94 mock_path_exists.return_value = True
95 mock_real_path.return_value = '/usr/lib/jvm/java-6-openjdk-amd64/bin/java'
96
97 finder = LinuxJVMFinder()
98 p = finder._get_from_bin()
99
100 self.assertEqual(
101 p, os.path.join('/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/amd64/server', 'libjvm.so'))
102
103 @mock.patch('platform.mac_ver')
104 def testDarwinBinary(self, mock_mac_ver):
105 # this version has java_home binary
106 mock_mac_ver.return_value = ('10.6.8', '', '')
107
108 expected = '/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home\n'
109
110 finder = DarwinJVMFinder()
111
112 # fake check_output
113 with mock.patch('subprocess.check_output') as mock_checkoutput:
114 mock_checkoutput.return_value = expected
115 p = finder._javahome_binary()
116 self.assertEqual(p, expected.strip())
117
118 # this version has no java_home binary
119 mock_mac_ver.return_value = ('10.5', '', '')
120 p = finder._javahome_binary()
121 self.assertEqual(p, None)
122
123 def testLinuxGetFromBin(self):
124 finder = LinuxJVMFinder()
125
126 def f(s):
127 return s
128 with mock.patch('os.path') as pathmock, \
129 mock.patch('jpype._jvmfinder.JVMFinder.find_libjvm') as mockfind:
130 pathmock.exists.return_value = True
131 pathmock.realpath = f
132 pathmock.abspath = f
133 pathmock.dirname = os.path.dirname
134 pathmock.join = os.path.join
135 finder._get_from_bin()
136 self.assertEqual(
137 pathmock.dirname.mock_calls[0][1], (finder._java,))
138
139 @unittest.skipIf(sys.platform != "win", "only on windows")
140 def testWindowsRegistry(self):
141 finder = WindowsJVMFinder()
142 jvm_home = pathlib.Path(finder.get_jvm_path())
143 assert jvm_home.exists()
144 assert jvm_home.is_dir()
145
146 if __name__ == '__main__':
147 unittest.main()