Repository navigation
Expand file tree
/
Copy pathBasicWrapJavaTests.swift
More file actions
132 lines (118 loc) · 3.65 KB
/
Copy pathBasicWrapJavaTests.swift
File metadata and controls
132 lines (118 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
@_spi(Testing) import SwiftJava
import SwiftJavaToolLib
import JavaUtilJar
import SwiftJavaShared
import JavaNet
import SwiftJavaConfigurationShared
import _Subprocess
import XCTest // NOTE: Workaround for https://github.com/swiftlang/swift-java/issues/43
final class BasicWrapJavaTests: XCTestCase {
func testWrapJavaFromCompiledJavaSource() async throws {
let classpathURL = try await compileJava(
"""
package com.example;
class ExampleSimpleClass {}
""")
try assertWrapJavaOutput(
javaClassNames: [
"com.example.ExampleSimpleClass"
],
classpath: [classpathURL],
expectedChunks: [
"""
import CSwiftJavaJNI
import SwiftJava
""",
"""
@JavaClass("com.example.ExampleSimpleClass")
open class ExampleSimpleClass: JavaObject {
"""
]
)
}
func test_wrapJava_doNotDupeImportNestedClassesFromSuperclassAutomatically() async throws {
let classpathURL = try await compileJava(
"""
package com.example;
class SuperClass {
class SuperNested {}
}
class ExampleSimpleClass {
class SimpleNested {}
}
""")
try assertWrapJavaOutput(
javaClassNames: [
"com.example.SuperClass",
"com.example.SuperClass$SuperNested",
"com.example.ExampleSimpleClass",
],
classpath: [classpathURL],
expectedChunks: [
"""
@JavaClass("com.example.SuperClass")
open class SuperClass: JavaObject {
""",
// FIXME: the mapping configuration could be used to nest this properly but today we don't automatically?
"""
@JavaClass("com.example.SuperClass$SuperNested")
open class SuperNested: JavaObject {
""",
"""
@JavaClass("com.example.SuperClass")
open class SuperClass: JavaObject {
""",
]
)
}
// Test that static fields from superclasses are not duplicated in generated code.
// This prevents duplicate serialVersionUID declarations when both a class and its
// superclass declare the field. The subclass field "hides" the superclass field,
// similar to how static methods work in Java.
func test_wrapJava_noDuplicateStaticFieldsFromSuperclass() async throws {
let classpathURL = try await compileJava(
"""
package com.example;
class SuperClass {
public static final long serialVersionUID = 1L;
}
class SubClass extends SuperClass {
public static final long serialVersionUID = 2L;
}
""")
try assertWrapJavaOutput(
javaClassNames: [
"com.example.SuperClass",
"com.example.SubClass"
],
classpath: [classpathURL],
expectedChunks: [
// SuperClass should have its static field
"""
extension JavaClass<SuperClass> {
@JavaStaticField(isFinal: true)
public var serialVersionUID: Int64
""",
// SubClass should only have its own static field, not the superclass one
"""
extension JavaClass<SubClass> {
@JavaStaticField(isFinal: true)
public var serialVersionUID: Int64
""",
]
)
}
}