|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 Dominik Schadow, dominikschadow@gmail.com |
| 3 | + * |
| 4 | + * This file is part of the Java Security project. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package de.dominikschadow.javasecurity.serialize; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.AfterEach; |
| 21 | +import org.junit.jupiter.api.BeforeEach; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import java.io.BufferedInputStream; |
| 25 | +import java.io.File; |
| 26 | +import java.io.FileInputStream; |
| 27 | +import java.io.FileOutputStream; |
| 28 | +import java.io.ObjectInputStream; |
| 29 | +import java.io.ObjectOutputStream; |
| 30 | + |
| 31 | +import static org.junit.jupiter.api.Assertions.*; |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests for the Deserializer class. |
| 35 | + * |
| 36 | + * @author Dominik Schadow |
| 37 | + */ |
| 38 | +class DeserializerTest { |
| 39 | + private static final String TEST_FILE = "test-deserialize-me.bin"; |
| 40 | + |
| 41 | + @AfterEach |
| 42 | + void tearDown() { |
| 43 | + File file = new File(TEST_FILE); |
| 44 | + if (file.exists()) { |
| 45 | + file.delete(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void deserialize_validFile_returnsCorrectObject() throws Exception { |
| 51 | + SerializeMe original = new SerializeMe(); |
| 52 | + original.setFirstname("Arthur"); |
| 53 | + original.setLastname("Dent"); |
| 54 | + |
| 55 | + try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(TEST_FILE))) { |
| 56 | + oos.writeObject(original); |
| 57 | + oos.flush(); |
| 58 | + } |
| 59 | + |
| 60 | + try (ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(new FileInputStream(TEST_FILE)))) { |
| 61 | + SerializeMe deserialized = (SerializeMe) is.readObject(); |
| 62 | + |
| 63 | + assertNotNull(deserialized); |
| 64 | + assertEquals("Arthur", deserialized.getFirstname()); |
| 65 | + assertEquals("Dent", deserialized.getLastname()); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void deserialize_withNullValues_returnsObjectWithNullFields() throws Exception { |
| 71 | + SerializeMe original = new SerializeMe(); |
| 72 | + |
| 73 | + try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(TEST_FILE))) { |
| 74 | + oos.writeObject(original); |
| 75 | + oos.flush(); |
| 76 | + } |
| 77 | + |
| 78 | + try (ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(new FileInputStream(TEST_FILE)))) { |
| 79 | + SerializeMe deserialized = (SerializeMe) is.readObject(); |
| 80 | + |
| 81 | + assertNotNull(deserialized); |
| 82 | + assertNull(deserialized.getFirstname()); |
| 83 | + assertNull(deserialized.getLastname()); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void deserialize_nonExistentFile_throwsException() { |
| 89 | + assertThrows(Exception.class, () -> { |
| 90 | + try (ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(new FileInputStream("non-existent-file.bin")))) { |
| 91 | + is.readObject(); |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void deserialize_multipleObjects_returnsAllCorrectly() throws Exception { |
| 98 | + SerializeMe first = new SerializeMe(); |
| 99 | + first.setFirstname("Ford"); |
| 100 | + first.setLastname("Prefect"); |
| 101 | + |
| 102 | + SerializeMe second = new SerializeMe(); |
| 103 | + second.setFirstname("Zaphod"); |
| 104 | + second.setLastname("Beeblebrox"); |
| 105 | + |
| 106 | + try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(TEST_FILE))) { |
| 107 | + oos.writeObject(first); |
| 108 | + oos.writeObject(second); |
| 109 | + oos.flush(); |
| 110 | + } |
| 111 | + |
| 112 | + try (ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(new FileInputStream(TEST_FILE)))) { |
| 113 | + SerializeMe deserializedFirst = (SerializeMe) is.readObject(); |
| 114 | + SerializeMe deserializedSecond = (SerializeMe) is.readObject(); |
| 115 | + |
| 116 | + assertEquals("Ford", deserializedFirst.getFirstname()); |
| 117 | + assertEquals("Prefect", deserializedFirst.getLastname()); |
| 118 | + assertEquals("Zaphod", deserializedSecond.getFirstname()); |
| 119 | + assertEquals("Beeblebrox", deserializedSecond.getLastname()); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments