1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.mail.resolver;
18
19 import junit.framework.TestCase;
20 import org.apache.commons.io.IOUtils;
21 import org.apache.commons.mail.DataSourceResolver;
22
23 import javax.activation.DataSource;
24 import java.io.IOException;
25 import java.io.InputStream;
26
27
28
29
30
31
32 public class DataSourceClassPathResolverTest extends TestCase
33 {
34 private final int IMG_SIZE = 5866;
35
36 public DataSourceClassPathResolverTest(String name)
37 {
38 super(name);
39 }
40
41
42
43
44
45 public void testResolvingClassPathLenient() throws Exception
46 {
47 DataSourceResolver dataSourceResolver;
48
49 dataSourceResolver = new DataSourceClassPathResolver("/", true);
50 assertTrue(toByteArray(dataSourceResolver.resolve("images/asf_logo_wide.gif")).length == IMG_SIZE);
51 assertTrue(toByteArray(dataSourceResolver.resolve("./images/asf_logo_wide.gif")).length == IMG_SIZE);
52 assertTrue(toByteArray(dataSourceResolver.resolve("/images/asf_logo_wide.gif")).length == IMG_SIZE);
53 assertNull(dataSourceResolver.resolve("/asf_logo_wide.gif"));
54
55 dataSourceResolver = new DataSourceClassPathResolver("/images", true);
56 assertTrue(toByteArray(dataSourceResolver.resolve("asf_logo_wide.gif")).length == IMG_SIZE);
57 assertTrue(toByteArray(dataSourceResolver.resolve("./asf_logo_wide.gif")).length == IMG_SIZE);
58 assertTrue(toByteArray(dataSourceResolver.resolve("/asf_logo_wide.gif")).length == IMG_SIZE);
59 assertNull(dataSourceResolver.resolve("./images/asf_logo_wide.gif"));
60 }
61
62 public void testResolvingClassPathNonLenient() throws Exception
63 {
64 DataSourceResolver dataSourceResolver;
65
66 dataSourceResolver = new DataSourceClassPathResolver("/", false);
67 assertNotNull(dataSourceResolver.resolve("images/asf_logo_wide.gif"));
68
69 try
70 {
71 dataSourceResolver.resolve("asf_logo_wide.gif");
72 fail("Expecting an IOException");
73 }
74 catch(IOException e)
75 {
76 return;
77 }
78 }
79
80 private byte[] toByteArray(DataSource dataSource) throws IOException
81 {
82 if(dataSource != null)
83 {
84 InputStream is = dataSource.getInputStream();
85 return IOUtils.toByteArray(is);
86 }
87 else
88 {
89 return null;
90 }
91 }
92
93 }