From 4dda5a1a7c7d92fcc489767094e36002bb9374ca Mon Sep 17 00:00:00 2001 From: "Kent@lhind.hp.g5" Date: Wed, 10 Feb 2021 22:24:14 +0100 Subject: [PATCH] unchecked cast warning article --- .../baeldung/uncheckedcast/UncheckedCast.java | 26 +++++++++++++++++++ .../uncheckedcast/UncheckedCastUnitTest.java | 25 ++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/uncheckedcast/UncheckedCast.java create mode 100644 core-java-modules/core-java-lang-oop-generics/src/test/java/com/baeldung/uncheckedcast/UncheckedCastUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/uncheckedcast/UncheckedCast.java b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/uncheckedcast/UncheckedCast.java new file mode 100644 index 0000000000..fe79488d11 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/uncheckedcast/UncheckedCast.java @@ -0,0 +1,26 @@ +package com.baeldung.uncheckedcast; + +import java.time.LocalDate; +import java.time.Month; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +public class UncheckedCast { + public static Map getRawMap() { + Map rawMap = new HashMap(); + rawMap.put("date 1", LocalDate.of(2021, Month.FEBRUARY, 10)); + rawMap.put("date 2", LocalDate.of(1992, Month.AUGUST, 8)); + rawMap.put("date 3", LocalDate.of(1976, Month.NOVEMBER, 18)); + return rawMap; + } + + public static Map getRawMapWithMixedTypes() { + Map rawMap = new HashMap(); + rawMap.put("date 1", LocalDate.of(2021, Month.FEBRUARY, 10)); + rawMap.put("date 2", LocalDate.of(1992, Month.AUGUST, 8)); + rawMap.put("date 3", LocalDate.of(1976, Month.NOVEMBER, 18)); + rawMap.put("date 4", new Date()); + return rawMap; + } +} diff --git a/core-java-modules/core-java-lang-oop-generics/src/test/java/com/baeldung/uncheckedcast/UncheckedCastUnitTest.java b/core-java-modules/core-java-lang-oop-generics/src/test/java/com/baeldung/uncheckedcast/UncheckedCastUnitTest.java new file mode 100644 index 0000000000..b1bd46555e --- /dev/null +++ b/core-java-modules/core-java-lang-oop-generics/src/test/java/com/baeldung/uncheckedcast/UncheckedCastUnitTest.java @@ -0,0 +1,25 @@ +package com.baeldung.uncheckedcast; + +import org.junit.Assert; +import org.junit.Test; + +import java.time.LocalDate; +import java.time.Month; +import java.util.Map; + +public class UncheckedCastUnitTest { + + @Test + public void givenRawMap_whenCastToTypedMap_shouldHaveCompilerWarning() { + Map castFromRawMap = (Map) UncheckedCast.getRawMap(); + Assert.assertEquals(3, castFromRawMap.size()); + Assert.assertEquals(castFromRawMap.get("date 2"), LocalDate.of(1992, Month.AUGUST, 8)); + } + + @Test(expected = ClassCastException.class) + public void givenMixTypedRawMap_whenCastToTypedMap_shouldThrowClassCastException() { + Map castFromRawMap = (Map) UncheckedCast.getRawMapWithMixedTypes(); + Assert.assertEquals(4, castFromRawMap.size()); + Assert.assertTrue(castFromRawMap.get("date 4").isAfter(castFromRawMap.get("date 3"))); + } +}