15
15
import JavaKit
16
16
import JavaKitNetwork
17
17
import JavaKitVM
18
- import Testing
19
- import Foundation
20
-
21
- #if os(Linux)
22
- import Glibc
23
- #else
24
- import Darwin
25
- #endif
18
+ import XCTest // NOTE: Workaround for https://github.com/swiftlang/swift-java/issues/43
26
19
27
20
@MainActor
28
21
let jvm = try ! JavaVirtualMachine ( vmOptions: [ ] )
29
22
30
- @Suite
31
23
@MainActor
32
- struct BasicRuntimeTests {
33
- @Test ( " Object management " , . disabled( if: isMacOSTerminal || isLinux, " JVM creation fails occasionally in terminal on macOS, and some issues on Linux " ) )
34
- func javaObjectManagement( ) throws {
24
+ class BasicRuntimeTests : XCTestCase {
25
+ func testJavaObjectManagement( ) async throws {
26
+ if isLinux {
27
+ throw XCTSkip ( " Attempts to refcount a null pointer on Linux " )
28
+ }
29
+
35
30
let sneakyJavaThis : jobject
36
31
do {
37
32
let object = JavaObject ( environment: jvm. environment)
38
- #expect ( object. toString ( ) . starts ( with: " java.lang.Object " ) )
33
+ XCTAssert ( object. toString ( ) . starts ( with: " java.lang.Object " ) )
39
34
40
35
// Make sure this object was promoted to a global reference.
41
- #expect ( object. javaEnvironment. pointee? . pointee. GetObjectRefType ( object. javaEnvironment, object. javaThis) == JNIGlobalRefType)
36
+ XCTAssertEqual ( object. javaEnvironment. pointee? . pointee. GetObjectRefType ( object. javaEnvironment, object. javaThis) , JNIGlobalRefType)
42
37
43
38
// Keep track of the Java object.
44
39
sneakyJavaThis = object. javaThis
45
40
}
46
41
47
42
// The reference should now be invalid, because we've deleted the
48
43
// global reference.
49
- #expect ( jvm. environment. pointee? . pointee. GetObjectRefType ( jvm. environment, sneakyJavaThis) == JNIInvalidRefType)
44
+ XCTAssertEqual ( jvm. environment. pointee? . pointee. GetObjectRefType ( jvm. environment, sneakyJavaThis) , JNIInvalidRefType)
50
45
51
46
// 'super' and 'as' don't require allocating a new holder.
52
47
let url = try URL ( " http://swift.org " , environment: jvm. environment)
53
48
let superURL = url. super
54
- #expect ( url. javaHolder === superURL. javaHolder)
49
+ XCTAssert ( url. javaHolder === superURL. javaHolder)
55
50
let urlAgain = superURL. as ( URL . self) !
56
- #expect ( url. javaHolder === urlAgain. javaHolder)
51
+ XCTAssert ( url. javaHolder === urlAgain. javaHolder)
57
52
}
58
53
59
- @Test ( " Java exceptions " , . disabled( if: isMacOSTerminal || isLinux, " JVM creation fails occasionally in terminal on macOS, and some issues on Linux " ) )
60
- func javaExceptionsInSwift( ) throws {
54
+ func testJavaExceptionsInSwift( ) async throws {
55
+ if isLinux {
56
+ throw XCTSkip ( " Attempts to refcount a null pointer on Linux " )
57
+ }
58
+
61
59
do {
62
60
_ = try URL ( " bad url " , environment: jvm. environment)
63
61
} catch {
64
- #expect ( String ( describing: error) == " no protocol: bad url " )
62
+ XCTAssert ( String ( describing: error) == " no protocol: bad url " )
65
63
}
66
64
}
67
65
68
- @Test ( " Static methods " , . disabled( if: isMacOSTerminal || isLinux, " JVM creation fails occasionally in terminal on macOS, and some issues on Linux " ) )
69
- func staticMethods( ) throws {
66
+ func testStaticMethods( ) async throws {
67
+ if isLinux {
68
+ throw XCTSkip ( " Attempts to refcount a null pointer on Linux " )
69
+ }
70
+
70
71
let urlConnectionClass = try JavaClass < URLConnection > ( in: jvm. environment)
71
- #expect ( urlConnectionClass. getDefaultAllowUserInteraction ( ) == false )
72
+ XCTAssert ( urlConnectionClass. getDefaultAllowUserInteraction ( ) == false )
72
73
}
73
74
74
- @Test ( " Class instance lookup " , . disabled( if: isMacOSTerminal || isLinux, " JVM creation fails occasionally in terminal on macOS, and some issues on Linux " ) )
75
- func classInstanceLookup( ) throws {
75
+ func testClassInstanceLookup( ) async throws {
76
76
do {
77
77
_ = try JavaClass < Nonexistent > ( in: jvm. environment)
78
78
} catch {
79
- #expect ( String ( describing: error) == " org/swift/javakit/Nonexistent " )
79
+ XCTAssertEqual ( String ( describing: error) , " org/swift/javakit/Nonexistent " )
80
80
}
81
81
}
82
82
}
@@ -92,20 +92,3 @@ var isLinux: Bool {
92
92
return false
93
93
#endif
94
94
}
95
-
96
- /// Whether we're running on MacOS in an interactive terminal session.
97
- var isMacOSTerminal : Bool {
98
- isMacOS && (
99
- isatty ( STDOUT_FILENO) == 1 ||
100
- ProcessInfo . processInfo. environment [ " IS_TTY " ] != nil // since 'swift test' still sometimes hides the fact we're in tty
101
- )
102
- }
103
-
104
- /// Whether we're running on MacOS.
105
- var isMacOS : Bool {
106
- #if os(macOS)
107
- return true
108
- #else
109
- return false
110
- #endif
111
- }
0 commit comments