blob: 12d97d392442fa8df898ed93417c72f56591a736 [file] [log] [blame]
/*
* Copyright 2015 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.emultest.java8.util;
import com.google.gwt.core.client.JavaScriptException;
import com.google.gwt.junit.client.GWTTestCase;
import java.util.NoSuchElementException;
import java.util.OptionalDouble;
/**
* Tests for OptionalDouble JRE emulation.
*/
public class OptionalDoubleTest extends GWTTestCase {
private static final double REFERENCE = 10d;
private static final double OTHER_REFERENCE = 20d;
private boolean[] mutableFlag;
private OptionalDouble empty;
private OptionalDouble present;
@Override
public String getModuleName() {
return "com.google.gwt.emultest.EmulSuite";
}
@Override
protected void gwtSetUp() throws Exception {
super.gwtSetUp();
mutableFlag = new boolean[1];
empty = OptionalDouble.empty();
present = OptionalDouble.of(REFERENCE);
}
public void testIsPresent() {
// empty case
assertFalse(empty.isPresent());
// non-empty case
assertTrue(present.isPresent());
}
public void testGetAsDouble() {
// empty case
try {
empty.getAsDouble();
fail("Empty Optional should throw NoSuchElementException");
} catch (NoSuchElementException e) {
// expected
}
// non-empty case
assertEquals(REFERENCE, present.getAsDouble());
}
public void testIfPresent() {
// empty case
empty.ifPresent(null); // should not fail as per JavaDoc
empty.ifPresent(wrapped -> fail("Empty Optional should not execute consumer"));
// non-empty case
try {
present.ifPresent(null);
fail("Non-Empty Optional must throw NullPointerException if consumer is null");
} catch (NullPointerException e) {
// expected
} catch (JavaScriptException e) {
// expected
}
present.ifPresent((wrapped) -> {
assertEquals(REFERENCE, wrapped);
mutableFlag[0] = true;
});
assertTrue("Consumer not executed", mutableFlag[0]);
}
public void testOrElse() {
// empty case
assertEquals(OTHER_REFERENCE, empty.orElse(OTHER_REFERENCE));
// non-empty case
assertEquals(REFERENCE, present.orElse(OTHER_REFERENCE));
}
public void testOrElseGet() {
// empty case
try {
empty.orElseGet(null);
fail("Empty Optional must throw NullPointerException if supplier is null");
} catch (NullPointerException e) {
// expected
} catch (JavaScriptException e) {
// expected
}
assertEquals(OTHER_REFERENCE, empty.orElseGet(() -> OTHER_REFERENCE));
// non-empty case
assertEquals(REFERENCE, present.orElseGet(() -> {
fail("Optional must not execute supplier");
return OTHER_REFERENCE;
}));
}
public void testOrElseThrow() {
// empty case
try {
empty.orElseThrow(null);
fail("Empty Optional must throw NullPointerException if supplier is null");
} catch (NullPointerException e) {
// expected
} catch (JavaScriptException e) {
// expected
}
try {
empty.orElseThrow(() -> null);
fail("Empty Optional must throw NullPointerException if supplier returns null");
} catch (NullPointerException e) {
// expected
} catch (JavaScriptException e) {
// expected
}
try {
empty.orElseThrow(IllegalStateException::new);
fail("Empty Optional must throw supplied exception");
} catch (IllegalStateException e) {
// expected
}
// non-empty case
try {
Object reference = present.orElseThrow(null);
assertEquals(REFERENCE, reference);
} catch (NullPointerException e) {
fail("Optional must not throw NullPointerException if supplier is null");
}
assertEquals(REFERENCE, present.orElseThrow(() -> {
fail("Optional must not execute supplier");
return new RuntimeException("should not execute");
}));
}
public void testEquals() {
// empty case
assertFalse(empty.equals(null));
assertFalse(empty.equals("should not be equal"));
assertFalse(empty.equals(present));
assertTrue(empty.equals(empty));
assertTrue(empty.equals(OptionalDouble.empty()));
// non empty case
assertFalse(present.equals(null));
assertFalse(present.equals("should not be equal"));
assertFalse(present.equals(empty));
assertFalse(present.equals(OptionalDouble.of(OTHER_REFERENCE)));
assertTrue(present.equals(present));
assertTrue(present.equals(OptionalDouble.of(REFERENCE)));
}
public void testHashcode() {
// empty case
assertEquals(0, empty.hashCode());
// non empty case
assertEquals(Double.hashCode(REFERENCE), present.hashCode());
}
}