Skip to content

Commit 90c4edd

Browse files
committed
Use Path where applicable.
Part of procedure to porting to Java7. Addressed to #140
1 parent a394da9 commit 90c4edd

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

src/main/java/ru/mystamps/web/service/FilesystemImagePersistenceStrategy.java

+15-9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.File;
2121
import java.io.IOException;
2222
import java.nio.file.Files;
23+
import java.nio.file.Path;
2324

2425
import javax.annotation.PostConstruct;
2526

@@ -63,7 +64,7 @@ public void init() {
6364
@Override
6465
public void save(MultipartFile file, Image image) {
6566
try {
66-
File dest = createFile(image);
67+
Path dest = createFile(image);
6768
writeToFile(file, dest);
6869

6970
LOG.debug("Image's data was written into file {}", dest);
@@ -75,8 +76,8 @@ public void save(MultipartFile file, Image image) {
7576

7677
@Override
7778
public ImageDto get(Image image) {
78-
File dest = createFile(image);
79-
if (!dest.exists()) {
79+
Path dest = createFile(image);
80+
if (!exists(dest)) {
8081
LOG.warn("Found image without content: #{} ({} doesn't exist)", image.getId(), dest);
8182
return null;
8283
}
@@ -91,20 +92,25 @@ public ImageDto get(Image image) {
9192
}
9293

9394
// protected to allow spying
94-
protected File createFile(Image image) {
95-
return new File(storageDir, generateFileName(image));
95+
protected Path createFile(Image image) {
96+
return new File(storageDir, generateFileName(image)).toPath();
9697
}
9798

9899
// protected to allow spying
99-
protected void writeToFile(MultipartFile file, File dest) throws IOException {
100+
protected void writeToFile(MultipartFile file, Path dest) throws IOException {
100101
// we can't use file.transferTo(dest) there because it creates file
101102
// relatively to directory from multipart-config/location in web.xml
102-
Files.copy(file.getInputStream(), dest.toPath());
103+
Files.copy(file.getInputStream(), dest);
104+
}
105+
106+
// protected to allow spying
107+
protected boolean exists(Path path) {
108+
return Files.exists(path);
103109
}
104110

105111
// protected to allow spying
106-
protected byte[] toByteArray(File dest) throws IOException {
107-
return Files.readAllBytes(dest.toPath());
112+
protected byte[] toByteArray(Path dest) throws IOException {
113+
return Files.readAllBytes(dest);
108114
}
109115

110116
private static String generateFileName(Image image) {

src/test/groovy/ru/mystamps/web/service/FilesystemImagePersistenceStrategyTest.groovy

+14-12
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ import ru.mystamps.web.entity.Image
2525
import ru.mystamps.web.service.dto.ImageDto
2626
import ru.mystamps.web.service.exception.ImagePersistenceException
2727

28+
import java.nio.file.Path
29+
2830
class FilesystemImagePersistenceStrategyTest extends Specification {
2931
private static final STORAGE_DIR = "/tmp"
3032

3133
private MultipartFile multipartFile = Mock()
3234
private Image image = TestObjects.createImage()
33-
private File mockFile = Mock(File, constructorArgs: ["/fake/path"])
35+
private Path mockFile = Mock(Path)
3436

3537
private ImagePersistenceStrategy strategy = Spy(FilesystemImagePersistenceStrategy, constructorArgs: [STORAGE_DIR])
3638

@@ -42,7 +44,7 @@ class FilesystemImagePersistenceStrategyTest extends Specification {
4244
when:
4345
strategy.save(multipartFile, image)
4446
then:
45-
1 * strategy.writeToFile(_ as MultipartFile, _ as File) >> {}
47+
1 * strategy.writeToFile(_ as MultipartFile, _ as Path) >> {}
4648
}
4749

4850
def "save() should saves file onto the configured directory"() {
@@ -51,8 +53,8 @@ class FilesystemImagePersistenceStrategyTest extends Specification {
5153
when:
5254
strategy.save(multipartFile, image)
5355
then:
54-
1 * strategy.writeToFile(_ as MultipartFile, { File file ->
55-
assert file.parent == expectedDirectoryName
56+
1 * strategy.writeToFile(_ as MultipartFile, { Path path ->
57+
assert path.parent.toString() == expectedDirectoryName
5658
return true
5759
}) >> {}
5860
}
@@ -65,15 +67,15 @@ class FilesystemImagePersistenceStrategyTest extends Specification {
6567
when:
6668
strategy.save(multipartFile, image)
6769
then:
68-
1 * strategy.writeToFile(_ as MultipartFile, { File file ->
69-
assert file.name == expectedFileName
70+
1 * strategy.writeToFile(_ as MultipartFile, { Path path ->
71+
assert path.fileName.toString() == expectedFileName
7072
return true
7173
}) >> {}
7274
}
7375

7476
def "save() should converts IOException to ImagePersistenceException"() {
7577
given:
76-
strategy.writeToFile(_ as MultipartFile, _ as File) >> { throw new IOException() }
78+
strategy.writeToFile(_ as MultipartFile, _ as Path) >> { throw new IOException() }
7779
when:
7880
strategy.save(multipartFile, image)
7981
then:
@@ -88,7 +90,7 @@ class FilesystemImagePersistenceStrategyTest extends Specification {
8890

8991
def "get() should returns null when file doesn't exist"() {
9092
given:
91-
mockFile.exists() >> false
93+
strategy.exists(_ as Path) >> false
9294
and:
9395
strategy.createFile(_ as Image) >> mockFile
9496
when:
@@ -99,11 +101,11 @@ class FilesystemImagePersistenceStrategyTest extends Specification {
99101

100102
def "get() should converts IOException to ImagePersistenceException"() {
101103
given:
102-
mockFile.exists() >> true
104+
strategy.exists(_ as Path) >> true
103105
and:
104106
strategy.createFile(_ as Image) >> mockFile
105107
and:
106-
strategy.toByteArray(_ as File) >> { throw new IOException() }
108+
strategy.toByteArray(_ as Path) >> { throw new IOException() }
107109
when:
108110
strategy.get(image)
109111
then:
@@ -118,11 +120,11 @@ class FilesystemImagePersistenceStrategyTest extends Specification {
118120
and:
119121
byte[] expectedData = 'any data'.bytes
120122
and:
121-
mockFile.exists() >> true
123+
strategy.exists(_ as Path) >> true
122124
and:
123125
strategy.createFile(_ as Image) >> mockFile
124126
and:
125-
strategy.toByteArray(_ as File) >> expectedData
127+
strategy.toByteArray(_ as Path) >> expectedData
126128
when:
127129
ImageDto result = strategy.get(image)
128130
then:

0 commit comments

Comments
 (0)