JAVA-8279 Split or move Java core module (fix review comments)

This commit is contained in:
mikr
2021-11-14 23:02:03 +01:00
parent adce68ae25
commit 1027a6d76d
4 changed files with 75 additions and 7 deletions

View File

@@ -25,7 +25,7 @@ public class DeserializationUnitTest {
@Test
public void testDeserializeObj_compatible() throws IOException, ClassNotFoundException {
Assert.assertEquals(userDefinedSerialVersionUID, AppleProduct.getSerialVersionUID());
assertEquals(userDefinedSerialVersionUID, AppleProduct.getSerialVersionUID());
AppleProduct macBook = new AppleProduct();
macBook.headphonePort = "headphonePort2020";
@@ -61,7 +61,7 @@ public class DeserializationUnitTest {
@Test(expected = InvalidClassException.class)
public void testDeserializeObj_incompatible() throws ClassNotFoundException, IOException {
Assert.assertNotEquals(userDefinedSerialVersionUID, AppleProduct.getSerialVersionUID());
assertNotEquals(userDefinedSerialVersionUID, AppleProduct.getSerialVersionUID());
// attempts to deserialize the "AppleProduct" object
DeserializationUtility.deSerializeObjectFromString(serializedObj);
}

View File

@@ -0,0 +1,71 @@
package com.baeldung.externalizable;
import org.junit.Test;
import java.io.*;
import static org.junit.Assert.assertTrue;
public class ExternalizableUnitTest {
private final static String OUTPUT_FILE = "externalizable.txt";
@Test
public void whenSerializing_thenUseExternalizable() throws IOException, ClassNotFoundException {
Country c = new Country();
c.setCapital("Yerevan");
c.setCode(374);
c.setName("Armenia");
FileOutputStream fileOutputStream = new FileOutputStream(OUTPUT_FILE);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
c.writeExternal(objectOutputStream);
objectOutputStream.flush();
objectOutputStream.close();
fileOutputStream.close();
FileInputStream fileInputStream = new FileInputStream(OUTPUT_FILE);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
Country c2 = new Country();
c2.readExternal(objectInputStream);
objectInputStream.close();
fileInputStream.close();
assertTrue(c2.getCode() == c.getCode());
assertTrue(c2.getName().equals(c.getName()));
}
@Test
public void whenInheritanceSerialization_then_UseExternalizable() throws IOException, ClassNotFoundException {
Region r = new Region();
r.setCapital("Yerevan");
r.setCode(374);
r.setName("Armenia");
r.setClimate("Mediterranean");
r.setPopulation(120.000);
FileOutputStream fileOutputStream = new FileOutputStream(OUTPUT_FILE);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
r.writeExternal(objectOutputStream);
objectOutputStream.flush();
objectOutputStream.close();
fileOutputStream.close();
FileInputStream fileInputStream = new FileInputStream(OUTPUT_FILE);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
Region r2 = new Region();
r2.readExternal(objectInputStream);
objectInputStream.close();
fileInputStream.close();
assertTrue(r2.getPopulation() == null);
}
}