001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.shim;
022:
023: import java.util.*;
024: import java.io.*;
025: import java.sql.*;
026: import junit.framework.*;
027: import org.apache.log4j.*;
028: import com.methodhead.persistable.*;
029: import com.methodhead.test.*;
030: import org.apache.commons.io.*;
031:
032: public class ThumbnailerTest extends TestCase {
033:
034: private Thumbnailer thumbnailer = null;
035: private File src = null;
036: private File dest = null;
037:
038: private File testDir_ = null;
039:
040: static {
041: TestUtils.initLogger();
042: TestUtils.setLogLevel(Level.DEBUG);
043: }
044:
045: public ThumbnailerTest(String name) {
046: super (name);
047: }
048:
049: protected void setUp() {
050: try {
051: testDir_ = new File("build/testdir");
052:
053: if (testDir_.exists())
054: FileUtils.deleteDirectory(testDir_);
055:
056: testDir_.mkdir();
057: } catch (Exception e) {
058: fail(e.getMessage());
059: }
060: }
061:
062: protected void tearDown() {
063: }
064:
065: public void testThumbnail() {
066: try {
067:
068: thumbnailer = new Thumbnailer();
069:
070: src = new File("support/test300x200.jpg");
071: dest = new File(testDir_, "test300x200jpg.jpg");
072: thumbnailer.thumbnail(src, dest);
073: assertTrue(dest.exists());
074: assertTrue(dest.isFile());
075:
076: src = new File("support/test300x1.jpg");
077: dest = new File(testDir_, "test300x1jpg.jpg");
078: thumbnailer.thumbnail(src, dest);
079: assertTrue(dest.exists());
080: assertTrue(dest.isFile());
081:
082: src = new File("support/test1x300.jpg");
083: dest = new File(testDir_, "test1x300jpg.jpg");
084: thumbnailer.thumbnail(src, dest);
085: assertTrue(dest.exists());
086: assertTrue(dest.isFile());
087:
088: src = new File("support/test15x10.jpg");
089: dest = new File(testDir_, "test15x10jpg.jpg");
090: thumbnailer.thumbnail(src, dest);
091: assertTrue(dest.exists());
092: assertTrue(dest.isFile());
093:
094: src = new File("support/test200x300.jpg");
095: dest = new File(testDir_, "test200x300jpg.jpg");
096: thumbnailer.thumbnail(src, dest);
097: assertTrue(dest.exists());
098: assertTrue(dest.isFile());
099:
100: src = new File("support/test300x200.gif");
101: dest = new File(testDir_, "test300x200gif.jpg");
102: thumbnailer.thumbnail(src, dest);
103: assertTrue(dest.exists());
104: assertTrue(dest.isFile());
105:
106: src = new File("support/test300x200transparent.gif");
107: dest = new File(testDir_, "test300x200transparentgif.jpg");
108: thumbnailer.thumbnail(src, dest);
109: assertTrue(dest.exists());
110: assertTrue(dest.isFile());
111:
112: src = new File("support/test300x200.png");
113: dest = new File(testDir_, "test300x200png.jpg");
114: thumbnailer.thumbnail(src, dest);
115: assertTrue(dest.exists());
116: assertTrue(dest.isFile());
117:
118: src = new File("support/test300x200transparent.png");
119: dest = new File(testDir_, "test300x200transparentpng.jpg");
120: thumbnailer.thumbnail(src, dest);
121: assertTrue(dest.exists());
122: assertTrue(dest.isFile());
123: } catch (Exception e) {
124: e.printStackTrace();
125: fail();
126: }
127: }
128:
129: public void testThumbnailBadImage() throws Exception {
130:
131: //
132: // thumbnailer should throw an IllegalArgumentException because it can't handle the bad file
133: //
134: try {
135: thumbnailer = new Thumbnailer();
136: src = new File("support/build.properties");
137: dest = new File(testDir_, "build.properties.jpg");
138: thumbnailer.thumbnail(src, dest);
139: fail("No exception thrown");
140: } catch (IllegalArgumentException e) {
141: // success
142: }
143: }
144:
145: public void testSyncDir() {
146: try {
147: //
148: // support dir includes badimage.jpg which should be ignored by syncDir()
149: //
150: thumbnailer = new Thumbnailer();
151: thumbnailer.syncDir(new File("support"), testDir_);
152:
153: dest = new File(testDir_, "test300x200.jpg.jpg");
154: assertTrue(dest.exists());
155:
156: dest = new File(testDir_, "test200x300.jpg.jpg");
157: assertTrue(dest.exists());
158:
159: dest = new File(testDir_, "test200x300.jpeg.jpg");
160: assertTrue(dest.exists());
161:
162: dest = new File(testDir_, "test300x200.gif.jpg");
163: assertTrue(dest.exists());
164:
165: dest = new File(testDir_, "test300x200.png.jpg");
166: assertTrue(dest.exists());
167:
168: //
169: // not much of a test, but badimage.jpg.jpg shouldn't get created
170: //
171: dest = new File(testDir_, "badimage.jpg.jpg");
172: assertTrue(!dest.exists());
173:
174: long lastModified = dest.lastModified();
175: Thread.sleep(1000);
176: thumbnailer.syncDir(new File("support"), testDir_);
177: assertEquals(lastModified, dest.lastModified());
178: } catch (Exception e) {
179: e.printStackTrace();
180: fail();
181: }
182: }
183: }
|