From 28d708a2fa3f760b135793a398d1b8062e5d7df4 Mon Sep 17 00:00:00 2001 From: 515882294 <515882294@qq.com> Date: Tue, 14 Sep 2021 19:41:34 +0800 Subject: [PATCH] Add new section on instanceof --- .../IsInstanceIsAssignableFromUnitTest.java | 74 ++++++++++++++++--- 1 file changed, 62 insertions(+), 12 deletions(-) diff --git a/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/isinstancevsisassignablefrom/IsInstanceIsAssignableFromUnitTest.java b/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/isinstancevsisassignablefrom/IsInstanceIsAssignableFromUnitTest.java index e0ad264797..3e4ee0a1bb 100644 --- a/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/isinstancevsisassignablefrom/IsInstanceIsAssignableFromUnitTest.java +++ b/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/isinstancevsisassignablefrom/IsInstanceIsAssignableFromUnitTest.java @@ -6,57 +6,80 @@ import static org.junit.Assert.assertTrue; import org.junit.Test; public class IsInstanceIsAssignableFromUnitTest { - + @Test - public void whenUsingIsInstanceProperly_desiredResultsHappen() { + public void whenUsingInstanceOfProperly_thenDesiredResultsHappen() { + Shape shape = new Triangle(); + Triangle triangle = new Triangle(); + IsoscelesTriangle isoscelesTriangle = new IsoscelesTriangle(); + Shape nonspecificShape = null; + + assertTrue(shape instanceof Shape); + assertTrue(triangle instanceof Shape); + assertTrue(isoscelesTriangle instanceof Shape); + assertFalse(nonspecificShape instanceof Shape); + + assertTrue(shape instanceof Triangle); + assertTrue(triangle instanceof Triangle); + assertTrue(isoscelesTriangle instanceof Triangle); + assertFalse(nonspecificShape instanceof Triangle); + + assertFalse(shape instanceof IsoscelesTriangle); + assertFalse(triangle instanceof IsoscelesTriangle); + assertTrue(isoscelesTriangle instanceof IsoscelesTriangle); + assertFalse(nonspecificShape instanceof IsoscelesTriangle); + } + + @Test + public void whenUsingIsInstanceProperly_thenDesiredResultsHappen() { Shape shape = new Triangle(); Triangle triangle = new Triangle(); IsoscelesTriangle isoscelesTriangle = new IsoscelesTriangle(); Triangle isoscelesTriangle2 = new IsoscelesTriangle(); Shape nonspecificShape = null; - + assertTrue(Shape.class.isInstance(shape)); assertTrue(Shape.class.isInstance(triangle)); assertTrue(Shape.class.isInstance(isoscelesTriangle)); assertTrue(Shape.class.isInstance(isoscelesTriangle2)); assertFalse(Shape.class.isInstance(nonspecificShape)); - + assertTrue(Triangle.class.isInstance(shape)); assertTrue(Triangle.class.isInstance(triangle)); assertTrue(Triangle.class.isInstance(isoscelesTriangle)); assertTrue(Triangle.class.isInstance(isoscelesTriangle2)); - + assertFalse(IsoscelesTriangle.class.isInstance(shape)); assertFalse(IsoscelesTriangle.class.isInstance(triangle)); assertTrue(IsoscelesTriangle.class.isInstance(isoscelesTriangle)); - assertTrue(IsoscelesTriangle.class.isInstance(isoscelesTriangle2)); + assertTrue(IsoscelesTriangle.class.isInstance(isoscelesTriangle2)); } - + @Test - public void whenUsingIsAssignableFromProperly_desiredResultsHappen() { + public void whenUsingIsAssignableFromProperly_thenDesiredResultsHappen() { Shape shape = new Triangle(); Triangle triangle = new Triangle(); IsoscelesTriangle isoscelesTriangle = new IsoscelesTriangle(); Triangle isoscelesTriangle2 = new IsoscelesTriangle(); - + assertFalse(shape.getClass().isAssignableFrom(Shape.class)); assertTrue(shape.getClass().isAssignableFrom(shape.getClass())); assertTrue(shape.getClass().isAssignableFrom(triangle.getClass())); assertTrue(shape.getClass().isAssignableFrom(isoscelesTriangle.getClass())); assertTrue(shape.getClass().isAssignableFrom(isoscelesTriangle2.getClass())); - + assertFalse(triangle.getClass().isAssignableFrom(Shape.class)); assertTrue(triangle.getClass().isAssignableFrom(shape.getClass())); assertTrue(triangle.getClass().isAssignableFrom(triangle.getClass())); assertTrue(triangle.getClass().isAssignableFrom(isoscelesTriangle.getClass())); assertTrue(triangle.getClass().isAssignableFrom(isoscelesTriangle2.getClass())); - + assertFalse(isoscelesTriangle.getClass().isAssignableFrom(Shape.class)); assertFalse(isoscelesTriangle.getClass().isAssignableFrom(shape.getClass())); assertFalse(isoscelesTriangle.getClass().isAssignableFrom(triangle.getClass())); assertTrue(isoscelesTriangle.getClass().isAssignableFrom(isoscelesTriangle.getClass())); assertTrue(isoscelesTriangle.getClass().isAssignableFrom(isoscelesTriangle2.getClass())); - + assertFalse(isoscelesTriangle2.getClass().isAssignableFrom(Shape.class)); assertFalse(isoscelesTriangle2.getClass().isAssignableFrom(shape.getClass())); assertFalse(isoscelesTriangle2.getClass().isAssignableFrom(triangle.getClass())); @@ -64,4 +87,31 @@ public class IsInstanceIsAssignableFromUnitTest { assertTrue(isoscelesTriangle2.getClass().isAssignableFrom(isoscelesTriangle2.getClass())); } + @Test(expected = NullPointerException.class) + public void whenUsingNull_thenDesiredResultsHappen() { + assertFalse(null instanceof Shape); + assertFalse(Shape.class.isInstance(null)); + assertFalse(Shape.class.isAssignableFrom(null)); // NullPointerException + } + + @Test + public void whenUsingPrimitiveType_thenDesiredResultsHappen() { + //assertFalse(10 instanceof int); // illegal + assertFalse(int.class.isInstance(10)); + assertTrue(Integer.class.isInstance(10)); + assertTrue(int.class.isAssignableFrom(int.class)); + assertFalse(float.class.isAssignableFrom(int.class)); + } + + @Test + public void whenUsingClassInstanceVariable_thenDesiredResultsHappen() { + Shape shape = new Triangle(); + Triangle triangle = new Triangle(); + Class clazz = shape.getClass(); + + //assertFalse(triangle instanceof clazz); // illegal + assertTrue(clazz.isInstance(triangle)); + assertTrue(clazz.isAssignableFrom(triangle.getClass())); + } + }