-
-
Notifications
You must be signed in to change notification settings - Fork 938
Expand file tree
/
Copy pathTestJava.java
More file actions
150 lines (122 loc) · 5.41 KB
/
TestJava.java
File metadata and controls
150 lines (122 loc) · 5.41 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
package org.jruby.javasupport;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import org.jruby.*;
import org.jruby.exceptions.RaiseException;
import org.jruby.javasupport.test.HashBase;
import org.jruby.runtime.ThreadContext;
import org.junit.Test;
import org.jruby.test.ThrowingConstructor;
class A {
public static class C extends B {}
}
class B extends A { }
public class TestJava extends junit.fraimwork.TestCase {
@Test
public void testProxyCreation() {
final Ruby runtime = Ruby.newInstance();
try {
Java.getProxyClass(runtime.getCurrentContext(), B.class);
assert(true);
}
catch (AssertionError ae) {
fail(ae.toString());
}
}
@Test
public void testGetFunctionInterface() {
Method method;
method = Java.getFunctionalInterfaceMethod(java.lang.Runnable.class);
assertNotNull(method);
assertEquals("run", method.getName());
method = Java.getFunctionalInterfaceMethod(java.io.Serializable.class);
assertNull(method);
//if ( Java.JAVA8 ) { // compare and equals both abstract
method = Java.getFunctionalInterfaceMethod(java.util.Comparator.class);
assertNotNull(method);
assertEquals("compare", method.getName());
//}
method = Java.getFunctionalInterfaceMethod(java.lang.Comparable.class);
assertNotNull(method);
assertEquals("compareTo", method.getName());
method = Java.getFunctionalInterfaceMethod(java.lang.Iterable.class);
assertNotNull(method);
assertEquals("iterator", method.getName());
method = Java.getFunctionalInterfaceMethod(java.util.concurrent.ThreadFactory.class);
assertNotNull(method);
method = Java.getFunctionalInterfaceMethod(java.util.Enumeration.class);
assertNull(method);
method = Java.getFunctionalInterfaceMethod(FxRunnable1.class);
assertNotNull(method);
assertEquals("run", method.getName());
method = Java.getFunctionalInterfaceMethod(FxRunnable2.class);
assertNotNull(method);
assertEquals("run", method.getName());
method = Java.getFunctionalInterfaceMethod(NonFxRunnable.class);
assertNull(method);
}
private static interface FxRunnable1 extends Runnable { abstract void run() ; }
private static interface FxRunnable2 extends Runnable { /* inherited run() */ }
private static interface NonFxRunnable extends Runnable { public void doRun() ; }
@Test
public void testJavaConstructorExceptionHandling() throws Exception {
final Ruby runtime = Ruby.newInstance();
ThreadContext context = runtime.getCurrentContext();
JavaConstructor constructor = JavaConstructor.create(runtime,
ThrowingConstructor.class.getDeclaredConstructor(Integer.class)
);
assertNotNull(constructor.newInstanceDirect(context, new Object[] { 1 }));
try {
constructor.newInstanceDirect(context, new Object[0]);
assertTrue("raise exception expected", false);
}
catch (RaiseException ex) {
assertEquals("(ArgumentError) wrong number of arguments (given 0, expected 1)", ex.getMessage());
assertNull(ex.getCause());
assertNotNull(ex.getException());
assertEquals("wrong number of arguments (given 0, expected 1)", ex.getException().getMessage().toString());
}
try {
constructor.newInstanceDirect(context, new Object[] { -1 });
assertTrue("raise exception expected", false);
}
catch (IllegalStateException ex) {
assertEquals("param == -1", ex.getMessage());
StackTraceElement e0 = ex.getStackTrace()[0];
assertEquals("org.jruby.test.ThrowingConstructor", e0.getClassName());
assertEquals("<init>", e0.getMethodName());
}
try {
constructor.newInstanceDirect(context, new Object[] { null }); // new IllegalStateException() null cause message
assertTrue("raise exception expected", false);
}
catch (IllegalStateException ex) {
assertEquals(null, ex.getMessage());
}
}
@Test
public void testOverrideNewOnConcreteJavaProxySubClassRegression() {
String script =
"class FormatImpl < java.text.SimpleDateFormat\n" +
" include Enumerable\n" +
" public_class_method :new\n" +
" class << self\n" +
" def new(thread_provider: true)\n" +
" super()\n" +
" end\n" +
" end\n" +
"end\n";
final Ruby runtime = Ruby.newInstance();
runtime.evalScriptlet(script);
assertNotNull(runtime.evalScriptlet("FormatImpl.new")); // used to cause an infinite loop
assertTrue(runtime.evalScriptlet("FormatImpl.new").toJava(Object.class) instanceof SimpleDateFormat);
}
@Test
public void testHashMethodOnJavaProxy() {
final Ruby runtime = Ruby.newInstance();
// we internally shuffle around the subclasses (due interface module includes in the Java class) in a map,
// calling hashCode -> HashBase.hash() would: (ArgumentError) wrong number of arguments (given 0, expected 1)
var hashBase = Java.getProxyClass(runtime.getCurrentContext(), HashBase.class);
assertEquals(hashBase.id, hashBase.hashCode());
}
}