Search in sources :

Example 31 with OptionalInt

use of java.util.OptionalInt in project gerrit by GerritCodeReview.

the class PostReview method ensureSizeOfJsonInputIsWithinBounds.

private void ensureSizeOfJsonInputIsWithinBounds(RobotCommentInput robotCommentInput) throws BadRequestException {
    OptionalInt robotCommentSizeLimit = getRobotCommentSizeLimit();
    if (robotCommentSizeLimit.isPresent()) {
        int sizeLimit = robotCommentSizeLimit.getAsInt();
        byte[] robotCommentBytes = GSON.toJson(robotCommentInput).getBytes(StandardCharsets.UTF_8);
        int robotCommentSize = robotCommentBytes.length;
        if (robotCommentSize > sizeLimit) {
            throw new BadRequestException(String.format("Size %d (bytes) of robot comment is greater than limit %d (bytes)", robotCommentSize, sizeLimit));
        }
    }
}
Also used : BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) OptionalInt(java.util.OptionalInt)

Example 32 with OptionalInt

use of java.util.OptionalInt in project jdk8u_jdk by JetBrains.

the class BasicInt method testEmptyOrElseGetNull.

@Test(expectedExceptions = NullPointerException.class)
public void testEmptyOrElseGetNull() {
    OptionalInt empty = OptionalInt.empty();
    int got = empty.orElseGet(null);
}
Also used : OptionalInt(java.util.OptionalInt) Test(org.testng.annotations.Test)

Example 33 with OptionalInt

use of java.util.OptionalInt in project jdk8u_jdk by JetBrains.

the class BasicInt method testEmptyOrElseThrowNull.

@Test(expectedExceptions = NullPointerException.class)
public void testEmptyOrElseThrowNull() throws Throwable {
    OptionalInt empty = OptionalInt.empty();
    int got = empty.orElseThrow(null);
}
Also used : OptionalInt(java.util.OptionalInt) Test(org.testng.annotations.Test)

Example 34 with OptionalInt

use of java.util.OptionalInt in project jdk8u_jdk by JetBrains.

the class BasicInt method testEmptyOrElseThrow.

@Test(expectedExceptions = ObscureException.class)
public void testEmptyOrElseThrow() throws Exception {
    OptionalInt empty = OptionalInt.empty();
    int got = empty.orElseThrow(ObscureException::new);
}
Also used : OptionalInt(java.util.OptionalInt) Test(org.testng.annotations.Test)

Example 35 with OptionalInt

use of java.util.OptionalInt in project jdk8u_jdk by JetBrains.

the class BasicInt method testPresent.

@Test(groups = "unit")
public void testPresent() {
    OptionalInt empty = OptionalInt.empty();
    OptionalInt present = OptionalInt.of(1);
    // present
    assertTrue(present.equals(present));
    assertFalse(present.equals(OptionalInt.of(0)));
    assertTrue(present.equals(OptionalInt.of(1)));
    assertFalse(present.equals(empty));
    assertTrue(Integer.hashCode(1) == present.hashCode());
    assertFalse(present.toString().isEmpty());
    assertTrue(-1 != present.toString().indexOf(Integer.toString(present.getAsInt()).toString()));
    assertEquals(1, present.getAsInt());
    try {
        present.ifPresent(v -> {
            throw new ObscureException();
        });
        fail();
    } catch (ObscureException expected) {
    }
    assertEquals(1, present.orElse(2));
    assertEquals(1, present.orElseGet(null));
    assertEquals(1, present.orElseGet(() -> 2));
    assertEquals(1, present.orElseGet(() -> 3));
    assertEquals(1, present.<RuntimeException>orElseThrow(null));
    assertEquals(1, present.<RuntimeException>orElseThrow(ObscureException::new));
}
Also used : OptionalInt(java.util.OptionalInt) Test(org.testng.annotations.Test)

Aggregations

OptionalInt (java.util.OptionalInt)38 Test (org.testng.annotations.Test)11 List (java.util.List)8 UUID (java.util.UUID)7 Arrays (java.util.Arrays)6 IntStream (java.util.stream.IntStream)6 Optional (java.util.Optional)5 Test (org.junit.Test)5 ConnectivityManager (android.net.ConnectivityManager)4 NetworkCallback (android.net.ConnectivityManager.NetworkCallback)4 Network (android.net.Network)4 DnsEvent (android.net.metrics.DnsEvent)4 INetdEventListener (android.net.metrics.INetdEventListener)4 IpConnectivityLog (android.net.metrics.IpConnectivityLog)4 RemoteException (android.os.RemoteException)4 SmallTest (android.test.suitebuilder.annotation.SmallTest)4 PrestoException (com.facebook.presto.spi.PrestoException)4 Type (com.facebook.presto.spi.type.Type)4 FileOutputStream (java.io.FileOutputStream)4 PrintWriter (java.io.PrintWriter)4