KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > shim > ThumbnailerTest


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.shim;
22
23 import java.util.*;
24 import java.io.*;
25 import java.sql.*;
26 import junit.framework.*;
27 import org.apache.log4j.*;
28 import com.methodhead.persistable.*;
29 import com.methodhead.test.*;
30 import org.apache.commons.io.*;
31
32 public class ThumbnailerTest extends TestCase {
33
34   private Thumbnailer thumbnailer = null;
35   private File src = null;
36   private File dest = null;
37
38   private File testDir_ = null;
39
40   static {
41     TestUtils.initLogger();
42     TestUtils.setLogLevel( Level.DEBUG );
43   }
44
45   public ThumbnailerTest( String JavaDoc name ) {
46     super( name );
47   }
48
49   protected void setUp() {
50     try {
51       testDir_ = new File( "build/testdir" );
52
53       if ( testDir_.exists() )
54         FileUtils.deleteDirectory( testDir_ );
55
56       testDir_.mkdir();
57     }
58     catch ( Exception JavaDoc e ) {
59       fail( e.getMessage() );
60     }
61   }
62
63   protected void tearDown() {
64   }
65
66   public void testThumbnail() {
67     try {
68
69       thumbnailer = new Thumbnailer();
70
71       src = new File( "support/test300x200.jpg" );
72       dest = new File( testDir_, "test300x200jpg.jpg" );
73       thumbnailer.thumbnail( src, dest );
74       assertTrue( dest.exists() );
75       assertTrue( dest.isFile() );
76
77       src = new File( "support/test300x1.jpg" );
78       dest = new File( testDir_, "test300x1jpg.jpg" );
79       thumbnailer.thumbnail( src, dest );
80       assertTrue( dest.exists() );
81       assertTrue( dest.isFile() );
82
83       src = new File( "support/test1x300.jpg" );
84       dest = new File( testDir_, "test1x300jpg.jpg" );
85       thumbnailer.thumbnail( src, dest );
86       assertTrue( dest.exists() );
87       assertTrue( dest.isFile() );
88
89       src = new File( "support/test15x10.jpg" );
90       dest = new File( testDir_, "test15x10jpg.jpg" );
91       thumbnailer.thumbnail( src, dest );
92       assertTrue( dest.exists() );
93       assertTrue( dest.isFile() );
94
95       src = new File( "support/test200x300.jpg" );
96       dest = new File( testDir_, "test200x300jpg.jpg" );
97       thumbnailer.thumbnail( src, dest );
98       assertTrue( dest.exists() );
99       assertTrue( dest.isFile() );
100
101       src = new File( "support/test300x200.gif" );
102       dest = new File( testDir_, "test300x200gif.jpg" );
103       thumbnailer.thumbnail( src, dest );
104       assertTrue( dest.exists() );
105       assertTrue( dest.isFile() );
106
107       src = new File( "support/test300x200transparent.gif" );
108       dest = new File( testDir_, "test300x200transparentgif.jpg" );
109       thumbnailer.thumbnail( src, dest );
110       assertTrue( dest.exists() );
111       assertTrue( dest.isFile() );
112
113       src = new File( "support/test300x200.png" );
114       dest = new File( testDir_, "test300x200png.jpg" );
115       thumbnailer.thumbnail( src, dest );
116       assertTrue( dest.exists() );
117       assertTrue( dest.isFile() );
118
119       src = new File( "support/test300x200transparent.png" );
120       dest = new File( testDir_, "test300x200transparentpng.jpg" );
121       thumbnailer.thumbnail( src, dest );
122       assertTrue( dest.exists() );
123       assertTrue( dest.isFile() );
124     }
125     catch ( Exception JavaDoc e ) {
126       e.printStackTrace();
127       fail();
128     }
129   }
130
131   public void testThumbnailBadImage() throws Exception JavaDoc {
132
133     //
134
// thumbnailer should throw an IllegalArgumentException because it can't handle the bad file
135
//
136
try {
137       thumbnailer = new Thumbnailer();
138       src = new File( "support/build.properties" );
139       dest = new File( testDir_, "build.properties.jpg" );
140       thumbnailer.thumbnail( src, dest );
141       fail( "No exception thrown" );
142     }
143     catch ( IllegalArgumentException JavaDoc e ) {
144       // success
145
}
146   }
147
148   public void testSyncDir() {
149     try {
150       //
151
// support dir includes badimage.jpg which should be ignored by syncDir()
152
//
153
thumbnailer = new Thumbnailer();
154       thumbnailer.syncDir( new File( "support" ), testDir_ );
155
156       dest = new File( testDir_, "test300x200.jpg.jpg" );
157       assertTrue( dest.exists() );
158
159       dest = new File( testDir_, "test200x300.jpg.jpg" );
160       assertTrue( dest.exists() );
161
162       dest = new File( testDir_, "test200x300.jpeg.jpg" );
163       assertTrue( dest.exists() );
164
165       dest = new File( testDir_, "test300x200.gif.jpg" );
166       assertTrue( dest.exists() );
167
168       dest = new File( testDir_, "test300x200.png.jpg" );
169       assertTrue( dest.exists() );
170
171       //
172
// not much of a test, but badimage.jpg.jpg shouldn't get created
173
//
174
dest = new File( testDir_, "badimage.jpg.jpg" );
175       assertTrue( !dest.exists() );
176
177       long lastModified = dest.lastModified();
178       Thread.sleep( 1000 );
179       thumbnailer.syncDir( new File( "support" ), testDir_ );
180       assertEquals( lastModified, dest.lastModified() );
181     }
182     catch ( Exception JavaDoc e ) {
183       e.printStackTrace();
184       fail();
185     }
186   }
187 }
188
Popular Tags