more Unit tests

This commit is contained in:
anhefti 2022-03-07 16:07:16 +01:00
parent 6146f2e4ce
commit ef02fe2846
6 changed files with 185 additions and 33 deletions

View file

@ -164,11 +164,7 @@ public final class Result<T> {
return this.value != null ? this.value : supplier.get();
}
public Result<T> orElse(final Supplier<Result<T>> supplier) {
return this.value != null ? this : supplier.get();
}
public Result<T> orElseTry(final Supplier<T> supplier) {
public Result<T> orElse(final Supplier<T> supplier) {
return this.value != null ? this : Result.tryCatch(supplier::get);
}
@ -191,23 +187,6 @@ public final class Result<T> {
return this.value != null && this.error == null;
}
/** If a value is present, performs the given action with the value,
* otherwise performs the given empty-based action.
*
* @param action the action to be performed, if a value is present
* @param emptyAction the empty-based action to be performed, if no value is
* present
* @throws NullPointerException if a value is present and the given action
* is {@code null}, or no value is present and the given empty-based
* action is {@code null}. */
public void ifOrElse(final Consumer<? super T> action, final Runnable emptyAction) {
if (this.value != null) {
action.accept(this.value);
} else {
emptyAction.run();
}
}
public void ifPresent(final Consumer<T> consumer) {
if (this == EMPTY) {
consumer.accept(this.value);

View file

@ -31,7 +31,7 @@ public class Tuple<T> {
}
@SuppressWarnings("unchecked")
public <TT extends Tuple<T>> TT adaptTo(Class<TT> type) {
public <TT extends Tuple<T>> TT adaptTo(final Class<TT> type) {
if (type.equals(this.getClass())) {
return (TT) this;
}
@ -73,7 +73,7 @@ public class Tuple<T> {
@Override
public String toString() {
return "( " + this._1 + ", " + this._2 + ")";
return "(" + this._1 + ", " + this._2 + ")";
}
}

View file

@ -17,25 +17,33 @@ public class Tuple3<T> extends Tuple<T> {
public final T _3;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Tuple3<?> tuple3 = (Tuple3<?>) o;
return Objects.equals(_3, tuple3._3);
public boolean equals(final Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
if (!super.equals(o))
return false;
final Tuple3<?> tuple3 = (Tuple3<?>) o;
return Objects.equals(this._3, tuple3._3);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), _3);
return Objects.hash(super.hashCode(), this._3);
}
public Tuple3(T _1, T _2, T _3) {
public Tuple3(final T _1, final T _2, final T _3) {
super(_1, _2);
this._3 = _3;
}
public T get_3() {
return _3;
return this._3;
}
@Override
public String toString() {
return "(" + this._1 + ", " + this._2 + ", " + this._3 + ")";
}
}

View file

@ -10,6 +10,8 @@ package ch.ethz.seb.sebserver.gbl.util;
import static org.junit.Assert.*;
import java.util.concurrent.ExecutionException;
import org.junit.Test;
public class ResultTest {
@ -80,6 +82,21 @@ public class ResultTest {
assertEquals("ONE", resultOf.getOrElse(() -> "TWO"));
assertEquals("TWO", resultOfError.getOrElse(() -> "TWO"));
Result<String> orElse = resultOfError.orElse(() -> {
return "Else";
});
assertNotNull(orElse);
assertFalse(orElse.hasError());
assertEquals("Else", orElse.get());
orElse = resultOf.orElse(() -> {
return "Else";
});
assertNotNull(orElse);
assertFalse(orElse.hasError());
assertEquals("ONE", orElse.get());
}
@Test
@ -100,4 +117,91 @@ public class ResultTest {
}
}
@Test
public void testGetWithError() {
final Result<String> resultOf = Result.of("ONE");
String string = resultOf.get(error -> fail("should net be called"), () -> "ERROR");
assertEquals("ONE", string);
string = resultOf.getOrThrow();
assertEquals("ONE", string);
final Result<String> errorOf = Result.ofError(new RuntimeException("error"));
string = errorOf.get(error -> assertEquals("error", error.getMessage()), () -> "ERROR");
assertEquals("ERROR", string);
errorOf.handleError(error -> assertEquals("error", error.getMessage()));
try {
errorOf.getOrThrow();
} catch (final Exception e) {
assertEquals("error", e.getMessage());
}
try {
errorOf.getOrThrow(error -> new RuntimeException("error2"));
} catch (final Exception e) {
assertEquals("error2", e.getMessage());
}
final String orThrow = resultOf.getOrThrow();
assertEquals("ONE", orThrow);
assertTrue(resultOf.hasValue());
assertFalse(errorOf.hasValue());
resultOf.ifPresent(t -> {
assertEquals("ONE", t);
});
errorOf.ifPresent(t -> {
fail("Should not be called here");
});
final Result<String> onSuccess = resultOf.onSuccess(t -> {
assertEquals("ONE", t);
});
assertNotNull(onSuccess);
errorOf.onSuccess(t -> {
fail("Should not be called here");
});
final Result<String> onError = errorOf.onError(error -> {
assertNotNull(error);
assertEquals("error", error.getMessage());
});
assertNotNull(onError);
final Result<String> onErrorDo = errorOf.onErrorDo(error -> {
assertNotNull(error);
assertEquals("error", error.getMessage());
return error.getMessage();
});
assertNotNull(onErrorDo);
assertEquals("error", onErrorDo.get());
errorOf.onErrorDo(error -> {
assertNotNull(error);
assertEquals("error", error.getMessage());
return error.getMessage();
}, RuntimeException.class);
errorOf.onErrorDo(error -> {
fail("Should not be called here");
return "";
}, ExecutionException.class);
}
@Test
public void testEquals() {
final Result<String> one = Result.of("ONE");
final Result<String> two = Result.of("TWO");
final Result<String> one_ = Result.of("ONE");
assertEquals(one, one);
assertEquals(one, one_);
assertNotEquals(one, two);
assertNotEquals(null, two);
assertNotEquals(one, null);
}
}

View file

@ -0,0 +1,35 @@
/*
* Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package ch.ethz.seb.sebserver.gbl.util;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class SizedArrayNonBlockingQueueTest {
@Test
public void test() {
final SizedArrayNonBlockingQueue<String> candidate = new SizedArrayNonBlockingQueue<>(3);
candidate.add("1");
assertEquals("[1]", candidate.toString());
candidate.add("2");
assertEquals("[1, 2]", candidate.toString());
candidate.add("3");
assertEquals("[1, 2, 3]", candidate.toString());
candidate.add("4");
assertEquals("[2, 3, 4]", candidate.toString());
}
}

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2022 ETH Zürich, Educational Development and Technology (LET)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package ch.ethz.seb.sebserver.gbl.util;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class Tuple3Test {
@Test
public void test() {
final Tuple3<String> candidate = new Tuple3<>("1", "2", "3");
assertEquals("(1, 2, 3)", candidate.toString());
assertEquals("1", candidate.get_1());
assertEquals("2", candidate.get_2());
assertEquals("3", candidate.get_3());
}
}