Date: Saturday, August 2, 2014 @ 20:58:05 Author: guillaume Revision: 218519
Rewrite archlinux-java for cleanup Modified: java-common/trunk/PKGBUILD java-common/trunk/bin_archlinux-java java-common/trunk/install_java-common.sh java-common/trunk/profile_jre.csh java-common/trunk/profile_jre.sh java-common/trunk/test_archlinux-java ------------------------+ PKGBUILD | 8 - bin_archlinux-java | 131 +++++++++++++------------------ install_java-common.sh | 4 profile_jre.csh | 1 profile_jre.sh | 1 test_archlinux-java | 193 +++++++++++++++++++---------------------------- 6 files changed, 143 insertions(+), 195 deletions(-) Modified: PKGBUILD =================================================================== --- PKGBUILD 2014-08-02 17:46:07 UTC (rev 218518) +++ PKGBUILD 2014-08-02 18:58:05 UTC (rev 218519) @@ -19,10 +19,10 @@ bin_java-common-wrapper https://shunit2.googlecode.com/files/shunit2-2.1.6.tgz) -sha256sums=('9f0032e47705ce55a615ab3b942720b8447f490d1ff430b214038b81c3019ac2' - '8dbafa311f34d23c5bbef71fa942b97b224ebeb330b36195f22a1836285bbb83' - 'bf614a14c769c28923866e5d0b0b5836ea2111f39a60f74f5b7ac325157a6c1a' - '718c5fc1b7182da6b85b2e5e302c0e418fd2f4a93731c24b8fbd0ee338ae81b3' +sha256sums=('d5d583325751bf5cb43cca448f0d5c0a868958d19baf2db3694c9948b9d8974e' + '523c88cfe78667960a7c04ef1a0983371690f42153c54174711281337b556378' + '8ca7eea69eeaa62a5603d66c10a4f25ce57911b89220b46876355f604701c047' + '0dd93c9779c617af061b25f5e3d23cacb741d8d757f533312c3af976661e8cbc' '622927af9ec374140a878993936f7fdd4206fe069dddfcefc6d4e15fa6d67f39' '65a313a76fd5cc1c58c9e19fbc80fc0e418a4cbfbd46d54b35ed5b6e0025d4ee') Modified: bin_archlinux-java =================================================================== --- bin_archlinux-java 2014-08-02 17:46:07 UTC (rev 218518) +++ bin_archlinux-java 2014-08-02 18:58:05 UTC (rev 218519) @@ -52,20 +52,12 @@ echo ${javas[@]} } -# $1: Java environment name to test. MUST NOT BE "${DEFAULT_NAME}" +# $1: Java environment name to test is_java_valid() { test "x$1" != "x${DEFAULT_NAME}" && test -x ${JVM_DIR}/$1/bin/java } set_bin_links() { - # TODO this 'unlink' part should be in its own function - find -L ${BIN_PATH} -samefile ${WRAPPER_PATH} \ - | while read lpath; do - if [ ! -x "${DEFAULT_PATH}/bin/$(basename ${lpath})" ]; then - unlink ${lpath} 2>/dev/null - fi - done - find ${DEFAULT_PATH}/bin ! -type d -perm /a=x \ | while read bpath; do ln -sf ${WRAPPER_PATH} ${BIN_PATH}/$(basename ${bpath}) > /dev/null 2>&1 @@ -72,40 +64,31 @@ done } +unset_bin_links() { + find -L ${BIN_PATH} -samefile ${WRAPPER_PATH} \ + | while read lpath; do + unlink ${lpath} + done +} + # $1: Java environment name to set as default set_default_link_to() { - # FIXME if we do not "unlink", then test_force_set_default_with_default fails unlink ${DEFAULT_PATH} 2>/dev/null ln -sf $1 ${DEFAULT_PATH} } -# $1: Java environment name to set as default -set_java_to() { - set_default_link_to $1 - set_bin_links -# parent_dir=$(dirname $1) -# if is_java_valid ${parent_dir}; then -# echo "Warning: '${parent_dir}' looks like a valid JDK whereas you are only setting '$1' as default" -# echo "Fix this with 'archlinux-java --force-set ${parent_dir}'" -# fi -} - -unset_java() { - find ${DEFAULT_PATH}/bin ! -type d -perm /a=x \ - | while read bpath; do - unlink ${BIN_PATH}/$(basename ${bpath}) 2>/dev/null - done +unset_default_link() { unlink ${DEFAULT_PATH} 2>/dev/null } # First level functions -give_status() { +do_status() { installed_java=($(get_installed_javas)) - default_java=$(get_default_java) if [ ${#installed_java[@]} -eq 0 ]; then echo 'No compatible Java environment installed' else + default_java=$(get_default_java) echo 'Available Java environments:' for java in ${installed_java[@]}; do if [ ${java} = "${default_java}" ]; then @@ -120,26 +103,12 @@ fi } -# $1: Java environment name to try to set -try_set_default() { - if is_java_valid $1; then - default_java=$(get_default_java) - if [ "${default_java}" = "$1/jre" ] \ - || ( [ "x${default_java}" != "x" ] && ! is_java_valid ${default_java} ); then - unset_java - fi - if [ "x$(get_default_java)" = "x" ]; then - set_default_link_to $1 - fi - set_bin_links - fi - if [ "x$(get_default_java)" != "x$1" ]; then - exit 1 - fi +do_get() { + get_default_java } # $1: Java environment name to set as default -force_set_default() { +do_set() { if ! is_java_valid $1; then echo "'${JVM_DIR}/$1' is not a valid Java environment path" exit 1 @@ -146,62 +115,70 @@ fi default=$(get_default_java) if [ "x$1" != "x${default}" ] || ! is_java_valid ${default}; then + unset_default_link + unset_bin_links set_default_link_to $1 fi set_bin_links + + #parent_dir=$(dirname $1) + #if is_java_valid ${parent_dir}; then + # echo "Warning: '${parent_dir}' looks like a valid JDK whereas you only set '$1' as default" + # echo "Fix this with 'archlinux-java set ${parent_dir}'" + #fi } -# $1: Java environment name to try to unset -try_unset_default() { - # This function should be used by pacman '.install' scripts. Its goal is: - # "if the current Java env is set to $1, then remove it" - # So no need to report a failure if current Java is not equal to the one passed as param - if [ "x$1" != "x" -a "x$(get_default_java)" = "x$1" ]; then - unset_java - fi +# $1: Java environment name to unset +do_unset() { + unset_default_link + unset_bin_links } -fix_default() { +do_fix() { default=$(get_default_java) if is_java_valid ${default}; then if is_java_valid $(dirname ${default}); then - unset_java - set_java_to $(dirname ${default}) + unset_default_link + set_default_link_to $(dirname ${default}) fi + unset_bin_links + set_bin_links else - if [ "x${default}" != "x" ]; then - unset_java - fi - to_check=('java-7-openjdk' 'java-7-openjdk/jre' $(get_installed_javas)) + prev=$(readlink ${DEFAULT_PATH}) + unset_default_link + unset_bin_links + prev_fix=("${prev/\/jre}" "${prev}/jre") + openjdk7=('java-7-openjdk' 'java-7-openjdk/jre') + to_check=(${prev_fix[@]} ${openjdk7[@]} $(get_installed_javas)) for java in ${to_check[@]}; do if ! is_java_valid $(get_default_java) && is_java_valid ${java}; then - set_java_to ${java} + set_default_link_to ${java} + set_bin_links fi done fi - if is_java_valid $(get_default_java); then - set_bin_links - else + if ! is_java_valid $(get_default_java); then echo 'No valid Java environment found' fi } usage() { - echo "$(basename $0) [ --status | --try-set <JAVA_ENV> | --force-set <JAVA_ENV> | --try-unset <JAVA_ENV> | --fix ]" - echo -e "\n\t<JAVA_ENV> must be the name of a Java environment directory available in ${JVM_DIR}\n" - echo -e '\t--status\t\tList installed Java environments and enabled one' - echo -e '\t--try-set <JAVA_ENV>\tIf no valid Java environments is already set as default, then set this one' - echo -e '\t--force-set <JAVA_ENV>\tForce <JAVA_ENV> as default' - echo -e '\t--try-unset <JAVA_ENV>\tIf <JAVA_ENV> is set as default, then unset it' - echo -e '\t--fix\t\t\tFix an invalid default Java environment configuration' + echo "$(basename $0) <COMMAND>" + echo -e "\nCOMMAND:" + echo -e '\tstatus\t\tList installed Java environments and enabled one' + echo -e '\tget\t\tReturns the short name of the Java environment set as default' + echo -e '\tset <JAVA_ENV>\tForce <JAVA_ENV> as default' + echo -e '\tunset\t\tUnset current default Java environment' + echo -e '\tfix\t\tFix an invalid/broken default Java environment configuration' } ## Main case $1 in - '--status') give_status;; - '--try-set') check_root; check_param_count $# 2; try_set_default $2;; - '--force-set') check_root; check_param_count $# 2; force_set_default $2;; - '--try-unset') check_root; check_param_count $# 2; try_unset_default $2;; - '--fix') check_root; fix_default;; - *) usage;; + 'status') do_status;; + 'get') do_get;; + 'set') check_root; check_param_count $# 2; do_set $2;; + 'unset') check_root; do_unset;; + 'fix') check_root; do_fix;; + 'help' | '--help' | '-h' | '') usage;; + *) echo "$(basename $0): unknown option '$@'"; exit 1;; esac Modified: install_java-common.sh =================================================================== --- install_java-common.sh 2014-08-02 17:46:07 UTC (rev 218518) +++ install_java-common.sh 2014-08-02 18:58:05 UTC (rev 218519) @@ -1,7 +1,9 @@ post_install() { - echo 'Installing multiple non-conflicting Java environments is now supported.' + echo 'Installing multiple non-conflicting Java environments is now supported' echo 'Check the following wiki section for more info:' echo ' https://wiki.archlinux.org/index.php/Java#Multiple_official_Java_environments' + echo 'Please make sure JAVA_HOME is set to /usr/lib/jvm/java-default-runtime' + echo 'If not, you may have to logout and login again to set it according to /etc/profile.d/jre.sh' } post_upgrade() { Modified: profile_jre.csh =================================================================== --- profile_jre.csh 2014-08-02 17:46:07 UTC (rev 218518) +++ profile_jre.csh 2014-08-02 18:58:05 UTC (rev 218519) @@ -1,4 +1,3 @@ # Do not change this unless you want to completely by-pass Arch Linux' way # of handling Java versions and vendors. Instead, please use script `archlinux-java` -# to change what /usr/lib/jvm/java-default-runtime points at. setenv JAVA_HOME "/usr/lib/jvm/java-default-runtime" Modified: profile_jre.sh =================================================================== --- profile_jre.sh 2014-08-02 17:46:07 UTC (rev 218518) +++ profile_jre.sh 2014-08-02 18:58:05 UTC (rev 218519) @@ -1,4 +1,3 @@ # Do not change this unless you want to completely by-pass Arch Linux' way # of handling Java versions and vendors. Instead, please use script `archlinux-java` -# to change what /usr/lib/jvm/java-default-runtime points at. export JAVA_HOME=/usr/lib/jvm/java-default-runtime Modified: test_archlinux-java =================================================================== --- test_archlinux-java 2014-08-02 17:46:07 UTC (rev 218518) +++ test_archlinux-java 2014-08-02 18:58:05 UTC (rev 218519) @@ -41,7 +41,6 @@ rm -rf ${test_dir} } - # $1: path of Java environment to create create_valid_jre_env() { mkdir -p ${JVM_DIR}/$1/bin @@ -95,10 +94,9 @@ done } +# do_status -# give_status - -test_give_status() { +test_do_status() { JAVA_NAME_1="a$(get_rand_str)" JAVA_NAME_2="b$(get_rand_str)" create_valid_jre_env "${JAVA_NAME_1}/jre" @@ -105,10 +103,10 @@ set_default_for_test "${JAVA_NAME_1}/jre" create_valid_jdk_env "${JAVA_NAME_2}" expected=$(echo -e "Available Java environments:\n ${JAVA_NAME_1}/jre (default)\n ${JAVA_NAME_2}") - assertEquals 'Wrong status output' "${expected}" "$(give_status)" + assertEquals 'Wrong status output' "${expected}" "$(do_status)" } -test_give_status_no_default() { +test_do_status_no_default() { JAVA_NAME_1="a$(get_rand_str)" JAVA_NAME_2="b$(get_rand_str)" create_valid_jre_env "${JAVA_NAME_1}/jre" @@ -118,199 +116,172 @@ mess+="\n ${JAVA_NAME_2}" mess+="\nNo Java environment set as default" expected=$(echo -e "${mess}") - assertEquals 'Wrong status output' "${expected}" "$(give_status)" + assertEquals 'Wrong status output' "${expected}" "$(do_status)" } -test_give_status_no_javas() { - output=$(give_status) - assertEquals 'Wrong status output' 'No compatible Java environment installed' "${output}" +test_do_status_no_javas() { + assertEquals 'Wrong status output' 'No compatible Java environment installed' "$(do_status)" } +# do_get -# try_set_default - -test_try_set_default_no_default() { +test_do_get_with_default() { create_valid_jdk_env "${JAVA_NAME}" - output=$(try_set_default "${JAVA_NAME}") - assertEquals 'Wrong status returned' 0 $? + set_default_for_test "${JAVA_NAME}" + assertEquals 'Wrong result' "${JAVA_NAME}" "$(do_get)" assertDefaultSetTo "${JAVA_NAME}" } -test_try_set_default_invalid_default_link() { - create_valid_jre_env "${JAVA_NAME}/jre" - set_default_for_test "non_existing" - output=$(try_set_default "${JAVA_NAME}/jre") - assertEquals 'Wrong status returned' 0 $? - assertDefaultSetTo "${JAVA_NAME}/jre" -} - -test_try_set_default_invalid_bin_link() { - create_valid_jre_env "${JAVA_NAME}/jre" - set_default_for_test "${JAVA_NAME}/jre" - unlink "${BIN_PATH}/java" - output=$(try_set_default "${JAVA_NAME}/jre") - assertEquals 'Wrong status returned' 0 $? - assertDefaultSetTo "${JAVA_NAME}/jre" -} - -test_try_set_default_default_already_set() { - create_valid_jre_env "${JAVA_NAME}/jre" - set_default_for_test "${JAVA_NAME}/jre" - output=$(try_set_default "${JAVA_NAME}/jre") - assertEquals 'Wrong status returned' 0 $? - assertDefaultSetTo "${JAVA_NAME}/jre" -} - -test_try_set_default_other_java_set() { - JAVA_NAME_2="$(get_rand_str)" - create_valid_jre_env "${JAVA_NAME}/jre" - create_valid_jdk_env "${JAVA_NAME_2}" - set_default_for_test "${JAVA_NAME_2}" - output=$(try_set_default "${JAVA_NAME}/jre") - assertEquals 'Wrong status returned' 1 $? - assertDefaultSetTo "${JAVA_NAME_2}" -} - -test_try_set_default_jdk_where_jre_is_default() { +test_do_get_without_default() { create_valid_jdk_env "${JAVA_NAME}" - set_default_for_test "${JAVA_NAME}/jre" - output=$(try_set_default "${JAVA_NAME}") - assertEquals 'Wrong status returned' 0 $? - assertDefaultSetTo "${JAVA_NAME}" + assertNull 'Wrong result' "$(do_get)" } -test_try_set_default_new_is_invalid() { - output=$(try_set_default "INVALID") - assertEquals 'Wrong status returned' 1 $? - assertDefaultNotSet -} +# do_set - -# force_set_default - -test_force_set_default_new_invalid() { +test_do_set_new_invalid() { create_valid_jdk_env "${JAVA_NAME}" set_default_for_test "${JAVA_NAME}" - output=$(force_set_default "INVALID") + output=$(do_set "INVALID") assertEquals 'Wrong status returned' 1 $? assertEquals 'Wrong message' "'${JVM_DIR}/INVALID' is not a valid Java environment path" "${output}" assertDefaultSetTo "${JAVA_NAME}" } -test_force_set_default_current_invalid() { +test_do_set_current_invalid() { create_valid_jdk_env "${JAVA_NAME}" set_default_for_test "INVALID" - output=$(force_set_default "${JAVA_NAME}") + $(do_set "${JAVA_NAME}") assertEquals 'Wrong status returned' 0 $? assertDefaultSetTo "${JAVA_NAME}" } -test_force_set_default_same_java() { +test_do_set_same_java_missing_bin_link() { create_valid_jdk_env "${JAVA_NAME}" set_default_for_test "${JAVA_NAME}" unlink ${BIN_PATH}/java - output=$(force_set_default "${JAVA_NAME}") + $(do_set "${JAVA_NAME}") assertEquals 'Wrong status returned' 0 $? assertDefaultSetTo "${JAVA_NAME}" } -test_force_set_default_with_default() { +test_do_set_with_default_already_set() { create_valid_jre_env "${JAVA_NAME}/jre" set_default_for_test "${JAVA_NAME}/jre" JAVA_NAME_2="$(get_rand_str)" create_valid_jre_env "${JAVA_NAME_2}/jre" - output=$(force_set_default "${JAVA_NAME_2}/jre") + $(do_set "${JAVA_NAME_2}/jre") assertEquals 'Wrong status returned' 0 $? assertDefaultSetTo "${JAVA_NAME_2}/jre" } -test_force_set_default_no_default() { +test_do_set_no_default() { create_valid_jre_env "${JAVA_NAME}/jre" - output=$(force_set_default "${JAVA_NAME}/jre") + $(do_set "${JAVA_NAME}/jre") assertEquals 'Wrong status returned' 0 $? assertDefaultSetTo "${JAVA_NAME}/jre" } -test_force_set_default_target_invalid() { - create_valid_jre_env "${JAVA_NAME}/jre" - output=$(force_set_default "INVALID") - assertEquals 'Wrong status returned' 1 $? - expected="'${JVM_DIR}/INVALID' is not a valid Java environment path" - assertEquals 'Wrong message' "${expected}" "${output}" - assertDefaultNotSet +test_do_set_exists_jdk() { + create_valid_jdk_env "${JAVA_NAME}" + output=$(do_set "${JAVA_NAME}/jre") + assertEquals 'Wrong status returned' 0 $? + assertDefaultSetTo "${JAVA_NAME}/jre" + #mess="Warning: '${JAVA_NAME}' looks like a valid JDK whereas you only set '${JAVA_NAME}/jre' as default" + #mess+="\nFix this with 'archlinux-java set ${JAVA_NAME}'" + #expected=$(echo -e "${mess}") + #assertEquals 'Wrong message' "${expected}" "${output}" } - -# try_unset_default - -test_try_unset_default_no_default() { +test_do_set_cleanup_old_java_default() { create_valid_jdk_env "${JAVA_NAME}" - try_unset_default "${JAVA_NAME}" - assertDefaultNotSet + set_default_for_test "${JAVA_NAME}" + output=$(do_set "${JAVA_NAME}/jre") + assertEquals 'Wrong status returned' 0 $? + assertDefaultSetTo "${JAVA_NAME}/jre" } -test_try_unset_default_default_not_target() { +# do_unset + +test_do_unset_default_not_target() { create_valid_jre_env "${JAVA_NAME}/jre" set_default_for_test "${JAVA_NAME}/jre" - JAVA_NAME_2=$(get_rand_str) - create_valid_jdk_env "${JAVA_NAME_2}" - try_unset_default "${JAVA_NAME_2}" - assertDefaultSetTo "${JAVA_NAME}/jre" + do_unset + assertDefaultNotSet } -test_try_unset_default_default_is_target() { - create_valid_jre_env "${JAVA_NAME}/jre" - set_default_for_test "${JAVA_NAME}/jre" - try_unset_default "${JAVA_NAME}/jre" +test_do_unset_no_default_link() { + create_valid_jdk_env "${JAVA_NAME}" + set_default_for_test "${JAVA_NAME}" + unlink ${DEFAULT_PATH} + do_unset assertDefaultNotSet } +# do_fix -# fix_default - -test_fix_default_default_valid() { +test_do_fix_default_valid() { create_valid_jre_env "${JAVA_NAME}/jre" set_default_for_test "${JAVA_NAME}/jre" - fix_default + do_fix assertDefaultSetTo "${JAVA_NAME}/jre" } -test_fix_default_no_default() { +test_do_fix_no_default() { create_valid_jre_env "${JAVA_NAME}/jre" - fix_default + do_fix assertDefaultSetTo "${JAVA_NAME}/jre" } -test_fix_default_default_invalid_openjdk7_jdk_installed() { +test_do_fix_default_invalid_openjdk7_jdk_installed() { create_valid_jdk_env "java-7-openjdk" - fix_default + create_valid_jdk_env "${JAVA_NAME}" + do_fix assertDefaultSetTo "java-7-openjdk" } -test_fix_default_default_invalid_openjdk7_jre_installed() { +test_do_fix_default_invalid_openjdk7_jre_installed() { + create_valid_jdk_env "${JAVA_NAME}" create_valid_jre_env "java-7-openjdk/jre" - fix_default + do_fix assertDefaultSetTo "java-7-openjdk/jre" } -test_fix_default_default_invalid_random_java_installed() { +test_do_fix_default_invalid_random_java_installed() { create_valid_jdk_env "${JAVA_NAME}" - fix_default + do_fix assertDefaultSetTo "${JAVA_NAME}" } -test_fix_default_default_invalid_no_java_installed() { - assertEquals 'Wrong message' "No valid Java environment found" "$(fix_default)" +test_do_fix_default_invalid_no_java_installed() { + assertEquals 'Wrong message' "No valid Java environment found" "$(do_fix)" assertDefaultNotSet } -test_fix_default_jre_but_jdk_exists() { +test_do_fix_jre_but_jdk_exists() { create_valid_jdk_env "${JAVA_NAME}" set_default_for_test "${JAVA_NAME}/jre" - fix_default + do_fix assertDefaultSetTo "${JAVA_NAME}" } +test_do_fix_jdk_but_jre_exists() { + create_valid_jdk_env "java-7-openjdk" + create_valid_jre_env "${JAVA_NAME}/jre" + set_default_for_test "${JAVA_NAME}" + do_fix + assertDefaultSetTo "${JAVA_NAME}/jre" +} +test_do_fix_cleanup_old_java_default() { + create_valid_jre_env "${JAVA_NAME}/jre" + set_default_for_test "${JAVA_NAME}/jre" + # Simulate installation of a jre over a jre-headless + ln -s ${WRAPPER_PATH} ${BIN_PATH}/policytool + output=$(do_fix "${JAVA_NAME}/jre") + assertEquals 'Wrong status returned' 0 $? + assertDefaultSetTo "${JAVA_NAME}/jre" +} + # load shunit2 . shunit2-2.1.6/src/shunit2