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