Skip to content

Commit f86bc0b

Browse files
ketankares
authored andcommitted
Improve jvm version detection for java 9 (#143)
* Fix tests broken because of removal of JRuby <= 1.7.21 support * Parse java 9 style version strings as defined by JEP223 Fixes #142 * Move over to openjdk7 since oraclejdk7 is no longer available See travis-ci/travis-ci#8423
1 parent adc2f75 commit f86bc0b

File tree

3 files changed

+118
-23
lines changed

3 files changed

+118
-23
lines changed

.travis.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: ruby
22
sudo: false
33
jdk:
4-
- oraclejdk7
4+
- openjdk7
55
- oraclejdk8
66
env:
77
- TEST_PROFILE=test-1.7.20
@@ -22,13 +22,13 @@ matrix:
2222
- env: TEST_COMMAND="jruby -S rake integration:install integration:test"
2323
rvm: jruby-1.7.20
2424
include:
25-
- jdk: oraclejdk7
25+
- jdk: openjdk7
2626
env: TEST_COMMAND="jruby -rbundler/setup -S rmvn test-compile && jruby -S rake test" BUNDLE_INSTALL=true
2727
rvm: jruby-1.7.24
28-
- jdk: oraclejdk7
28+
- jdk: openjdk7
2929
env: TEST_COMMAND="jruby -rbundler/setup -S rmvn test-compile && jruby -S rake test" BUNDLE_INSTALL=true
3030
rvm: jruby-1.7.25
31-
- jdk: oraclejdk7
31+
- jdk: openjdk7
3232
env: TEST_COMMAND="jruby -rbundler/setup -S rmvn test-compile && jruby -S rake test" BUNDLE_INSTALL=true
3333
rvm: jruby-1.7.26
3434
#
@@ -42,13 +42,13 @@ matrix:
4242
- jdk: oraclejdk8
4343
env: TEST_COMMAND="jruby -S rake integration:install integration:test"
4444
rvm: jruby-1.7.26
45-
- jdk: oraclejdk7
45+
- jdk: openjdk7
4646
env: TEST_COMMAND="jruby -S rake integration:install integration:test"
4747
rvm: jruby-1.7.18
4848
- jdk: oraclejdk8
4949
env: TEST_COMMAND="jruby -S rake integration:install integration:test"
5050
rvm: jruby-9.0.5.0
51-
- jdk: oraclejdk7
51+
- jdk: openjdk7
5252
env: TEST_COMMAND="jruby -S rake integration:install integration:test"
5353
rvm: jruby-9.1.2.0
5454
- jdk: oraclejdk8

src/main/java/org/jruby/ext/openssl/OpenSSL.java

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* The MIT License
33
*
44
* Copyright (c) 2014 Karol Bucek LTD.
5+
* Copyright (c) 2017 Ketan Padegaonkar
56
*
67
* Permission is hereby granted, free of charge, to any person obtaining a copy
78
* of this software and associated documentation files (the "Software"), to deal
@@ -23,16 +24,7 @@
2324
*/
2425
package org.jruby.ext.openssl;
2526

26-
import java.security.NoSuchProviderException;
27-
import java.security.SecureRandom;
28-
import java.util.Map;
29-
30-
import org.jruby.CompatVersion;
31-
import org.jruby.Ruby;
32-
import org.jruby.RubyArray;
33-
import org.jruby.RubyClass;
34-
import org.jruby.RubyModule;
35-
import org.jruby.RubyString;
27+
import org.jruby.*;
3628
import org.jruby.anno.JRubyMethod;
3729
import org.jruby.anno.JRubyModule;
3830
import org.jruby.runtime.ThreadContext;
@@ -41,6 +33,10 @@
4133
import org.jruby.util.ByteList;
4234
import org.jruby.util.SafePropertyAccessor;
4335

36+
import java.security.NoSuchProviderException;
37+
import java.security.SecureRandom;
38+
import java.util.Map;
39+
4440
/**
4541
* OpenSSL (methods as well as an entry point)
4642
*
@@ -106,8 +102,7 @@ public static void createOpenSSL(final Ruby runtime) {
106102
_OpenSSL.setConstant("VERSION", StringHelper.newString(runtime, version));
107103

108104
final RubyModule _Jopenssl = runtime.getModule("Jopenssl");
109-
final RubyModule _Version = (RubyModule) _Jopenssl.getConstantAt("Version");
110-
final RubyString jVERSION = _Version.getConstantAt("VERSION").asString();
105+
final RubyString jVERSION = _Jopenssl.getConstantAt("VERSION").asString();
111106

112107
final byte[] JRuby_OpenSSL_ = { 'J','R','u','b','y','-','O','p','e','n','S','S','L',' ' };
113108
final int OPENSSL_VERSION_NUMBER = 999999999; // NOTE: smt more useful?
@@ -255,27 +250,27 @@ static void warn(final ThreadContext context, final IRubyObject msg) {
255250
public static String javaVersion(final String def) {
256251
final String javaVersionProperty =
257252
SafePropertyAccessor.getProperty("java.version", def);
258-
if ( javaVersionProperty == "0" ) return "1.7.0"; // Android
253+
if ("0".equals(javaVersionProperty)) return "1.7.0"; // Android
259254
return javaVersionProperty;
260255
}
261256

262257
static boolean javaVersion6(final boolean atLeast) {
263-
final int gt = "1.6".compareTo( javaVersion("0.0").substring(0, 3) );
258+
final int gt = new Version("1.6").compareTo(new Version(javaVersion("0.0")));
264259
return atLeast ? gt <= 0 : gt == 0;
265260
}
266261

267262
static boolean javaVersion7(final boolean atLeast) {
268-
final int gt = "1.7".compareTo( javaVersion("0.0").substring(0, 3) );
263+
final int gt = new Version("1.7").compareTo(new Version(javaVersion("0.0")));
269264
return atLeast ? gt <= 0 : gt == 0;
270265
}
271266

272267
static boolean javaVersion8(final boolean atLeast) {
273-
final int gt = "1.8".compareTo( javaVersion("0.0").substring(0, 3) );
268+
final int gt = new Version("1.8").compareTo(new Version(javaVersion("0.0")));
274269
return atLeast ? gt <= 0 : gt == 0;
275270
}
276271

277272
static boolean javaVersion9(final boolean atLeast) {
278-
final int gt = "9".compareTo( javaVersion("0").substring(0, 1) );
273+
final int gt = new Version("9").compareTo(new Version(javaVersion("0.0")));
279274
return atLeast ? gt <= 0 : gt == 0;
280275
}
281276

@@ -346,4 +341,29 @@ static String bcExceptionMessage(NoClassDefFoundError ex) {
346341
return "You need to configure JVM/classpath to enable BouncyCastle Security Provider: " + ex;
347342
}
348343

344+
static class Version implements Comparable<Version> {
345+
public final int[] numbers;
346+
347+
public Version(String version) {
348+
final String split[] = version.split("[-_]")[0].split("\\.");
349+
numbers = new int[split.length];
350+
for (int i = 0; i < split.length; i++) {
351+
numbers[i] = Integer.valueOf(split[i]);
352+
}
353+
}
354+
355+
@Override
356+
public int compareTo(Version another) {
357+
final int maxLength = Math.max(numbers.length, another.numbers.length);
358+
for (int i = 0; i < maxLength; i++) {
359+
final int left = i < numbers.length ? numbers[i] : 0;
360+
final int right = i < another.numbers.length ? another.numbers[i] : 0;
361+
if (left != right) {
362+
return left < right ? -1 : 1;
363+
}
364+
}
365+
return 0;
366+
}
367+
}
368+
349369
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright 2017 Ketan Padegaonkar
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package org.jruby.ext.openssl;
26+
27+
import org.junit.Test;
28+
29+
import static org.hamcrest.CoreMatchers.is;
30+
import static org.junit.Assert.assertThat;
31+
32+
public class VersionTest {
33+
@Test
34+
public void newInstance_withTwoDotRelease_isParsedCorrectly() {
35+
final OpenSSL.Version version = new OpenSSL.Version("1.8.1");
36+
assertThat(version.numbers, is(new int[] { 1, 8, 1 }));
37+
}
38+
39+
@Test
40+
public void newInstance_withTwoDotReleaseAndPreReleaseName_isParsedCorrectly() {
41+
assertThat(new OpenSSL.Version("1.8.1-EA").numbers, is(new int[] { 1, 8, 1 }));
42+
assertThat(new OpenSSL.Version("1.7.0_79").numbers, is(new int[] { 1, 7, 0 }));
43+
}
44+
45+
@Test
46+
public void compareTo_withEarlierVersion_isGreaterThan() {
47+
assertThat(new OpenSSL.Version("1.8").compareTo(new OpenSSL.Version("1.7")), is(1));
48+
}
49+
50+
@Test
51+
public void compareTo_withSameVersion_isEqual() {
52+
assertThat(new OpenSSL.Version("1.8").compareTo(new OpenSSL.Version("1.8")), is(0));
53+
}
54+
55+
@Test
56+
public void compareTo_withLaterVersion_isLessThan() {
57+
assertThat(new OpenSSL.Version("1.7").compareTo(new OpenSSL.Version("1.8")), is(-1));
58+
}
59+
60+
@Test
61+
public void compareTo_withMorePreciseSameVersion_isFalse() {
62+
assertThat(new OpenSSL.Version("9").compareTo(new OpenSSL.Version("9.0")), is(0));
63+
}
64+
65+
@Test
66+
public void compareTo_withMorePreciseEarlierVersion_isFalse() {
67+
assertThat(new OpenSSL.Version("9").compareTo(new OpenSSL.Version("1.7")), is(1));
68+
}
69+
70+
@Test
71+
public void compareTo_withMorePreciseLaterVersion_isLessThan() {
72+
assertThat(new OpenSSL.Version("1").compareTo(new OpenSSL.Version("1.0.1")), is(-1));
73+
}
74+
75+
}

0 commit comments

Comments
 (0)