tests + jpeg
This commit is contained in:
1 parent
1b978f9b93
commit
f78f37c166
11 files changed
+45
-4
No files matched your search
@@ -16,20 +16,23 @@ import org.springframework.stereotype.Repository;
|
|||||||
@Repository
|
@Repository
|
||||||
public class ImageDao implements Dao<Image> {
|
public class ImageDao implements Dao<Image> {
|
||||||
|
|
||||||
|
private final static String[] ACCEPTED_EXTENSION = { "jpg", "png", "jpeg" };
|
||||||
|
|
||||||
private final Map<Long, Image> images = new HashMap<>();
|
private final Map<Long, Image> images = new HashMap<>();
|
||||||
|
|
||||||
public ImageDao() throws IOException {
|
public ImageDao() throws IOException {
|
||||||
this(new ClassPathResource("images"));
|
this("images");
|
||||||
}
|
}
|
||||||
|
|
||||||
public ImageDao(ClassPathResource folderPath) throws IOException {
|
public ImageDao(final String folderPathStr) throws IOException {
|
||||||
|
final var folderPath = new ClassPathResource(folderPathStr);
|
||||||
if (!folderPath.exists())
|
if (!folderPath.exists())
|
||||||
throw new ImageResourceMissingException("folder images doesn't not exist");
|
throw new ImageResourceMissingException("folder images doesn't not exist");
|
||||||
final var folder = folderPath.getFile();
|
final var folder = folderPath.getFile();
|
||||||
getAllImages(folder);
|
getAllImages(folder);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void getAllImages(final File folder) {
|
private void getAllImages(final File folder) {
|
||||||
for (final File file : folder.listFiles()) {
|
for (final File file : folder.listFiles()) {
|
||||||
if (file.isDirectory()) {
|
if (file.isDirectory()) {
|
||||||
getAllImages(file);
|
getAllImages(file);
|
||||||
@@ -40,7 +43,14 @@ public class ImageDao implements Dao<Image> {
|
|||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
extension = file.getName().substring(i + 1);
|
extension = file.getName().substring(i + 1);
|
||||||
}
|
}
|
||||||
if (!extension.equals("jpg") && !extension.equals("png")) {
|
boolean accepted = false;
|
||||||
|
for (final String ext : ACCEPTED_EXTENSION) {
|
||||||
|
if (ext.equals(extension)) {
|
||||||
|
accepted = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!accepted) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
byte[] fileContent;
|
byte[] fileContent;
|
||||||
|
|||||||
File renamed without changes.
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 203 KiB |
@@ -0,0 +1 @@
|
|||||||
|
LION
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
GUITARE
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
# BOO
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 38 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 314 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 13 KiB |
@@ -0,0 +1,28 @@
|
|||||||
|
package pdl.backend;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class ImageDaoTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("ImageDao should throw an exception when the image folder is missing")
|
||||||
|
public void exceptionImageFolderNotFound() throws Exception {
|
||||||
|
assertThrows(IOException.class, () -> {
|
||||||
|
new ImageDao("testing");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("ImageDao should have all images of the folder / subfolders and should ignore non (JPG/PNG) files")
|
||||||
|
public void initImageDao() throws Exception {
|
||||||
|
ImageDao imageDao = new ImageDao("imagesTest");
|
||||||
|
var listImages = imageDao.retrieveAll();
|
||||||
|
assert (listImages.size() == 5);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user