3 # To run this script, ensure shunit2 (https://github.com/kward/shunit2)
4 # is available on the local directory, then just run sh bin_archlinux-java_tests
6 # inotifywait -r -m -e MODIFY . | while read l; do sh bin_archlinux-java_tests; done
10 . .
/bin_archlinux-java
> /dev
/null
12 INIT_JVM_DIR
=${JVM_DIR}
13 INIT_DEFAULT_PATH
=${DEFAULT_PATH}
14 INIT_DEFAULT_PATH_JRE
=${DEFAULT_PATH_JRE}
16 # Redefining this so that tested script can be invoked without root privileges
23 echo $
(cat /dev
/urandom |
tr -dc 'a-zA-Z0-9' |
fold -w 10 |
head -n 1)
28 JVM_DIR
=${test_dir}${INIT_JVM_DIR}
29 DEFAULT_PATH
=${test_dir}${INIT_DEFAULT_PATH}
30 DEFAULT_PATH_JRE
=${test_dir}${INIT_DEFAULT_PATH_JRE}
31 JAVA_NAME
=$
(get_rand_str
)
39 # $1: path of Java environment to create
40 create_raw_java_env
() {
43 mkdir
-p ${JVM_DIR}/${java_env_name}/bin
44 touch ${JVM_DIR}/${java_env_name}/bin
/$bin_name
45 chmod +x
${JVM_DIR}/${java_env_name}/bin
/$bin_name
48 create_valid_jre8_env
() {
49 create_raw_java_env
"$1/jre" "java"
52 # $1: path of Java environment to create
53 create_valid_jre8_jdk8_env
() {
54 create_raw_java_env
"$1" "java"
55 create_raw_java_env
"$1" "javac"
56 create_valid_jre8_env
"$1"
59 create_valid_jre9_env
() {
60 create_raw_java_env
"$1" "java"
63 create_valid_jre9_jdk9_env
() {
64 create_raw_java_env
"$1" "java"
65 create_raw_java_env
"$1" "javac"
68 set_default_for_test
() {
69 ln -s $1 ${DEFAULT_PATH}
70 if [ -d ${DEFAULT_PATH}/jre
]; then
71 ln -s $1/jre
${DEFAULT_PATH_JRE}
73 ln -s $1 ${DEFAULT_PATH_JRE}
77 assertDefaultNotSet
() {
78 assertNull
'Default is set' "$(readlink ${DEFAULT_PATH})"
79 assertNull
'Default runtime is set' "$(readlink ${DEFAULT_PATH_JRE})"
82 assertDefaultSetTo
() {
84 assertEquals
"Default points at wrong Java env" "${new_java}" "$(readlink ${DEFAULT_PATH})"
85 if [ ${new_java} == */jre
]; then
86 assertEquals
"Default runtime points at wrong Java env" "${new_java}/jre" "$(readlink ${DEFAULT_PATH_JRE})"
92 test_do_status_with_jre8_default
() {
93 JAVA_NAME_1
="a$(get_rand_str)"
94 JAVA_NAME_2
="b$(get_rand_str)"
95 JAVA_NAME_3
="c$(get_rand_str)"
96 create_valid_jre8_env
"${JAVA_NAME_1}"
97 set_default_for_test
"${JAVA_NAME_1}/jre"
98 create_valid_jre8_jdk8_env
"${JAVA_NAME_2}"
99 create_valid_jre9_env
"${JAVA_NAME_3}"
100 mess
="Available Java environments:"
101 mess
+="\n ${JAVA_NAME_1}/jre (default)"
102 mess
+="\n ${JAVA_NAME_2}"
103 mess
+="\n ${JAVA_NAME_3}"
104 expected
=$
(echo -e "${mess}")
105 assertEquals
'Wrong status output' "${expected}" "$(do_status)"
108 test_do_status_with_jre9_default
() {
109 JAVA_NAME_1
="a$(get_rand_str)"
110 JAVA_NAME_2
="b$(get_rand_str)"
111 JAVA_NAME_3
="c$(get_rand_str)"
112 create_valid_jre8_env
"${JAVA_NAME_1}"
113 create_valid_jre8_jdk8_env
"${JAVA_NAME_2}"
114 create_valid_jre9_env
"${JAVA_NAME_3}"
115 set_default_for_test
"${JAVA_NAME_3}"
116 mess
="Available Java environments:"
117 mess
+="\n ${JAVA_NAME_1}/jre"
118 mess
+="\n ${JAVA_NAME_2}"
119 mess
+="\n ${JAVA_NAME_3} (default)"
120 expected
=$
(echo -e "${mess}")
121 assertEquals
'Wrong status output' "${expected}" "$(do_status)"
124 test_do_status_with_jdk9_default
() {
125 JAVA_NAME_1
="a$(get_rand_str)"
126 JAVA_NAME_2
="b$(get_rand_str)"
127 JAVA_NAME_3
="c$(get_rand_str)"
128 create_valid_jre8_env
"${JAVA_NAME_1}"
129 create_valid_jre8_jdk8_env
"${JAVA_NAME_2}"
130 create_valid_jre9_jdk9_env
"${JAVA_NAME_3}"
131 set_default_for_test
"${JAVA_NAME_3}"
132 mess
="Available Java environments:"
133 mess
+="\n ${JAVA_NAME_1}/jre"
134 mess
+="\n ${JAVA_NAME_2}"
135 mess
+="\n ${JAVA_NAME_3} (default)"
136 expected
=$
(echo -e "${mess}")
137 assertEquals
'Wrong status output' "${expected}" "$(do_status)"
140 test_do_status_no_default
() {
141 JAVA_NAME_1
="a$(get_rand_str)"
142 JAVA_NAME_2
="b$(get_rand_str)"
143 JAVA_NAME_3
="c$(get_rand_str)"
144 JAVA_NAME_4
="d$(get_rand_str)"
145 create_valid_jre8_env
"${JAVA_NAME_1}"
146 create_valid_jre8_jdk8_env
"${JAVA_NAME_2}"
147 create_valid_jre9_env
"${JAVA_NAME_3}"
148 create_valid_jre9_jdk9_env
"${JAVA_NAME_4}"
149 mess
="Available Java environments:"
150 mess
+="\n ${JAVA_NAME_1}/jre"
151 mess
+="\n ${JAVA_NAME_2}"
152 mess
+="\n ${JAVA_NAME_3}"
153 mess
+="\n ${JAVA_NAME_4}"
154 mess
+="\nNo Java environment set as default"
155 expected
=$
(echo -e "${mess}")
156 assertEquals
'Wrong status output' "${expected}" "$(do_status)"
159 test_do_status_no_duplicate_entry_jre9_jdk9
() {
160 JAVA_NAME_1
="a$(get_rand_str)"
161 create_valid_jre9_jdk9_env
"${JAVA_NAME_1}"
162 mess
="Available Java environments:"
163 mess
+="\n ${JAVA_NAME_1}"
164 mess
+="\nNo Java environment set as default"
165 expected
=$
(echo -e "${mess}")
166 assertEquals
'Wrong status output' "${expected}" "$(do_status)"
169 test_do_status_no_javas
() {
170 assertEquals
'Wrong status output' 'No compatible Java environment installed' "$(do_status)"
173 test_do_status_jre_but_jdk_exists
() {
174 create_valid_jre8_jdk8_env
"${JAVA_NAME}"
175 set_default_for_test
"${JAVA_NAME}/jre"
176 mess
="Available Java environments:"
177 mess
+="\n ${JAVA_NAME} (${JAVA_NAME}/jre default)"
178 expected
=$
(echo -e "${mess}")
179 assertEquals
'Wrong status output' "${expected}" "$(do_status)"
184 test_do_get_with_jdk8_default
() {
185 create_valid_jre8_jdk8_env
"${JAVA_NAME}"
186 set_default_for_test
"${JAVA_NAME}"
187 assertEquals
'Wrong result' "${JAVA_NAME}" "$(do_get)"
188 assertDefaultSetTo
"${JAVA_NAME}"
191 test_do_get_without_default
() {
192 create_valid_jre8_jdk8_env
"$(get_rand_str)"
193 create_valid_jre9_jdk9_env
"$(get_rand_str)"
194 assertNull
'Wrong result' "$(do_get)"
197 test_do_get_with_initial_null
() {
198 set_default_for_test
"/dev/null"
199 assertNull
'Wrong result' "$(do_get)"
202 test_do_get_with_jdk9_default
() {
203 create_valid_jre9_jdk9_env
"${JAVA_NAME}"
204 set_default_for_test
"${JAVA_NAME}"
205 assertEquals
'Wrong result' "${JAVA_NAME}" "$(do_get)"
206 assertDefaultSetTo
"${JAVA_NAME}"
211 test_do_set_new_invalid
() {
212 create_valid_jre8_jdk8_env
"${JAVA_NAME}"
213 set_default_for_test
"${JAVA_NAME}"
214 output
=$
(do_set
"INVALID")
215 assertEquals
'Wrong status returned' 1 $?
216 assertEquals
'Wrong message' "'${JVM_DIR}/INVALID' is not a valid Java environment path" "${output}"
217 assertDefaultSetTo
"${JAVA_NAME}"
220 test_do_set_current_invalid
() {
221 create_valid_jre8_jdk8_env
"${JAVA_NAME}"
222 set_default_for_test
"INVALID"
223 $
(do_set
"${JAVA_NAME}")
224 assertEquals
'Wrong status returned' 0 $?
225 assertDefaultSetTo
"${JAVA_NAME}"
228 test_do_set_jre8_with_default_already_set_to_jre8
() {
229 create_valid_jre8_env
"${JAVA_NAME}"
230 set_default_for_test
"${JAVA_NAME}/jre"
231 JAVA_NAME_2
="$(get_rand_str)"
232 create_valid_jre8_env
"${JAVA_NAME_2}"
233 $
(do_set
"${JAVA_NAME_2}/jre")
234 assertEquals
'Wrong status returned' 0 $?
235 assertDefaultSetTo
"${JAVA_NAME_2}/jre"
238 test_do_set_jre9_with_default_already_set_to_jre8
() {
239 create_valid_jre8_env
"${JAVA_NAME}"
240 set_default_for_test
"${JAVA_NAME}/jre"
241 JAVA_NAME_2
="$(get_rand_str)"
242 create_valid_jre9_env
"${JAVA_NAME_2}"
243 $
(do_set
"${JAVA_NAME_2}")
244 assertEquals
'Wrong status returned' 0 $?
245 assertDefaultSetTo
"${JAVA_NAME_2}"
248 test_do_set_jdk9_with_default_already_set_to_jdk8
() {
249 create_valid_jre8_jdk8_env
"${JAVA_NAME}"
250 set_default_for_test
"${JAVA_NAME}"
251 JAVA_NAME_2
="$(get_rand_str)"
252 create_valid_jre9_jdk9_env
"${JAVA_NAME_2}"
253 $
(do_set
"${JAVA_NAME_2}")
254 assertEquals
'Wrong status returned' 0 $?
255 assertDefaultSetTo
"${JAVA_NAME_2}"
258 test_do_set_jre8_no_previous_default
() {
259 create_valid_jre8_env
"${JAVA_NAME}"
260 $
(do_set
"${JAVA_NAME}/jre")
261 assertEquals
'Wrong status returned' 0 $?
262 assertDefaultSetTo
"${JAVA_NAME}/jre"
265 test_do_set_jre9_no_previous_default
() {
266 create_valid_jre9_env
"${JAVA_NAME}"
267 $
(do_set
"${JAVA_NAME}")
268 assertEquals
'Wrong status returned' 0 $?
269 assertDefaultSetTo
"${JAVA_NAME}"
272 test_do_set_jdk9_no_previous_default
() {
273 create_valid_jre9_jdk9_env
"${JAVA_NAME}"
274 $
(do_set
"${JAVA_NAME}")
275 assertEquals
'Wrong status returned' 0 $?
276 assertDefaultSetTo
"${JAVA_NAME}"
279 test_do_set_jre8_while_jdk8_also_exists
() {
280 create_valid_jre8_jdk8_env
"${JAVA_NAME}"
281 output
=$
(do_set
"${JAVA_NAME}/jre")
282 assertEquals
'Wrong status returned' 0 $?
283 assertDefaultSetTo
"${JAVA_NAME}/jre"
284 #mess="Warning: '${JAVA_NAME}' looks like a valid JDK whereas you only set '${JAVA_NAME}/jre' as default"
285 #mess+="\nFix this with 'archlinux-java set ${JAVA_NAME}'"
286 #expected=$(echo -e "${mess}")
287 #assertEquals 'Wrong message' "${expected}" "${output}"
290 test_do_set_jdk8_cleanup_old_jre8_default
() {
291 create_valid_jre8_jdk8_env
"${JAVA_NAME}"
292 set_default_for_test
"${JAVA_NAME}"
293 output
=$
(do_set
"${JAVA_NAME}/jre")
294 assertEquals
'Wrong status returned' 0 $?
295 assertDefaultSetTo
"${JAVA_NAME}/jre"
298 test_do_set_jdk9_cleanup_old_jdk8_default
() {
299 JAVA8_NAME
=$
(get_rand_str
)
300 create_valid_jre8_jdk8_env
"${JAVA8_NAME}"
301 set_default_for_test
"${JAVA8_NAME}"
302 JAVA9_NAME
=$
(get_rand_str
)
303 create_valid_jre9_jdk9_env
"${JAVA9_NAME}"
304 output
=$
(do_set
"${JAVA9_NAME}")
305 assertEquals
'Wrong status returned' 0 $?
306 assertDefaultSetTo
"${JAVA9_NAME}"
309 test_do_set_jdk8_devnull
() {
310 create_valid_jre8_jdk8_env
"${JAVA_NAME}"
311 set_default_for_test
"/dev/null"
312 output
=$
(do_set
"${JAVA_NAME}/jre")
313 assertEquals
'Wrong status returned' 0 $?
314 assertDefaultSetTo
"${JAVA_NAME}/jre"
317 test_do_set_jdk9_devnull
() {
318 create_valid_jre9_jdk9_env
"${JAVA_NAME}"
319 set_default_for_test
"/dev/null"
320 output
=$
(do_set
"${JAVA_NAME}")
321 assertEquals
'Wrong status returned' 0 $?
322 assertDefaultSetTo
"${JAVA_NAME}"
327 test_do_unset_jre8
() {
328 create_valid_jre8_env
"${JAVA_NAME}"
329 set_default_for_test
"${JAVA_NAME}/jre"
334 test_do_unset_no_default_link
() {
335 create_valid_jre8_jdk8_env
"${JAVA_NAME}"
336 set_default_for_test
"${JAVA_NAME}"
337 unlink
${DEFAULT_PATH}
342 test_do_unset_devnull
() {
343 set_default_for_test
"/dev/null"
348 test_do_unset_jre9
() {
349 create_valid_jre9_env
"${JAVA_NAME}"
350 set_default_for_test
"${JAVA_NAME}"
355 test_do_unset_jdk9
() {
356 create_valid_jre9_jdk9_env
"${JAVA_NAME}"
357 set_default_for_test
"${JAVA_NAME}"
364 test_do_fix_default_valid_jre8
() {
365 create_valid_jre8_env
"${JAVA_NAME}"
366 set_default_for_test
"${JAVA_NAME}/jre"
368 assertDefaultSetTo
"${JAVA_NAME}/jre"
371 test_do_fix_default_valid_jre9
() {
372 create_valid_jre9_env
"${JAVA_NAME}"
373 set_default_for_test
"${JAVA_NAME}"
375 assertDefaultSetTo
"${JAVA_NAME}"
378 test_do_fix_default_valid_jdk9
() {
379 create_valid_jre9_jdk9_env
"${JAVA_NAME}"
380 set_default_for_test
"${JAVA_NAME}"
382 assertDefaultSetTo
"${JAVA_NAME}"
385 test_do_fix_no_default_jre8
() {
386 create_valid_jre8_env
"${JAVA_NAME}"
388 assertDefaultSetTo
"${JAVA_NAME}/jre"
391 test_do_fix_no_default_jre9
() {
392 create_valid_jre9_env
"${JAVA_NAME}"
394 assertDefaultSetTo
"${JAVA_NAME}"
397 test_do_fix_no_default_jdk9
() {
398 create_valid_jre9_jdk9_env
"${JAVA_NAME}"
400 assertDefaultSetTo
"${JAVA_NAME}"
403 test_do_fix_default_invalid_openjdk7_jdk_installed
() {
404 create_valid_jre8_jdk8_env
"java-8-openjdk"
405 create_valid_jre8_jdk8_env
"${JAVA_NAME}"
407 assertDefaultSetTo
"java-8-openjdk"
410 test_do_fix_jdk8_not_default_but_openjdk7_jre_installed
() {
411 create_valid_jre8_jdk8_env
"${JAVA_NAME}"
412 create_valid_jre8_env
"java-8-openjdk"
414 assertDefaultSetTo
"java-8-openjdk/jre"
417 test_do_fix_jdk9_not_default_but_openjdk7_jre_installed
() {
418 create_valid_jre9_jdk9_env
"${JAVA_NAME}"
419 create_valid_jre8_env
"java-8-openjdk"
421 assertDefaultSetTo
"java-8-openjdk/jre"
424 test_do_fix_default_invalid_random_java8_installed
() {
425 create_valid_jre8_jdk8_env
"${JAVA_NAME}"
427 assertDefaultSetTo
"${JAVA_NAME}"
430 test_do_fix_default_invalid_random_java9_installed
() {
431 create_valid_jre9_jdk9_env
"${JAVA_NAME}"
433 assertDefaultSetTo
"${JAVA_NAME}"
436 test_do_fix_default_invalid_no_java_installed
() {
437 assertEquals
'Wrong message' "No valid Java environment found" "$(do_fix)"
441 test_do_fix_jre8_but_jdk8_exists
() {
442 create_valid_jre8_jdk8_env
"${JAVA_NAME}"
443 set_default_for_test
"${JAVA_NAME}/jre"
445 assertDefaultSetTo
"${JAVA_NAME}"
448 test_do_fix_jdk_but_jre_exists
() {
449 create_valid_jre8_jdk8_env
"java-7-openjdk"
450 create_valid_jre8_env
"${JAVA_NAME}"
451 # Only JRE is installed but setting JDK as default
452 set_default_for_test
"${JAVA_NAME}"
454 assertDefaultSetTo
"${JAVA_NAME}/jre"
457 test_do_fix_devnull
() {
458 create_valid_jre8_jdk8_env
"${JAVA_NAME}"
459 set_default_for_test
"/dev/null"
461 assertDefaultSetTo
"${JAVA_NAME}"