test-ob-java.el 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. ;;; test-ob-java.el --- tests for ob-java.el -*- lexical-binding: t; -*-
  2. ;; Copyright (c) 2020-2022 Free Software Foundation, Inc.
  3. ;; Authors: Eric Schulte
  4. ;; Dan Davison
  5. ;; Maintainer: Ian Martins <ianxm@jhu.edu>
  6. ;; This file is not part of GNU Emacs.
  7. ;; This program is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (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.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. ;;; Code:
  18. (require 'org-test "../testing/org-test")
  19. (require 'ob-core)
  20. (org-test-for-executable "java")
  21. (org-test-for-executable "javac")
  22. (unless (featurep 'ob-java)
  23. (signal 'missing-test-dependency "Support for java code blocks"))
  24. ; simple tests
  25. (ert-deftest ob-java/simple ()
  26. "Hello world program that writes output. Also tests that
  27. ob-java defaults to scripting mode."
  28. (org-test-with-temp-text
  29. "#+begin_src java :dir 'nil :results silent
  30. System.out.print(42);
  31. #+end_src"
  32. (should (string= "42" (org-babel-execute-src-block)))))
  33. (ert-deftest ob-java/simple-with-bracket ()
  34. "Hello world program that outputs an open square bracket."
  35. (org-test-with-temp-text
  36. "#+begin_src java :dir 'nil :results output silent
  37. System.out.print(\"[42\");
  38. #+end_src"
  39. (should (string= "[42" (org-babel-execute-src-block)))))
  40. (ert-deftest ob-java/simple-with-quote ()
  41. "Hello world program that writes quotes."
  42. (org-test-with-temp-text
  43. "#+begin_src java :dir 'nil :results output silent
  44. System.out.print(\"\\\"42\\\"\");
  45. #+end_src"
  46. (should (string= "\"42\"" (org-babel-execute-src-block)))))
  47. (ert-deftest ob-java/simple-return-int ()
  48. "Hello world program that returns an int value."
  49. (org-test-with-temp-text
  50. "#+begin_src java :dir 'nil :results value silent
  51. return 42;
  52. #+end_src"
  53. (should (eq 42 (org-babel-execute-src-block)))))
  54. (ert-deftest ob-java/simple-return-float ()
  55. "Hello world program that returns a float value."
  56. (org-test-with-temp-text
  57. "#+begin_src java :dir 'nil :results value silent
  58. return 42.0;
  59. #+end_src"
  60. (should (equal 42.0 (org-babel-execute-src-block)))))
  61. (ert-deftest ob-java/simple-return-string ()
  62. "Hello world program that returns a string value."
  63. (org-test-with-temp-text
  64. "#+begin_src java :dir 'nil :results value silent
  65. return \"forty two\";
  66. #+end_src"
  67. (should (string= "forty two" (org-babel-execute-src-block)))))
  68. (ert-deftest ob-java/simple-with-main ()
  69. "Hello world program that defines a main function."
  70. (org-test-with-temp-text
  71. "#+begin_src java :dir 'nil :results output silent
  72. public static void main(String[] args) {
  73. System.out.print(42);
  74. }
  75. #+end_src"
  76. (should (string= "42" (org-babel-execute-src-block)))))
  77. (ert-deftest ob-java/simple-with-two-methods ()
  78. "Hello world program with two methods and no class."
  79. (org-test-with-temp-text
  80. "#+begin_src java :dir 'nil :results output silent
  81. public static void main(String[] args) {
  82. System.out.print(foo());
  83. }
  84. public static int foo() {
  85. return 42;
  86. }
  87. #+end_src"
  88. (should (string= "42" (org-babel-execute-src-block)))))
  89. (ert-deftest ob-java/simple-with-no-main ()
  90. "Hello world program with no main method. Babel adds a dummy one so it can run without error."
  91. (org-test-with-temp-text
  92. "#+begin_src java :dir 'nil :results output silent
  93. public static int foo() {
  94. return 42;
  95. }
  96. #+end_src"
  97. (should (string= "success" (org-babel-execute-src-block)))))
  98. (ert-deftest ob-java/simple-with-main-args-array ()
  99. "Hello world program that defines a main function with the square brackets after `args'."
  100. (org-test-with-temp-text
  101. "#+begin_src java :dir 'nil :results output silent
  102. public static void main(String args[]) {
  103. System.out.print(42);
  104. }
  105. #+end_src"
  106. (should (string= "42" (org-babel-execute-src-block)))))
  107. (ert-deftest ob-java/simple-with-main-whitespace ()
  108. "Hello world program that defines a main function with the square brackets after `args'."
  109. (org-test-with-temp-text
  110. "#+begin_src java :dir 'nil :results output silent
  111. public
  112. static
  113. void
  114. main
  115. (
  116. String
  117. args []
  118. )
  119. {
  120. System.out.print(42);
  121. }
  122. #+end_src"
  123. (should (string= "42" (org-babel-execute-src-block)))))
  124. (ert-deftest ob-java/simple-with-class ()
  125. "Hello world program that defines a class."
  126. (org-test-with-temp-text
  127. "#+begin_src java :dir 'nil :results output silent
  128. public class Simple {
  129. public static void main(String[] args) {
  130. System.out.print(42);
  131. }
  132. }
  133. #+end_src"
  134. (should (string= "42" (org-babel-execute-src-block)))))
  135. (ert-deftest ob-java/simple-with-non-public-class ()
  136. "Hello world program that defines a non-public class."
  137. (org-test-with-temp-text
  138. "#+begin_src java :dir 'nil :results output silent
  139. class Simple {
  140. public static void main(String[] args) {
  141. System.out.print(42);
  142. }
  143. }
  144. #+end_src"
  145. (should (string= "42" (org-babel-execute-src-block)))))
  146. (ert-deftest ob-java/simple-with-class-and-package ()
  147. "Hello world program that defines a class and package."
  148. (org-test-with-temp-text
  149. "#+begin_src java :dir 'nil :results output silent
  150. package pkg;
  151. public class Simple {
  152. public static void main(String[] args) {
  153. System.out.print(42);
  154. }
  155. }
  156. #+end_src"
  157. (should (string= "42" (org-babel-execute-src-block)))))
  158. (ert-deftest ob-java/simple-with-class-attr ()
  159. "Hello world program with class header attribute."
  160. (org-test-with-temp-text
  161. "#+begin_src java :dir 'nil :results output silent :classname Simple
  162. public static void main(String[] args) {
  163. System.out.print(42);
  164. }
  165. #+end_src"
  166. (should (string= "42" (org-babel-execute-src-block)))))
  167. (ert-deftest ob-java/simple-with-class-attr-with-package ()
  168. "Hello world program with class attr with package."
  169. (org-test-with-temp-text
  170. "#+begin_src java :dir 'nil :results output silent :classname pkg.Simple
  171. public static void main(String[] args) {
  172. System.out.print(42);
  173. }
  174. #+end_src"
  175. (should (string= "42" (org-babel-execute-src-block)))))
  176. (ert-deftest ob-java/one-arg ()
  177. "Command line arg."
  178. (org-test-with-temp-text
  179. "#+begin_src java :dir 'nil :results output silent :cmdargs \"fortytwo\"
  180. System.out.print(args[0]);
  181. #+end_src"
  182. (should (string= "fortytwo" (org-babel-execute-src-block)))))
  183. (ert-deftest ob-java/args-quoted-string ()
  184. "Two command line args, first contains a space."
  185. (org-test-with-temp-text
  186. "#+begin_src java :dir 'nil :results output silent :cmdargs \"\\\"forty two\\\" 42\"
  187. System.out.println(args[0]);
  188. System.out.println(args[1]);
  189. #+end_src"
  190. (should (string= "forty two\n42\n" (org-babel-execute-src-block)))))
  191. ;; var tests
  192. (ert-deftest ob-java/integer-var ()
  193. "Read and write an integer variable."
  194. (org-test-with-temp-text
  195. "#+begin_src java :dir 'nil :var a=42 :results output silent
  196. System.out.print(a);
  197. #+end_src"
  198. (should (string= "42" (org-babel-execute-src-block)))))
  199. (ert-deftest ob-java/var-with-main ()
  200. "Read and write an integer variable, with main function provided."
  201. (org-test-with-temp-text
  202. "#+begin_src java :dir 'nil :var a=42 :results output silent
  203. public static void main(String[] args) {
  204. System.out.print(a);
  205. }
  206. #+end_src"
  207. (should (string= "42" (org-babel-execute-src-block)))))
  208. (ert-deftest ob-java/var-with-class ()
  209. "Read and write an integer variable, with class provided."
  210. (org-test-with-temp-text
  211. "#+begin_src java :dir 'nil :var a=42 :results output silent
  212. public class Main {
  213. public static void main(String[] args) {
  214. System.out.print(a);
  215. }
  216. }
  217. #+end_src"
  218. (should (string= "42" (org-babel-execute-src-block)))))
  219. (ert-deftest ob-java/var-with-class-and-package ()
  220. "Read and write an integer variable, with class and package provided."
  221. (org-test-with-temp-text
  222. "#+begin_src java :dir 'nil :var a=42 :results output silent
  223. package pkg;
  224. public class Main {
  225. public static void main(String[] args) {
  226. System.out.print(a);
  227. }
  228. }
  229. #+end_src"
  230. (should (string= "42" (org-babel-execute-src-block)))))
  231. (ert-deftest ob-java/var-with-class-and-hanging-curlies ()
  232. "Read and write an integer variable, with class with hanging curlies."
  233. (org-test-with-temp-text
  234. "#+begin_src java :dir 'nil :var a=42 :results output silent
  235. public class Main
  236. {
  237. public static void main(String[] args)
  238. {
  239. System.out.print(a);
  240. }
  241. }
  242. #+end_src"
  243. (should (string= "42" (org-babel-execute-src-block)))))
  244. (ert-deftest ob-java/two-vars ()
  245. "Read two integer variables, combine and write them."
  246. (org-test-with-temp-text
  247. "#+begin_src java :dir 'nil :var a=21 b=2 :results output silent
  248. System.out.print(a*b);
  249. #+end_src"
  250. (should (string= "42" (org-babel-execute-src-block)))))
  251. (ert-deftest ob-java/string-var ()
  252. "Read and write a string variable."
  253. (org-test-with-temp-text
  254. "#+begin_src java :dir 'nil :var a=\"forty two\" :results output silent
  255. System.out.print(String.format(\"%s, len=%d\", a, a.length()));
  256. #+end_src"
  257. (should (string= "forty two, len=9" (org-babel-execute-src-block)))))
  258. (ert-deftest ob-java/multiline-string-var ()
  259. "Java doesn't support multiline string literals, so this errors."
  260. (org-test-with-temp-text
  261. "#+begin_src java :dir 'nil :var a=\"forty\ntwo\" :results output silent
  262. System.out.print(String.format(\"%s, len=%d\", a, a.length()));
  263. #+end_src"
  264. (should-error (org-babel-execute-src-block)))
  265. :type 'error)
  266. ;; return list
  267. (ert-deftest ob-java/return-vector-using-list ()
  268. "Return a vector using a list."
  269. (org-test-with-temp-text
  270. "#+begin_src java :dir 'nil :results value vector silent
  271. import java.util.List;
  272. import java.util.Arrays;
  273. List<List<Integer>> a = Arrays.asList(Arrays.asList(4),
  274. Arrays.asList(2));
  275. return a;
  276. #+end_src"
  277. (should (equal '((4) (2))
  278. (org-babel-execute-src-block)))))
  279. (ert-deftest ob-java/return-vector-using-array ()
  280. "Return a vector using an array."
  281. (org-test-with-temp-text
  282. "#+begin_src java :dir 'nil :results value vector silent
  283. Integer[][] a = {{4}, {2}};
  284. return a;
  285. #+end_src"
  286. (should (equal '((4) (2))
  287. (org-babel-execute-src-block)))))
  288. (ert-deftest ob-java/read-return-list ()
  289. "Read and return a list."
  290. (org-test-with-temp-text
  291. "#+begin_src java :dir 'nil :var a=java_list :results value silent
  292. import java.util.List;
  293. import java.util.Arrays;
  294. List<String> b = Arrays.asList(a.get(0).get(0),
  295. a.get(1).get(0));
  296. return b;
  297. #+end_src
  298. #+name: java_list
  299. - forty
  300. - two"
  301. (should (equal '("forty" "two")
  302. (org-babel-execute-src-block)))))
  303. (ert-deftest ob-java/read-list-return-array ()
  304. "Read a list and return an array."
  305. (org-test-with-temp-text
  306. "#+begin_src java :dir 'nil :var a=java_list :results value silent
  307. String[] b = {a.get(0).get(0), a.get(1).get(0)};
  308. return b;
  309. #+end_src
  310. #+name: java_list
  311. - forty
  312. - two"
  313. (should (equal '("forty" "two")
  314. (org-babel-execute-src-block)))))
  315. (ert-deftest ob-java/read-return-list-with-package ()
  316. "Return a vector."
  317. (org-test-with-temp-text
  318. "#+begin_src java :dir 'nil :var a=java_list :results value silent
  319. package pkg;
  320. import java.util.List;
  321. import java.util.Arrays;
  322. List<String> b = Arrays.asList(a.get(0).get(0),
  323. a.get(1).get(0));
  324. return b;
  325. #+end_src
  326. #+name: java_list
  327. - forty
  328. - two"
  329. (should (equal '("forty" "two")
  330. (org-babel-execute-src-block)))))
  331. (ert-deftest ob-java/output-list-with-spaces ()
  332. "Return a vector."
  333. (org-test-with-temp-text
  334. "#+begin_src java :dir 'nil :results output list raw silent
  335. System.out.println(\"forty two\");
  336. System.out.println(\"forty two\");
  337. #+end_src"
  338. (should (equal "forty two\nforty two\n"
  339. (org-babel-execute-src-block)))))
  340. ;; list vars
  341. (ert-deftest ob-java/list-var ()
  342. "Read and write a list variable."
  343. (org-test-with-temp-text
  344. "#+begin_src java :dir 'nil :var a='(\"forty\" \"two\") :results value silent
  345. import java.util.List;
  346. List<String> b = a;
  347. return b;
  348. #+end_src"
  349. (should (equal '("forty" "two")
  350. (org-babel-execute-src-block)))))
  351. (ert-deftest ob-java/vector-var ()
  352. "Read and write a vector variable."
  353. (org-test-with-temp-text
  354. "#+begin_src java :dir 'nil :var a='[\"forty\" \"two\"] :results value silent
  355. import java.util.List;
  356. List<String> b = a;
  357. return b;
  358. #+end_src"
  359. (should (equal '("forty" "two")
  360. (org-babel-execute-src-block)))))
  361. (ert-deftest ob-java/matrix-var ()
  362. "Read and write matrix variable."
  363. (org-test-with-temp-text
  364. "#+begin_src java :dir 'nil :var a=java_matrix :results value silent
  365. import java.util.List;
  366. import java.util.Arrays;
  367. List<List<Integer>> b = Arrays.asList(Arrays.asList(a.get(0).get(0), a.get(1).get(0)),
  368. Arrays.asList(a.get(0).get(1), a.get(1).get(1)));
  369. return b; // transpose
  370. #+end_src
  371. #+name: java_matrix
  372. | 2 | 1 |
  373. | 4 | 2 |"
  374. (should (equal '((2 4) (1 2))
  375. (org-babel-execute-src-block)))))
  376. (ert-deftest ob-java/matrix-var-with-header ()
  377. "Read matrix variable and write it with header."
  378. (org-test-with-temp-text
  379. "#+begin_src java :dir 'nil :var a=java_matrix :results value table silent
  380. import java.util.List;
  381. import java.util.Arrays;
  382. List<List> b = Arrays.asList(Arrays.asList(\"col1\", \"col2\"),
  383. null,
  384. Arrays.asList(a.get(0).get(0), a.get(1).get(0)),
  385. Arrays.asList(a.get(0).get(1), a.get(1).get(1)));
  386. return b; // transpose
  387. #+end_src
  388. #+name: java_matrix
  389. | 2 | 1 |
  390. | 4 | 2 |"
  391. (should (equal '(("col1" "col2") hline (2 4) (1 2))
  392. (org-babel-execute-src-block)))))
  393. ;; output table
  394. (ert-deftest ob-java/output-table-with-header ()
  395. "Write a table that includes a header."
  396. (org-test-with-temp-text
  397. "#+begin_src java :dir 'nil :var a=java_matrix :results output raw table silent
  398. System.out.println(\"|col1|col2|\");
  399. System.out.println(\"|-\");
  400. for (int ii=0; ii<a.size(); ii++) {
  401. for (int jj=0; jj<a.get(0).size(); jj++) {
  402. System.out.print(\"|\" + a.get(ii).get(jj));
  403. }
  404. System.out.println(\"\");
  405. }
  406. #+end_src
  407. #+name: java_matrix
  408. | 2 | 1 |
  409. | 4 | 2 |"
  410. (should (equal "|col1|col2|\n|-\n|2|1\n|4|2\n"
  411. (org-babel-execute-src-block)))))
  412. (ert-deftest ob-java/inhomogeneous_table ()
  413. "Read and write an inhomogeneous table."
  414. (org-test-with-temp-text
  415. "#+begin_src java :dir 'nil :var a=java_table :results value silent
  416. import java.util.List;
  417. import java.util.Arrays;
  418. List<List> b = Arrays.asList(Arrays.asList(a.get(0).get(0),
  419. Integer.parseInt(a.get(0).get(1))*2),
  420. Arrays.asList(a.get(1).get(0),
  421. Integer.parseInt(a.get(1).get(1))*2));
  422. return b;
  423. #+end_src
  424. #+name: java_table
  425. | string | number |
  426. |--------+--------|
  427. | forty | 2 |
  428. | two | 1 |"
  429. (should (equal
  430. '(("forty" 4) ("two" 2))
  431. (org-babel-execute-src-block)))))
  432. ;; imports
  433. (ert-deftest ob-java/import_library ()
  434. "Import a standard java library."
  435. (org-test-with-temp-text
  436. "#+begin_src java :dir 'nil :results output silent :imports java.util.Base64
  437. byte[] encoded = Base64.getEncoder().encode(\"42\".getBytes());
  438. String decoded = new String(Base64.getDecoder().decode(encoded));
  439. System.out.print(String.format(\"encoded=%s, decoded=%s\", new String(encoded), decoded));
  440. #+end_src"
  441. (should (string=
  442. "encoded=NDI=, decoded=42"
  443. (org-babel-execute-src-block)))))
  444. (ert-deftest ob-java/import_library_inline ()
  445. "Import a standard java library."
  446. (org-test-with-temp-text
  447. "#+begin_src java :dir 'nil :results output silent
  448. import java.util.Base64;
  449. byte[] encoded = Base64.getEncoder().encode(\"42\".getBytes());
  450. String decoded = new String(Base64.getDecoder().decode(encoded));
  451. System.out.print(String.format(\"encoded=%s, decoded=%s\", new String(encoded), decoded));
  452. #+end_src"
  453. (should (string=
  454. "encoded=NDI=, decoded=42"
  455. (org-babel-execute-src-block)))))
  456. ;; tangle
  457. (ert-deftest ob-java/tangle ()
  458. "Tangle a source block."
  459. (org-test-with-temp-text-in-file
  460. "#+begin_src java :dir 'nil :tangle \"Tangle.java\" :results value :classname Tangle
  461. return \"tangled\";
  462. #+end_src"
  463. (should
  464. (string=
  465. "public class Tangle {
  466. public static void main(String[] args) {
  467. return \"tangled\";
  468. }
  469. }
  470. "
  471. (unwind-protect
  472. (progn (org-babel-tangle)
  473. (with-temp-buffer
  474. (insert-file-contents "Tangle.java")
  475. (untabify (point-min) (point-max))
  476. (buffer-string)))
  477. (delete-file "Tangle.java"))))))
  478. (ert-deftest ob-java/tangle-with-package ()
  479. "Tangle a source block."
  480. (org-test-with-temp-text-in-file
  481. "#+begin_src java :dir 'nil :tangle \"tangle/Tangle.java\" :results value :classname tangle.Tangle
  482. return \"tangled\";
  483. #+end_src"
  484. (should
  485. (string=
  486. "package tangle;
  487. public class Tangle {
  488. public static void main(String[] args) {
  489. return \"tangled\";
  490. }
  491. }
  492. "
  493. (unwind-protect
  494. (progn
  495. (make-directory "tangle")
  496. (org-babel-tangle)
  497. (with-temp-buffer
  498. (insert-file-contents "tangle/Tangle.java")
  499. (untabify (point-min) (point-max))
  500. (buffer-string)))
  501. (delete-file "tangle/Tangle.java")
  502. (delete-directory "tangle"))))))
  503. ;; specify output dir
  504. (ert-deftest ob-java/simple-dir ()
  505. "Hello world program that writes output."
  506. (org-test-with-temp-text
  507. (format "#+begin_src java :dir %s :results output silent
  508. System.out.print(42);
  509. #+end_src" temporary-file-directory)
  510. (should (string=
  511. "42"
  512. (unwind-protect
  513. (org-babel-execute-src-block)
  514. (delete-file (concat (file-name-as-directory temporary-file-directory)
  515. "Main.java"))
  516. (delete-file (concat (file-name-as-directory temporary-file-directory)
  517. "Main.class")))))))
  518. (ert-deftest ob-java/simple-dir-with-package ()
  519. "Hello world program that writes output."
  520. (org-test-with-temp-text
  521. (format "#+begin_src java :dir %s :results output silent
  522. package pkg;
  523. public class Main {
  524. public static void main(String[] args) {
  525. System.out.print(42);
  526. }
  527. }
  528. #+end_src" temporary-file-directory)
  529. (should (string=
  530. "42"
  531. (unwind-protect
  532. (org-babel-execute-src-block)
  533. (delete-file (concat (file-name-as-directory temporary-file-directory)
  534. "pkg/Main.java"))
  535. (delete-file (concat (file-name-as-directory temporary-file-directory)
  536. "pkg/Main.class"))
  537. (delete-directory (concat (file-name-as-directory temporary-file-directory)
  538. "pkg")))))))
  539. (provide 'test-ob-java)
  540. ;;; test-ob-java.el ends here