/* * 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.database.file; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Map; import java.util.TreeMap; import org.jmock.cglib.MockObjectTestCase; import org.wamblee.cache.Cache; import org.wamblee.cache.ForeverCache; import org.wamblee.photos.model.Album; import org.wamblee.photos.model.Photo; import org.wamblee.photos.model.PhotoEntry; import org.wamblee.photos.model.file.FileSystemAlbum; import org.wamblee.test.TestSupport; /** * Test of the file based album. */ public class AlbumTest extends MockObjectTestCase { private static final String ADDED_METHOD = "photoAdded"; private static final String REMOVED_METHOD = "photoRemoved"; private Cache _cache; /* * (non-Javadoc) * * @see junit.framework.TestCase#setUp() */ protected void setUp() throws Exception { super.setUp(); _cache = createCache(); _cache.clear(); // Just to make sure we don't have any caching left from the previous // test cases. } protected Cache createCache() { return new ForeverCache(); } /* * (non-Javadoc) * * @see junit.framework.TestCase#tearDown() */ protected void tearDown() throws Exception { super.tearDown(); TestSupport.removeDir(TestSupport.getTmpDir()); } /** * Verifies that a non-existing album cannot be opened. * */ public void testNonExistingAlbum() { File dir = new File(TestSupport.getTestRootDir(), "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. * */ public void testListPhotos() throws IOException { File _dir = new File(TestSupport.getTestRootDir(), "Album1"); File dir = TestSupport.getTmpDir(); TestSupport.copyDir(_dir, dir); try { Album album = new FileSystemAlbum(dir, "/", _cache); int nentries = album.size(); assertEquals(3, nentries); // including CVS folder. 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()); } finally { TestSupport.removeDir(dir); } } /** * List the contents of an album. Verify that non JPEG files are skipped. * */ public void testSkipNonJpegFiles() throws IOException { File _dir = new File(TestSupport.getTestRootDir(), "Album2"); File dir = TestSupport.getTmpDir(); TestSupport.copyDir(_dir, dir); try { Album album = new FileSystemAlbum(dir, "/", _cache); assertEquals(0, album.size()); } finally { TestSupport.removeDir(dir); } } /** * Verify that a photo with a missing thumbnail is not listed. * */ public void testThumbNailMissing() throws IOException { File _dir = new File(TestSupport.getTestRootDir(), "AlbumMissingThumbnail"); File dir = TestSupport.getTmpDir(); TestSupport.copyDir(_dir, dir); try { Album album = new FileSystemAlbum(dir, "/", _cache); assertEquals(0, album.size()); } finally { TestSupport.removeDir(dir); } } /** * Verify that a photo with a missing fullsize photo is not listed. * */ public void testPhotoMissing() throws IOException { File _dir = new File(TestSupport.getTestRootDir(), "AlbumPhotoMissing"); File dir = TestSupport.getTmpDir(); TestSupport.copyDir(_dir, dir); try { Album album = new FileSystemAlbum(dir, "/", _cache); assertEquals(0, album.size()); } finally { TestSupport.removeDir(dir); } } 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. * */ public void testAddPhotoToRootAlbum() throws IOException { File dir = new File(TestSupport.getTestRootDir(), "AlbumNew"); assertTrue(dir.mkdir()); assertTrue(new File(dir, FileSystemAlbum.PHOTOS_DIR).mkdir()); assertTrue(new File(dir, FileSystemAlbum.THUMBNAILS_DIR).mkdir()); try { Album album = new FileSystemAlbum(dir, "/", _cache); File image = new File(TestSupport.getTestRootDir(), "a.jpg"); album.addImage("xyz", getImage(image)); assertTrue(album.getEntry("/xyz") != null); Album album2 = new FileSystemAlbum(dir, "/", _cache); assertTrue(album2.getEntry("/xyz") != null); } finally { TestSupport.removeDir(dir); } } /** * Add a photo twice, and verify that the second time an IO Exception occurs. */ public void testDoublePhotoNotAllowed() throws IOException { File dir = new File(TestSupport.getTestRootDir(), "AlbumNew"); assertTrue(dir.mkdir()); assertTrue(new File(dir, FileSystemAlbum.PHOTOS_DIR).mkdir()); assertTrue(new File(dir, FileSystemAlbum.THUMBNAILS_DIR).mkdir()); try { Album album = new FileSystemAlbum(dir, "/", _cache); File image = new File(TestSupport.getTestRootDir(), "a.jpg"); album.addImage("xyz", getImage(image)); assertTrue(album.getEntry("/xyz") != null); try { album.addImage("xyz", getImage(image)); } catch (IOException e) { return; // ok, test succeeded. } } finally { TestSupport.removeDir(dir); } } /** * Adds an album and adds a photo to it. Verifies that they are added. * */ public void testAddAlbum() throws IOException { File dir = new File(TestSupport.getTestRootDir(), "AlbumNew"); assertTrue(dir.mkdir()); assertTrue(new File(dir, FileSystemAlbum.PHOTOS_DIR).mkdir()); assertTrue(new File(dir, FileSystemAlbum.THUMBNAILS_DIR).mkdir()); try { 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, "/", _cache); PhotoEntry entry = album2.getEntry("/sub"); assertTrue(entry != null); assertTrue(entry instanceof Album); } finally { TestSupport.removeDir(dir); } } /** * Tests that adding an album which already exists is not allowed. * */ public void testAddAlreadyExistingAlbum() throws IOException { File dir = new File(TestSupport.getTestRootDir(), "AlbumNew"); assertTrue(dir.mkdir()); assertTrue(new File(dir, FileSystemAlbum.PHOTOS_DIR).mkdir()); assertTrue(new File(dir, FileSystemAlbum.THUMBNAILS_DIR).mkdir()); try { 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. } } finally { TestSupport.removeDir(dir); } } /** * 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. * */ public void testRemove() throws IOException { File dir = new File(TestSupport.getTestRootDir(), "AlbumNew"); File origDir = new File(TestSupport.getTestRootDir(), "AlbumRemove"); TestSupport.copyDir(origDir, dir); try { 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(); } finally { TestSupport.removeDir(dir); } } }