[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 <MeenaGawande@users.noreply.github.com>
This commit is contained in:
MeenaGawande
2021-01-06 23:36:07 +05:30
committed by GitHub
parent 0c7b0dfdf5
commit 8e7f37775c
6 changed files with 189 additions and 0 deletions

View File

@@ -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();
}
}

View File

@@ -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());
}
}