Skip to content

Commit dcdf4bb

Browse files
committed
actually execute tests in JavaKitSampleApp not just run the app
1 parent af05e0a commit dcdf4bb

File tree

8 files changed

+162
-6
lines changed

8 files changed

+162
-6
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;
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+
import Testing
20+
21+
@Suite
22+
struct JavaKitMethodCalledInitTests {
23+
24+
@Test
25+
func test() throws {
26+
let jvm = try JavaKitSampleJVM.shared()
27+
let env = try jvm.environment()
28+
29+
let hello = HelloSwift(environment: env)
30+
31+
let reply = hello.`init`(128)
32+
#expect(reply == 128)
33+
}
34+
35+
}

Samples/JavaKitSampleApp/Tests/JavaKitExampleTests/JavaKitOptionalTests.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ struct ManglingTests {
2323

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

3329
let helper = ThreadSafeHelperClass(environment: env)
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 func shared() throws -> 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
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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 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 SwiftJava
16+
import CSwiftJavaJNI
17+
18+
final class JNI {
19+
static var shared: JNI!
20+
21+
let applicationClassLoader: JavaClassLoader
22+
23+
init(fromVM javaVM: JavaVirtualMachine) {
24+
self.applicationClassLoader = try! JavaClass<JavaThread>(environment: javaVM.environment()).currentThread().getContextClassLoader()
25+
}
26+
}
27+
28+
// Called by generated code, and not automatically by Java.
29+
public func _JNI_OnLoad(_ javaVM: JavaVMPointer, _ reserved: UnsafeMutableRawPointer) {
30+
JNI.shared = JNI(fromVM: JavaVirtualMachine(adoptingJVM: javaVM))
31+
}

0 commit comments

Comments
 (0)