From ef02fe2846988c7ab4b6b301e6c0b9643af25bac Mon Sep 17 00:00:00 2001 From: anhefti Date: Mon, 7 Mar 2022 16:07:16 +0100 Subject: [PATCH] more Unit tests --- .../ethz/seb/sebserver/gbl/util/Result.java | 23 +--- .../ch/ethz/seb/sebserver/gbl/util/Tuple.java | 4 +- .../ethz/seb/sebserver/gbl/util/Tuple3.java | 26 +++-- .../seb/sebserver/gbl/util/ResultTest.java | 104 ++++++++++++++++++ .../util/SizedArrayNonBlockingQueueTest.java | 35 ++++++ .../seb/sebserver/gbl/util/Tuple3Test.java | 26 +++++ 6 files changed, 185 insertions(+), 33 deletions(-) create mode 100644 src/test/java/ch/ethz/seb/sebserver/gbl/util/SizedArrayNonBlockingQueueTest.java create mode 100644 src/test/java/ch/ethz/seb/sebserver/gbl/util/Tuple3Test.java diff --git a/src/main/java/ch/ethz/seb/sebserver/gbl/util/Result.java b/src/main/java/ch/ethz/seb/sebserver/gbl/util/Result.java index fb67d943..956e1b9b 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gbl/util/Result.java +++ b/src/main/java/ch/ethz/seb/sebserver/gbl/util/Result.java @@ -164,11 +164,7 @@ public final class Result { return this.value != null ? this.value : supplier.get(); } - public Result orElse(final Supplier> supplier) { - return this.value != null ? this : supplier.get(); - } - - public Result orElseTry(final Supplier supplier) { + public Result orElse(final Supplier supplier) { return this.value != null ? this : Result.tryCatch(supplier::get); } @@ -191,23 +187,6 @@ public final class Result { 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 action, final Runnable emptyAction) { - if (this.value != null) { - action.accept(this.value); - } else { - emptyAction.run(); - } - } - public void ifPresent(final Consumer consumer) { if (this == EMPTY) { consumer.accept(this.value); diff --git a/src/main/java/ch/ethz/seb/sebserver/gbl/util/Tuple.java b/src/main/java/ch/ethz/seb/sebserver/gbl/util/Tuple.java index 7f2e67d5..21de222f 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gbl/util/Tuple.java +++ b/src/main/java/ch/ethz/seb/sebserver/gbl/util/Tuple.java @@ -31,7 +31,7 @@ public class Tuple { } @SuppressWarnings("unchecked") - public > TT adaptTo(Class type) { + public > TT adaptTo(final Class type) { if (type.equals(this.getClass())) { return (TT) this; } @@ -73,7 +73,7 @@ public class Tuple { @Override public String toString() { - return "( " + this._1 + ", " + this._2 + ")"; + return "(" + this._1 + ", " + this._2 + ")"; } } diff --git a/src/main/java/ch/ethz/seb/sebserver/gbl/util/Tuple3.java b/src/main/java/ch/ethz/seb/sebserver/gbl/util/Tuple3.java index f244cec2..32f59fae 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gbl/util/Tuple3.java +++ b/src/main/java/ch/ethz/seb/sebserver/gbl/util/Tuple3.java @@ -17,25 +17,33 @@ public class Tuple3 extends Tuple { 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 + ")"; } } diff --git a/src/test/java/ch/ethz/seb/sebserver/gbl/util/ResultTest.java b/src/test/java/ch/ethz/seb/sebserver/gbl/util/ResultTest.java index b94ef1c6..ba2918c0 100644 --- a/src/test/java/ch/ethz/seb/sebserver/gbl/util/ResultTest.java +++ b/src/test/java/ch/ethz/seb/sebserver/gbl/util/ResultTest.java @@ -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 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 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 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 onSuccess = resultOf.onSuccess(t -> { + assertEquals("ONE", t); + }); + assertNotNull(onSuccess); + + errorOf.onSuccess(t -> { + fail("Should not be called here"); + }); + + final Result onError = errorOf.onError(error -> { + assertNotNull(error); + assertEquals("error", error.getMessage()); + }); + assertNotNull(onError); + + final Result 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 one = Result.of("ONE"); + final Result two = Result.of("TWO"); + final Result one_ = Result.of("ONE"); + + assertEquals(one, one); + assertEquals(one, one_); + assertNotEquals(one, two); + assertNotEquals(null, two); + assertNotEquals(one, null); + } + } diff --git a/src/test/java/ch/ethz/seb/sebserver/gbl/util/SizedArrayNonBlockingQueueTest.java b/src/test/java/ch/ethz/seb/sebserver/gbl/util/SizedArrayNonBlockingQueueTest.java new file mode 100644 index 00000000..3f074be8 --- /dev/null +++ b/src/test/java/ch/ethz/seb/sebserver/gbl/util/SizedArrayNonBlockingQueueTest.java @@ -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 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()); + + } + +} diff --git a/src/test/java/ch/ethz/seb/sebserver/gbl/util/Tuple3Test.java b/src/test/java/ch/ethz/seb/sebserver/gbl/util/Tuple3Test.java new file mode 100644 index 00000000..24528ece --- /dev/null +++ b/src/test/java/ch/ethz/seb/sebserver/gbl/util/Tuple3Test.java @@ -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 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()); + } + +}