Skip to content

Commit 8e7f377

Browse files
authored
[BAEL-4720] Java File.separator vs File.pathSeparator (eugenp#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 <[email protected]>
1 parent 0c7b0df commit 8e7f377

6 files changed

Lines changed: 189 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test-link*
2+
0.*
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Core Java IO
2+
3+
This module contains articles about core Java input and output (IO)
4+
5+
### Relevant Articles:
6+
7+
- [Java File Separator vs File Path Separator]
8+
- [[<-- Prev]](/core-java-modules/core-java-io-3)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
<artifactId>core-java-io-4</artifactId>
8+
<version>0.1.0-SNAPSHOT</version>
9+
<name>core-java-io-4</name>
10+
<packaging>jar</packaging>
11+
<parent>
12+
<groupId>com.baeldung.core-java-modules</groupId>
13+
<artifactId>core-java-modules</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
<relativePath>../</relativePath>
16+
</parent>
17+
18+
<dependencies>
19+
<!-- utils -->
20+
<dependency>
21+
<groupId>commons-io</groupId>
22+
<artifactId>commons-io</artifactId>
23+
<version>${commons-io.version}</version>
24+
</dependency>
25+
<!-- logging -->
26+
<dependency>
27+
<groupId>log4j</groupId>
28+
<artifactId>log4j</artifactId>
29+
<version>${log4j.version}</version>
30+
</dependency>
31+
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
32+
<groupId>org.slf4j</groupId>
33+
<artifactId>log4j-over-slf4j</artifactId>
34+
<version>${org.slf4j.version}</version>
35+
</dependency>
36+
<!-- test scoped -->
37+
<dependency>
38+
<groupId>org.assertj</groupId>
39+
<artifactId>assertj-core</artifactId>
40+
<version>${assertj.version}</version>
41+
<scope>test</scope>
42+
</dependency>
43+
</dependencies>
44+
45+
<build>
46+
</build>
47+
48+
<properties>
49+
<assertj.version>3.6.1</assertj.version>
50+
</properties>
51+
52+
</project>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.fileseparator;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.util.StringJoiner;
8+
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.condition.EnabledOnOs;
11+
import org.junit.jupiter.api.condition.OS;
12+
13+
public class FilePathSeparatorUnitTest {
14+
15+
@Test
16+
@EnabledOnOs(OS.WINDOWS)
17+
public void whenCheckPathSeparator_thenResultIsAsExpectedOnWindows() throws IOException {
18+
assertEquals(";", File.pathSeparator);
19+
assertEquals(';', File.pathSeparatorChar);
20+
}
21+
22+
@Test
23+
@EnabledOnOs({ OS.LINUX, OS.MAC })
24+
public void whenCheckPathSeparator_thenResultIsAsExpected() throws IOException {
25+
assertEquals(":", File.pathSeparator);
26+
assertEquals(':', File.pathSeparatorChar);
27+
}
28+
29+
@Test
30+
@EnabledOnOs(OS.WINDOWS)
31+
public void whenBuildPathUsingString_thenResultIsAsExpectedOnWindows() throws IOException {
32+
String[] pathNames = { "path1", "path2", "path3" };
33+
String path = String.join(File.pathSeparator, pathNames);
34+
assertEquals("path1;path2;path3",path);
35+
}
36+
37+
@Test
38+
@EnabledOnOs({ OS.LINUX, OS.MAC })
39+
public void whenBuildPathUsingString_thenResultIsAsExpected() throws IOException {
40+
String[] pathNames = { "path1", "path2", "path3" };
41+
String path = String.join(File.pathSeparator, pathNames);
42+
assertEquals("path1:path2:path3", path);
43+
}
44+
45+
@Test
46+
@EnabledOnOs(OS.WINDOWS)
47+
public void whenbuildPathUsingStringJoiner_thenResultIsAsExpectedOnWindows() throws IOException {
48+
assertEquals("path1;path2", buildPathUsingStringJoiner("path1", "path2"));
49+
}
50+
51+
@Test
52+
@EnabledOnOs({ OS.LINUX, OS.MAC })
53+
public void whenbuildPathUsingStringJoiner_thenResultIsAsExpected() throws IOException {
54+
assertEquals("path1:path2", buildPathUsingStringJoiner("path1", "path2"));
55+
}
56+
57+
private String buildPathUsingStringJoiner(String path1, String path2) {
58+
StringJoiner joiner = new StringJoiner(File.pathSeparator);
59+
joiner.add(path1);
60+
joiner.add(path2);
61+
return joiner.toString();
62+
}
63+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.fileseparator;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.File;
6+
import java.nio.file.FileSystems;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.api.condition.EnabledOnOs;
12+
import org.junit.jupiter.api.condition.OS;
13+
14+
public class FileSeparatorUnitTest {
15+
16+
@Test
17+
@EnabledOnOs(OS.WINDOWS)
18+
public void whenCheckFileSeparator_thenCorrectOnWindows() {
19+
assertEquals("\\", File.separator);
20+
assertEquals('\\', File.separatorChar);
21+
22+
String fileSeparator = FileSystems.getDefault().getSeparator();
23+
assertEquals("\\",fileSeparator);
24+
}
25+
26+
@Test
27+
@EnabledOnOs({ OS.LINUX, OS.MAC })
28+
public void whenCheckFileSeparator_thenCorrect() {
29+
assertEquals("/", File.separator);
30+
assertEquals('/', File.separatorChar);
31+
32+
String fileSeparator = FileSystems.getDefault().getSeparator();
33+
assertEquals("/",fileSeparator);
34+
}
35+
36+
@Test
37+
@EnabledOnOs(OS.WINDOWS)
38+
public void whenBuildFilePathUsingPathsClass_thenCorrectOnWindows() {
39+
Path path = Paths.get("dir1", "dir2");
40+
assertEquals("dir1\\dir2", path.toString());
41+
}
42+
43+
@Test
44+
@EnabledOnOs({ OS.LINUX, OS.MAC })
45+
public void whenBuildFilePathUsingPathsClass_thenCorrect() {
46+
Path path = Paths.get("dir1", "dir2");
47+
assertEquals("dir1/dir2", path.toString());
48+
}
49+
50+
@Test
51+
@EnabledOnOs(OS.WINDOWS)
52+
public void whenBuildFilePathUsingFileClass_thenOutputIsAsExpectedOnWindows() {
53+
File file = new File("file1", "file2");
54+
assertEquals("file1\\file2", file.toString());
55+
}
56+
57+
@Test
58+
@EnabledOnOs({ OS.LINUX, OS.MAC })
59+
public void whenBuildFilePathUsingFileClass_thenOutputIsAsExpected() {
60+
File file = new File("file1", "file2");
61+
assertEquals("file1/file2", file.toString());
62+
}
63+
}

core-java-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<module>core-java-io</module>
6868
<module>core-java-io-2</module>
6969
<module>core-java-io-3</module>
70+
<module>core-java-io-4</module>
7071
<module>core-java-io-apis</module>
7172
<module>core-java-io-conversions</module>
7273
<module>core-java-io-conversions-2</module>

0 commit comments

Comments
 (0)