java-tests.lisp 12.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
;;; java-tests.lisp
;;;
;;; Copyright (C) 2005 Peter Graves
;;; $Id$
;;;
;;; This program is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License
;;; as published by the Free Software Foundation; either version 2
;;; of the License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

20
(in-package #:abcl.test.lisp)
21

22
23
#+abcl
(use-package '#:java)
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
189
190
191
192
193
194
195
196
197

#+allegro
(require :jlinker)
#+allegro
(use-package '#:javatools.jlinker)
#+allegro
(use-package '#:javatools.jlinker '#:cl-user) ;; For convenience only.
#+(and allegro mswindows)
(use-package '#:javatools.jlinker '#:cg-user) ;; For convenience only.
#+allegro
(load (merge-pathnames "jl-config.cl" *load-truename*))
#+allegro
(or (jlinker-query) (jlinker-init))

#+abcl
(defmacro with-registered-exception (exception condition &body body)
  `(unwind-protect
       (progn
         (register-java-exception ,exception ,condition)
         ,@body)
     (unregister-java-exception ,exception)))

#+abcl
(deftest java-object.1
  (class-name (find-class 'java-object nil))
  java-object)

(deftest jclass.1
  (jcall (jmethod "java.lang.Object" "toString") (jclass "java.lang.String"))
  "class java.lang.String")

(deftest jclass.2
  (equal (jcall (jmethod "java.lang.Object" "getClass") "foo")
         (jclass "java.lang.String"))
  #+abcl    t
  #+allegro nil)

(deftest jclass.3
  (equal (jclass '|java.lang.String|) (jclass "java.lang.String"))
  t)

(deftest jclass.4
  (let ((class1 (jcall (jmethod "java.lang.Object" "getClass") "foo"))
        (class2 (jclass "java.lang.String")))
    (jcall (jmethod "java.lang.Object" "equals" "java.lang.Object")
           class1 class2))
  t)

(deftest jclass.5
  (jcall (jmethod "java.lang.Object" "toString") (jclass "int"))
  "int")

(deftest jclass.6
  (equal (jclass '|int|) (jclass "int"))
  t)

;; No such class.
(deftest jclass.error.1
  (signals-error (jclass "foo") 'error)
  t)

;; Silly argument.
(deftest jclass.error.2
  (signals-error (jclass 42) 'error)
  t)

(deftest jclass-of.1
  (jclass-of "foo")
  "java.lang.String"
  "java.lang.String")

(deftest jclass-of.2
  (jclass-of "foo" "java.lang.String")
  t
  "java.lang.String")

(deftest jclass-of.3
  (jclass-of "foo" "bar")
  nil
  "java.lang.String")

(deftest jclass-of.4
  (jclass-of 42)
  nil
  nil)

(deftest jclass-of.5
  (jclass-of 'foo)
  nil
  nil)

(deftest jclass-name.1
  (jclass-name "java.lang.String")
  "java.lang.String")

(deftest jclass-name.2
  (signals-error (jclass-name "foo") 'error)
  t)

(deftest jclass-name.3
  (signals-error (jclass-name 42) 'error)
  t)

(deftest jclass-name.4
  (jclass-name (jclass "java.lang.String"))
  "java.lang.String")

(deftest jclass-name.5
  (jclass-name (jclass "java.lang.String") "java.lang.String")
  t
  "java.lang.String")

(deftest jclass-name.6
  (jclass-name (jclass "java.lang.String") "java.lang.Object")
  nil
  "java.lang.String")

(deftest jclass-name.7
  (jclass-name (jclass "java.lang.String") "foo")
  nil
  "java.lang.String")

(deftest jclass-name.8
  (jclass-name (jclass "int"))
  "int")

(deftest jconstructor.1
  (jclass-of (jconstructor "java.lang.String" "java.lang.String"))
  "java.lang.reflect.Constructor"
  "java.lang.reflect.Constructor")

(deftest jnew.1
  (let ((constructor (jconstructor "java.lang.String" "java.lang.String")))
    (jclass-of (jnew constructor "foo")))
  "java.lang.String"
  "java.lang.String")

(deftest jnew.2
  (jclass-of (jnew (jconstructor "java.awt.Point")))
  "java.awt.Point"
  "java.awt.Point")

#-abcl
(deftest jnew.3
  (jclass-of (jnew "java.awt.Point") "java.awt.Point")
  t
  "java.awt.Point")

(deftest jnew.error.1
  (signals-error (jnew (jconstructor "java.lang.String" "java.lang.String")
                       (make-immediate-object nil :ref))
                 #+abcl    'java-exception
                 #+allegro 'jlinker-error)
  t)

(deftest jcall.1
  (let ((method (jmethod "java.lang.String" "length")))
    (jcall method "test"))
  4)

(deftest jcall.2
  (jcall "length" "test")
  4)

(deftest jcall.3
  (let ((method (jmethod "java.lang.String" "regionMatches" 4)))
    (jcall method "test" 0 "this is a test" 10 4))
  t)

(deftest jcall.4
  (let ((method (jmethod "java.lang.String" "regionMatches" 5)))
    (jcall method "test" (make-immediate-object nil :boolean) 0 "this is a test" 10 4))
  t)

198
199
200
201
202
203
204
205
(deftest jcall.5
  (jcall "join" (jstatic "currentThread" "java.lang.Thread") 1 1)
  nil)

(deftest jcall.6
  (jcall "offsetByCodePoints" "foobar" 0 #\Nul)
  0)

206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
(deftest jfield.1
  (type-of (jfield "java.lang.Integer" "TYPE"))
  #+abcl    java-object
  #+allegro tran-struct)

(deftest jmethod.1
  (jcall (jmethod "java.lang.Object" "toString")
         (jmethod "java.lang.String" "substring" 1))
  "public java.lang.String java.lang.String.substring(int)")

(deftest jmethod.2
  (jcall (jmethod "java.lang.Object" "toString")
         (jmethod "java.lang.String" "substring" 2))
  "public java.lang.String java.lang.String.substring(int,int)")

(deftest jmethod.3
  (signals-error (jmethod "java.lang.String" "substring" 3) 'error)
  t)

#+abcl
(deftest jmethod-return-type.1
  (jclass-name (jmethod-return-type (jmethod "java.lang.String" "length")))
  "int")

#+abcl
(deftest jmethod-return-type.2
  (jclass-name (jmethod-return-type (jmethod "java.lang.String" "substring" 1)))
  "java.lang.String")

#+abcl
(deftest jmethod-return-type.error.1
  (signals-error (jmethod-return-type (jclass "java.lang.String")) 'error)
  t)

#+abcl
(deftest jmethod-return-type.error.2
  (signals-error (jmethod-return-type 42) 'error)
  t)

#+abcl
(deftest define-condition.1
  (progn
    (define-condition throwable (java-exception) ())
    (let ((c (make-condition 'throwable)))
      (signals-error (simple-condition-format-control c) 'unbound-slot)))
  t)

#+abcl
(deftest define-condition.2
  (progn
    (define-condition throwable (java-exception) ())
    (let ((c (make-condition 'throwable)))
      (simple-condition-format-arguments c)))
  nil)

#+abcl
(deftest define-condition.3
  (progn
    (define-condition throwable (java-exception) ())
    (let ((c (make-condition 'throwable
                             :format-control "The bear is armed.")))
      (simple-condition-format-control c)))
  "The bear is armed.")

#+abcl
(deftest define-condition.4
  (progn
    (define-condition throwable (java-exception) ())
    (let ((c (make-condition 'throwable
                             :format-control "The bear is armed.")))
      (simple-condition-format-arguments c)))
  nil)

#+abcl
(deftest java-exception-cause.1
  (progn
    (define-condition throwable (java-exception) ())
    (signals-error (java-exception-cause (make-condition 'throwable))
                   'unbound-slot))
  t)

#+abcl
(deftest java-exception-cause.2
  (progn
    (define-condition throwable (java-exception) ())
    (java-exception-cause (make-condition 'throwable :cause 42)))
  42)

#+abcl
(deftest unregister-java-exception.1
  (progn
    (define-condition throwable (java-exception) ())
    (register-java-exception "java.lang.Throwable" 'throwable)
    (unregister-java-exception "java.lang.Throwable"))
  t)

#+abcl
(deftest unregister-java-exception.2
  (unregister-java-exception "java.lang.Throwable")
  nil)

#+abcl
(deftest register-java-exception.1
  (progn
    (define-condition throwable (java-exception) ())
    (with-registered-exception "java.lang.Throwable" 'throwable
      (signals-error
       (jnew (jconstructor "java.lang.String" "java.lang.String")
             (make-immediate-object nil :ref))
       'throwable)))
  t)

#+abcl
(deftest register-java-exception.1a
  (progn
    (define-condition throwable (java-exception) ())
    (with-registered-exception "java.lang.Throwable" 'throwable
      (handler-case
          (jnew (jconstructor "java.lang.String" "java.lang.String")
                (make-immediate-object nil :ref))
        (condition (c) (values (type-of c) (princ-to-string c))))))
  throwable
  "java.lang.NullPointerException")

#+abcl
(deftest register-java-exception.2
  (progn
    (define-condition throwable (java-exception) ())
    (with-registered-exception "java.lang.Throwable" 'throwable
      (signals-error
       (jnew (jconstructor "java.lang.String" "java.lang.String") 42)
       'throwable)))
  t)

#+abcl
;; Behavior is non-deterministic.
(deftest register-java-exception.2a
  (progn
    (define-condition throwable (java-exception) ())
    (with-registered-exception "java.lang.Throwable" 'throwable
      (handler-case
          (jnew (jconstructor "java.lang.String" "java.lang.String") 42)
        (condition (c) (let* ((s (princ-to-string c)))
                         ;; The actual string returned by Throwable.getMessage()
                         ;; is either "argument type mismatch" or something
                         ;; like "java.lang.ClassCastException@9d0366".
                         (or (string= s "argument type mismatch")
                             (and (> (length s) (length "java.lang.ClassCastException"))
                                  (string= (subseq s 0 (length "java.lang.ClassCastException"))
                                           "java.lang.ClassCastException"))))))))
  t)

#+abcl
(deftest register-java-exception.3
  (progn
    (define-condition throwable (java-exception) ())
    (with-registered-exception "java.lang.Throwable" 'throwable
      (signals-error
       (jstatic (jmethod "java.lang.String" "valueOf" "int") "java.lang.String" "12")
       'throwable)))
  t)

#+abcl
;; Behavior is non-deterministic.
(deftest register-java-exception.3a
  (progn
    (define-condition throwable (java-exception) ())
    (with-registered-exception "java.lang.Throwable" 'throwable
      (handler-case
          (jstatic (jmethod "java.lang.String" "valueOf" "int") "java.lang.String" "12")
        (condition (c) (let ((s (princ-to-string c)))
                         (or (string= s "argument type mismatch")
                             (string= s "java.lang.IllegalArgumentException")))))))
  t)

#+abcl
(deftest register-java-exception.4
  (progn
    (define-condition throwable (java-exception) ())
    (define-condition illegal-argument-exception (java-exception) ())
    (with-registered-exception "java.lang.Throwable" 'throwable
      (with-registered-exception "java.lang.IllegalArgumentException" 'illegal-argument-exception
        (signals-error
         (jstatic (jmethod "java.lang.String" "valueOf" "int") "java.lang.String" "12")
         'throwable))))
  nil)

#+abcl
(deftest register-java-exception.5
  (progn
    (define-condition throwable (java-exception) ())
    (define-condition illegal-argument-exception (java-exception) ())
    (with-registered-exception "java.lang.Throwable" 'throwable
      (with-registered-exception "java.lang.IllegalArgumentException" 'illegal-argument-exception
        (signals-error
         (jstatic (jmethod "java.lang.String" "valueOf" "int") "java.lang.String" "12")
         'illegal-argument-exception))))
  t)


#+abcl
(deftest register-java-exception.6
  (progn 
    (define-condition foo () ())
    (register-java-exception "java.lang.Throwable" 'foo))
  nil)

#+abcl
(deftest register-java-exception.7
  (progn 
    (define-condition throwable (java-exception) ())
    (register-java-exception "java.lang.Throwable" 'throwable))
  t)

#+abcl
(deftest register-java-exception.8
  (progn 
    (define-condition throwable (java-exception) ())
    (with-registered-exception "java.lang.Throwable" 'throwable
      (define-condition throwable () ())
      (signals-error
       (jstatic (jmethod "java.lang.String" "valueOf" "int") "java.lang.String" "12")
       'java-exception)))
  t)
      
#+abcl
(deftest register-java-exception.9
  (progn 
    (define-condition throwable (java-exception) ())
    (define-condition illegal-argument-exception (throwable) ())
    (with-registered-exception "java.lang.IllegalArgumentException" 'illegal-argument-exception
      (signals-error
       (jstatic (jmethod "java.lang.String" "valueOf" "int") "java.lang.String" "12")
       'illegal-argument-exception)))
  t)

;;#+allegro
;;(jlinker-end)