2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.concepts;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertSame;
13 import static org.junit.Assert.assertThrows;
14 import static org.junit.Assert.assertTrue;
15 import static org.mockito.ArgumentMatchers.any;
16 import static org.mockito.Mockito.doNothing;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.verify;
20 import static org.mockito.Mockito.verifyZeroInteractions;
22 import java.util.function.Consumer;
23 import java.util.function.Function;
24 import java.util.function.Supplier;
25 import org.junit.Test;
27 public class CheckedValueTest {
29 public void testNullValue() {
30 assertThrows(NullPointerException.class, () -> CheckedValue.ofValue(null));
33 public void testExceptionGet() {
34 final CheckedValue<?, ?> value = CheckedValue.ofException(new Exception());
35 assertThrows(IllegalStateException.class, () -> value.get());
39 public void testValueException() {
40 final CheckedValue<?, ?> value = CheckedValue.ofValue("foo");
41 assertThrows(IllegalStateException.class, () -> value.getException());
45 public void testGet() {
46 final String value = "foo";
47 final CheckedValue<String, ?> val = CheckedValue.ofValue(value);
48 assertTrue(val.isPresent());
49 assertSame(value, val.get());
53 public void testThrowableGetThrowable() {
54 final Exception cause = new Exception();
55 final CheckedValue<?, ?> val = CheckedValue.ofException(cause);
56 assertFalse(val.isPresent());
57 assertSame(cause, val.getException());
61 public void testToString() {
62 assertEquals("CheckedValue{first=foo}", CheckedValue.ofValue("foo").toString());
63 assertEquals("CheckedValue{second=java.lang.NullPointerException: foo}",
64 CheckedValue.ofException(new NullPointerException("foo")).toString());
68 public void testEqualsHashCode() {
69 final CheckedValue<String, ?> fooVal = CheckedValue.ofValue("foo");
70 final CheckedValue<String, ?> fooVal2 = CheckedValue.ofValue("foo");
71 final CheckedValue<Integer, ?> oneVal = CheckedValue.ofValue(1);
72 final CheckedValue<?, ?> errBar = CheckedValue.ofException(new NullPointerException("bar"));
73 final CheckedValue<?, ?> errFoo = CheckedValue.ofException(new NullPointerException("foo"));
74 final CheckedValue<?, ?> errFoo2 = CheckedValue.ofException(new NullPointerException("foo"));
76 assertFalse(fooVal.equals(null));
77 assertFalse(fooVal.equals("foo"));
78 assertTrue(fooVal.equals(fooVal));
79 assertTrue(fooVal.equals(fooVal2));
80 assertFalse(fooVal.equals(oneVal));
81 assertEquals(fooVal.hashCode(), fooVal2.hashCode());
83 assertFalse(errFoo.equals(null));
84 assertFalse(errFoo.equals("foo"));
85 assertTrue(errFoo.equals(errFoo));
87 assertFalse(errBar.equals(errFoo));
91 public void testIfPresent() {
92 final String foo = "foo";
93 @SuppressWarnings("unchecked")
94 final Consumer<Object> consumer = mock(Consumer.class);
95 doNothing().when(consumer).accept(any(Object.class));
96 CheckedValue.ofValue(foo).ifPresent(consumer);
97 verify(consumer).accept(foo);
101 public void testThrowableIfPresent() {
102 @SuppressWarnings("unchecked")
103 final Consumer<Object> consumer = mock(Consumer.class);
104 doNothing().when(consumer).accept(any(Object.class));
105 CheckedValue.ofException(new NullPointerException()).ifPresent(consumer);
106 verifyZeroInteractions(consumer);
110 public void testOrElse() {
111 final String foo = "foo";
112 final String bar = "bar";
113 assertSame(foo, CheckedValue.ofValue(foo).orElse(bar));
114 assertSame(bar, CheckedValue.ofException(new NullPointerException()).orElse(bar));
118 public void testMap() {
119 final String foo = "foo";
120 final String bar = "bar";
121 final CheckedValue<Object, ?> errVal = CheckedValue.ofValue(foo);
122 @SuppressWarnings("unchecked")
123 final Function<Object, Object> mapper = mock(Function.class);
124 doReturn(bar).when(mapper).apply(any(Object.class));
125 assertSame(bar, errVal.map(mapper).get());
126 verify(mapper).apply(foo);
130 public void testExceptionMap() {
131 final CheckedValue<Object, ?> errVal = CheckedValue.ofException(new NullPointerException());
132 @SuppressWarnings("unchecked")
133 final Function<Object, Object> mapper = mock(Function.class);
134 doReturn(null).when(mapper).apply(any(Object.class));
135 assertSame(errVal, errVal.map(mapper));
136 verifyZeroInteractions(mapper);
140 public void testOrElseThrow() {
141 final String foo = "foo";
142 assertSame(foo, CheckedValue.ofValue(foo)
143 .orElseThrow((Supplier<NullPointerException>)NullPointerException::new));
147 public void testThrowableOrElseThrow() {
148 final CheckedValue<?, ?> value = CheckedValue.ofException(new NullPointerException("foo"));
149 final Exception mock = mock(Exception.class);
150 assertThrows(mock.getClass(), () -> value.orElseThrow(() -> mock));
154 public void testOrElseGet() {
155 final String foo = "foo";
156 @SuppressWarnings("unchecked")
157 final Supplier<String> supplier = mock(Supplier.class);
158 doReturn(null).when(supplier).get();
159 assertSame(foo, CheckedValue.ofValue(foo).orElseGet(supplier));
160 verifyZeroInteractions(supplier);
164 public void testExceptionOrElseGet() {
165 final String bar = "bar";
166 @SuppressWarnings("unchecked")
167 final Supplier<Object> supplier = mock(Supplier.class);
168 doReturn(bar).when(supplier).get();
170 assertSame(bar, CheckedValue.ofException(new NullPointerException()).orElseGet(supplier));
171 verify(supplier).get();