2 * Copyright 2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.photos.model;
18 import java.io.BufferedInputStream;
20 import java.io.FileInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
24 import java.util.ArrayList;
26 import java.util.TreeMap;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.wamblee.cache.Cache;
32 import org.wamblee.cache.ForeverCache;
33 import org.wamblee.io.FileSystemUtils;
34 import org.wamblee.io.TestData;
35 import org.wamblee.photos.model.filesystem.FileSystemAlbum;
36 import static org.junit.Assert.*;
39 * Test of the file based album.
41 public class AlbumTest {
43 private static final String TEST_RESOURCES = "src/test/resources/albumdata";
45 private String resourcesPath;
46 private TestData _testData;
52 * @see junit.framework.TestCase#setUp()
56 public void setUp() throws Exception {
57 _testData = new TestData(this);
60 _cache = createCache();
61 _cache.clear(); // Just to make sure we don't have any caching left from the previous
63 URL url = Thread.currentThread().getContextClassLoader().getResource(".");
64 String path = url.toString();
65 assertTrue(path.startsWith("file:"));
66 path = path.substring("file:".length());
67 resourcesPath = path + "../../" + TEST_RESOURCES;
70 private void copyDir(File aSource) {
71 FileSystemUtils.copyDir(aSource, getTestOutputDir());
74 protected Cache createCache() {
75 return new ForeverCache();
81 * @see junit.framework.TestCase#tearDown()
84 public void tearDown() throws Exception {
88 private File getTestAlbumData() {
89 File dir = new File(resourcesPath);
90 assertTrue(dir.exists());
94 private File getTestOutputDir() {
95 return new File(_testData.getRoot(), "data");
98 protected Album createAlbum(File aDir, String aPath, Cache<String, ArrayList<PhotoEntry>> aCache)
100 return new FileSystemAlbum(aDir, aPath, aCache);
104 * Verifies that a non-existing album cannot be opened.
107 public void testNonExistingAlbum() {
108 File dir = new File(_testData.getRoot(), "NonExisting");
110 Album album = createAlbum(dir, "", _cache);
112 catch (IOException e) {
119 * List photos in a given album. Verify that all entries are returned and
120 * that the relative paths of the photos are correct.
123 public void testListPhotos() throws IOException {
124 File _dir = new File(getTestAlbumData(), "Album1");
126 File dir = getTestOutputDir();
128 Album album = createAlbum(dir, "/", _cache);
130 int nentries = album.size();
131 assertEquals(3, nentries);
132 Map<String, PhotoEntry> map = new TreeMap<String, PhotoEntry>();
133 for (int i = 0; i < nentries; i++) {
134 PhotoEntry entry = album.getEntry(i);
135 map.put(entry.getId(), entry);
137 PhotoEntry e1 = map.get("100_0179");
138 PhotoEntry e2 = map.get("100_0162");
139 PhotoEntry e3 = map.get("Nested");
143 assertTrue(e1 instanceof Photo);
144 assertTrue(e2 instanceof Photo);
145 assertTrue(e3 instanceof Album);
146 Album nestedAlbum = (Album) e3;
147 assertEquals(1, nestedAlbum.size());
148 PhotoEntry e4 = nestedAlbum.getEntry(0);
149 assertEquals("mondeo", e4.getId());
150 assertTrue(e4 instanceof Photo);
152 // Checking access based on path
153 assertEquals(album, album.getEntry("/"));
154 assertEquals(e1, album.getEntry("/100_0179"));
155 assertEquals(e2, album.getEntry("/100_0162"));
156 assertEquals(e3, album.getEntry("/Nested"));
157 assertEquals(e4, album.getEntry("/Nested/mondeo"));
159 // Verify that the photos have the correct ids
160 assertEquals("100_0179", e1.getId());
161 assertEquals("100_0162", e2.getId());
162 assertEquals("Nested", e3.getId());
163 assertEquals("mondeo", e4.getId());
165 // Verify that the photos have the correct paths
166 assertEquals("/100_0179", e1.getPath());
167 assertEquals("/100_0162", e2.getPath());
168 assertEquals("/Nested", e3.getPath());
169 assertEquals("/Nested/mondeo", e4.getPath());
173 * List the contents of an album. Verify that non JPEG files are skipped.
176 public void testSkipNonJpegFiles() throws IOException {
177 File _dir = new File(getTestAlbumData(), "Album2");
179 File dir = getTestOutputDir();
181 Album album = createAlbum(dir, "/", _cache);
182 assertEquals(0, album.size());
186 * Verify that a photo with a missing thumbnail is not listed.
189 public void testThumbNailMissing() throws IOException {
190 File _dir = new File(getTestAlbumData(), "AlbumMissingThumbnail");
192 File dir = getTestOutputDir();
194 Album album = createAlbum(dir, "/", _cache);
195 assertEquals(0, album.size());
199 * Verify that a photo with a missing fullsize photo is not listed.
202 public void testPhotoMissing() throws IOException {
203 File _dir = new File(getTestAlbumData(), "AlbumPhotoMissing");
204 File dir = getTestOutputDir();
206 Album album = createAlbum(dir, "/", _cache);
207 assertEquals(0, album.size());
210 private InputStream getImage(File dir) throws IOException {
211 return new BufferedInputStream(new FileInputStream(dir));
215 * Add a photo to the root album and verify it is added correctly and can be
216 * retrieved using a path.
219 public void testAddPhotoToRootAlbum() throws IOException {
220 File dir = new File(_testData.getRoot(), "AlbumNew");
222 assertTrue(dir.mkdir());
223 assertTrue(new File(dir, FileSystemAlbum.PHOTOS_DIR).mkdir());
224 assertTrue(new File(dir, FileSystemAlbum.THUMBNAILS_DIR).mkdir());
226 Album album = createAlbum(dir, "/", _cache);
228 File image = new File(getTestAlbumData(), "a.jpg");
229 album.addImage("xyz", getImage(image));
230 assertTrue(album.getEntry("/xyz") != null);
232 Album album2 = createAlbum(dir, "/", new ForeverCache<String, ArrayList<PhotoEntry>>());
233 assertTrue(album2.getEntry("/xyz") != null);
237 * Add a photo twice, and verify that the second time an IO Exception occurs.
240 public void testDoublePhotoNotAllowed() throws IOException {
241 File dir = new File(_testData.getRoot(), "AlbumNew");
243 assertTrue(dir.mkdir());
244 assertTrue(new File(dir, FileSystemAlbum.PHOTOS_DIR).mkdir());
245 assertTrue(new File(dir, FileSystemAlbum.THUMBNAILS_DIR).mkdir());
247 Album album = createAlbum(dir, "/", _cache);
249 File image = new File(getTestAlbumData(), "a.jpg");
250 album.addImage("xyz", getImage(image));
251 assertTrue(album.getEntry("/xyz") != null);
254 album.addImage("xyz", getImage(image));
257 catch (IOException e) {
258 return; // ok, test succeeded.
263 * Adds an album and adds a photo to it. Verifies that they are added.
266 public void testAddAlbum() throws IOException {
267 File dir = new File(_testData.getRoot(), "AlbumNew");
269 assertTrue(dir.mkdir());
270 assertTrue(new File(dir, FileSystemAlbum.PHOTOS_DIR).mkdir());
271 assertTrue(new File(dir, FileSystemAlbum.THUMBNAILS_DIR).mkdir());
273 Album album = createAlbum(dir, "/", _cache);
275 File sub = new File(dir, "sub");
276 album.addAlbum("sub");
277 assertTrue(album.getEntry("/sub") != null);
278 assertTrue(sub.isDirectory());
279 assertTrue(new File(sub, FileSystemAlbum.PHOTOS_DIR).isDirectory());
280 assertTrue(new File(sub, FileSystemAlbum.THUMBNAILS_DIR).isDirectory());
282 Album album2 = createAlbum(dir, "/", new ForeverCache<String, ArrayList<PhotoEntry>>());
283 PhotoEntry entry = album2.getEntry("/sub");
285 assertTrue(entry != null);
286 assertTrue(entry instanceof Album);
290 * Tests that adding an album which already exists is not allowed.
293 public void testAddAlreadyExistingAlbum() throws IOException {
294 File dir = new File(_testData.getRoot(), "AlbumNew");
296 assertTrue(dir.mkdir());
297 assertTrue(new File(dir, FileSystemAlbum.PHOTOS_DIR).mkdir());
298 assertTrue(new File(dir, FileSystemAlbum.THUMBNAILS_DIR).mkdir());
300 Album album = createAlbum(dir, "/", _cache);
302 File sub = new File(dir, "sub");
303 album.addAlbum("sub");
304 assertTrue(album.getEntry("/sub") != null);
305 assertTrue(sub.isDirectory());
308 album.addAlbum("sub");
310 catch (IOException e) {
316 * Tries to remove a non-empty album and verifies the directory is not
317 * removed. Removes all photos in the album and verifies the photos have
318 * been removed. Verfies that the album can be removed after that.
321 public void testRemove() throws IOException {
322 File dir = getTestOutputDir();
323 File origDir = new File(getTestAlbumData(), "AlbumRemove");
325 Album album = createAlbum(dir, "/", _cache);
327 album.removeEntry("Nested");
329 catch (IOException e) {
330 Album nested = (Album) album.getEntry("/Nested");
331 nested.removeEntry("mondeo");
332 album.removeEntry("Nested");