java_dir_test.py
4.55 KB
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
from __future__ import unicode_literals, absolute_import
from py4j.java_gateway import (
java_import, UserHelpAutoCompletion)
from py4j.protocol import Py4JError
from py4j.tests.java_gateway_test import (
example_app_process, gateway)
ExampleClassFields = sorted([
"field10",
"field11",
"field20",
"field21",
"static_field"
])
ExampleClassMethods = sorted([
# From ExampleClass
"method1",
"method2",
"method3",
"method4",
"method5",
"method6",
"sleepFirstTimeOnly",
# overloaded
"method7",
"method8",
"method9",
# overloaded
"method10",
"method11",
"getList",
"getField1",
"setField1",
"getStringArray",
"getIntArray",
"callHello",
"callHello2",
"static_method",
"getInteger",
"getBrokenStream",
"getStream",
# From Object
"getClass",
"hashCode",
"equals",
"toString",
"notify",
"notifyAll",
"wait"
])
ExampleClassStatics = sorted([
"StaticClass",
"static_field",
"static_method"
])
def test_dir_object():
with example_app_process():
with gateway() as g:
ex = g.getNewExample()
print(sorted(dir(ex)))
print(ExampleClassMethods)
assert sorted(dir(ex)) == ExampleClassMethods
def test_dir_object_fields():
with example_app_process():
with gateway(auto_field=True) as g:
ex = g.getNewExample()
assert sorted(dir(ex)) == sorted(
ExampleClassMethods + ExampleClassFields)
def test_dir_object_shows_manually_called_after_dir():
with example_app_process():
with gateway() as g:
ex = g.getNewExample()
assert sorted(dir(ex)) == ExampleClassMethods
try:
ex.does_not_exist_in_example()
raise AssertionError("Method should not have succeeded")
except Py4JError:
pass
# Make sure the manually called method now shows up
assert sorted(dir(ex)) == sorted(
ExampleClassMethods + ["does_not_exist_in_example"])
def test_dir_object_shows_manually_called_before_dir():
with example_app_process():
with gateway() as g:
ex = g.getNewExample()
try:
ex.does_not_exist_in_example()
raise AssertionError("Method should not have succeeded")
except Py4JError:
pass
# Make sure the manually called method now shows up
assert sorted(dir(ex)) == sorted(
ExampleClassMethods + ["does_not_exist_in_example"])
def test_dir_class():
with example_app_process():
with gateway() as g:
exclass = g.jvm.py4j.examples.ExampleClass
assert sorted(dir(exclass)) == ExampleClassStatics
def helper_dir_jvmview(view):
assert sorted(dir(view)) == [UserHelpAutoCompletion.KEY]
java_import(view, "com.example.Class1")
java_import(view, "com.another.Class2")
assert sorted(dir(view)) == [
UserHelpAutoCompletion.KEY, "Class1", "Class2"]
assert sorted(dir(view)) == [
UserHelpAutoCompletion.KEY, "Class1", "Class2"]
java_import(view, "com.third.Class3")
assert sorted(dir(view)) == [
UserHelpAutoCompletion.KEY, "Class1", "Class2", "Class3"]
def test_dir_jvmview_default():
with example_app_process():
with gateway() as g:
helper_dir_jvmview(g.jvm)
def test_dir_jvmview_new():
with example_app_process():
with gateway() as g:
view = g.new_jvm_view()
helper_dir_jvmview(view)
def test_dir_jvmview_two():
with example_app_process():
with gateway() as g:
view1 = g.new_jvm_view()
view2 = g.new_jvm_view()
helper_dir_jvmview(view1)
helper_dir_jvmview(view2)
# now give them different contents
java_import(view1, "com.fourth.Class4")
java_import(view2, "com.fiftg.Class5")
assert sorted(dir(view1)) == [
UserHelpAutoCompletion.KEY, "Class1", "Class2", "Class3",
"Class4"]
assert sorted(dir(view2)) == [
UserHelpAutoCompletion.KEY, "Class1", "Class2", "Class3",
"Class5"]
def test_dir_package():
with example_app_process():
with gateway() as g:
assert sorted(dir(g.jvm)) == [UserHelpAutoCompletion.KEY]
assert sorted(dir(g.jvm.java)) == [UserHelpAutoCompletion.KEY]
assert sorted(dir(g.jvm.java.util)) == [UserHelpAutoCompletion.KEY]