Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Optional/BasicInt.java
47019 views
1
/*
2
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/* @test
25
* @summary Basic functional test of OptionalInt
26
* @author Mike Duigou
27
* @run testng BasicInt
28
*/
29
30
import java.util.NoSuchElementException;
31
import java.util.OptionalInt;
32
33
import static org.testng.Assert.*;
34
import org.testng.annotations.Test;
35
36
37
public class BasicInt {
38
39
@Test(groups = "unit")
40
public void testEmpty() {
41
OptionalInt empty = OptionalInt.empty();
42
OptionalInt present = OptionalInt.of(1);
43
44
// empty
45
assertTrue(empty.equals(empty));
46
assertTrue(empty.equals(OptionalInt.empty()));
47
assertTrue(!empty.equals(present));
48
assertTrue(0 == empty.hashCode());
49
assertTrue(!empty.toString().isEmpty());
50
assertTrue(!empty.isPresent());
51
empty.ifPresent(v -> { fail(); });
52
assertEquals(2, empty.orElse(2));
53
assertEquals(2, empty.orElseGet(()-> 2));
54
}
55
56
@Test(expectedExceptions=NoSuchElementException.class)
57
public void testEmptyGet() {
58
OptionalInt empty = OptionalInt.empty();
59
60
int got = empty.getAsInt();
61
}
62
63
@Test(expectedExceptions=NullPointerException.class)
64
public void testEmptyOrElseGetNull() {
65
OptionalInt empty = OptionalInt.empty();
66
67
int got = empty.orElseGet(null);
68
}
69
70
@Test(expectedExceptions=NullPointerException.class)
71
public void testEmptyOrElseThrowNull() throws Throwable {
72
OptionalInt empty = OptionalInt.empty();
73
74
int got = empty.orElseThrow(null);
75
}
76
77
@Test(expectedExceptions=ObscureException.class)
78
public void testEmptyOrElseThrow() throws Exception {
79
OptionalInt empty = OptionalInt.empty();
80
81
int got = empty.orElseThrow(ObscureException::new);
82
}
83
84
@Test(groups = "unit")
85
public void testPresent() {
86
OptionalInt empty = OptionalInt.empty();
87
OptionalInt present = OptionalInt.of(1);
88
89
// present
90
assertTrue(present.equals(present));
91
assertFalse(present.equals(OptionalInt.of(0)));
92
assertTrue(present.equals(OptionalInt.of(1)));
93
assertFalse(present.equals(empty));
94
assertTrue(Integer.hashCode(1) == present.hashCode());
95
assertFalse(present.toString().isEmpty());
96
assertTrue(-1 != present.toString().indexOf(Integer.toString(present.getAsInt()).toString()));
97
assertEquals(1, present.getAsInt());
98
try {
99
present.ifPresent(v -> { throw new ObscureException(); });
100
fail();
101
} catch(ObscureException expected) {
102
103
}
104
assertEquals(1, present.orElse(2));
105
assertEquals(1, present.orElseGet(null));
106
assertEquals(1, present.orElseGet(()-> 2));
107
assertEquals(1, present.orElseGet(()-> 3));
108
assertEquals(1, present.<RuntimeException>orElseThrow(null));
109
assertEquals(1, present.<RuntimeException>orElseThrow(ObscureException::new));
110
}
111
112
private static class ObscureException extends RuntimeException {
113
114
}
115
}
116
117