|
| 1 | +package com.howtoprogram.java9ex; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import java.io.*; |
| 6 | +import java.util.*; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 10 | + |
| 11 | + |
| 12 | +public class Java9SetFactoryMethodTest { |
| 13 | + |
| 14 | + |
| 15 | + @Test |
| 16 | + public void testImmutableSetJava9() { |
| 17 | + |
| 18 | + Set<String> japanNoodles = Set.of("Ramen", "Yakisoba", "Udon", "Soba", "Somen"); |
| 19 | + assertEquals(5, japanNoodles.size()); |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + public void testImmutableSetNotModified() { |
| 24 | + |
| 25 | + |
| 26 | + Set<String> japanNoodles = Set.of("Ramen", "Yakisoba", "Udon", "Soba", "Somen"); |
| 27 | + |
| 28 | + assertEquals(5, japanNoodles.size()); |
| 29 | + |
| 30 | + assertThrows(UnsupportedOperationException.class, () -> { |
| 31 | + // Add more element |
| 32 | + japanNoodles.add("Negi"); |
| 33 | + // Remove element |
| 34 | + japanNoodles.remove(1); |
| 35 | + }); |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testImmutableSetNullAttempts() { |
| 41 | + |
| 42 | + assertThrows(NullPointerException.class, () -> { |
| 43 | + Set<String> stringList = Set.of(null); |
| 44 | + }); |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testImmutableSetRejectDupplicates() { |
| 50 | + |
| 51 | + assertThrows(IllegalArgumentException.class, () -> { |
| 52 | + Set<?> teaSet = Set.of("Shincha", "Aki Bancha", "Hojicha", "Hojicha"); |
| 53 | + }); |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testImmutableSetMutableElements() { |
| 59 | + |
| 60 | + Map<Integer, String> noodleToppingsMap = new HashMap<>(); |
| 61 | + noodleToppingsMap.put(1, "Negi"); |
| 62 | + noodleToppingsMap.put(2, "Tamago"); |
| 63 | + noodleToppingsMap.put(3, "Nori"); |
| 64 | + |
| 65 | + Map<Integer, String> japanTeaMap = new HashMap<>(); |
| 66 | + japanTeaMap.put(1, "Ryokucha"); |
| 67 | + japanTeaMap.put(2, "Yamecha"); |
| 68 | + japanTeaMap.put(3, "Ujicha"); |
| 69 | + |
| 70 | + List<Map<Integer, String>> choices = List.of(noodleToppingsMap, japanTeaMap); |
| 71 | + |
| 72 | + assertEquals(3, choices.get(0).size()); |
| 73 | + |
| 74 | + //Add more elements to the mutable element of the List |
| 75 | + noodleToppingsMap.put(3, "Tsuyu"); |
| 76 | + choices.get(0).put(4, "Aracha"); |
| 77 | + |
| 78 | + assertEquals(4, choices.get(0).size()); |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testImmutableSetSerializable() throws IOException { |
| 84 | + |
| 85 | + Set<String> drinks = Set.of("Gyokuro", "Tamaryokucha", "Kamairicha"); |
| 86 | + //serialize the list |
| 87 | + ObjectOutputStream oos = new ObjectOutputStream(System.out); |
| 88 | + oos.writeObject(drinks); |
| 89 | + |
| 90 | + //create a list of non-serializable objects |
| 91 | + List<OutputStream> outputStreamList = List.of(new ByteArrayOutputStream(), |
| 92 | + new PrintStream(System.out)); |
| 93 | + |
| 94 | + assertThrows(NotSerializableException.class, () -> { |
| 95 | + |
| 96 | + oos.writeObject(outputStreamList); |
| 97 | + } |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments