Skip to content

Temporarily switch JavaKitTests target to XCTest #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Tests/JExtractSwiftTests/VariableImportTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ final class VariableImportTests {
)
st.log.logLevel = .error

try st.analyze(swiftInterfacePath: "/fake/Fake.swiftinterface", text: class_interfaceFile)
try await st.analyze(swiftInterfacePath: "/fake/Fake.swiftinterface", text: class_interfaceFile)

let identifier = "counterInt"
let varDecl: ImportedVariable? =
Expand All @@ -59,6 +59,7 @@ final class VariableImportTests {
}

assertOutput(
dump: true,
output,
expected:
"""
Expand Down
69 changes: 26 additions & 43 deletions Tests/JavaKitTests/BasicRuntimeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,68 +15,68 @@
import JavaKit
import JavaKitNetwork
import JavaKitVM
import Testing
import Foundation

#if os(Linux)
import Glibc
#else
import Darwin
#endif
import XCTest // NOTE: Workaround for https://github.com/swiftlang/swift-java/issues/43

@MainActor
let jvm = try! JavaVirtualMachine(vmOptions: [])

@Suite
@MainActor
struct BasicRuntimeTests {
@Test("Object management", .disabled(if: isMacOSTerminal || isLinux, "JVM creation fails occasionally in terminal on macOS, and some issues on Linux"))
func javaObjectManagement() throws {
class BasicRuntimeTests: XCTestCase {
func testJavaObjectManagement() async throws {
if isLinux {
throw XCTSkip("Attempts to refcount a null pointer on Linux")
}

let sneakyJavaThis: jobject
do {
let object = JavaObject(environment: jvm.environment)
#expect(object.toString().starts(with: "java.lang.Object"))
XCTAssert(object.toString().starts(with: "java.lang.Object"))

// Make sure this object was promoted to a global reference.
#expect(object.javaEnvironment.pointee?.pointee.GetObjectRefType(object.javaEnvironment, object.javaThis) == JNIGlobalRefType)
XCTAssertEqual(object.javaEnvironment.pointee?.pointee.GetObjectRefType(object.javaEnvironment, object.javaThis), JNIGlobalRefType)

// Keep track of the Java object.
sneakyJavaThis = object.javaThis
}

// The reference should now be invalid, because we've deleted the
// global reference.
#expect(jvm.environment.pointee?.pointee.GetObjectRefType(jvm.environment, sneakyJavaThis) == JNIInvalidRefType)
XCTAssertEqual(jvm.environment.pointee?.pointee.GetObjectRefType(jvm.environment, sneakyJavaThis), JNIInvalidRefType)

// 'super' and 'as' don't require allocating a new holder.
let url = try URL("http://swift.org", environment: jvm.environment)
let superURL = url.super
#expect(url.javaHolder === superURL.javaHolder)
XCTAssert(url.javaHolder === superURL.javaHolder)
let urlAgain = superURL.as(URL.self)!
#expect(url.javaHolder === urlAgain.javaHolder)
XCTAssert(url.javaHolder === urlAgain.javaHolder)
}

@Test("Java exceptions", .disabled(if: isMacOSTerminal || isLinux, "JVM creation fails occasionally in terminal on macOS, and some issues on Linux"))
func javaExceptionsInSwift() throws {
func testJavaExceptionsInSwift() async throws {
if isLinux {
throw XCTSkip("Attempts to refcount a null pointer on Linux")
}

do {
_ = try URL("bad url", environment: jvm.environment)
} catch {
#expect(String(describing: error) == "no protocol: bad url")
XCTAssert(String(describing: error) == "no protocol: bad url")
}
}

@Test("Static methods", .disabled(if: isMacOSTerminal || isLinux, "JVM creation fails occasionally in terminal on macOS, and some issues on Linux"))
func staticMethods() throws {
func testStaticMethods() async throws {
if isLinux {
throw XCTSkip("Attempts to refcount a null pointer on Linux")
}

let urlConnectionClass = try JavaClass<URLConnection>(in: jvm.environment)
#expect(urlConnectionClass.getDefaultAllowUserInteraction() == false)
XCTAssert(urlConnectionClass.getDefaultAllowUserInteraction() == false)
}

@Test("Class instance lookup", .disabled(if: isMacOSTerminal || isLinux, "JVM creation fails occasionally in terminal on macOS, and some issues on Linux"))
func classInstanceLookup() throws {
func testClassInstanceLookup() async throws {
do {
_ = try JavaClass<Nonexistent>(in: jvm.environment)
} catch {
#expect(String(describing: error) == "org/swift/javakit/Nonexistent")
XCTAssertEqual(String(describing: error), "org/swift/javakit/Nonexistent")
}
}
}
Expand All @@ -92,20 +92,3 @@ var isLinux: Bool {
return false
#endif
}

/// Whether we're running on MacOS in an interactive terminal session.
var isMacOSTerminal: Bool {
isMacOS && (
isatty(STDOUT_FILENO) == 1 ||
ProcessInfo.processInfo.environment["IS_TTY"] != nil // since 'swift test' still sometimes hides the fact we're in tty
)
}

/// Whether we're running on MacOS.
var isMacOS: Bool {
#if os(macOS)
return true
#else
return false
#endif
}
Loading