Skip to content

Commit afdfec2

Browse files
committed
actually execute tests in JavaKitSampleApp not just run the app
1 parent 10162c7 commit afdfec2

File tree

8 files changed

+128
-10
lines changed

8 files changed

+128
-10
lines changed

Samples/JavaKitSampleApp/Sources/JavaKitExample/com/example/swift/HelloSwift.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public void greet(String name) {
4242
System.out.println("Salutations, " + name);
4343
}
4444

45+
// method called 'init' to check we're able to handle these and not clash with swift 'init' keyword
46+
public long init(long value) {
47+
return value;
48+
}
49+
4550
public Predicate<Integer> lessThanTen() {
4651
Predicate<Integer> predicate = i -> (i < 10);
4752
return predicate;

Samples/JavaKitSampleApp/Tests/JavaKitExampleTests/JavaKitOptionalTests.swift renamed to Samples/JavaKitSampleApp/Tests/JavaKitExampleTests/JavaKitExampleRuntimeTests.swift

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,13 @@ import JavaUtilFunction
1919
import Testing
2020

2121
@Suite
22-
struct ManglingTests {
22+
struct JavaKitExampleRuntimeTests {
23+
24+
let jvm = try JavaKitSampleJVM.shared
2325

2426
@Test
2527
func methodMangling() throws {
26-
let jvm = try! JavaVirtualMachine.shared(
27-
classpath: [
28-
".build/plugins/outputs/javakitsampleapp/JavaKitExample/destination/JavaCompilerPlugin/Java"
29-
]
30-
)
31-
let env = try! jvm.environment()
28+
let env = try jvm.environment()
3229

3330
let helper = ThreadSafeHelperClass(environment: env)
3431

@@ -53,4 +50,14 @@ struct ManglingTests {
5350
#expect(#"Optional(21)"# == String(describing: longOpt))
5451
}
5552

56-
}
53+
@Test
54+
func methodNamedInit() throws {
55+
let env = try jvm.environment()
56+
57+
let hello = HelloSwift(environment: env)
58+
59+
let reply = hello.`init`(128)
60+
#expect(reply == 128)
61+
}
62+
63+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import JavaKitExample
16+
17+
import SwiftJava
18+
import JavaUtilFunction
19+
20+
/// Utility to configure the classpath and native libraries paths for writing tests against
21+
/// classes defined in this JavaKitExample project
22+
struct JavaKitSampleJVM {
23+
24+
static var shared: JavaVirtualMachine = {
25+
try! JavaVirtualMachine.shared(
26+
classpath: [
27+
".build/plugins/outputs/javakitsampleapp/JavaKitExample/destination/JavaCompilerPlugin/Java",
28+
],
29+
vmOptions: [
30+
KnownJavaVMOptions.javaLibraryPath(".build/\(SwiftPlatform.debugOrRelease)/"),
31+
]
32+
)
33+
}()
34+
35+
}

Samples/JavaKitSampleApp/ci-validate.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ else
1010
DISABLE_EXPERIMENTAL_PREBUILTS='--disable-experimental-prebuilts'
1111
fi
1212

13-
swift build $DISABLE_EXPERIMENTAL_PREBUILTS
13+
swift build --build-tests $DISABLE_EXPERIMENTAL_PREBUILTS
1414

15+
echo "java application run: ..."
1516
"$JAVA_HOME/bin/java" \
1617
-cp .build/plugins/outputs/javakitsampleapp/JavaKitExample/destination/JavaCompilerPlugin/Java \
1718
-Djava.library.path=.build/debug \
1819
"com.example.swift.JavaKitSampleMain"
20+
echo "java application run: OK"
21+
22+
23+
swift test $DISABLE_EXPERIMENTAL_PREBUILTS

Sources/SwiftJava/JNI.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ package final class JNI {
3737
}
3838
let environment = try! javaVM.environment()
3939
self.applicationClassLoader = try! JavaClass<JavaThread>(environment: environment).currentThread().getContextClassLoader()
40+
// do {
41+
// let clazz = try JavaClass<JavaThread>(environment: environment)
42+
// guard let thread: JavaThread = clazz.currentThread() else {
43+
// applicationClassLoader = nil
44+
// return
45+
// }
46+
// guard let cl = thread.getContextClassLoader() else {
47+
// applicationClassLoader = nil
48+
// return
49+
// }
50+
// self.applicationClassLoader = cl
51+
// } catch {
52+
// fatalError("Failed to get current thread's ContextClassLoader: \(error)")
53+
// }
4054
}
4155
}
4256

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
public struct KnownJavaVMOptions {
16+
17+
/// Helper for the option to configure where native libraries should be searched for: `-Djava.library.path`
18+
public static func javaLibraryPath(_ path: String) -> String {
19+
return "-Djava.library.path=" + path
20+
}
21+
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import CSwiftJavaJNI
16+
17+
/// Helpers for forming platform specific directory names and paths.
18+
public struct SwiftPlatform {
19+
20+
public static var debugOrRelease: String {
21+
#if DEBUG
22+
"debug"
23+
#else
24+
"release"
25+
#endif
26+
}
27+
}

Sources/SwiftJavaRuntimeSupport/_JNIMethodIDCache.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ public final class _JNIMethodIDCache: Sendable {
5656
environment.interface.ExceptionClear(environment)
5757

5858
// OK to force unwrap, we are in a jextract environment.
59-
if let javaClass = try? JNI.shared!.applicationClassLoader.loadClass(
59+
guard let jni = JNI.shared else {
60+
fatalError("Cannot get JNI.shared, it should have been initialized by JNI_OnLoad when loading the library")
61+
}
62+
if let javaClass = try? jni.applicationClassLoader.loadClass(
6063
className.replacingOccurrences(of: "/", with: ".")
6164
) {
6265
clazz = javaClass.javaThis

0 commit comments

Comments
 (0)