test_parabola-java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. #! /bin/sh
  2. #
  3. # To run this script, ensure shunit2 (https://github.com/kward/shunit2)
  4. # is available on the local directory, then just run sh bin_parabola-java_tests
  5. #
  6. # inotifywait -r -m -e MODIFY . | while read l; do sh bin_parabola-java_tests; done
  7. declare test_dir
  8. . ./bin_parabola-java > /dev/null
  9. INIT_JVM_DIR=${JVM_DIR}
  10. INIT_DEFAULT_PATH=${DEFAULT_PATH}
  11. INIT_DEFAULT_PATH_JRE=${DEFAULT_PATH_JRE}
  12. # Redefining this so that tested script can be invoked without root privileges
  13. check_root() {
  14. true
  15. }
  16. get_rand_str() {
  17. echo $(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 10 | head -n 1)
  18. }
  19. setUp() {
  20. test_dir=$(mktemp -d)
  21. JVM_DIR=${test_dir}${INIT_JVM_DIR}
  22. DEFAULT_PATH=${test_dir}${INIT_DEFAULT_PATH}
  23. DEFAULT_PATH_JRE=${test_dir}${INIT_DEFAULT_PATH_JRE}
  24. JAVA_NAME=$(get_rand_str)
  25. mkdir -p ${JVM_DIR}
  26. }
  27. tearDown() {
  28. rm -rf ${test_dir}
  29. }
  30. # $1: path of Java environment to create
  31. create_raw_java_env() {
  32. java_env_name=$1
  33. bin_name=$2
  34. mkdir -p ${JVM_DIR}/${java_env_name}/bin
  35. touch ${JVM_DIR}/${java_env_name}/bin/$bin_name
  36. chmod +x ${JVM_DIR}/${java_env_name}/bin/$bin_name
  37. }
  38. create_valid_jre8_env() {
  39. create_raw_java_env "$1/jre" "java"
  40. }
  41. # $1: path of Java environment to create
  42. create_valid_jre8_jdk8_env() {
  43. create_raw_java_env "$1" "java"
  44. create_raw_java_env "$1" "javac"
  45. create_valid_jre8_env "$1"
  46. }
  47. create_valid_jre9_env() {
  48. create_raw_java_env "$1" "java"
  49. }
  50. create_valid_jre9_jdk9_env() {
  51. create_raw_java_env "$1" "java"
  52. create_raw_java_env "$1" "javac"
  53. }
  54. set_default_for_test() {
  55. ln -s $1 ${DEFAULT_PATH}
  56. if [ -d ${DEFAULT_PATH}/jre ]; then
  57. ln -s $1/jre ${DEFAULT_PATH_JRE}
  58. else
  59. ln -s $1 ${DEFAULT_PATH_JRE}
  60. fi
  61. }
  62. assertDefaultNotSet() {
  63. assertNull 'Default is set' "$(readlink ${DEFAULT_PATH})"
  64. assertNull 'Default runtime is set' "$(readlink ${DEFAULT_PATH_JRE})"
  65. }
  66. assertDefaultSetTo() {
  67. new_java=$1
  68. assertEquals "Default points at wrong Java env" "${new_java}" "$(readlink ${DEFAULT_PATH})"
  69. if [ ${new_java} == */jre ]; then
  70. assertEquals "Default runtime points at wrong Java env" "${new_java}/jre" "$(readlink ${DEFAULT_PATH_JRE})"
  71. fi
  72. }
  73. # do_status
  74. test_do_status_with_jre8_default() {
  75. JAVA_NAME_1="a$(get_rand_str)"
  76. JAVA_NAME_2="b$(get_rand_str)"
  77. JAVA_NAME_3="c$(get_rand_str)"
  78. create_valid_jre8_env "${JAVA_NAME_1}"
  79. set_default_for_test "${JAVA_NAME_1}/jre"
  80. create_valid_jre8_jdk8_env "${JAVA_NAME_2}"
  81. create_valid_jre9_env "${JAVA_NAME_3}"
  82. mess="Available Java environments:"
  83. mess+="\n ${JAVA_NAME_1}/jre (default)"
  84. mess+="\n ${JAVA_NAME_2}"
  85. mess+="\n ${JAVA_NAME_3}"
  86. expected=$(echo -e "${mess}")
  87. assertEquals 'Wrong status output' "${expected}" "$(do_status)"
  88. }
  89. test_do_status_with_jre9_default() {
  90. JAVA_NAME_1="a$(get_rand_str)"
  91. JAVA_NAME_2="b$(get_rand_str)"
  92. JAVA_NAME_3="c$(get_rand_str)"
  93. create_valid_jre8_env "${JAVA_NAME_1}"
  94. create_valid_jre8_jdk8_env "${JAVA_NAME_2}"
  95. create_valid_jre9_env "${JAVA_NAME_3}"
  96. set_default_for_test "${JAVA_NAME_3}"
  97. mess="Available Java environments:"
  98. mess+="\n ${JAVA_NAME_1}/jre"
  99. mess+="\n ${JAVA_NAME_2}"
  100. mess+="\n ${JAVA_NAME_3} (default)"
  101. expected=$(echo -e "${mess}")
  102. assertEquals 'Wrong status output' "${expected}" "$(do_status)"
  103. }
  104. test_do_status_with_jdk9_default() {
  105. JAVA_NAME_1="a$(get_rand_str)"
  106. JAVA_NAME_2="b$(get_rand_str)"
  107. JAVA_NAME_3="c$(get_rand_str)"
  108. create_valid_jre8_env "${JAVA_NAME_1}"
  109. create_valid_jre8_jdk8_env "${JAVA_NAME_2}"
  110. create_valid_jre9_jdk9_env "${JAVA_NAME_3}"
  111. set_default_for_test "${JAVA_NAME_3}"
  112. mess="Available Java environments:"
  113. mess+="\n ${JAVA_NAME_1}/jre"
  114. mess+="\n ${JAVA_NAME_2}"
  115. mess+="\n ${JAVA_NAME_3} (default)"
  116. expected=$(echo -e "${mess}")
  117. assertEquals 'Wrong status output' "${expected}" "$(do_status)"
  118. }
  119. test_do_status_no_default() {
  120. JAVA_NAME_1="a$(get_rand_str)"
  121. JAVA_NAME_2="b$(get_rand_str)"
  122. JAVA_NAME_3="c$(get_rand_str)"
  123. JAVA_NAME_4="d$(get_rand_str)"
  124. create_valid_jre8_env "${JAVA_NAME_1}"
  125. create_valid_jre8_jdk8_env "${JAVA_NAME_2}"
  126. create_valid_jre9_env "${JAVA_NAME_3}"
  127. create_valid_jre9_jdk9_env "${JAVA_NAME_4}"
  128. mess="Available Java environments:"
  129. mess+="\n ${JAVA_NAME_1}/jre"
  130. mess+="\n ${JAVA_NAME_2}"
  131. mess+="\n ${JAVA_NAME_3}"
  132. mess+="\n ${JAVA_NAME_4}"
  133. mess+="\nNo Java environment set as default"
  134. expected=$(echo -e "${mess}")
  135. assertEquals 'Wrong status output' "${expected}" "$(do_status)"
  136. }
  137. test_do_status_no_duplicate_entry_jre9_jdk9() {
  138. JAVA_NAME_1="a$(get_rand_str)"
  139. create_valid_jre9_jdk9_env "${JAVA_NAME_1}"
  140. mess="Available Java environments:"
  141. mess+="\n ${JAVA_NAME_1}"
  142. mess+="\nNo Java environment set as default"
  143. expected=$(echo -e "${mess}")
  144. assertEquals 'Wrong status output' "${expected}" "$(do_status)"
  145. }
  146. test_do_status_no_javas() {
  147. assertEquals 'Wrong status output' 'No compatible Java environment installed' "$(do_status)"
  148. }
  149. test_do_status_jre_but_jdk_exists() {
  150. create_valid_jre8_jdk8_env "${JAVA_NAME}"
  151. set_default_for_test "${JAVA_NAME}/jre"
  152. mess="Available Java environments:"
  153. mess+="\n ${JAVA_NAME} (${JAVA_NAME}/jre default)"
  154. expected=$(echo -e "${mess}")
  155. assertEquals 'Wrong status output' "${expected}" "$(do_status)"
  156. }
  157. # do_get
  158. test_do_get_with_jdk8_default() {
  159. create_valid_jre8_jdk8_env "${JAVA_NAME}"
  160. set_default_for_test "${JAVA_NAME}"
  161. assertEquals 'Wrong result' "${JAVA_NAME}" "$(do_get)"
  162. assertDefaultSetTo "${JAVA_NAME}"
  163. }
  164. test_do_get_without_default() {
  165. create_valid_jre8_jdk8_env "$(get_rand_str)"
  166. create_valid_jre9_jdk9_env "$(get_rand_str)"
  167. assertNull 'Wrong result' "$(do_get)"
  168. }
  169. test_do_get_with_initial_null() {
  170. set_default_for_test "/dev/null"
  171. assertNull 'Wrong result' "$(do_get)"
  172. }
  173. test_do_get_with_jdk9_default() {
  174. create_valid_jre9_jdk9_env "${JAVA_NAME}"
  175. set_default_for_test "${JAVA_NAME}"
  176. assertEquals 'Wrong result' "${JAVA_NAME}" "$(do_get)"
  177. assertDefaultSetTo "${JAVA_NAME}"
  178. }
  179. # do_set
  180. test_do_set_new_invalid() {
  181. create_valid_jre8_jdk8_env "${JAVA_NAME}"
  182. set_default_for_test "${JAVA_NAME}"
  183. output=$(do_set "INVALID")
  184. assertEquals 'Wrong status returned' 1 $?
  185. assertEquals 'Wrong message' "'${JVM_DIR}/INVALID' is not a valid Java environment path" "${output}"
  186. assertDefaultSetTo "${JAVA_NAME}"
  187. }
  188. test_do_set_current_invalid() {
  189. create_valid_jre8_jdk8_env "${JAVA_NAME}"
  190. set_default_for_test "INVALID"
  191. $(do_set "${JAVA_NAME}")
  192. assertEquals 'Wrong status returned' 0 $?
  193. assertDefaultSetTo "${JAVA_NAME}"
  194. }
  195. test_do_set_jre8_with_default_already_set_to_jre8() {
  196. create_valid_jre8_env "${JAVA_NAME}"
  197. set_default_for_test "${JAVA_NAME}/jre"
  198. JAVA_NAME_2="$(get_rand_str)"
  199. create_valid_jre8_env "${JAVA_NAME_2}"
  200. $(do_set "${JAVA_NAME_2}/jre")
  201. assertEquals 'Wrong status returned' 0 $?
  202. assertDefaultSetTo "${JAVA_NAME_2}/jre"
  203. }
  204. test_do_set_jre9_with_default_already_set_to_jre8() {
  205. create_valid_jre8_env "${JAVA_NAME}"
  206. set_default_for_test "${JAVA_NAME}/jre"
  207. JAVA_NAME_2="$(get_rand_str)"
  208. create_valid_jre9_env "${JAVA_NAME_2}"
  209. $(do_set "${JAVA_NAME_2}")
  210. assertEquals 'Wrong status returned' 0 $?
  211. assertDefaultSetTo "${JAVA_NAME_2}"
  212. }
  213. test_do_set_jdk9_with_default_already_set_to_jdk8() {
  214. create_valid_jre8_jdk8_env "${JAVA_NAME}"
  215. set_default_for_test "${JAVA_NAME}"
  216. JAVA_NAME_2="$(get_rand_str)"
  217. create_valid_jre9_jdk9_env "${JAVA_NAME_2}"
  218. $(do_set "${JAVA_NAME_2}")
  219. assertEquals 'Wrong status returned' 0 $?
  220. assertDefaultSetTo "${JAVA_NAME_2}"
  221. }
  222. test_do_set_jre8_no_previous_default() {
  223. create_valid_jre8_env "${JAVA_NAME}"
  224. $(do_set "${JAVA_NAME}/jre")
  225. assertEquals 'Wrong status returned' 0 $?
  226. assertDefaultSetTo "${JAVA_NAME}/jre"
  227. }
  228. test_do_set_jre9_no_previous_default() {
  229. create_valid_jre9_env "${JAVA_NAME}"
  230. $(do_set "${JAVA_NAME}")
  231. assertEquals 'Wrong status returned' 0 $?
  232. assertDefaultSetTo "${JAVA_NAME}"
  233. }
  234. test_do_set_jdk9_no_previous_default() {
  235. create_valid_jre9_jdk9_env "${JAVA_NAME}"
  236. $(do_set "${JAVA_NAME}")
  237. assertEquals 'Wrong status returned' 0 $?
  238. assertDefaultSetTo "${JAVA_NAME}"
  239. }
  240. test_do_set_jre8_while_jdk8_also_exists() {
  241. create_valid_jre8_jdk8_env "${JAVA_NAME}"
  242. output=$(do_set "${JAVA_NAME}/jre")
  243. assertEquals 'Wrong status returned' 0 $?
  244. assertDefaultSetTo "${JAVA_NAME}/jre"
  245. #mess="Warning: '${JAVA_NAME}' looks like a valid JDK whereas you only set '${JAVA_NAME}/jre' as default"
  246. #mess+="\nFix this with 'parabola-java set ${JAVA_NAME}'"
  247. #expected=$(echo -e "${mess}")
  248. #assertEquals 'Wrong message' "${expected}" "${output}"
  249. }
  250. test_do_set_jdk8_cleanup_old_jre8_default() {
  251. create_valid_jre8_jdk8_env "${JAVA_NAME}"
  252. set_default_for_test "${JAVA_NAME}"
  253. output=$(do_set "${JAVA_NAME}/jre")
  254. assertEquals 'Wrong status returned' 0 $?
  255. assertDefaultSetTo "${JAVA_NAME}/jre"
  256. }
  257. test_do_set_jdk9_cleanup_old_jdk8_default() {
  258. JAVA8_NAME=$(get_rand_str)
  259. create_valid_jre8_jdk8_env "${JAVA8_NAME}"
  260. set_default_for_test "${JAVA8_NAME}"
  261. JAVA9_NAME=$(get_rand_str)
  262. create_valid_jre9_jdk9_env "${JAVA9_NAME}"
  263. output=$(do_set "${JAVA9_NAME}")
  264. assertEquals 'Wrong status returned' 0 $?
  265. assertDefaultSetTo "${JAVA9_NAME}"
  266. }
  267. test_do_set_jdk8_devnull() {
  268. create_valid_jre8_jdk8_env "${JAVA_NAME}"
  269. set_default_for_test "/dev/null"
  270. output=$(do_set "${JAVA_NAME}/jre")
  271. assertEquals 'Wrong status returned' 0 $?
  272. assertDefaultSetTo "${JAVA_NAME}/jre"
  273. }
  274. test_do_set_jdk9_devnull() {
  275. create_valid_jre9_jdk9_env "${JAVA_NAME}"
  276. set_default_for_test "/dev/null"
  277. output=$(do_set "${JAVA_NAME}")
  278. assertEquals 'Wrong status returned' 0 $?
  279. assertDefaultSetTo "${JAVA_NAME}"
  280. }
  281. # do_unset
  282. test_do_unset_jre8() {
  283. create_valid_jre8_env "${JAVA_NAME}"
  284. set_default_for_test "${JAVA_NAME}/jre"
  285. do_unset
  286. assertDefaultNotSet
  287. }
  288. test_do_unset_no_default_link() {
  289. create_valid_jre8_jdk8_env "${JAVA_NAME}"
  290. set_default_for_test "${JAVA_NAME}"
  291. unlink ${DEFAULT_PATH}
  292. do_unset
  293. assertDefaultNotSet
  294. }
  295. test_do_unset_devnull() {
  296. set_default_for_test "/dev/null"
  297. do_unset
  298. assertDefaultNotSet
  299. }
  300. test_do_unset_jre9() {
  301. create_valid_jre9_env "${JAVA_NAME}"
  302. set_default_for_test "${JAVA_NAME}"
  303. do_unset
  304. assertDefaultNotSet
  305. }
  306. test_do_unset_jdk9() {
  307. create_valid_jre9_jdk9_env "${JAVA_NAME}"
  308. set_default_for_test "${JAVA_NAME}"
  309. do_unset
  310. assertDefaultNotSet
  311. }
  312. # do_fix
  313. test_do_fix_default_valid_jre8() {
  314. create_valid_jre8_env "${JAVA_NAME}"
  315. set_default_for_test "${JAVA_NAME}/jre"
  316. do_fix
  317. assertDefaultSetTo "${JAVA_NAME}/jre"
  318. }
  319. test_do_fix_default_valid_jre9() {
  320. create_valid_jre9_env "${JAVA_NAME}"
  321. set_default_for_test "${JAVA_NAME}"
  322. do_fix
  323. assertDefaultSetTo "${JAVA_NAME}"
  324. }
  325. test_do_fix_default_valid_jdk9() {
  326. create_valid_jre9_jdk9_env "${JAVA_NAME}"
  327. set_default_for_test "${JAVA_NAME}"
  328. do_fix
  329. assertDefaultSetTo "${JAVA_NAME}"
  330. }
  331. test_do_fix_no_default_jre8() {
  332. create_valid_jre8_env "${JAVA_NAME}"
  333. do_fix
  334. assertDefaultSetTo "${JAVA_NAME}/jre"
  335. }
  336. test_do_fix_no_default_jre9() {
  337. create_valid_jre9_env "${JAVA_NAME}"
  338. do_fix
  339. assertDefaultSetTo "${JAVA_NAME}"
  340. }
  341. test_do_fix_no_default_jdk9() {
  342. create_valid_jre9_jdk9_env "${JAVA_NAME}"
  343. do_fix
  344. assertDefaultSetTo "${JAVA_NAME}"
  345. }
  346. test_do_fix_default_invalid_openjdk7_jdk_installed() {
  347. create_valid_jre8_jdk8_env "java-8-openjdk"
  348. create_valid_jre8_jdk8_env "${JAVA_NAME}"
  349. do_fix
  350. assertDefaultSetTo "java-8-openjdk"
  351. }
  352. test_do_fix_jdk8_not_default_but_openjdk7_jre_installed() {
  353. create_valid_jre8_jdk8_env "${JAVA_NAME}"
  354. create_valid_jre8_env "java-8-openjdk"
  355. do_fix
  356. assertDefaultSetTo "java-8-openjdk/jre"
  357. }
  358. test_do_fix_jdk9_not_default_but_openjdk7_jre_installed() {
  359. create_valid_jre9_jdk9_env "${JAVA_NAME}"
  360. create_valid_jre8_env "java-8-openjdk"
  361. do_fix
  362. assertDefaultSetTo "java-8-openjdk/jre"
  363. }
  364. test_do_fix_default_invalid_random_java8_installed() {
  365. create_valid_jre8_jdk8_env "${JAVA_NAME}"
  366. do_fix
  367. assertDefaultSetTo "${JAVA_NAME}"
  368. }
  369. test_do_fix_default_invalid_random_java9_installed() {
  370. create_valid_jre9_jdk9_env "${JAVA_NAME}"
  371. do_fix
  372. assertDefaultSetTo "${JAVA_NAME}"
  373. }
  374. test_do_fix_default_invalid_no_java_installed() {
  375. assertEquals 'Wrong message' "No valid Java environment found" "$(do_fix)"
  376. assertDefaultNotSet
  377. }
  378. test_do_fix_jre8_but_jdk8_exists() {
  379. create_valid_jre8_jdk8_env "${JAVA_NAME}"
  380. set_default_for_test "${JAVA_NAME}/jre"
  381. do_fix
  382. assertDefaultSetTo "${JAVA_NAME}"
  383. }
  384. test_do_fix_jdk_but_jre_exists() {
  385. create_valid_jre8_jdk8_env "java-7-openjdk"
  386. create_valid_jre8_env "${JAVA_NAME}"
  387. # Only JRE is installed but setting JDK as default
  388. set_default_for_test "${JAVA_NAME}"
  389. do_fix
  390. assertDefaultSetTo "${JAVA_NAME}/jre"
  391. }
  392. test_do_fix_devnull() {
  393. create_valid_jre8_jdk8_env "${JAVA_NAME}"
  394. set_default_for_test "/dev/null"
  395. do_fix
  396. assertDefaultSetTo "${JAVA_NAME}"
  397. }
  398. # load shunit2
  399. . shunit2/src/shunit2