From 8e7f37775c2b64223980be81c9fe83c0ec7302be Mon Sep 17 00:00:00 2001 From: MeenaGawande <45625809+MeenaGawande@users.noreply.github.com> Date: Wed, 6 Jan 2021 23:36:07 +0530 Subject: [PATCH] [BAEL-4720] Java File.separator vs File.pathSeparator (#10330) * [BAEL-4720] Java File.separator vs File.pathSeparator Removed src code and modified junit tests * [BAEL-4720] Java File.separator vs File.pathSeparator Code formatting: Removed extra spaces in the code * [BAEL-4720] Java File.separator vs File.pathSeparator Added more junit tests * [BAEL-4720] Java File.separator vs File.pathSeparator Added new module core-java-io4 and moved the code from core-java-io3 module. Co-authored-by: MeenaGawande --- core-java-modules/core-java-io-4/.gitignore | 2 + core-java-modules/core-java-io-4/README.md | 8 +++ core-java-modules/core-java-io-4/pom.xml | 52 +++++++++++++++ .../FilePathSeparatorUnitTest.java | 63 +++++++++++++++++++ .../fileseparator/FileSeparatorUnitTest.java | 63 +++++++++++++++++++ core-java-modules/pom.xml | 1 + 6 files changed, 189 insertions(+) create mode 100644 core-java-modules/core-java-io-4/.gitignore create mode 100644 core-java-modules/core-java-io-4/README.md create mode 100644 core-java-modules/core-java-io-4/pom.xml create mode 100644 core-java-modules/core-java-io-4/src/test/java/com/baeldung/fileseparator/FilePathSeparatorUnitTest.java create mode 100644 core-java-modules/core-java-io-4/src/test/java/com/baeldung/fileseparator/FileSeparatorUnitTest.java diff --git a/core-java-modules/core-java-io-4/.gitignore b/core-java-modules/core-java-io-4/.gitignore new file mode 100644 index 0000000000..0c0cd871c5 --- /dev/null +++ b/core-java-modules/core-java-io-4/.gitignore @@ -0,0 +1,2 @@ +test-link* +0.* \ No newline at end of file diff --git a/core-java-modules/core-java-io-4/README.md b/core-java-modules/core-java-io-4/README.md new file mode 100644 index 0000000000..7cc38bb9fe --- /dev/null +++ b/core-java-modules/core-java-io-4/README.md @@ -0,0 +1,8 @@ +## Core Java IO + +This module contains articles about core Java input and output (IO) + +### Relevant Articles: + +- [Java File Separator vs File Path Separator] +- [[<-- Prev]](/core-java-modules/core-java-io-3) diff --git a/core-java-modules/core-java-io-4/pom.xml b/core-java-modules/core-java-io-4/pom.xml new file mode 100644 index 0000000000..ee31b35ba9 --- /dev/null +++ b/core-java-modules/core-java-io-4/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + core-java-io-4 + 0.1.0-SNAPSHOT + core-java-io-4 + jar + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + ../ + + + + + + commons-io + commons-io + ${commons-io.version} + + + + log4j + log4j + ${log4j.version} + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + + + + org.assertj + assertj-core + ${assertj.version} + test + + + + + + + + 3.6.1 + + + \ No newline at end of file diff --git a/core-java-modules/core-java-io-4/src/test/java/com/baeldung/fileseparator/FilePathSeparatorUnitTest.java b/core-java-modules/core-java-io-4/src/test/java/com/baeldung/fileseparator/FilePathSeparatorUnitTest.java new file mode 100644 index 0000000000..959aae8aff --- /dev/null +++ b/core-java-modules/core-java-io-4/src/test/java/com/baeldung/fileseparator/FilePathSeparatorUnitTest.java @@ -0,0 +1,63 @@ +package com.baeldung.fileseparator; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.File; +import java.io.IOException; +import java.util.StringJoiner; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledOnOs; +import org.junit.jupiter.api.condition.OS; + +public class FilePathSeparatorUnitTest { + + @Test + @EnabledOnOs(OS.WINDOWS) + public void whenCheckPathSeparator_thenResultIsAsExpectedOnWindows() throws IOException { + assertEquals(";", File.pathSeparator); + assertEquals(';', File.pathSeparatorChar); + } + + @Test + @EnabledOnOs({ OS.LINUX, OS.MAC }) + public void whenCheckPathSeparator_thenResultIsAsExpected() throws IOException { + assertEquals(":", File.pathSeparator); + assertEquals(':', File.pathSeparatorChar); + } + + @Test + @EnabledOnOs(OS.WINDOWS) + public void whenBuildPathUsingString_thenResultIsAsExpectedOnWindows() throws IOException { + String[] pathNames = { "path1", "path2", "path3" }; + String path = String.join(File.pathSeparator, pathNames); + assertEquals("path1;path2;path3",path); + } + + @Test + @EnabledOnOs({ OS.LINUX, OS.MAC }) + public void whenBuildPathUsingString_thenResultIsAsExpected() throws IOException { + String[] pathNames = { "path1", "path2", "path3" }; + String path = String.join(File.pathSeparator, pathNames); + assertEquals("path1:path2:path3", path); + } + + @Test + @EnabledOnOs(OS.WINDOWS) + public void whenbuildPathUsingStringJoiner_thenResultIsAsExpectedOnWindows() throws IOException { + assertEquals("path1;path2", buildPathUsingStringJoiner("path1", "path2")); + } + + @Test + @EnabledOnOs({ OS.LINUX, OS.MAC }) + public void whenbuildPathUsingStringJoiner_thenResultIsAsExpected() throws IOException { + assertEquals("path1:path2", buildPathUsingStringJoiner("path1", "path2")); + } + + private String buildPathUsingStringJoiner(String path1, String path2) { + StringJoiner joiner = new StringJoiner(File.pathSeparator); + joiner.add(path1); + joiner.add(path2); + return joiner.toString(); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-io-4/src/test/java/com/baeldung/fileseparator/FileSeparatorUnitTest.java b/core-java-modules/core-java-io-4/src/test/java/com/baeldung/fileseparator/FileSeparatorUnitTest.java new file mode 100644 index 0000000000..f908dcc9bb --- /dev/null +++ b/core-java-modules/core-java-io-4/src/test/java/com/baeldung/fileseparator/FileSeparatorUnitTest.java @@ -0,0 +1,63 @@ +package com.baeldung.fileseparator; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.File; +import java.nio.file.FileSystems; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledOnOs; +import org.junit.jupiter.api.condition.OS; + +public class FileSeparatorUnitTest { + + @Test + @EnabledOnOs(OS.WINDOWS) + public void whenCheckFileSeparator_thenCorrectOnWindows() { + assertEquals("\\", File.separator); + assertEquals('\\', File.separatorChar); + + String fileSeparator = FileSystems.getDefault().getSeparator(); + assertEquals("\\",fileSeparator); + } + + @Test + @EnabledOnOs({ OS.LINUX, OS.MAC }) + public void whenCheckFileSeparator_thenCorrect() { + assertEquals("/", File.separator); + assertEquals('/', File.separatorChar); + + String fileSeparator = FileSystems.getDefault().getSeparator(); + assertEquals("/",fileSeparator); + } + + @Test + @EnabledOnOs(OS.WINDOWS) + public void whenBuildFilePathUsingPathsClass_thenCorrectOnWindows() { + Path path = Paths.get("dir1", "dir2"); + assertEquals("dir1\\dir2", path.toString()); + } + + @Test + @EnabledOnOs({ OS.LINUX, OS.MAC }) + public void whenBuildFilePathUsingPathsClass_thenCorrect() { + Path path = Paths.get("dir1", "dir2"); + assertEquals("dir1/dir2", path.toString()); + } + + @Test + @EnabledOnOs(OS.WINDOWS) + public void whenBuildFilePathUsingFileClass_thenOutputIsAsExpectedOnWindows() { + File file = new File("file1", "file2"); + assertEquals("file1\\file2", file.toString()); + } + + @Test + @EnabledOnOs({ OS.LINUX, OS.MAC }) + public void whenBuildFilePathUsingFileClass_thenOutputIsAsExpected() { + File file = new File("file1", "file2"); + assertEquals("file1/file2", file.toString()); + } +} \ No newline at end of file diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 0a9e818156..711d1e03b9 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -67,6 +67,7 @@ core-java-io core-java-io-2 core-java-io-3 + core-java-io-4 core-java-io-apis core-java-io-conversions core-java-io-conversions-2