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 ADDED_METHOD = "photoAdded";
44 private static final String REMOVED_METHOD = "photoRemoved";
46 private static final String TEST_RESOURCES = "src/test/resources/albumdata";
48 private String resourcesPath;
49 private TestData _testData;
55 * @see junit.framework.TestCase#setUp()
59 public void setUp() throws Exception {
60 _testData = new TestData(this);
63 _cache = createCache();
64 _cache.clear(); // Just to make sure we don't have any caching left from the previous
66 URL url = Thread.currentThread().getContextClassLoader().getResource(".");
67 String path = url.toString();
68 assertTrue(path.startsWith("file:"));
69 path = path.substring("file:".length());
70 resourcesPath = path + "../../" + TEST_RESOURCES;
71 System.out.println(resourcesPath);
74 private void copyDir(File aSource) {
75 FileSystemUtils.copyDir(aSource, getTestOutputDir());
78 protected Cache createCache() {
79 return new ForeverCache();
85 * @see junit.framework.TestCase#tearDown()
88 public void tearDown() throws Exception {
92 private File getTestAlbumData() {
93 File dir = new File(resourcesPath);
94 assertTrue(dir.exists());
98 private File getTestOutputDir() {
99 return new File(_testData.getRoot(), "data");
103 * Verifies that a non-existing album cannot be opened.
106 public void testNonExistingAlbum() {
107 File dir = new File(_testData.getRoot(), "NonExisting");
109 Album album = new FileSystemAlbum(dir, "", _cache);
111 catch (IOException e) {
118 * List photos in a given album. Verify that all entries are returned and
119 * that the relative paths of the photos are correct.
122 public void testListPhotos() throws IOException {
123 File _dir = new File(getTestAlbumData(), "Album1");
125 File dir = getTestOutputDir();
127 Album album = new FileSystemAlbum(dir, "/", _cache);
129 int nentries = album.size();
130 assertEquals(3, nentries);
131 Map<String, PhotoEntry> map = new TreeMap<String, PhotoEntry>();
132 for (int i = 0; i < nentries; i++) {
133 PhotoEntry entry = album.getEntry(i);
134 map.put(entry.getId(), entry);
136 PhotoEntry e1 = map.get("100_0179");
137 PhotoEntry e2 = map.get("100_0162");
138 PhotoEntry e3 = map.get("Nested");
142 assertTrue(e1 instanceof Photo);
143 assertTrue(e2 instanceof Photo);
144 assertTrue(e3 instanceof Album);
145 Album nestedAlbum = (Album) e3;
146 assertEquals(1, nestedAlbum.size());
147 PhotoEntry e4 = nestedAlbum.getEntry(0);
148 assertEquals("mondeo", e4.getId());
149 assertTrue(e4 instanceof Photo);
151 // Checking access based on path
152 assertEquals(album, album.getEntry("/"));
153 assertEquals(e1, album.getEntry("/100_0179"));
154 assertEquals(e2, album.getEntry("/100_0162"));
155 assertEquals(e3, album.getEntry("/Nested"));
156 assertEquals(e4, album.getEntry("/Nested/mondeo"));
158 // Verify that the photos have the correct ids
159 assertEquals("100_0179", e1.getId());
160 assertEquals("100_0162", e2.getId());
161 assertEquals("Nested", e3.getId());
162 assertEquals("mondeo", e4.getId());
164 // Verify that the photos have the correct paths
165 assertEquals("/100_0179", e1.getPath());
166 assertEquals("/100_0162", e2.getPath());
167 assertEquals("/Nested", e3.getPath());
168 assertEquals("/Nested/mondeo", e4.getPath());
172 * List the contents of an album. Verify that non JPEG files are skipped.
175 public void testSkipNonJpegFiles() throws IOException {
176 File _dir = new File(getTestAlbumData(), "Album2");
178 File dir = getTestOutputDir();
180 Album album = new FileSystemAlbum(dir, "/", _cache);
181 assertEquals(0, album.size());
185 * Verify that a photo with a missing thumbnail is not listed.
188 public void testThumbNailMissing() throws IOException {
189 File _dir = new File(getTestAlbumData(), "AlbumMissingThumbnail");
191 File dir = getTestOutputDir();
193 Album album = new FileSystemAlbum(dir, "/", _cache);
194 assertEquals(0, album.size());
198 * Verify that a photo with a missing fullsize photo is not listed.
201 public void testPhotoMissing() throws IOException {
202 File _dir = new File(getTestAlbumData(), "AlbumPhotoMissing");
203 File dir = getTestOutputDir();
205 Album album = new FileSystemAlbum(dir, "/", _cache);
206 assertEquals(0, album.size());
209 private InputStream getImage(File dir) throws IOException {
210 return new BufferedInputStream(new FileInputStream(dir));
214 * Add a photo to the root album and verify it is added correctly and can be
215 * retrieved using a path.
218 public void testAddPhotoToRootAlbum() throws IOException {
219 File dir = new File(_testData.getRoot(), "AlbumNew");
221 assertTrue(dir.mkdir());
222 assertTrue(new File(dir, FileSystemAlbum.PHOTOS_DIR).mkdir());
223 assertTrue(new File(dir, FileSystemAlbum.THUMBNAILS_DIR).mkdir());
225 Album album = new FileSystemAlbum(dir, "/", _cache);
227 File image = new File(getTestAlbumData(), "a.jpg");
228 album.addImage("xyz", getImage(image));
229 assertTrue(album.getEntry("/xyz") != null);
231 Album album2 = new FileSystemAlbum(dir, "/", new ForeverCache<String, ArrayList<PhotoEntry>>());
232 assertTrue(album2.getEntry("/xyz") != null);
236 * Add a photo twice, and verify that the second time an IO Exception occurs.
239 public void testDoublePhotoNotAllowed() throws IOException {
240 File dir = new File(_testData.getRoot(), "AlbumNew");
242 assertTrue(dir.mkdir());
243 assertTrue(new File(dir, FileSystemAlbum.PHOTOS_DIR).mkdir());
244 assertTrue(new File(dir, FileSystemAlbum.THUMBNAILS_DIR).mkdir());
246 Album album = new FileSystemAlbum(dir, "/", _cache);
248 File image = new File(getTestAlbumData(), "a.jpg");
249 album.addImage("xyz", getImage(image));
250 assertTrue(album.getEntry("/xyz") != null);
253 album.addImage("xyz", getImage(image));
256 catch (IOException e) {
257 return; // ok, test succeeded.
262 * Adds an album and adds a photo to it. Verifies that they are added.
265 public void testAddAlbum() throws IOException {
266 File dir = new File(_testData.getRoot(), "AlbumNew");
268 assertTrue(dir.mkdir());
269 assertTrue(new File(dir, FileSystemAlbum.PHOTOS_DIR).mkdir());
270 assertTrue(new File(dir, FileSystemAlbum.THUMBNAILS_DIR).mkdir());
272 Album album = new FileSystemAlbum(dir, "/", _cache);
274 File sub = new File(dir, "sub");
275 album.addAlbum("sub");
276 assertTrue(album.getEntry("/sub") != null);
277 assertTrue(sub.isDirectory());
278 assertTrue(new File(sub, FileSystemAlbum.PHOTOS_DIR).isDirectory());
279 assertTrue(new File(sub, FileSystemAlbum.THUMBNAILS_DIR).isDirectory());
281 Album album2 = new FileSystemAlbum(dir, "/", new ForeverCache<String, ArrayList<PhotoEntry>>());
282 PhotoEntry entry = album2.getEntry("/sub");
284 assertTrue(entry != null);
285 assertTrue(entry instanceof Album);
289 * Tests that adding an album which already exists is not allowed.
292 public void testAddAlreadyExistingAlbum() throws IOException {
293 File dir = new File(_testData.getRoot(), "AlbumNew");
295 assertTrue(dir.mkdir());
296 assertTrue(new File(dir, FileSystemAlbum.PHOTOS_DIR).mkdir());
297 assertTrue(new File(dir, FileSystemAlbum.THUMBNAILS_DIR).mkdir());
299 Album album = new FileSystemAlbum(dir, "/", _cache);
301 File sub = new File(dir, "sub");
302 album.addAlbum("sub");
303 assertTrue(album.getEntry("/sub") != null);
304 assertTrue(sub.isDirectory());
307 album.addAlbum("sub");
309 catch (IOException e) {
315 * Tries to remove a non-empty album and verifies the directory is not
316 * removed. Removes all photos in the album and verifies the photos have
317 * been removed. Verfies that the album can be removed after that.
320 public void testRemove() throws IOException {
321 File dir = getTestOutputDir();
322 File origDir = new File(getTestAlbumData(), "AlbumRemove");
324 Album album = new FileSystemAlbum(dir, "/", _cache);
326 album.removeEntry("Nested");
328 catch (IOException e) {
329 Album nested = (Album) album.getEntry("/Nested");
330 nested.removeEntry("mondeo");
331 album.removeEntry("Nested");