[JAVA-9021] Clean up uncommitted artifacts (#11754)

This commit is contained in:
Haroon Khan 2022-02-01 14:08:56 +00:00 committed by GitHub
parent 19d7034d42
commit a7980094d1
10 changed files with 382 additions and 329 deletions

View File

@ -1,3 +1,5 @@
*.docx
temp.xls
temp.xlsx
CellStyleTest_output.xlsx

View File

@ -2,3 +2,5 @@
.settings/
.classpath
.project
*.log

View File

@ -1,14 +1,29 @@
package com.baeldung.externalizable;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public class ExternalizableUnitTest {
private final static String OUTPUT_FILE = "externalizable.txt";
private final static String OUTPUT_FILE_NAME = "externalizable.txt";
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
private File outputFile;
@Before
public void setUp() throws Exception {
outputFile = tempFolder.newFile(OUTPUT_FILE_NAME);
}
@Test
public void whenSerializing_thenUseExternalizable() throws IOException, ClassNotFoundException {
@ -18,7 +33,7 @@ public class ExternalizableUnitTest {
c.setCode(374);
c.setName("Armenia");
FileOutputStream fileOutputStream = new FileOutputStream(OUTPUT_FILE);
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
c.writeExternal(objectOutputStream);
@ -26,7 +41,7 @@ public class ExternalizableUnitTest {
objectOutputStream.close();
fileOutputStream.close();
FileInputStream fileInputStream = new FileInputStream(OUTPUT_FILE);
FileInputStream fileInputStream = new FileInputStream(outputFile);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
Country c2 = new Country();
@ -35,8 +50,8 @@ public class ExternalizableUnitTest {
objectInputStream.close();
fileInputStream.close();
assertTrue(c2.getCode() == c.getCode());
assertTrue(c2.getName().equals(c.getName()));
assertEquals(c2.getCode(), c.getCode());
assertEquals(c2.getName(), c.getName());
}
@Test
@ -49,7 +64,7 @@ public class ExternalizableUnitTest {
r.setClimate("Mediterranean");
r.setPopulation(120.000);
FileOutputStream fileOutputStream = new FileOutputStream(OUTPUT_FILE);
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
r.writeExternal(objectOutputStream);
@ -57,7 +72,7 @@ public class ExternalizableUnitTest {
objectOutputStream.close();
fileOutputStream.close();
FileInputStream fileInputStream = new FileInputStream(OUTPUT_FILE);
FileInputStream fileInputStream = new FileInputStream(outputFile);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
Region r2 = new Region();
@ -66,6 +81,6 @@ public class ExternalizableUnitTest {
objectInputStream.close();
fileInputStream.close();
assertTrue(r2.getPopulation() == null);
assertNull(r2.getPopulation());
}
}

View File

@ -1,36 +1,54 @@
package com.baeldung.serialization;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class PersonUnitTest {
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
private File outputFile;
private File outputFile2;
@Before
public void setUp() throws Exception {
outputFile = tempFolder.newFile("yourfile.txt");
outputFile2 = tempFolder.newFile("yourfile2.txt");
}
@Test
public void whenSerializingAndDeserializing_ThenObjectIsTheSame() throws IOException, ClassNotFoundException {
Person p = new Person();
p.setAge(20);
p.setName("Joe");
FileOutputStream fileOutputStream = new FileOutputStream("yofile.txt");
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(p);
objectOutputStream.flush();
objectOutputStream.close();
FileInputStream fileInputStream = new FileInputStream("yofile.txt");
FileInputStream fileInputStream = new FileInputStream(outputFile);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
Person p2 = (Person) objectInputStream.readObject();
objectInputStream.close();
assertTrue(p2.getAge() == p.getAge());
assertTrue(p2.getName().equals(p.getName()));
assertEquals(p2.getAge(), p.getAge());
assertEquals(p2.getName(), p.getName());
}
@Test
@ -46,19 +64,19 @@ public class PersonUnitTest {
e.setPerson(p);
e.setAddress(a);
FileOutputStream fileOutputStream = new FileOutputStream("yofile2.txt");
FileOutputStream fileOutputStream = new FileOutputStream(outputFile2);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(e);
objectOutputStream.flush();
objectOutputStream.close();
FileInputStream fileInputStream = new FileInputStream("yofile2.txt");
FileInputStream fileInputStream = new FileInputStream(outputFile2);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
Employee e2 = (Employee) objectInputStream.readObject();
objectInputStream.close();
assertTrue(e2.getPerson().getAge() == e.getPerson().getAge());
assertTrue(e2.getAddress().getHouseNumber() == (e.getAddress().getHouseNumber()));
assertEquals(e2.getPerson().getAge(), e.getPerson().getAge());
assertEquals(e2.getAddress().getHouseNumber(), (e.getAddress().getHouseNumber()));
}
}

View File

@ -4,6 +4,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
@ -13,17 +14,32 @@ import java.io.ObjectOutputStream;
import java.io.Serializable;
import org.apache.commons.lang3.SerializationUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import com.baeldung.util.MySerializationUtils;
import org.junit.rules.TemporaryFolder;
public class SerializationUnitTest {
private final static String OUTPUT_FILE_NAME = "yourfile.txt";
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
private File outputFile;
@Before
public void setUp() throws Exception {
outputFile = tempFolder.newFile(OUTPUT_FILE_NAME);
}
@Test(expected = NotSerializableException.class)
public void whenSerializing_ThenThrowsError() throws IOException {
Address address = new Address();
address.setHouseNumber(10);
FileOutputStream fileOutputStream = new FileOutputStream("yofile.txt");
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) {
objectOutputStream.writeObject(address);
}
@ -35,12 +51,12 @@ public class SerializationUnitTest {
p.setAge(20);
p.setName("Joe");
FileOutputStream fileOutputStream = new FileOutputStream("yofile.txt");
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) {
objectOutputStream.writeObject(p);
}
FileInputStream fileInputStream = new FileInputStream("yofile.txt");
FileInputStream fileInputStream = new FileInputStream(outputFile);
try (ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)) {
Person p2 = (Person) objectInputStream.readObject();
assertEquals(p2.getAge(), p.getAge());