diff --git a/core-java/src/test/java/org/baeldung/java/CoreJavaIoIntegrationTest.java b/core-java/src/test/java/org/baeldung/java/JavaIoIntegrationTest.java similarity index 98% rename from core-java/src/test/java/org/baeldung/java/CoreJavaIoIntegrationTest.java rename to core-java/src/test/java/org/baeldung/java/JavaIoIntegrationTest.java index 66cea6df43..ff92410bc4 100644 --- a/core-java/src/test/java/org/baeldung/java/CoreJavaIoIntegrationTest.java +++ b/core-java/src/test/java/org/baeldung/java/JavaIoIntegrationTest.java @@ -14,7 +14,7 @@ import org.slf4j.LoggerFactory; import com.google.common.base.Charsets; import com.google.common.io.Files; -public class CoreJavaIoIntegrationTest { +public class JavaIoIntegrationTest { protected final Logger logger = LoggerFactory.getLogger(getClass()); // tests - iterate lines in a file diff --git a/core-java/src/test/java/org/baeldung/java/CoreJavaRandomUnitTest.java b/core-java/src/test/java/org/baeldung/java/JavaRandomUnitTest.java similarity index 99% rename from core-java/src/test/java/org/baeldung/java/CoreJavaRandomUnitTest.java rename to core-java/src/test/java/org/baeldung/java/JavaRandomUnitTest.java index 2b688ecb10..4febe7c9fc 100644 --- a/core-java/src/test/java/org/baeldung/java/CoreJavaRandomUnitTest.java +++ b/core-java/src/test/java/org/baeldung/java/JavaRandomUnitTest.java @@ -7,7 +7,7 @@ import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.math3.random.RandomDataGenerator; import org.junit.Test; -public class CoreJavaRandomUnitTest { +public class JavaRandomUnitTest { // tests - random long diff --git a/core-java/src/test/java/org/baeldung/java/JavaTimerUnitTest.java b/core-java/src/test/java/org/baeldung/java/JavaTimerUnitTest.java new file mode 100644 index 0000000000..3bdedc81bd --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/JavaTimerUnitTest.java @@ -0,0 +1,30 @@ +package org.baeldung.java; + +import java.util.Date; +import java.util.Timer; +import java.util.TimerTask; + +import org.junit.Test; + +public class JavaTimerUnitTest { + + // tests + + @Test + public void givenUsingTimer_whenSchedulingTaskOnce_thenCorrect() throws InterruptedException { + final TimerTask timerTask = new TimerTask() { + @Override + public void run() { + System.out.println("Task performed on " + new Date() + "\n" + "Thread's name: " + Thread.currentThread().getName()); + } + }; + + final Timer timer = new Timer("Timer"); + final long delay = 1000L; + timer.schedule(timerTask, delay); + + Thread.sleep(delay * 2); + timer.cancel(); + } + +} diff --git a/core-java/src/test/java/org/baeldung/java/io/JavaXToReaderUnitTest.java b/core-java/src/test/java/org/baeldung/java/io/JavaXToReaderUnitTest.java index 42ecef4086..faca2f270a 100644 --- a/core-java/src/test/java/org/baeldung/java/io/JavaXToReaderUnitTest.java +++ b/core-java/src/test/java/org/baeldung/java/io/JavaXToReaderUnitTest.java @@ -30,6 +30,7 @@ public class JavaXToReaderUnitTest { public void givenUsingPlainJava_whenConvertingStringIntoReader_thenCorrect() throws IOException { final String initialString = "With Plain Java"; final Reader targetReader = new StringReader(initialString); + targetReader.close(); } @@ -37,6 +38,7 @@ public class JavaXToReaderUnitTest { public void givenUsingGuava_whenConvertingStringIntoReader_thenCorrect() throws IOException { final String initialString = "With Google Guava"; final Reader targetReader = CharSource.wrap(initialString).openStream(); + targetReader.close(); } @@ -44,6 +46,7 @@ public class JavaXToReaderUnitTest { public void givenUsingCommonsIO_whenConvertingStringIntoReader_thenCorrect() throws IOException { final String initialString = "With Apache Commons IO"; final Reader targetReader = new CharSequenceReader(initialString); + targetReader.close(); } @@ -53,6 +56,7 @@ public class JavaXToReaderUnitTest { public void givenUsingPlainJava_whenConvertingByteArrayIntoReader_thenCorrect() throws IOException { final byte[] initialArray = "Hello world!".getBytes(); final Reader targetReader = new StringReader(new String(initialArray)); + targetReader.close(); } @@ -69,6 +73,7 @@ public class JavaXToReaderUnitTest { public void givenUsingCommonsIO_whenConvertingByteArrayIntoReader_thenCorrect() throws IOException { final byte[] initialArray = "With Commons IO".getBytes(); final Reader targetReader = new CharSequenceReader(new String(initialArray)); + targetReader.close(); } @@ -79,6 +84,7 @@ public class JavaXToReaderUnitTest { final File initialFile = new File("src/test/resources/initialFile.txt"); initialFile.createNewFile(); final Reader targetReader = new FileReader(initialFile); + targetReader.close(); } @@ -87,6 +93,7 @@ public class JavaXToReaderUnitTest { final File initialFile = new File("src/test/resources/initialFile.txt"); com.google.common.io.Files.touch(initialFile); final Reader targetReader = com.google.common.io.Files.newReader(initialFile, Charset.defaultCharset()); + targetReader.close(); } @@ -97,6 +104,7 @@ public class JavaXToReaderUnitTest { FileUtils.write(initialFile, "With Commons IO"); final byte[] buffer = FileUtils.readFileToByteArray(initialFile); final Reader targetReader = new CharSequenceReader(new String(buffer)); + targetReader.close(); } @@ -107,7 +115,6 @@ public class JavaXToReaderUnitTest { final InputStream initialStream = new ByteArrayInputStream("With Java".getBytes()); final Reader targetReader = new InputStreamReader(initialStream); - initialStream.close(); targetReader.close(); } @@ -117,7 +124,6 @@ public class JavaXToReaderUnitTest { final byte[] buffer = ByteStreams.toByteArray(initialStream); final Reader targetReader = CharSource.wrap(new String(buffer)).openStream(); - initialStream.close(); targetReader.close(); } @@ -127,7 +133,6 @@ public class JavaXToReaderUnitTest { final byte[] buffer = IOUtils.toByteArray(initialStream); final Reader targetReader = new CharSequenceReader(new String(buffer)); - initialStream.close(); targetReader.close(); }