Added the old unit tests from the old Photos application again.
[photos] / src / test / java / org / wamblee / photos / model / AlbumTest.java
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 (file)
index 0000000..a0c4d07
--- /dev/null
@@ -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<String, PhotoEntry> map = new TreeMap<String, PhotoEntry>();
+        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<String, ArrayList<PhotoEntry>>());
+        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<String, ArrayList<PhotoEntry>>());
+        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();
+    }
+}