Skip to content

Commit

Permalink
Improve jvm version detection for java 9 (#143)
Browse files Browse the repository at this point in the history
* 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
ketan authored and kares committed Oct 1, 2017
1 parent adc2f75 commit f86bc0b
Showing 3 changed files with 118 additions and 23 deletions.
12 changes: 6 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: ruby
sudo: false
jdk:
- oraclejdk7
- openjdk7
- oraclejdk8
env:
- TEST_PROFILE=test-1.7.20
@@ -22,13 +22,13 @@ matrix:
- env: TEST_COMMAND="jruby -S rake integration:install integration:test"
rvm: jruby-1.7.20
include:
- jdk: oraclejdk7
- jdk: openjdk7
env: TEST_COMMAND="jruby -rbundler/setup -S rmvn test-compile && jruby -S rake test" BUNDLE_INSTALL=true
rvm: jruby-1.7.24
- jdk: oraclejdk7
- jdk: openjdk7
env: TEST_COMMAND="jruby -rbundler/setup -S rmvn test-compile && jruby -S rake test" BUNDLE_INSTALL=true
rvm: jruby-1.7.25
- jdk: oraclejdk7
- jdk: openjdk7
env: TEST_COMMAND="jruby -rbundler/setup -S rmvn test-compile && jruby -S rake test" BUNDLE_INSTALL=true
rvm: jruby-1.7.26
#
@@ -42,13 +42,13 @@ matrix:
- jdk: oraclejdk8
env: TEST_COMMAND="jruby -S rake integration:install integration:test"
rvm: jruby-1.7.26
- jdk: oraclejdk7
- jdk: openjdk7
env: TEST_COMMAND="jruby -S rake integration:install integration:test"
rvm: jruby-1.7.18
- jdk: oraclejdk8
env: TEST_COMMAND="jruby -S rake integration:install integration:test"
rvm: jruby-9.0.5.0
- jdk: oraclejdk7
- jdk: openjdk7
env: TEST_COMMAND="jruby -S rake integration:install integration:test"
rvm: jruby-9.1.2.0
- jdk: oraclejdk8
54 changes: 37 additions & 17 deletions src/main/java/org/jruby/ext/openssl/OpenSSL.java
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
* The MIT License
*
* Copyright (c) 2014 Karol Bucek LTD.
* Copyright (c) 2017 Ketan Padegaonkar
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -23,16 +24,7 @@
*/
package org.jruby.ext.openssl;

import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.util.Map;

import org.jruby.CompatVersion;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyClass;
import org.jruby.RubyModule;
import org.jruby.RubyString;
import org.jruby.*;
import org.jruby.anno.JRubyMethod;
import org.jruby.anno.JRubyModule;
import org.jruby.runtime.ThreadContext;
@@ -41,6 +33,10 @@
import org.jruby.util.ByteList;
import org.jruby.util.SafePropertyAccessor;

import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.util.Map;

/**
* OpenSSL (methods as well as an entry point)
*
@@ -106,8 +102,7 @@ public static void createOpenSSL(final Ruby runtime) {
_OpenSSL.setConstant("VERSION", StringHelper.newString(runtime, version));

final RubyModule _Jopenssl = runtime.getModule("Jopenssl");
final RubyModule _Version = (RubyModule) _Jopenssl.getConstantAt("Version");
final RubyString jVERSION = _Version.getConstantAt("VERSION").asString();
final RubyString jVERSION = _Jopenssl.getConstantAt("VERSION").asString();

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

static boolean javaVersion6(final boolean atLeast) {
final int gt = "1.6".compareTo( javaVersion("0.0").substring(0, 3) );
final int gt = new Version("1.6").compareTo(new Version(javaVersion("0.0")));
return atLeast ? gt <= 0 : gt == 0;
}

static boolean javaVersion7(final boolean atLeast) {
final int gt = "1.7".compareTo( javaVersion("0.0").substring(0, 3) );
final int gt = new Version("1.7").compareTo(new Version(javaVersion("0.0")));
return atLeast ? gt <= 0 : gt == 0;
}

static boolean javaVersion8(final boolean atLeast) {
final int gt = "1.8".compareTo( javaVersion("0.0").substring(0, 3) );
final int gt = new Version("1.8").compareTo(new Version(javaVersion("0.0")));
return atLeast ? gt <= 0 : gt == 0;
}

static boolean javaVersion9(final boolean atLeast) {
final int gt = "9".compareTo( javaVersion("0").substring(0, 1) );
final int gt = new Version("9").compareTo(new Version(javaVersion("0.0")));
return atLeast ? gt <= 0 : gt == 0;
}

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

static class Version implements Comparable<Version> {
public final int[] numbers;

public Version(String version) {
final String split[] = version.split("[-_]")[0].split("\\.");
numbers = new int[split.length];
for (int i = 0; i < split.length; i++) {
numbers[i] = Integer.valueOf(split[i]);
}
}

@Override
public int compareTo(Version another) {
final int maxLength = Math.max(numbers.length, another.numbers.length);
for (int i = 0; i < maxLength; i++) {
final int left = i < numbers.length ? numbers[i] : 0;
final int right = i < another.numbers.length ? another.numbers[i] : 0;
if (left != right) {
return left < right ? -1 : 1;
}
}
return 0;
}
}

}
75 changes: 75 additions & 0 deletions src/test/java/org/jruby/ext/openssl/VersionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* The MIT License
*
* Copyright 2017 Ketan Padegaonkar
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jruby.ext.openssl;

import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class VersionTest {
@Test
public void newInstance_withTwoDotRelease_isParsedCorrectly() {
final OpenSSL.Version version = new OpenSSL.Version("1.8.1");
assertThat(version.numbers, is(new int[] { 1, 8, 1 }));
}

@Test
public void newInstance_withTwoDotReleaseAndPreReleaseName_isParsedCorrectly() {
assertThat(new OpenSSL.Version("1.8.1-EA").numbers, is(new int[] { 1, 8, 1 }));
assertThat(new OpenSSL.Version("1.7.0_79").numbers, is(new int[] { 1, 7, 0 }));
}

@Test
public void compareTo_withEarlierVersion_isGreaterThan() {
assertThat(new OpenSSL.Version("1.8").compareTo(new OpenSSL.Version("1.7")), is(1));
}

@Test
public void compareTo_withSameVersion_isEqual() {
assertThat(new OpenSSL.Version("1.8").compareTo(new OpenSSL.Version("1.8")), is(0));
}

@Test
public void compareTo_withLaterVersion_isLessThan() {
assertThat(new OpenSSL.Version("1.7").compareTo(new OpenSSL.Version("1.8")), is(-1));
}

@Test
public void compareTo_withMorePreciseSameVersion_isFalse() {
assertThat(new OpenSSL.Version("9").compareTo(new OpenSSL.Version("9.0")), is(0));
}

@Test
public void compareTo_withMorePreciseEarlierVersion_isFalse() {
assertThat(new OpenSSL.Version("9").compareTo(new OpenSSL.Version("1.7")), is(1));
}

@Test
public void compareTo_withMorePreciseLaterVersion_isLessThan() {
assertThat(new OpenSSL.Version("1").compareTo(new OpenSSL.Version("1.0.1")), is(-1));
}

}

0 comments on commit f86bc0b

Please sign in to comment.