JAVA-21187 Cleanup un-committed or un-ignored artifacts - Week 21 - 2023 (moved-5) (#14312)

Co-authored-by: timis1 <noreplay@yahoo.com>
This commit is contained in:
timis1 2023-06-30 07:03:31 +03:00 committed by GitHub
parent 7047e858de
commit 8e800d1a2d
4 changed files with 67 additions and 14 deletions

1
.gitignore vendored
View File

@ -70,6 +70,7 @@ jmeter/src/main/resources/*-JMeter*.csv
jmeter/src/main/resources/*ReportsDashboard*.csv
jmeter/src/main/resources/dashboard/*ReportsDashboard*.csv
jmeter/src/main/resources/*FileExtractionExample.csv
jmeter/src/main/resources/*_Summary-Report.csv
ninja/devDb.mv.db

View File

@ -1,47 +1,70 @@
package com.baeldung.readresolvevsreadobject;
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 org.junit.After;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
public class SingletonUnitTest {
private static final String SINGLETON_SER = "singleton.ser";
@After
public void tearDown() {
final File file = new File(SINGLETON_SER);
if (file.exists()) {
file.deleteOnExit();
}
}
@Test
public void testSingletonObj_withNoReadResolve() throws ClassNotFoundException, IOException {
// Serialization
FileOutputStream fos = new FileOutputStream("singleton.ser");
FileOutputStream fos = new FileOutputStream(SINGLETON_SER);
ObjectOutputStream oos = new ObjectOutputStream(fos);
Singleton actualSingletonObject = Singleton.getInstance();
oos.writeObject(actualSingletonObject);
// Deserialization
Singleton deserializedSingletonObject = null;
FileInputStream fis = new FileInputStream("singleton.ser");
FileInputStream fis = new FileInputStream(SINGLETON_SER);
ObjectInputStream ois = new ObjectInputStream(fis);
deserializedSingletonObject = (Singleton) ois.readObject();
// remove readResolve() from Singleton class and uncomment this to test.
//assertNotEquals(actualSingletonObject.hashCode(), deserializedSingletonObject.hashCode());
fos.close();
oos.close();
fis.close();
ois.close();
}
@Test
public void testSingletonObj_withCustomReadResolve()
throws ClassNotFoundException, IOException {
// Serialization
FileOutputStream fos = new FileOutputStream("singleton.ser");
FileOutputStream fos = new FileOutputStream(SINGLETON_SER);
ObjectOutputStream oos = new ObjectOutputStream(fos);
Singleton actualSingletonObject = Singleton.getInstance();
oos.writeObject(actualSingletonObject);
// Deserialization
Singleton deserializedSingletonObject = null;
FileInputStream fis = new FileInputStream("singleton.ser");
Singleton deserializedSingletonObject;
FileInputStream fis = new FileInputStream(SINGLETON_SER);
ObjectInputStream ois = new ObjectInputStream(fis);
deserializedSingletonObject = (Singleton) ois.readObject();
assertEquals(actualSingletonObject.hashCode(), deserializedSingletonObject.hashCode());
fos.close();
oos.close();
fis.close();
ois.close();
}
}

View File

@ -1,52 +1,74 @@
package com.baeldung.readresolvevsreadobject;
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 org.junit.After;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
public class UserUnitTest {
private static final String USER_SER = "user.ser";
@After
public void tearDown() {
final File file = new File(USER_SER);
if (file.exists()) {
file.deleteOnExit();
}
}
@Test
public void testDeserializeObj_withOverriddenReadObject()
throws ClassNotFoundException, IOException {
public void testDeserializeObj_withOverriddenReadObject() throws ClassNotFoundException, IOException {
// Serialization
FileOutputStream fos = new FileOutputStream("user.ser");
FileOutputStream fos = new FileOutputStream(USER_SER);
ObjectOutputStream oos = new ObjectOutputStream(fos);
User acutalObject = new User("Sachin", "Kumar");
oos.writeObject(acutalObject);
// Deserialization
User deserializedUser = null;
FileInputStream fis = new FileInputStream("user.ser");
User deserializedUser;
FileInputStream fis = new FileInputStream(USER_SER);
ObjectInputStream ois = new ObjectInputStream(fis);
deserializedUser = (User) ois.readObject();
assertNotEquals(deserializedUser.hashCode(), acutalObject.hashCode());
assertEquals(deserializedUser.getUserName(), "Sachin");
assertEquals(deserializedUser.getPassword(), "Kumar");
fos.close();
oos.close();
fis.close();
ois.close();
}
@Test
public void testDeserializeObj_withDefaultReadObject()
throws ClassNotFoundException, IOException {
// Serialization
FileOutputStream fos = new FileOutputStream("user.ser");
FileOutputStream fos = new FileOutputStream(USER_SER);
ObjectOutputStream oos = new ObjectOutputStream(fos);
User acutalObject = new User("Sachin", "Kumar");
oos.writeObject(acutalObject);
// Deserialization
User deserializedUser = null;
FileInputStream fis = new FileInputStream("user.ser");
User deserializedUser;
FileInputStream fis = new FileInputStream(USER_SER);
ObjectInputStream ois = new ObjectInputStream(fis);
deserializedUser = (User) ois.readObject();
assertNotEquals(deserializedUser.hashCode(), acutalObject.hashCode());
assertEquals(deserializedUser.getUserName(), "Sachin");
// remove readObject() from User class and uncomment this to test.
//assertEquals(deserializedUser.getPassword(), "xyzKumar");
fos.close();
oos.close();
fis.close();
ois.close();
}
}

View File

@ -5,6 +5,7 @@ import java.io.File;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@ -33,12 +34,18 @@ public class FileProcessorIntegrationTest {
file2.createNewFile();
}
@After
public void tearDown() {
cleanFolder(new File(SOURCE_FOLDER));
cleanFolder(new File(DESTINATION_FOLDER));
}
private void cleanFolder(File folder) {
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
if (file.isFile()) {
file.delete();
file.deleteOnExit();
}
}
}