From ac6390da3b7d9851a9ffe5d8e9e1b2cebe37a149 Mon Sep 17 00:00:00 2001 From: Gergo Petrik Date: Wed, 24 Jun 2020 08:16:17 +0200 Subject: [PATCH] Bal 4152 covariant return type (#9561) * BAEL-4152: added covariant return type material * BAEL-4152: covariant return type article related code is moved to another package --- .../baeldung/covariance/IntegerProducer.java | 8 +++ .../com/baeldung/covariance/Producer.java | 8 +++ .../CovariantProducersUnitTest.java | 49 +++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/covariance/IntegerProducer.java create mode 100644 core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/covariance/Producer.java create mode 100644 core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/covariance/CovariantProducersUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/covariance/IntegerProducer.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/covariance/IntegerProducer.java new file mode 100644 index 0000000000..6a631d45b0 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/covariance/IntegerProducer.java @@ -0,0 +1,8 @@ +package com.baeldung.covariance; + +public class IntegerProducer extends Producer { + @Override + public Integer produce(String input) { + return Integer.parseInt(input); + } +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/covariance/Producer.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/covariance/Producer.java new file mode 100644 index 0000000000..94f79ae525 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/covariance/Producer.java @@ -0,0 +1,8 @@ +package com.baeldung.covariance; + +public class Producer { + public Object produce(String input) { + Object result = input.toLowerCase(); + return result; + } +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/covariance/CovariantProducersUnitTest.java b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/covariance/CovariantProducersUnitTest.java new file mode 100644 index 0000000000..2645889ac8 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/covariance/CovariantProducersUnitTest.java @@ -0,0 +1,49 @@ +package com.baeldung.covariance; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class CovariantProducersUnitTest { + + @Test + public void whenInputIsArbitrary_thenProducerProducesString() { + String arbitraryInput = "just a random text"; + Producer producer = new Producer(); + + Object objectOutput = producer.produce(arbitraryInput); + + assertEquals(arbitraryInput, objectOutput); + assertEquals(String.class, objectOutput.getClass()); + } + + @Test + public void whenInputIsArbitrary_thenIntegerProducerFails() { + String arbitraryInput = "just a random text"; + Producer producer = new IntegerProducer(); + + assertThrows(NumberFormatException.class, () -> producer.produce(arbitraryInput)); + } + + @Test + public void whenInputIsSupported_thenProducerCreatesInteger() { + String integerAsString = "42"; + Producer producer = new IntegerProducer(); + + Object result = producer.produce(integerAsString); + + assertEquals(Integer.class, result.getClass()); + assertEquals(Integer.parseInt(integerAsString), result); + } + + @Test + public void whenInputIsSupported_thenIntegerProducerCreatesIntegerWithoutCasting() { + String integerAsString = "42"; + IntegerProducer producer = new IntegerProducer(); + + Integer result = producer.produce(integerAsString); + + assertEquals(Integer.parseInt(integerAsString), result); + } +}