Skip to content

Commit e377b8a

Browse files
committed
[test] introduce a helper for loading in JUnit
1 parent c7408cb commit e377b8a

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.jruby.ext.openssl;
2+
3+
import org.jruby.Ruby;
4+
import org.jruby.runtime.ThreadContext;
5+
6+
import java.io.ByteArrayOutputStream;
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.nio.charset.StandardCharsets;
11+
12+
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.assertTrue;
14+
15+
abstract class OpenSSLHelper {
16+
17+
protected Ruby runtime;
18+
19+
void setUpRuntime() throws ClassNotFoundException {
20+
runtime = Ruby.newInstance();
21+
loadOpenSSL(runtime);
22+
}
23+
24+
void tearDownRuntime() {
25+
if (runtime != null) runtime.tearDown(false);
26+
}
27+
28+
protected void loadOpenSSL(final Ruby runtime) throws ClassNotFoundException {
29+
// prepend lib/ so openssl.rb + jopenssl/ are loaded instead of bundled OpenSSL in jruby-stdlib
30+
final String libDir = new File("lib").getAbsolutePath();
31+
runtime.evalScriptlet("$LOAD_PATH.unshift '" + libDir + "'");
32+
runtime.evalScriptlet("require 'openssl'");
33+
34+
// sanity: verify openssl was loaded from the project, not jruby-stdlib :
35+
final String versionFile = new File(libDir, "jopenssl/version.rb").getAbsolutePath();
36+
final String expectedVersion = runtime.evalScriptlet(
37+
"File.read('" + versionFile + "').match( /.*\\sVERSION\\s*=\\s*['\"](.*)['\"]/ )[1]")
38+
.toString();
39+
final String loadedVersion = runtime.evalScriptlet("JOpenSSL::VERSION").toString();
40+
assertEquals("OpenSSL must be loaded from project (got version " + loadedVersion +
41+
"), not from jruby-stdlib", expectedVersion, loadedVersion);
42+
43+
// Also check the Java extension classes were resolved from the project, not jruby-stdlib :
44+
final String classOrigin = runtime.getJRubyClassLoader()
45+
.loadClass("org.jruby.ext.openssl.OpenSSL")
46+
.getProtectionDomain().getCodeSource().getLocation().toString();
47+
assertTrue("OpenSSL.class (via JRuby classloader) come from project, got: " + classOrigin,
48+
classOrigin.endsWith("/pkg/classes/"));
49+
}
50+
51+
// HELPERS
52+
53+
public ThreadContext currentContext() {
54+
return runtime.getCurrentContext();
55+
}
56+
57+
public static String readResource(final String resource) {
58+
int n;
59+
try (InputStream in = SSLSocketTest.class.getResourceAsStream(resource)) {
60+
if (in == null) throw new IllegalArgumentException(resource + " not found on classpath");
61+
62+
ByteArrayOutputStream out = new ByteArrayOutputStream();
63+
byte[] buf = new byte[8192];
64+
while ((n = in.read(buf)) != -1) out.write(buf, 0, n);
65+
return new String(out.toByteArray(), StandardCharsets.UTF_8);
66+
} catch (IOException e) {
67+
throw new IllegalStateException("failed to load" + resource, e);
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)