diff --git a/.gitignore b/.gitignore index b731787052..d22c792f43 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/core-java-modules/core-java-serialization/src/test/java/com/baeldung/readresolvevsreadobject/SingletonUnitTest.java b/core-java-modules/core-java-serialization/src/test/java/com/baeldung/readresolvevsreadobject/SingletonUnitTest.java index d5133ae976..3d233327e3 100644 --- a/core-java-modules/core-java-serialization/src/test/java/com/baeldung/readresolvevsreadobject/SingletonUnitTest.java +++ b/core-java-modules/core-java-serialization/src/test/java/com/baeldung/readresolvevsreadobject/SingletonUnitTest.java @@ -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(); } } diff --git a/core-java-modules/core-java-serialization/src/test/java/com/baeldung/readresolvevsreadobject/UserUnitTest.java b/core-java-modules/core-java-serialization/src/test/java/com/baeldung/readresolvevsreadobject/UserUnitTest.java index ffd56d67e9..cd07fd2bc7 100644 --- a/core-java-modules/core-java-serialization/src/test/java/com/baeldung/readresolvevsreadobject/UserUnitTest.java +++ b/core-java-modules/core-java-serialization/src/test/java/com/baeldung/readresolvevsreadobject/UserUnitTest.java @@ -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(); } } diff --git a/messaging-modules/apache-camel/src/test/java/com/apache/camel/file/processor/FileProcessorIntegrationTest.java b/messaging-modules/apache-camel/src/test/java/com/apache/camel/file/processor/FileProcessorIntegrationTest.java index bc5de17537..e4390a95e5 100644 --- a/messaging-modules/apache-camel/src/test/java/com/apache/camel/file/processor/FileProcessorIntegrationTest.java +++ b/messaging-modules/apache-camel/src/test/java/com/apache/camel/file/processor/FileProcessorIntegrationTest.java @@ -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(); } } }