Skip to content
This repository was archived by the owner on Feb 4, 2023. It is now read-only.

Commit bf72c76

Browse files
committed
[refactoring] made function comparableJavaVersionNumber() more robust // refs #47
the function now creates a 8 digit numeral from whatever form of Java version string or requirement string. See test results below for supported version strings test results: ######################################################## Testing function JavaVersionSatisfiesRequirement() Tests with Java 1.6: [TEST OK] Version number '1.6' has comparable form '06000000' (matches expected result '06000000') [TEST OK] Version number '1.6+' has comparable form '06000000' (matches expected result '06000000') [TEST OK] Version number '1.6.0_45' has comparable form '06000045' (matches expected result '06000045') [TEST OK] Version number '1.6.0_100' has comparable form '06000100' (matches expected result '06000100') [TEST OK] Version number '1.6.1_87' has comparable form '06001087' (matches expected result '06001087') Tests with Java 1.7: [TEST OK] Version number '1.7.0_76' has comparable form '07000076' (matches expected result '07000076') [TEST OK] Version number '1.7.0_144' has comparable form '07000144' (matches expected result '07000144') Tests with Java 1.8: [TEST OK] Version number '1.8' has comparable form '08000000' (matches expected result '08000000') [TEST OK] Version number '1.8*' has comparable form '08000000' (matches expected result '08000000') [TEST OK] Version number '1.8.0_98' has comparable form '08000098' (matches expected result '08000098') [TEST OK] Version number '1.8.0_151' has comparable form '08000151' (matches expected result '08000151') Tests with Java 9: [TEST OK] Version number '9' has comparable form '09000000' (matches expected result '09000000') [TEST OK] Version number '9+' has comparable form '09000000' (matches expected result '09000000') [TEST OK] Version number '9-ea' has comparable form '09000000' (matches expected result '09000000') [TEST OK] Version number '9.2' has comparable form '09002000' (matches expected result '09002000') [TEST OK] Version number '9.2*' has comparable form '09002000' (matches expected result '09002000') [TEST OK] Version number '9.0.1' has comparable form '09000001' (matches expected result '09000001') [TEST OK] Version number '9.0.13' has comparable form '09000013' (matches expected result '09000013') [TEST OK] Version number '9.1.3' has comparable form '09001003' (matches expected result '09001003') [TEST OK] Version number '9.11' has comparable form '09011000' (matches expected result '09011000') [TEST OK] Version number '9.10.23' has comparable form '09010023' (matches expected result '09010023') [TEST OK] Version number '9.10.101' has comparable form '09010101' (matches expected result '09010101') Tests with Java 10: [TEST OK] Version number '10' has comparable form '10000000' (matches expected result '10000000') [TEST OK] Version number '10*' has comparable form '10000000' (matches expected result '10000000') [TEST OK] Version number '10-ea' has comparable form '10000000' (matches expected result '10000000') [TEST OK] Version number '10.1' has comparable form '10001000' (matches expected result '10001000') [TEST OK] Version number '10.1+' has comparable form '10001000' (matches expected result '10001000') [TEST OK] Version number '10.0.1' has comparable form '10000001' (matches expected result '10000001') [TEST OK] Version number '10.0.13' has comparable form '10000013' (matches expected result '10000013') [TEST OK] Version number '10.1.3' has comparable form '10001003' (matches expected result '10001003') [TEST OK] Version number '10.12' has comparable form '10012000' (matches expected result '10012000') [TEST OK] Version number '10.10.23' has comparable form '10010023' (matches expected result '10010023') [TEST OK] Version number '10.10.113' has comparable form '10010113' (matches expected result '10010113')
1 parent 65100ec commit bf72c76

2 files changed

Lines changed: 81 additions & 6 deletions

File tree

src/universalJavaApplicationStub

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# #
1414
# @author Tobias Fischer #
1515
# @url https://github.com/tofi86/universalJavaApplicationStub #
16-
# @date 2017-10-29 #
16+
# @date 2017-10-30 #
1717
# @version 2.1.0 #
1818
# #
1919
# #
@@ -323,10 +323,15 @@ function extractJavaMajorVersion() {
323323

324324
# helper function:
325325
# return comparable version for java version string
326-
# basically just strip punctuation and leading '1.'
326+
# return value is an 8 digit numeral
327327
##########################################################
328328
function comparableJavaVersionNumber() {
329-
echo $1 | sed -E 's/^1\.//g;s/[[:punct:]]//g'
329+
# cleaning: 1) remove leading '1.'; 2) remove 'a-Z' and '-*+' (e.g. '-ea'); 3) replace '_' with '.'
330+
cleaned=$(echo "$1" | sed -E 's/^1\.//g;s/[a-zA-Z+*\-]//g;s/_/./g')
331+
# splitting at '.' into an array
332+
arr=( ${cleaned//./ } )
333+
# echo a string with left padded version numbers
334+
echo "$(printf '%02s' ${arr[0]})$(printf '%03s' ${arr[1]})$(printf '%03s' ${arr[2]})"
330335
}
331336

332337

test/java-version-tester.sh

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

33
# Java JRE version tester
4-
# tofi86 @ 2017-10-29
4+
# tofi86 @ 2017-10-30
55

66

77

@@ -25,10 +25,15 @@ function extractJavaMajorVersion() {
2525

2626
# helper function:
2727
# return comparable version for java version string
28-
# basically just strip punctuation and leading '1.'
28+
# return value is an 8 digit numeral
2929
##########################################################
3030
function comparableJavaVersionNumber() {
31-
echo $1 | sed -E 's/^1\.//g;s/[[:punct:]]//g'
31+
# cleaning: 1) remove leading '1.'; 2) remove 'a-Z' and '-*+' (e.g. '-ea'); 3) replace '_' with '.'
32+
cleaned=$(echo "$1" | sed -E 's/^1\.//g;s/[a-zA-Z+*\-]//g;s/_/./g')
33+
# splitting at '.' into an array
34+
arr=( ${cleaned//./ } )
35+
# echo a string with left padded version numbers
36+
echo "$(printf '%02s' ${arr[0]})$(printf '%03s' ${arr[1]})$(printf '%03s' ${arr[2]})"
3237
}
3338

3439

@@ -149,6 +154,71 @@ testExtractMajor "10.10.120+" "10"
149154
testExtractMajor "10.100.120+" "10"
150155

151156

157+
158+
# test function:
159+
# tests the comparableJavaVersionNumber() function
160+
##########################################################
161+
function testComparable() {
162+
version=$1
163+
expected=$2
164+
actual=$(comparableJavaVersionNumber $version)
165+
if [ "$actual" == "$expected" ] ; then
166+
echo "[TEST OK] Version number '$version' has comparable form '$actual' (matches expected result '$expected')"
167+
else
168+
echo "[TEST FAILED] Version number '$version' has comparable form '$actual' (DOES NOT MATCH expected result '$expected')"
169+
fi
170+
}
171+
172+
173+
echo ""
174+
echo ""
175+
echo "########################################################"
176+
echo "Testing function JavaVersionSatisfiesRequirement()"
177+
echo ""
178+
echo "Tests with Java 1.6:"
179+
testComparable "1.6" "06000000"
180+
testComparable "1.6+" "06000000"
181+
testComparable "1.6.0_45" "06000045"
182+
testComparable "1.6.0_100" "06000100"
183+
testComparable "1.6.1_87" "06001087"
184+
echo ""
185+
echo "Tests with Java 1.7:"
186+
testComparable "1.7.0_76" "07000076"
187+
testComparable "1.7.0_144" "07000144"
188+
echo ""
189+
echo "Tests with Java 1.8:"
190+
testComparable "1.8" "08000000"
191+
testComparable "1.8*" "08000000"
192+
testComparable "1.8.0_98" "08000098"
193+
testComparable "1.8.0_151" "08000151"
194+
echo ""
195+
echo "Tests with Java 9:"
196+
testComparable "9" "09000000"
197+
testComparable "9+" "09000000"
198+
testComparable "9-ea" "09000000"
199+
testComparable "9.2" "09002000"
200+
testComparable "9.2*" "09002000"
201+
testComparable "9.0.1" "09000001"
202+
testComparable "9.0.13" "09000013"
203+
testComparable "9.1.3" "09001003"
204+
testComparable "9.11" "09011000"
205+
testComparable "9.10.23" "09010023"
206+
testComparable "9.10.101" "09010101"
207+
echo ""
208+
echo "Tests with Java 10:"
209+
testComparable "10" "10000000"
210+
testComparable "10*" "10000000"
211+
testComparable "10-ea" "10000000"
212+
testComparable "10.1" "10001000"
213+
testComparable "10.1+" "10001000"
214+
testComparable "10.0.1" "10000001"
215+
testComparable "10.0.13" "10000013"
216+
testComparable "10.1.3" "10001003"
217+
testComparable "10.12" "10012000"
218+
testComparable "10.10.23" "10010023"
219+
testComparable "10.10.113" "10010113"
220+
221+
152222
# test function:
153223
# tests the JavaVersionSatisfiesRequirement() function
154224
##########################################################

0 commit comments

Comments
 (0)