3 ;;; Copyright (C) 2005 Peter Graves
4 ;;; $Id: java-tests.lisp,v 1.17 2005/11/07 19:49:36 asimon Exp $
6 ;;; This program is free software; you can redistribute it and/or
7 ;;; modify it under the terms of the GNU General Public License
8 ;;; as published by the Free Software Foundation; either version 2
9 ;;; of the License, or (at your option) any later version.
11 ;;; This program is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with this program; if not, write to the Free Software
18 ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 (load (merge-pathnames "test-utilities.lisp" *load-truename
*))
30 (use-package '#:javatools.jlinker
)
32 (use-package '#:javatools.jlinker
'#:cl-user
) ;; For convenience only.
33 #+(and allegro mswindows
)
34 (use-package '#:javatools.jlinker
'#:cg-user
) ;; For convenience only.
36 (load (merge-pathnames "jl-config.cl" *load-truename
*))
38 (or (jlinker-query) (jlinker-init))
41 (defmacro with-registered-exception
(exception condition
&body body
)
44 (register-java-exception ,exception
,condition
)
46 (unregister-java-exception ,exception
)))
49 (deftest java-object
.1
50 (class-name (find-class 'java-object nil
))
54 (jcall (jmethod "java.lang.Object" "toString") (jclass "java.lang.String"))
55 "class java.lang.String")
58 (equal (jcall (jmethod "java.lang.Object" "getClass") "foo")
59 (jclass "java.lang.String"))
64 (equal (jclass '|java.lang.String|
) (jclass "java.lang.String"))
68 (let ((class1 (jcall (jmethod "java.lang.Object" "getClass") "foo"))
69 (class2 (jclass "java.lang.String")))
70 (jcall (jmethod "java.lang.Object" "equals" "java.lang.Object")
75 (jcall (jmethod "java.lang.Object" "toString") (jclass "int"))
79 (equal (jclass '|int|
) (jclass "int"))
83 (deftest jclass.error
.1
84 (signals-error (jclass "foo") 'error
)
88 (deftest jclass.error
.2
89 (signals-error (jclass 42) 'error
)
98 (jclass-of "foo" "java.lang.String")
103 (jclass-of "foo" "bar")
117 (deftest jclass-name
.1
118 (jclass-name "java.lang.String")
121 (deftest jclass-name
.2
122 (signals-error (jclass-name "foo") 'error
)
125 (deftest jclass-name
.3
126 (signals-error (jclass-name 42) 'error
)
129 (deftest jclass-name
.4
130 (jclass-name (jclass "java.lang.String"))
133 (deftest jclass-name
.5
134 (jclass-name (jclass "java.lang.String") "java.lang.String")
138 (deftest jclass-name
.6
139 (jclass-name (jclass "java.lang.String") "java.lang.Object")
143 (deftest jclass-name
.7
144 (jclass-name (jclass "java.lang.String") "foo")
148 (deftest jclass-name
.8
149 (jclass-name (jclass "int"))
152 (deftest jconstructor
.1
153 (jclass-of (jconstructor "java.lang.String" "java.lang.String"))
154 "java.lang.reflect.Constructor"
155 "java.lang.reflect.Constructor")
158 (let ((constructor (jconstructor "java.lang.String" "java.lang.String")))
159 (jclass-of (jnew constructor
"foo")))
164 (jclass-of (jnew (jconstructor "java.awt.Point")))
170 (jclass-of (jnew "java.awt.Point") "java.awt.Point")
174 (deftest jnew.error
.1
175 (signals-error (jnew (jconstructor "java.lang.String" "java.lang.String")
176 (make-immediate-object nil
:ref
))
177 #+abcl
'java-exception
178 #+allegro
'jlinker-error
)
182 (let ((method (jmethod "java.lang.String" "length")))
183 (jcall method
"test"))
187 (jcall "length" "test")
191 (let ((method (jmethod "java.lang.String" "regionMatches" 4)))
192 (jcall method
"test" 0 "this is a test" 10 4))
196 (let ((method (jmethod "java.lang.String" "regionMatches" 5)))
197 (jcall method
"test" (make-immediate-object nil
:boolean
) 0 "this is a test" 10 4))
201 (type-of (jfield "java.lang.Integer" "TYPE"))
203 #+allegro tran-struct
)
206 (jcall (jmethod "java.lang.Object" "toString")
207 (jmethod "java.lang.String" "substring" 1))
208 "public java.lang.String java.lang.String.substring(int)")
211 (jcall (jmethod "java.lang.Object" "toString")
212 (jmethod "java.lang.String" "substring" 2))
213 "public java.lang.String java.lang.String.substring(int,int)")
216 (signals-error (jmethod "java.lang.String" "substring" 3) 'error
)
220 (deftest jmethod-return-type
.1
221 (jclass-name (jmethod-return-type (jmethod "java.lang.String" "length")))
225 (deftest jmethod-return-type
.2
226 (jclass-name (jmethod-return-type (jmethod "java.lang.String" "substring" 1)))
230 (deftest jmethod-return-type.error
.1
231 (signals-error (jmethod-return-type (jclass "java.lang.String")) 'error
)
235 (deftest jmethod-return-type.error
.2
236 (signals-error (jmethod-return-type 42) 'error
)
240 (deftest define-condition
.1
242 (define-condition throwable
(java-exception) ())
243 (let ((c (make-condition 'throwable
)))
244 (signals-error (simple-condition-format-control c
) 'unbound-slot
)))
248 (deftest define-condition
.2
250 (define-condition throwable
(java-exception) ())
251 (let ((c (make-condition 'throwable
)))
252 (simple-condition-format-arguments c
)))
256 (deftest define-condition
.3
258 (define-condition throwable
(java-exception) ())
259 (let ((c (make-condition 'throwable
260 :format-control
"The bear is armed.")))
261 (simple-condition-format-control c
)))
262 "The bear is armed.")
265 (deftest define-condition
.4
267 (define-condition throwable
(java-exception) ())
268 (let ((c (make-condition 'throwable
269 :format-control
"The bear is armed.")))
270 (simple-condition-format-arguments c
)))
274 (deftest java-exception-cause
.1
276 (define-condition throwable
(java-exception) ())
277 (signals-error (java-exception-cause (make-condition 'throwable
))
282 (deftest java-exception-cause
.2
284 (define-condition throwable
(java-exception) ())
285 (java-exception-cause (make-condition 'throwable
:cause
42)))
289 (deftest unregister-java-exception
.1
291 (define-condition throwable
(java-exception) ())
292 (register-java-exception "java.lang.Throwable" 'throwable
)
293 (unregister-java-exception "java.lang.Throwable"))
297 (deftest unregister-java-exception
.2
298 (unregister-java-exception "java.lang.Throwable")
302 (deftest register-java-exception
.1
304 (define-condition throwable
(java-exception) ())
305 (with-registered-exception "java.lang.Throwable" 'throwable
307 (jnew (jconstructor "java.lang.String" "java.lang.String")
308 (make-immediate-object nil
:ref
))
313 (deftest register-java-exception
.1a
315 (define-condition throwable
(java-exception) ())
316 (with-registered-exception "java.lang.Throwable" 'throwable
318 (jnew (jconstructor "java.lang.String" "java.lang.String")
319 (make-immediate-object nil
:ref
))
320 (condition (c) (values (type-of c
) (princ-to-string c
))))))
322 "java.lang.NullPointerException")
325 (deftest register-java-exception
.2
327 (define-condition throwable
(java-exception) ())
328 (with-registered-exception "java.lang.Throwable" 'throwable
330 (jnew (jconstructor "java.lang.String" "java.lang.String") 42)
335 ;; Behavior is non-deterministic.
336 (deftest register-java-exception
.2a
338 (define-condition throwable
(java-exception) ())
339 (with-registered-exception "java.lang.Throwable" 'throwable
341 (jnew (jconstructor "java.lang.String" "java.lang.String") 42)
342 (condition (c) (let* ((s (princ-to-string c
)))
343 ;; The actual string returned by Throwable.getMessage()
344 ;; is either "argument type mismatch" or something
345 ;; like "java.lang.ClassCastException@9d0366".
346 (or (string= s
"argument type mismatch")
347 (and (> (length s
) (length "java.lang.ClassCastException"))
348 (string= (subseq s
0 (length "java.lang.ClassCastException"))
349 "java.lang.ClassCastException"))))))))
353 (deftest register-java-exception
.3
355 (define-condition throwable
(java-exception) ())
356 (with-registered-exception "java.lang.Throwable" 'throwable
358 (jstatic (jmethod "java.lang.String" "valueOf" "int") "java.lang.String" "12")
363 ;; Behavior is non-deterministic.
364 (deftest register-java-exception
.3a
366 (define-condition throwable
(java-exception) ())
367 (with-registered-exception "java.lang.Throwable" 'throwable
369 (jstatic (jmethod "java.lang.String" "valueOf" "int") "java.lang.String" "12")
370 (condition (c) (let ((s (princ-to-string c
)))
371 (or (string= s
"argument type mismatch")
372 (string= s
"java.lang.IllegalArgumentException")))))))
376 (deftest register-java-exception
.4
378 (define-condition throwable
(java-exception) ())
379 (define-condition illegal-argument-exception
(java-exception) ())
380 (with-registered-exception "java.lang.Throwable" 'throwable
381 (with-registered-exception "java.lang.IllegalArgumentException" 'illegal-argument-exception
383 (jstatic (jmethod "java.lang.String" "valueOf" "int") "java.lang.String" "12")
388 (deftest register-java-exception
.5
390 (define-condition throwable
(java-exception) ())
391 (define-condition illegal-argument-exception
(java-exception) ())
392 (with-registered-exception "java.lang.Throwable" 'throwable
393 (with-registered-exception "java.lang.IllegalArgumentException" 'illegal-argument-exception
395 (jstatic (jmethod "java.lang.String" "valueOf" "int") "java.lang.String" "12")
396 'illegal-argument-exception
))))
401 (deftest register-java-exception
.6
403 (define-condition foo
() ())
404 (register-java-exception "java.lang.Throwable" 'foo
))
408 (deftest register-java-exception
.7
410 (define-condition throwable
(java-exception) ())
411 (register-java-exception "java.lang.Throwable" 'throwable
))
415 (deftest register-java-exception
.8
417 (define-condition throwable
(java-exception) ())
418 (with-registered-exception "java.lang.Throwable" 'throwable
419 (define-condition throwable
() ())
421 (jstatic (jmethod "java.lang.String" "valueOf" "int") "java.lang.String" "12")
426 (deftest register-java-exception
.9
428 (define-condition throwable
(java-exception) ())
429 (define-condition illegal-argument-exception
(throwable) ())
430 (with-registered-exception "java.lang.IllegalArgumentException" 'illegal-argument-exception
432 (jstatic (jmethod "java.lang.String" "valueOf" "int") "java.lang.String" "12")
433 'illegal-argument-exception
)))