From 115a52a124ce455bbe1c844a6cea93ed3d56cae8 Mon Sep 17 00:00:00 2001 From: Urvy Agrawal Date: Mon, 7 Jan 2019 22:03:52 +0530 Subject: [PATCH] Adding files for BAEL-2543 --- .../java/com/baeldung/keyword/Circle.java | 4 ++ .../main/java/com/baeldung/keyword/Ring.java | 4 ++ .../main/java/com/baeldung/keyword/Round.java | 4 ++ .../main/java/com/baeldung/keyword/Shape.java | 4 ++ .../java/com/baeldung/keyword/Triangle.java | 4 ++ .../keyword/test/InstanceOfUnitTest.java | 49 +++++++++++++++++++ 6 files changed, 69 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/keyword/Circle.java create mode 100644 core-java/src/main/java/com/baeldung/keyword/Ring.java create mode 100644 core-java/src/main/java/com/baeldung/keyword/Round.java create mode 100644 core-java/src/main/java/com/baeldung/keyword/Shape.java create mode 100644 core-java/src/main/java/com/baeldung/keyword/Triangle.java create mode 100644 core-java/src/test/java/com/baeldung/keyword/test/InstanceOfUnitTest.java diff --git a/core-java/src/main/java/com/baeldung/keyword/Circle.java b/core-java/src/main/java/com/baeldung/keyword/Circle.java new file mode 100644 index 0000000000..4ec91d1b8a --- /dev/null +++ b/core-java/src/main/java/com/baeldung/keyword/Circle.java @@ -0,0 +1,4 @@ +package com.baeldung.keyword; + +public class Circle extends Round implements Shape { +} diff --git a/core-java/src/main/java/com/baeldung/keyword/Ring.java b/core-java/src/main/java/com/baeldung/keyword/Ring.java new file mode 100644 index 0000000000..99873f9640 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/keyword/Ring.java @@ -0,0 +1,4 @@ +package com.baeldung.keyword; + +public class Ring extends Round { +} diff --git a/core-java/src/main/java/com/baeldung/keyword/Round.java b/core-java/src/main/java/com/baeldung/keyword/Round.java new file mode 100644 index 0000000000..0e2cc2c8c7 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/keyword/Round.java @@ -0,0 +1,4 @@ +package com.baeldung.keyword; + +public class Round { +} diff --git a/core-java/src/main/java/com/baeldung/keyword/Shape.java b/core-java/src/main/java/com/baeldung/keyword/Shape.java new file mode 100644 index 0000000000..8d00c165a3 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/keyword/Shape.java @@ -0,0 +1,4 @@ +package com.baeldung.keyword; + +public interface Shape { +} diff --git a/core-java/src/main/java/com/baeldung/keyword/Triangle.java b/core-java/src/main/java/com/baeldung/keyword/Triangle.java new file mode 100644 index 0000000000..406b8f23e5 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/keyword/Triangle.java @@ -0,0 +1,4 @@ +package com.baeldung.keyword; + +public class Triangle implements Shape { +} diff --git a/core-java/src/test/java/com/baeldung/keyword/test/InstanceOfUnitTest.java b/core-java/src/test/java/com/baeldung/keyword/test/InstanceOfUnitTest.java new file mode 100644 index 0000000000..fbeec3a077 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/keyword/test/InstanceOfUnitTest.java @@ -0,0 +1,49 @@ +package com.baeldung.keyword.test; + +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +import com.baeldung.keyword.Circle; +import com.baeldung.keyword.Ring; +import com.baeldung.keyword.Round; +import com.baeldung.keyword.Shape; +import com.baeldung.keyword.Triangle; + +public class InstanceOfUnitTest { + + @Test + public void giveWhenInstanceIsCorrect_thenReturnTrue() { + Ring ring = new Ring(); + Assert.assertTrue("ring is instance of Round ", ring instanceof Round); + } + + @Test + public void giveWhenObjectIsInstanceOfType_thenReturnTrue() { + Circle circle = new Circle(); + Assert.assertTrue("circle is instance of Circle ", circle instanceof Circle); + } + + + @Test + public void giveWhenInstanceIsOfSubtype_thenReturnTrue() { + Circle circle = new Circle(); + Assert.assertTrue("circle is instance of Round", circle instanceof Round); + } + + @Test + public void giveWhenTypeIsInterface_thenReturnTrue() { + Circle circle = new Circle(); + Assert.assertTrue("circle is instance of Shape", circle instanceof Shape); + } + + @Test + public void giveWhenInstanceValueIsNull_thenReturnFalse() { + Circle circle = null; + Assert.assertFalse("circle is instance of Round", circle instanceof Round); + } + + @Test + public void giveWhenComparingClassInDiffHierarchy_thenCompilationError() { + // Assert.assertFalse("circle is instance of Triangle", circle instanceof Triangle); + } +}