Working upload of photos (individual and zip)
[photos] / src / test / java / org / wamblee / photos / model / AlbumTest.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.photos.model;
17
18 import java.io.BufferedInputStream;
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.net.URL;
24 import java.util.ArrayList;
25 import java.util.Map;
26 import java.util.TreeMap;
27
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.*;
37
38 /**
39  * Test of the file based album.
40  */
41 public class AlbumTest {
42
43     private static final String TEST_RESOURCES = "src/test/resources/albumdata";
44
45     private String resourcesPath;
46     private TestData _testData;
47     private Cache _cache;
48
49     /*
50      * (non-Javadoc)
51      *
52      * @see junit.framework.TestCase#setUp()
53      */
54
55     @Before
56     public void setUp() throws Exception {
57         _testData = new TestData(this);
58         _testData.clean();
59
60         _cache = createCache();
61         _cache.clear(); // Just to make sure we don't have any caching left from the previous
62
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;
68     }
69
70     private void copyDir(File aSource) {
71         FileSystemUtils.copyDir(aSource, getTestOutputDir());
72     }
73
74     protected Cache createCache() {
75         return new ForeverCache();
76     }
77
78     /*
79      * (non-Javadoc)
80      *
81      * @see junit.framework.TestCase#tearDown()
82      */
83     @After
84     public void tearDown() throws Exception {
85         // Empty
86     }
87
88     private File getTestAlbumData() {
89         File dir = new File(resourcesPath);
90         assertTrue(dir.exists());
91         return dir;
92     }
93
94     private File getTestOutputDir() {
95         return new File(_testData.getRoot(), "data");
96     }
97
98     protected Album createAlbum(File aDir, String aPath, Cache<String, ArrayList<PhotoEntry>> aCache)
99             throws IOException {
100         return new FileSystemAlbum(aDir, aPath, aCache);
101     }
102
103     /**
104      * Verifies that a non-existing album cannot be opened.
105      */
106     @Test
107     public void testNonExistingAlbum() {
108         File dir = new File(_testData.getRoot(), "NonExisting");
109         try {
110             Album album = createAlbum(dir, "", _cache);
111         }
112         catch (IOException e) {
113             return; // ok
114         }
115         fail();
116     }
117
118     /**
119      * List photos in a given album. Verify that all entries are returned and
120      * that the relative paths of the photos are correct.
121      */
122     @Test
123     public void testListPhotos() throws IOException {
124         File _dir = new File(getTestAlbumData(), "Album1");
125
126         File dir = getTestOutputDir();
127         copyDir(_dir);
128         Album album = createAlbum(dir, "/", _cache);
129
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);
136         }
137         PhotoEntry e1 = map.get("100_0179");
138         PhotoEntry e2 = map.get("100_0162");
139         PhotoEntry e3 = map.get("Nested");
140         assertNotNull(e1);
141         assertNotNull(e2);
142         assertNotNull(e3);
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);
151
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"));
158
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());
164
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());
170     }
171
172     /**
173      * List the contents of an album. Verify that non JPEG files are skipped.
174      */
175     @Test
176     public void testSkipNonJpegFiles() throws IOException {
177         File _dir = new File(getTestAlbumData(), "Album2");
178
179         File dir = getTestOutputDir();
180         copyDir(_dir);
181         Album album = createAlbum(dir, "/", _cache);
182         assertEquals(0, album.size());
183     }
184
185     /**
186      * Verify that a photo with a missing thumbnail is not listed.
187      */
188     @Test
189     public void testThumbNailMissing() throws IOException {
190         File _dir = new File(getTestAlbumData(), "AlbumMissingThumbnail");
191
192         File dir = getTestOutputDir();
193         copyDir(_dir);
194         Album album = createAlbum(dir, "/", _cache);
195         assertEquals(0, album.size());
196     }
197
198     /**
199      * Verify that a photo with a missing fullsize photo is not listed.
200      */
201     @Test
202     public void testPhotoMissing() throws IOException {
203         File _dir = new File(getTestAlbumData(), "AlbumPhotoMissing");
204         File dir = getTestOutputDir();
205         copyDir(_dir);
206         Album album = createAlbum(dir, "/", _cache);
207         assertEquals(0, album.size());
208     }
209
210     private InputStream getImage(File dir) throws IOException {
211         return new BufferedInputStream(new FileInputStream(dir));
212     }
213
214     /**
215      * Add a photo to the root album and verify it is added correctly and can be
216      * retrieved using a path.
217      */
218     @Test
219     public void testAddPhotoToRootAlbum() throws IOException {
220         File dir = new File(_testData.getRoot(), "AlbumNew");
221
222         assertTrue(dir.mkdir());
223         assertTrue(new File(dir, FileSystemAlbum.PHOTOS_DIR).mkdir());
224         assertTrue(new File(dir, FileSystemAlbum.THUMBNAILS_DIR).mkdir());
225
226         Album album = createAlbum(dir, "/", _cache);
227
228         File image = new File(getTestAlbumData(), "a.jpg");
229         album.addImage("xyz", getImage(image));
230         assertTrue(album.getEntry("/xyz") != null);
231
232         Album album2 = createAlbum(dir, "/", new ForeverCache<String, ArrayList<PhotoEntry>>());
233         assertTrue(album2.getEntry("/xyz") != null);
234     }
235
236     /**
237      * Add a photo twice, and verify that the second time an IO Exception occurs.
238      */
239     @Test
240     public void testDoublePhotoNotAllowed() throws IOException {
241         File dir = new File(_testData.getRoot(), "AlbumNew");
242
243         assertTrue(dir.mkdir());
244         assertTrue(new File(dir, FileSystemAlbum.PHOTOS_DIR).mkdir());
245         assertTrue(new File(dir, FileSystemAlbum.THUMBNAILS_DIR).mkdir());
246
247         Album album = createAlbum(dir, "/", _cache);
248
249         File image = new File(getTestAlbumData(), "a.jpg");
250         album.addImage("xyz", getImage(image));
251         assertTrue(album.getEntry("/xyz") != null);
252
253         try {
254             album.addImage("xyz", getImage(image));
255             fail();
256         }
257         catch (IOException e) {
258             return; // ok, test succeeded.
259         }
260     }
261
262     /**
263      * Adds an album and adds a photo to it. Verifies that they are added.
264      */
265     @Test
266     public void testAddAlbum() throws IOException {
267         File dir = new File(_testData.getRoot(), "AlbumNew");
268
269         assertTrue(dir.mkdir());
270         assertTrue(new File(dir, FileSystemAlbum.PHOTOS_DIR).mkdir());
271         assertTrue(new File(dir, FileSystemAlbum.THUMBNAILS_DIR).mkdir());
272
273         Album album = createAlbum(dir, "/", _cache);
274
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());
281
282         Album album2 = createAlbum(dir, "/", new ForeverCache<String, ArrayList<PhotoEntry>>());
283         PhotoEntry entry = album2.getEntry("/sub");
284
285         assertTrue(entry != null);
286         assertTrue(entry instanceof Album);
287     }
288
289     /**
290      * Tests that adding an album which already exists is not allowed.
291      */
292     @Test
293     public void testAddAlreadyExistingAlbum() throws IOException {
294         File dir = new File(_testData.getRoot(), "AlbumNew");
295
296         assertTrue(dir.mkdir());
297         assertTrue(new File(dir, FileSystemAlbum.PHOTOS_DIR).mkdir());
298         assertTrue(new File(dir, FileSystemAlbum.THUMBNAILS_DIR).mkdir());
299
300         Album album = createAlbum(dir, "/", _cache);
301
302         File sub = new File(dir, "sub");
303         album.addAlbum("sub");
304         assertTrue(album.getEntry("/sub") != null);
305         assertTrue(sub.isDirectory());
306
307         try {
308             album.addAlbum("sub");
309         }
310         catch (IOException e) {
311             return;  // ok.
312         }
313     }
314
315     /**
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.
319      */
320     @Test
321     public void testRemove() throws IOException {
322         File dir = getTestOutputDir();
323         File origDir = new File(getTestAlbumData(), "AlbumRemove");
324         copyDir(origDir);
325         Album album = createAlbum(dir, "/", _cache);
326         try {
327             album.removeEntry("Nested");
328         }
329         catch (IOException e) {
330             Album nested = (Album) album.getEntry("/Nested");
331             nested.removeEntry("mondeo");
332             album.removeEntry("Nested");
333             return;
334         }
335         fail();
336     }
337 }