[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:
parent
66b453758b
commit
567e910903
|
@ -0,0 +1,2 @@
|
|||
test-link*
|
||||
0.*
|
|
@ -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)
|
|
@ -0,0 +1,52 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-io-4</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-io-4</name>
|
||||
<packaging>jar</packaging>
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- utils -->
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
<!-- logging -->
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>log4j-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -67,6 +67,7 @@
|
|||
<module>core-java-io</module>
|
||||
<module>core-java-io-2</module>
|
||||
<module>core-java-io-3</module>
|
||||
<module>core-java-io-4</module>
|
||||
<module>core-java-io-apis</module>
|
||||
<module>core-java-io-conversions</module>
|
||||
<module>core-java-io-conversions-2</module>
|
||||
|
|
Loading…
Reference in New Issue