Skip to content

Commit b871797

Browse files
authored
Merge pull request eugenp#7972 from lukaszrys/feature/BAEL-3322_jimf
Feature/bael 3322 jimfs
2 parents 22deaaa + bb01783 commit b871797

File tree

9 files changed

+313
-55
lines changed

9 files changed

+313
-55
lines changed

testing-modules/mocks/pom.xml

Lines changed: 62 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,62 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3-
<modelVersion>4.0.0</modelVersion>
4-
<artifactId>mocks</artifactId>
5-
<name>mocks</name>
6-
7-
<parent>
8-
<groupId>com.baeldung</groupId>
9-
<artifactId>parent-modules</artifactId>
10-
<version>1.0.0-SNAPSHOT</version>
11-
<relativePath>../../</relativePath>
12-
</parent>
13-
14-
<dependencies>
15-
<dependency>
16-
<groupId>com.github.javafaker</groupId>
17-
<artifactId>javafaker</artifactId>
18-
<version>${javafaker.version}</version>
19-
</dependency>
20-
<dependency>
21-
<groupId>org.jmockit</groupId>
22-
<artifactId>jmockit</artifactId>
23-
<version>${jmockit.version}</version>
24-
<scope>test</scope>
25-
</dependency>
26-
<dependency>
27-
<groupId>org.jukito</groupId>
28-
<artifactId>jukito</artifactId>
29-
<version>${jukito.version}</version>
30-
<scope>test</scope>
31-
</dependency>
32-
<dependency>
33-
<groupId>org.mockito</groupId>
34-
<artifactId>mockito-core</artifactId>
35-
<version>${mockito.version}</version>
36-
<scope>test</scope>
37-
</dependency>
38-
39-
<dependency>
40-
<groupId>org.easymock</groupId>
41-
<artifactId>easymock</artifactId>
42-
<version>${easymock.version}</version>
43-
<scope>test</scope>
44-
</dependency>
45-
</dependencies>
46-
47-
<properties>
48-
<javafaker.version>0.15</javafaker.version>
49-
<jukito.version>1.5</jukito.version>
50-
<mockito.version>2.21.0</mockito.version>
51-
<easymock.version>3.5.1</easymock.version>
52-
<jmockit.version>1.41</jmockit.version>
53-
</properties>
54-
55-
</project>
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<artifactId>mocks</artifactId>
5+
<name>mocks</name>
6+
7+
<parent>
8+
<groupId>com.baeldung</groupId>
9+
<artifactId>parent-modules</artifactId>
10+
<version>1.0.0-SNAPSHOT</version>
11+
<relativePath>../../</relativePath>
12+
</parent>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.github.javafaker</groupId>
17+
<artifactId>javafaker</artifactId>
18+
<version>${javafaker.version}</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.jmockit</groupId>
22+
<artifactId>jmockit</artifactId>
23+
<version>${jmockit.version}</version>
24+
<scope>test</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.jukito</groupId>
28+
<artifactId>jukito</artifactId>
29+
<version>${jukito.version}</version>
30+
<scope>test</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.mockito</groupId>
34+
<artifactId>mockito-core</artifactId>
35+
<version>${mockito.version}</version>
36+
<scope>test</scope>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.easymock</groupId>
41+
<artifactId>easymock</artifactId>
42+
<version>${easymock.version}</version>
43+
<scope>test</scope>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>com.google.jimfs</groupId>
48+
<artifactId>jimfs</artifactId>
49+
<version>${jimf.version}</version>
50+
</dependency>
51+
</dependencies>
52+
53+
<properties>
54+
<javafaker.version>0.15</javafaker.version>
55+
<jukito.version>1.5</jukito.version>
56+
<mockito.version>2.21.0</mockito.version>
57+
<easymock.version>3.5.1</easymock.version>
58+
<jmockit.version>1.41</jmockit.version>
59+
<jimf.version>1.1</jimf.version>
60+
</properties>
61+
62+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.jimfs;
2+
3+
import java.io.IOException;
4+
import java.io.UncheckedIOException;
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
7+
import java.nio.file.StandardCopyOption;
8+
9+
public class FileManipulation {
10+
11+
void move(final Path origin, final Path destination) {
12+
try {
13+
Files.createDirectories(destination);
14+
Files.move(origin, destination, StandardCopyOption.REPLACE_EXISTING);
15+
} catch (final IOException ex) {
16+
throw new UncheckedIOException(ex);
17+
}
18+
}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.jimfs;
2+
3+
import java.io.IOException;
4+
import java.io.UncheckedIOException;
5+
import java.nio.file.Path;
6+
7+
class FilePathReader {
8+
9+
String getSystemPath(final Path path) {
10+
try {
11+
return path
12+
.toRealPath()
13+
.toString();
14+
} catch (final IOException ex) {
15+
throw new UncheckedIOException(ex);
16+
}
17+
}
18+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.jimfs;
2+
3+
import java.io.IOException;
4+
import java.io.UncheckedIOException;
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
7+
8+
public class FileRepository {
9+
10+
void create(final Path path, final String fileName) {
11+
final Path filePath = path.resolve(fileName);
12+
try {
13+
Files.createFile(filePath);
14+
} catch (final IOException ex) {
15+
throw new UncheckedIOException(ex);
16+
}
17+
}
18+
19+
String read(final Path path) {
20+
try {
21+
return new String(Files.readAllBytes(path));
22+
} catch (final IOException ex) {
23+
throw new UncheckedIOException(ex);
24+
}
25+
}
26+
27+
String update(final Path path, final String newContent) {
28+
try {
29+
Files.write(path, newContent.getBytes());
30+
return newContent;
31+
} catch (final IOException ex) {
32+
throw new UncheckedIOException(ex);
33+
}
34+
}
35+
36+
void delete(final Path path) {
37+
try {
38+
Files.deleteIfExists(path);
39+
} catch (final IOException ex) {
40+
throw new UncheckedIOException(ex);
41+
}
42+
}
43+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.jimfs;
2+
3+
import com.google.common.jimfs.Configuration;
4+
import com.google.common.jimfs.Jimfs;
5+
import org.junit.jupiter.api.DisplayName;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
import java.nio.file.FileSystem;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.util.stream.Stream;
14+
15+
import static org.junit.jupiter.api.Assertions.assertFalse;
16+
import static org.junit.jupiter.api.Assertions.assertTrue;
17+
18+
class FileManipulationUnitTest implements FileTestProvider {
19+
20+
private final FileManipulation fileManipulation = new FileManipulation();
21+
22+
private static Stream<Arguments> provideFileSystem() {
23+
return Stream.of(Arguments.of(Jimfs.newFileSystem(Configuration.unix())), Arguments.of(Jimfs.newFileSystem(Configuration.windows())), Arguments.of(Jimfs.newFileSystem(Configuration.osX())));
24+
}
25+
26+
@ParameterizedTest
27+
@DisplayName("Should move file to new destination")
28+
@MethodSource("provideFileSystem")
29+
void givenEachSystem_whenMovingFile_thenMovedToNewPath(final FileSystem fileSystem) throws Exception {
30+
final Path origin = fileSystem.getPath(RESOURCE_FILE_NAME);
31+
Files.copy(getResourceFilePath(), origin);
32+
final Path destination = fileSystem.getPath("newDirectory", RESOURCE_FILE_NAME);
33+
34+
fileManipulation.move(origin, destination);
35+
36+
assertFalse(Files.exists(origin));
37+
assertTrue(Files.exists(destination));
38+
}
39+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.jimfs;
2+
3+
import com.google.common.jimfs.Configuration;
4+
import com.google.common.jimfs.Jimfs;
5+
import org.junit.jupiter.api.DisplayName;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.nio.file.FileSystem;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
14+
class FilePathReaderUnitTest {
15+
16+
private static final String DIRECTORY_NAME = "baeldung";
17+
18+
private final FilePathReader filePathReader = new FilePathReader();
19+
20+
@Test
21+
@DisplayName("Should get path on windows")
22+
void givenWindowsSystem_shouldGetPath_thenReturnWindowsPath() throws Exception {
23+
final FileSystem fileSystem = Jimfs.newFileSystem(Configuration.windows());
24+
final Path path = getPathToFile(fileSystem);
25+
26+
final String stringPath = filePathReader.getSystemPath(path);
27+
28+
assertEquals("C:\\work\\" + DIRECTORY_NAME, stringPath);
29+
}
30+
31+
@Test
32+
@DisplayName("Should get path on unix")
33+
void givenUnixSystem_shouldGetPath_thenReturnUnixPath() throws Exception {
34+
final FileSystem fileSystem = Jimfs.newFileSystem(Configuration.unix());
35+
final Path path = getPathToFile(fileSystem);
36+
37+
final String stringPath = filePathReader.getSystemPath(path);
38+
39+
assertEquals("/work/" + DIRECTORY_NAME, stringPath);
40+
}
41+
42+
private Path getPathToFile(final FileSystem fileSystem) throws Exception {
43+
final Path path = fileSystem.getPath(DIRECTORY_NAME);
44+
Files.createDirectory(path);
45+
46+
return path;
47+
}
48+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.baeldung.jimfs;
2+
3+
import com.google.common.jimfs.Configuration;
4+
import com.google.common.jimfs.Jimfs;
5+
import org.junit.jupiter.api.DisplayName;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.nio.file.FileSystem;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
12+
import static org.junit.jupiter.api.Assertions.*;
13+
14+
class FileRepositoryUnitTest implements FileTestProvider {
15+
16+
private final FileRepository fileRepository = new FileRepository();
17+
18+
@Test
19+
@DisplayName("Should create a file on a file system")
20+
void givenUnixSystem_whenCreatingFile_thenCreatedInPath() {
21+
final FileSystem fileSystem = Jimfs.newFileSystem(Configuration.unix());
22+
final String fileName = "newFile.txt";
23+
final Path pathToStore = fileSystem.getPath("");
24+
25+
fileRepository.create(pathToStore, fileName);
26+
27+
assertTrue(Files.exists(pathToStore.resolve(fileName)));
28+
}
29+
30+
@Test
31+
@DisplayName("Should read the content of the file")
32+
void givenOSXSystem_whenReadingFile_thenContentIsReturned() throws Exception {
33+
final FileSystem fileSystem = Jimfs.newFileSystem(Configuration.osX());
34+
final Path resourceFilePath = fileSystem.getPath(RESOURCE_FILE_NAME);
35+
Files.copy(getResourceFilePath(), resourceFilePath);
36+
37+
final String content = fileRepository.read(resourceFilePath);
38+
39+
assertEquals(FILE_CONTENT, content);
40+
}
41+
42+
@Test
43+
@DisplayName("Should update the content of the file")
44+
void givenWindowsSystem_whenUpdatingFile_thenContentHasChanged() throws Exception {
45+
final FileSystem fileSystem = Jimfs.newFileSystem(Configuration.windows());
46+
final Path resourceFilePath = fileSystem.getPath(RESOURCE_FILE_NAME);
47+
Files.copy(getResourceFilePath(), resourceFilePath);
48+
final String newContent = "I'm updating you.";
49+
50+
final String content = fileRepository.update(resourceFilePath, newContent);
51+
52+
assertEquals(newContent, content);
53+
assertEquals(newContent, fileRepository.read(resourceFilePath));
54+
}
55+
56+
@Test
57+
@DisplayName("Should delete file")
58+
void givenCurrentSystem_whenDeletingFile_thenFileHasBeenDeleted() throws Exception {
59+
final FileSystem fileSystem = Jimfs.newFileSystem();
60+
final Path resourceFilePath = fileSystem.getPath(RESOURCE_FILE_NAME);
61+
Files.copy(getResourceFilePath(), resourceFilePath);
62+
63+
fileRepository.delete(resourceFilePath);
64+
65+
assertFalse(Files.exists(resourceFilePath));
66+
}
67+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.jimfs;
2+
3+
import java.nio.file.Path;
4+
import java.nio.file.Paths;
5+
6+
public interface FileTestProvider {
7+
String FILE_CONTENT = "I'm the file content.";
8+
String RESOURCE_FILE_NAME = "fileRepositoryRead.txt";
9+
10+
default Path getResourceFilePath() {
11+
final String resourceFilePath = getClass()
12+
.getResource("/" + RESOURCE_FILE_NAME)
13+
.getPath();
14+
return Paths.get(resourceFilePath);
15+
}
16+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I'm the file content.

0 commit comments

Comments
 (0)