From: Erik Brakkee Date: Sun, 22 Sep 2013 18:31:43 +0000 (+0200) Subject: Added the old unit tests from the old Photos application again. X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=8c73cdd2b64e2bb86183a21e175fd0a14717529c;hp=8b1bc175311d80b5ca67d8f6ee8a266428cb5fa3;p=photos Added the old unit tests from the old Photos application again. --- diff --git a/pom.xml b/pom.xml index 9246313..dc1ba4a 100644 --- a/pom.xml +++ b/pom.xml @@ -105,6 +105,13 @@ wamblee-security-usermgt ${utils.version} + + org.wamblee + wamblee-support-general + ${utils.version} + test + test-jar + diff --git a/src/main/java/org/wamblee/photos/model/filesystem/FileSystemAlbum.java b/src/main/java/org/wamblee/photos/model/filesystem/FileSystemAlbum.java index bfff860..c08288b 100644 --- a/src/main/java/org/wamblee/photos/model/filesystem/FileSystemAlbum.java +++ b/src/main/java/org/wamblee/photos/model/filesystem/FileSystemAlbum.java @@ -119,7 +119,7 @@ public class FileSystemAlbum implements Album { * * @param aDir Directory where the album is located. * @param aPath Path that this album represents. - * @param aCache Cache to use. + * @param aCache Cache to use. Note that a cache usedin one album hierarachy should not be used in another. * @throws IOException */ public FileSystemAlbum(File aDir, String aPath, Cache> aCache) throws IOException { diff --git a/src/test/java/org/wamblee/photos/model/AlbumEhCacheTest.java b/src/test/java/org/wamblee/photos/model/AlbumEhCacheTest.java new file mode 100644 index 0000000..fab4892 --- /dev/null +++ b/src/test/java/org/wamblee/photos/model/AlbumEhCacheTest.java @@ -0,0 +1,42 @@ +/* + * Copyright 2005 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.wamblee.photos.model; + +import org.wamblee.cache.Cache; +import org.wamblee.cache.EhCache; +import org.wamblee.io.ClassPathResource; + +/** + * Same tests as AlbumTest but now with EhCache. + */ +public class AlbumEhCacheTest extends AlbumTest { + + /* + * (non-Javadoc) + * + * @see org.wamblee.photos.database.file.AlbumTest#createCache() + */ + @Override + protected Cache createCache() { + try { + return new EhCache(new ClassPathResource("META-INF/ehcache.xml"), "test"); + } + catch (Exception e) { + throw new RuntimeException(e.getMessage(), e); + } + } +} diff --git a/src/test/java/org/wamblee/photos/model/AlbumTest.java b/src/test/java/org/wamblee/photos/model/AlbumTest.java new file mode 100644 index 0000000..a0c4d07 --- /dev/null +++ b/src/test/java/org/wamblee/photos/model/AlbumTest.java @@ -0,0 +1,336 @@ +/* + * Copyright 2005 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.photos.model; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.ArrayList; +import java.util.Map; +import java.util.TreeMap; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.wamblee.cache.Cache; +import org.wamblee.cache.ForeverCache; +import org.wamblee.io.FileSystemUtils; +import org.wamblee.io.TestData; +import org.wamblee.photos.model.filesystem.FileSystemAlbum; +import static org.junit.Assert.*; + +/** + * Test of the file based album. + */ +public class AlbumTest { + + private static final String ADDED_METHOD = "photoAdded"; + private static final String REMOVED_METHOD = "photoRemoved"; + + private static final String TEST_RESOURCES = "src/test/resources/albumdata"; + + private String resourcesPath; + private TestData _testData; + private Cache _cache; + + /* + * (non-Javadoc) + * + * @see junit.framework.TestCase#setUp() + */ + + @Before + public void setUp() throws Exception { + _testData = new TestData(this); + _testData.clean(); + + _cache = createCache(); + _cache.clear(); // Just to make sure we don't have any caching left from the previous + + URL url = Thread.currentThread().getContextClassLoader().getResource("."); + String path = url.toString(); + assertTrue(path.startsWith("file:")); + path = path.substring("file:".length()); + resourcesPath = path + "../../" + TEST_RESOURCES; + System.out.println(resourcesPath); + } + + private void copyDir(File aSource) { + FileSystemUtils.copyDir(aSource, getTestOutputDir()); + } + + protected Cache createCache() { + return new ForeverCache(); + } + + /* + * (non-Javadoc) + * + * @see junit.framework.TestCase#tearDown() + */ + @After + public void tearDown() throws Exception { + // Empty + } + + private File getTestAlbumData() { + File dir = new File(resourcesPath); + assertTrue(dir.exists()); + return dir; + } + + private File getTestOutputDir() { + return new File(_testData.getRoot(), "data"); + } + + /** + * Verifies that a non-existing album cannot be opened. + */ + @Test + public void testNonExistingAlbum() { + File dir = new File(_testData.getRoot(), "NonExisting"); + try { + Album album = new FileSystemAlbum(dir, "", _cache); + } + catch (IOException e) { + return; // ok + } + fail(); + } + + /** + * List photos in a given album. Verify that all entries are returned and + * that the relative paths of the photos are correct. + */ + @Test + public void testListPhotos() throws IOException { + File _dir = new File(getTestAlbumData(), "Album1"); + + File dir = getTestOutputDir(); + copyDir(_dir); + Album album = new FileSystemAlbum(dir, "/", _cache); + + int nentries = album.size(); + assertEquals(3, nentries); + Map map = new TreeMap(); + for (int i = 0; i < nentries; i++) { + PhotoEntry entry = album.getEntry(i); + map.put(entry.getId(), entry); + } + PhotoEntry e1 = map.get("100_0179"); + PhotoEntry e2 = map.get("100_0162"); + PhotoEntry e3 = map.get("Nested"); + assertNotNull(e1); + assertNotNull(e2); + assertNotNull(e3); + assertTrue(e1 instanceof Photo); + assertTrue(e2 instanceof Photo); + assertTrue(e3 instanceof Album); + Album nestedAlbum = (Album) e3; + assertEquals(1, nestedAlbum.size()); + PhotoEntry e4 = nestedAlbum.getEntry(0); + assertEquals("mondeo", e4.getId()); + assertTrue(e4 instanceof Photo); + + // Checking access based on path + assertEquals(album, album.getEntry("/")); + assertEquals(e1, album.getEntry("/100_0179")); + assertEquals(e2, album.getEntry("/100_0162")); + assertEquals(e3, album.getEntry("/Nested")); + assertEquals(e4, album.getEntry("/Nested/mondeo")); + + // Verify that the photos have the correct ids + assertEquals("100_0179", e1.getId()); + assertEquals("100_0162", e2.getId()); + assertEquals("Nested", e3.getId()); + assertEquals("mondeo", e4.getId()); + + // Verify that the photos have the correct paths + assertEquals("/100_0179", e1.getPath()); + assertEquals("/100_0162", e2.getPath()); + assertEquals("/Nested", e3.getPath()); + assertEquals("/Nested/mondeo", e4.getPath()); + } + + /** + * List the contents of an album. Verify that non JPEG files are skipped. + */ + @Test + public void testSkipNonJpegFiles() throws IOException { + File _dir = new File(getTestAlbumData(), "Album2"); + + File dir = getTestOutputDir(); + copyDir(_dir); + Album album = new FileSystemAlbum(dir, "/", _cache); + assertEquals(0, album.size()); + } + + /** + * Verify that a photo with a missing thumbnail is not listed. + */ + @Test + public void testThumbNailMissing() throws IOException { + File _dir = new File(getTestAlbumData(), "AlbumMissingThumbnail"); + + File dir = getTestOutputDir(); + copyDir(_dir); + Album album = new FileSystemAlbum(dir, "/", _cache); + assertEquals(0, album.size()); + } + + /** + * Verify that a photo with a missing fullsize photo is not listed. + */ + @Test + public void testPhotoMissing() throws IOException { + File _dir = new File(getTestAlbumData(), "AlbumPhotoMissing"); + File dir = getTestOutputDir(); + copyDir(_dir); + Album album = new FileSystemAlbum(dir, "/", _cache); + assertEquals(0, album.size()); + } + + private InputStream getImage(File dir) throws IOException { + return new BufferedInputStream(new FileInputStream(dir)); + } + + /** + * Add a photo to the root album and verify it is added correctly and can be + * retrieved using a path. + */ + @Test + public void testAddPhotoToRootAlbum() throws IOException { + File dir = new File(_testData.getRoot(), "AlbumNew"); + + assertTrue(dir.mkdir()); + assertTrue(new File(dir, FileSystemAlbum.PHOTOS_DIR).mkdir()); + assertTrue(new File(dir, FileSystemAlbum.THUMBNAILS_DIR).mkdir()); + + Album album = new FileSystemAlbum(dir, "/", _cache); + + File image = new File(getTestAlbumData(), "a.jpg"); + album.addImage("xyz", getImage(image)); + assertTrue(album.getEntry("/xyz") != null); + + Album album2 = new FileSystemAlbum(dir, "/", new ForeverCache>()); + assertTrue(album2.getEntry("/xyz") != null); + } + + /** + * Add a photo twice, and verify that the second time an IO Exception occurs. + */ + @Test + public void testDoublePhotoNotAllowed() throws IOException { + File dir = new File(_testData.getRoot(), "AlbumNew"); + + assertTrue(dir.mkdir()); + assertTrue(new File(dir, FileSystemAlbum.PHOTOS_DIR).mkdir()); + assertTrue(new File(dir, FileSystemAlbum.THUMBNAILS_DIR).mkdir()); + + Album album = new FileSystemAlbum(dir, "/", _cache); + + File image = new File(getTestAlbumData(), "a.jpg"); + album.addImage("xyz", getImage(image)); + assertTrue(album.getEntry("/xyz") != null); + + try { + album.addImage("xyz", getImage(image)); + fail(); + } + catch (IOException e) { + return; // ok, test succeeded. + } + } + + /** + * Adds an album and adds a photo to it. Verifies that they are added. + */ + @Test + public void testAddAlbum() throws IOException { + File dir = new File(_testData.getRoot(), "AlbumNew"); + + assertTrue(dir.mkdir()); + assertTrue(new File(dir, FileSystemAlbum.PHOTOS_DIR).mkdir()); + assertTrue(new File(dir, FileSystemAlbum.THUMBNAILS_DIR).mkdir()); + + Album album = new FileSystemAlbum(dir, "/", _cache); + + File sub = new File(dir, "sub"); + album.addAlbum("sub"); + assertTrue(album.getEntry("/sub") != null); + assertTrue(sub.isDirectory()); + assertTrue(new File(sub, FileSystemAlbum.PHOTOS_DIR).isDirectory()); + assertTrue(new File(sub, FileSystemAlbum.THUMBNAILS_DIR).isDirectory()); + + Album album2 = new FileSystemAlbum(dir, "/", new ForeverCache>()); + PhotoEntry entry = album2.getEntry("/sub"); + + assertTrue(entry != null); + assertTrue(entry instanceof Album); + } + + /** + * Tests that adding an album which already exists is not allowed. + */ + @Test + public void testAddAlreadyExistingAlbum() throws IOException { + File dir = new File(_testData.getRoot(), "AlbumNew"); + + assertTrue(dir.mkdir()); + assertTrue(new File(dir, FileSystemAlbum.PHOTOS_DIR).mkdir()); + assertTrue(new File(dir, FileSystemAlbum.THUMBNAILS_DIR).mkdir()); + + Album album = new FileSystemAlbum(dir, "/", _cache); + + File sub = new File(dir, "sub"); + album.addAlbum("sub"); + assertTrue(album.getEntry("/sub") != null); + assertTrue(sub.isDirectory()); + + try { + album.addAlbum("sub"); + } + catch (IOException e) { + return; // ok. + } + } + + /** + * Tries to remove a non-empty album and verifies the directory is not + * removed. Removes all photos in the album and verifies the photos have + * been removed. Verfies that the album can be removed after that. + */ + @Test + public void testRemove() throws IOException { + File dir = getTestOutputDir(); + File origDir = new File(getTestAlbumData(), "AlbumRemove"); + copyDir(origDir); + Album album = new FileSystemAlbum(dir, "/", _cache); + try { + album.removeEntry("Nested"); + } + catch (IOException e) { + Album nested = (Album) album.getEntry("/Nested"); + nested.removeEntry("mondeo"); + album.removeEntry("Nested"); + return; + } + fail(); + } +} diff --git a/src/test/java/org/wamblee/photos/model/AlbumZeroCacheTest.java b/src/test/java/org/wamblee/photos/model/AlbumZeroCacheTest.java new file mode 100644 index 0000000..760e5db --- /dev/null +++ b/src/test/java/org/wamblee/photos/model/AlbumZeroCacheTest.java @@ -0,0 +1,35 @@ +/* + * Copyright 2005 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.wamblee.photos.model; + +import org.wamblee.cache.Cache; +import org.wamblee.cache.ZeroCache; + +/** + * Same as album test but now without any caching causing + * a lot of file system access. + */ +public class AlbumZeroCacheTest extends AlbumTest { + + /* (non-Javadoc) + * @see org.wamblee.photos.database.file.AlbumTest#createCache() + */ + @Override + protected Cache createCache() { + return new ZeroCache(); + } +} diff --git a/src/test/resources/albumdata/Album1/Nested/fotos/mondeo.jpg b/src/test/resources/albumdata/Album1/Nested/fotos/mondeo.jpg new file mode 100644 index 0000000..908a958 Binary files /dev/null and b/src/test/resources/albumdata/Album1/Nested/fotos/mondeo.jpg differ diff --git a/src/test/resources/albumdata/Album1/Nested/thumbnails/mondeo_thumb.jpg b/src/test/resources/albumdata/Album1/Nested/thumbnails/mondeo_thumb.jpg new file mode 100644 index 0000000..a0f50f8 Binary files /dev/null and b/src/test/resources/albumdata/Album1/Nested/thumbnails/mondeo_thumb.jpg differ diff --git a/src/test/resources/albumdata/Album1/fotos/100_0162.jpg b/src/test/resources/albumdata/Album1/fotos/100_0162.jpg new file mode 100644 index 0000000..cddc464 Binary files /dev/null and b/src/test/resources/albumdata/Album1/fotos/100_0162.jpg differ diff --git a/src/test/resources/albumdata/Album1/fotos/100_0179.jpg b/src/test/resources/albumdata/Album1/fotos/100_0179.jpg new file mode 100644 index 0000000..154d8d5 Binary files /dev/null and b/src/test/resources/albumdata/Album1/fotos/100_0179.jpg differ diff --git a/src/test/resources/albumdata/Album1/thumbnails/100_0162_thumb.jpg b/src/test/resources/albumdata/Album1/thumbnails/100_0162_thumb.jpg new file mode 100644 index 0000000..51b5a67 Binary files /dev/null and b/src/test/resources/albumdata/Album1/thumbnails/100_0162_thumb.jpg differ diff --git a/src/test/resources/albumdata/Album1/thumbnails/100_0179_thumb.jpg b/src/test/resources/albumdata/Album1/thumbnails/100_0179_thumb.jpg new file mode 100644 index 0000000..42b75c6 Binary files /dev/null and b/src/test/resources/albumdata/Album1/thumbnails/100_0179_thumb.jpg differ diff --git a/src/test/resources/albumdata/Album2/c.txt b/src/test/resources/albumdata/Album2/c.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/albumdata/Album2/fotos/a.txt b/src/test/resources/albumdata/Album2/fotos/a.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/albumdata/Album2/thumbnails/b.txt b/src/test/resources/albumdata/Album2/thumbnails/b.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/albumdata/AlbumMissingThumbnail/fotos/100_0162.jpg b/src/test/resources/albumdata/AlbumMissingThumbnail/fotos/100_0162.jpg new file mode 100644 index 0000000..cddc464 Binary files /dev/null and b/src/test/resources/albumdata/AlbumMissingThumbnail/fotos/100_0162.jpg differ diff --git a/src/test/resources/albumdata/AlbumMissingThumbnail/thumbnails/.empty b/src/test/resources/albumdata/AlbumMissingThumbnail/thumbnails/.empty new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/albumdata/AlbumPhotoMissing/fotos/.empty b/src/test/resources/albumdata/AlbumPhotoMissing/fotos/.empty new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/albumdata/AlbumPhotoMissing/thumbnails/100_0179_thumb.jpg b/src/test/resources/albumdata/AlbumPhotoMissing/thumbnails/100_0179_thumb.jpg new file mode 100644 index 0000000..42b75c6 Binary files /dev/null and b/src/test/resources/albumdata/AlbumPhotoMissing/thumbnails/100_0179_thumb.jpg differ diff --git a/src/test/resources/albumdata/AlbumRemove/Nested/fotos/mondeo.jpg b/src/test/resources/albumdata/AlbumRemove/Nested/fotos/mondeo.jpg new file mode 100644 index 0000000..908a958 Binary files /dev/null and b/src/test/resources/albumdata/AlbumRemove/Nested/fotos/mondeo.jpg differ diff --git a/src/test/resources/albumdata/AlbumRemove/Nested/thumbnails/mondeo_thumb.jpg b/src/test/resources/albumdata/AlbumRemove/Nested/thumbnails/mondeo_thumb.jpg new file mode 100644 index 0000000..a0f50f8 Binary files /dev/null and b/src/test/resources/albumdata/AlbumRemove/Nested/thumbnails/mondeo_thumb.jpg differ diff --git a/src/test/resources/albumdata/AlbumRemove/fotos/100_0162.jpg b/src/test/resources/albumdata/AlbumRemove/fotos/100_0162.jpg new file mode 100644 index 0000000..cddc464 Binary files /dev/null and b/src/test/resources/albumdata/AlbumRemove/fotos/100_0162.jpg differ diff --git a/src/test/resources/albumdata/AlbumRemove/fotos/100_0179.jpg b/src/test/resources/albumdata/AlbumRemove/fotos/100_0179.jpg new file mode 100644 index 0000000..154d8d5 Binary files /dev/null and b/src/test/resources/albumdata/AlbumRemove/fotos/100_0179.jpg differ diff --git a/src/test/resources/albumdata/AlbumRemove/thumbnails/100_0162_thumb.jpg b/src/test/resources/albumdata/AlbumRemove/thumbnails/100_0162_thumb.jpg new file mode 100644 index 0000000..51b5a67 Binary files /dev/null and b/src/test/resources/albumdata/AlbumRemove/thumbnails/100_0162_thumb.jpg differ diff --git a/src/test/resources/albumdata/AlbumRemove/thumbnails/100_0179_thumb.jpg b/src/test/resources/albumdata/AlbumRemove/thumbnails/100_0179_thumb.jpg new file mode 100644 index 0000000..42b75c6 Binary files /dev/null and b/src/test/resources/albumdata/AlbumRemove/thumbnails/100_0179_thumb.jpg differ diff --git a/src/test/resources/albumdata/a.jpg b/src/test/resources/albumdata/a.jpg new file mode 100644 index 0000000..154d8d5 Binary files /dev/null and b/src/test/resources/albumdata/a.jpg differ diff --git a/src/test/resources/albumdata/b.jpg b/src/test/resources/albumdata/b.jpg new file mode 100644 index 0000000..cddc464 Binary files /dev/null and b/src/test/resources/albumdata/b.jpg differ diff --git a/src/test/resources/albumdata/c.jpg b/src/test/resources/albumdata/c.jpg new file mode 100644 index 0000000..908a958 Binary files /dev/null and b/src/test/resources/albumdata/c.jpg differ