|
1 |
| package photospace.web; |
|
2 |
| |
|
3 |
| import java.io.*; |
|
4 |
| import java.net.*; |
|
5 |
| import javax.servlet.http.*; |
|
6 |
| import org.apache.commons.logging.*; |
|
7 |
| import junit.framework.*; |
|
8 |
| import photospace.vfs.*; |
|
9 |
| |
|
10 |
| public class ImageServletTest |
|
11 |
| extends TestCase |
|
12 |
| { |
|
13 |
| private static final Log log = LogFactory.getLog(ImageServletTest.class); |
|
14 |
| |
|
15 |
| File file; |
|
16 |
| String url; |
|
17 |
| |
|
18 |
0
| public void setUp() throws Exception
|
|
19 |
| { |
|
20 |
0
| FileSystem filesystem = new FileSystemImpl();
|
|
21 |
0
| filesystem.setRoot(new File("images/"));
|
|
22 |
| |
|
23 |
0
| file = filesystem.getFile("/2003/gps.jpg");
|
|
24 |
0
| url = "http://localhost:8080/photospace/image/2003/gps.jpg";
|
|
25 |
| |
|
26 |
0
| assertTrue(file.exists());
|
|
27 |
| } |
|
28 |
| |
|
29 |
0
| public void tearDown() throws Exception
|
|
30 |
| { |
|
31 |
| } |
|
32 |
| |
|
33 |
| |
|
34 |
0
| public void testDoGet() throws Exception
|
|
35 |
| { |
|
36 |
0
| HttpURLConnection connection = getConnection(url);
|
|
37 |
0
| connection.connect();
|
|
38 |
0
| assertEquals(HttpServletResponse.SC_OK, connection.getResponseCode());
|
|
39 |
0
| assertEquals("image/jpeg", connection.getContentType());
|
|
40 |
0
| assertEquals(file.lastModified(), connection.getLastModified(), 999);
|
|
41 |
0
| assertTrue(connection.getExpiration() > file.lastModified());
|
|
42 |
0
| assertEquals(connection.getContentLength(), getBytes(connection.getInputStream()).length);
|
|
43 |
| } |
|
44 |
| |
|
45 |
0
| public void testDoGetThumbnail() throws Exception
|
|
46 |
| { |
|
47 |
0
| HttpURLConnection connection = getConnection(url + "?w=100&h=100");
|
|
48 |
0
| connection.connect();
|
|
49 |
0
| assertEquals(HttpServletResponse.SC_OK, connection.getResponseCode());
|
|
50 |
0
| assertEquals("image/jpeg", connection.getContentType());
|
|
51 |
0
| assertEquals(file.lastModified(), connection.getLastModified(), 999);
|
|
52 |
0
| assertEquals(connection.getContentLength(), getBytes(connection.getInputStream()).length);
|
|
53 |
0
| assertTrue(file.length() > connection.getContentLength());
|
|
54 |
| } |
|
55 |
| |
|
56 |
0
| public void testIfModifiedSince() throws Exception
|
|
57 |
| { |
|
58 |
0
| HttpURLConnection connection = getConnection(url);
|
|
59 |
0
| connection.setIfModifiedSince(file.lastModified());
|
|
60 |
0
| connection.connect();
|
|
61 |
0
| assertEquals(HttpServletResponse.SC_NOT_MODIFIED, connection.getResponseCode());
|
|
62 |
| } |
|
63 |
| |
|
64 |
0
| private HttpURLConnection getConnection(String url) throws IOException
|
|
65 |
| { |
|
66 |
0
| HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
|
67 |
0
| connection.setUseCaches(false);
|
|
68 |
0
| return connection;
|
|
69 |
| } |
|
70 |
| |
|
71 |
0
| private byte [] getBytes(InputStream in) throws IOException
|
|
72 |
| { |
|
73 |
0
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
74 |
0
| ImageServlet.write(in, out);
|
|
75 |
0
| return out.toByteArray();
|
|
76 |
| } |
|
77 |
| |
|
78 |
| } |