jdk/test/java/util/Optional/BasicInt.java
author duke
Wed, 05 Jul 2017 22:06:37 +0200
changeset 40471 ad45133ff99e
parent 28963 8498cdb7c54b
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     1
/*
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     4
 *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     8
 *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    13
 * accompanied this code).
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    14
 *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    18
 *
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    21
 * questions.
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    22
 */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    23
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    24
/* @test
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    25
 * @summary Basic functional test of OptionalInt
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    26
 * @author Mike Duigou
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    27
 * @run testng BasicInt
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    28
 */
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    29
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    30
import java.util.NoSuchElementException;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    31
import java.util.OptionalInt;
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    32
import java.util.concurrent.atomic.AtomicBoolean;
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
    33
import java.util.stream.IntStream;
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    34
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    35
import static org.testng.Assert.*;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    36
import org.testng.annotations.Test;
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    37
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    38
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    39
public class BasicInt {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    40
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    41
    @Test(groups = "unit")
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    42
    public void testEmpty() {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    43
        OptionalInt empty = OptionalInt.empty();
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    44
        OptionalInt present = OptionalInt.of(1);
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    45
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    46
        // empty
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    47
        assertTrue(empty.equals(empty));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    48
        assertTrue(empty.equals(OptionalInt.empty()));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    49
        assertTrue(!empty.equals(present));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    50
        assertTrue(0 == empty.hashCode());
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    51
        assertTrue(!empty.toString().isEmpty());
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    52
        assertTrue(!empty.isPresent());
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    53
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    54
        empty.ifPresent(v -> { fail(); });
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    55
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    56
        AtomicBoolean emptyCheck = new AtomicBoolean();
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    57
        empty.ifPresentOrElse(v -> fail(), () -> emptyCheck.set(true));
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    58
        assertTrue(emptyCheck.get());
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    59
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    60
        try {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    61
            empty.ifPresentOrElse(v -> fail(), () -> { throw new ObscureException(); });
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    62
            fail();
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    63
        } catch (ObscureException expected) {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    64
        } catch (AssertionError e) {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    65
            throw e;
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    66
        } catch (Throwable t) {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    67
            fail();
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    68
        }
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    69
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    70
        assertEquals(2, empty.orElse(2));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    71
        assertEquals(2, empty.orElseGet(()-> 2));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    72
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
    73
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    74
    @Test(groups = "unit")
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    75
    public void testIfPresentAndOrElseAndNull() {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    76
        OptionalInt empty = OptionalInt.empty();
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    77
        OptionalInt present = OptionalInt.of(1);
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    78
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    79
        // No NPE
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    80
        present.ifPresentOrElse(v -> {}, null);
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    81
        empty.ifPresent(null);
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    82
        empty.ifPresentOrElse(null, () -> {});
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    83
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    84
        // NPE
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    85
        try {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    86
            present.ifPresent(null);
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    87
            fail();
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    88
        } catch (NullPointerException ex) {}
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    89
        try {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    90
            present.ifPresentOrElse(null, () -> {});
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    91
            fail();
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    92
        } catch (NullPointerException ex) {}
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    93
        try {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    94
            empty.ifPresentOrElse(v -> {}, null);
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    95
            fail();
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    96
        } catch (NullPointerException ex) {}
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    97
    }
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
    98
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
    99
    @Test(expectedExceptions=NoSuchElementException.class)
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   100
    public void testEmptyGet() {
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   101
        OptionalInt empty = OptionalInt.empty();
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   102
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   103
        int got = empty.getAsInt();
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   104
    }
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   105
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   106
    @Test(expectedExceptions=NullPointerException.class)
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   107
    public void testEmptyOrElseGetNull() {
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   108
        OptionalInt empty = OptionalInt.empty();
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   109
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   110
        int got = empty.orElseGet(null);
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   111
    }
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   112
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   113
    @Test(expectedExceptions=NullPointerException.class)
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   114
    public void testEmptyOrElseThrowNull() throws Throwable {
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   115
        OptionalInt empty = OptionalInt.empty();
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   116
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   117
        int got = empty.orElseThrow(null);
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   118
    }
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   119
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   120
    @Test(expectedExceptions=ObscureException.class)
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   121
    public void testEmptyOrElseThrow() throws Exception {
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   122
        OptionalInt empty = OptionalInt.empty();
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   123
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   124
        int got = empty.orElseThrow(ObscureException::new);
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   125
    }
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   126
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   127
    @Test(groups = "unit")
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   128
    public void testPresent() {
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   129
        OptionalInt empty = OptionalInt.empty();
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   130
        OptionalInt present = OptionalInt.of(1);
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   131
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   132
        // present
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   133
        assertTrue(present.equals(present));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   134
        assertFalse(present.equals(OptionalInt.of(0)));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   135
        assertTrue(present.equals(OptionalInt.of(1)));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   136
        assertFalse(present.equals(empty));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   137
        assertTrue(Integer.hashCode(1) == present.hashCode());
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   138
        assertFalse(present.toString().isEmpty());
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   139
        assertTrue(-1 != present.toString().indexOf(Integer.toString(present.getAsInt()).toString()));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   140
        assertEquals(1, present.getAsInt());
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   141
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   142
        AtomicBoolean presentCheck = new AtomicBoolean();
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   143
        present.ifPresent(v -> presentCheck.set(true));
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   144
        assertTrue(presentCheck.get());
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   145
        presentCheck.set(false);
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   146
        present.ifPresentOrElse(v -> presentCheck.set(true), () -> fail());
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   147
        assertTrue(presentCheck.get());
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   148
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   149
        try {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   150
            present.ifPresent(v -> { throw new ObscureException(); });
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   151
            fail();
28963
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   152
        } catch (ObscureException expected) {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   153
        } catch (AssertionError e) {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   154
            throw e;
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   155
        } catch (Throwable t) {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   156
            fail();
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   157
        }
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   158
        try {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   159
            present.ifPresentOrElse(v -> { throw new ObscureException(); }, () -> fail());
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   160
            fail();
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   161
        } catch (ObscureException expected) {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   162
        } catch (AssertionError e) {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   163
            throw e;
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   164
        } catch (Throwable t) {
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   165
            fail();
8498cdb7c54b 8071670: java.util.Optional: please add a way to specify if-else behavior
psandoz
parents: 28754
diff changeset
   166
        }
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   167
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   168
        assertEquals(1, present.orElse(2));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   169
        assertEquals(1, present.orElseGet(null));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   170
        assertEquals(1, present.orElseGet(()-> 2));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   171
        assertEquals(1, present.orElseGet(()-> 3));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   172
        assertEquals(1, present.<RuntimeException>orElseThrow(null));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   173
        assertEquals(1, present.<RuntimeException>orElseThrow(ObscureException::new));
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   174
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   175
28754
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   176
    @Test(groups = "unit")
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   177
    public void testStream() {
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   178
        {
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   179
            IntStream s = OptionalInt.empty().stream();
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   180
            assertFalse(s.isParallel());
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   181
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   182
            int[] es = s.toArray();
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   183
            assertEquals(es.length, 0);
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   184
        }
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   185
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   186
        {
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   187
            IntStream s = OptionalInt.of(42).stream();
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   188
            assertFalse(s.isParallel());
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   189
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   190
            int[] es = OptionalInt.of(42).stream().toArray();
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   191
            assertEquals(es.length, 1);
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   192
            assertEquals(es[0], 42);
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   193
        }
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   194
    }
8fe59917548d 8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
psandoz
parents: 16489
diff changeset
   195
16489
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   196
    private static class ObscureException extends RuntimeException {
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   197
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   198
    }
53619efef3fb 8001642: Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong
mduigou
parents:
diff changeset
   199
}