Repository navigation
Expand file tree
/
Copy pathThrowTest.java
More file actions
102 lines (89 loc) · 3.21 KB
/
Copy pathThrowTest.java
File metadata and controls
102 lines (89 loc) · 3.21 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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 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
//
//===----------------------------------------------------------------------===//
package com.example.swift;
import com.example.swift.MySwiftLibrary;
import org.junit.jupiter.api.Test;
import org.swift.swiftkit.core.SwiftErrorException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import static org.junit.jupiter.api.Assertions.*;
public class ThrowTest {
@Test
void throwString() throws Exception {
String result = MySwiftLibrary.throwString("hey");
assertEquals("hey", result);
}
@Test
void throwStringActuallyThrows() {
// snippet.throwUsageJava
Exception exception = assertThrows(Exception.class, () -> {
MySwiftLibrary.throwString("");
});
assertNotNull(exception.getMessage());
assertTrue(exception.getMessage().contains("swiftError"));
// snippet.end
}
@Test
void catchSwiftErrorEnum() {
MySwiftError error = assertThrows(MySwiftError.class, () -> {
MySwiftLibrary.throwString("");
});
switch (error.getCase()) {
case MySwiftError.Case.SwiftError _ -> {}
case MySwiftError.Case.InvalidInput _ -> fail("unexpected case");
}
}
@Test
void typedThrowsIsCheckedException() {
// Only `MySwiftError` is caught here: this compiles because the
// Java signature is `throws MySwiftError`, not `throws Exception`.
try {
MySwiftLibrary.throwTyped("");
fail("expected MySwiftError");
} catch (MySwiftError e) {
assertTrue(e.getMessage().contains("swiftError"));
}
}
@Test
void typedThrowsPayload() {
try {
MySwiftLibrary.throwTyped("invalid");
fail("expected MySwiftError");
} catch (MySwiftError e) {
switch (e.getCase()) {
case MySwiftError.Case.InvalidInput(var reason) -> assertEquals("input was 'invalid'", reason);
default -> fail("unexpected case");
}
}
}
@Test
void typedThrowsSuccess() throws MySwiftError {
assertEquals("ok", MySwiftLibrary.throwTyped("ok"));
}
@Test
void nonExtractedErrorFallsBackToSwiftErrorException() {
SwiftErrorException e = assertThrows(SwiftErrorException.class, () -> {
MySwiftLibrary.throwInternalError();
});
assertNotNull(e.getMessage());
assertTrue(e.getMessage().contains("InternalOnlyError"));
assertTrue(e.as(MySwiftError.class).isEmpty());
}
@Test
void asyncThrowsTypedCause() {
Future<String> future = MySwiftLibrary.asyncThrowTyped("");
ExecutionException ex = assertThrows(ExecutionException.class, future::get);
assertInstanceOf(MySwiftError.class, ex.getCause());
}
}