test-ob-java.el 21 KB

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