1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.mail2.javax.resolver;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNull;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.net.URL;
27
28 import org.apache.commons.mail2.javax.DataSourceResolver;
29 import org.junit.jupiter.api.Test;
30
31
32
33
34 public class DataSourceUrlResolverTest extends AbstractDataSourceResolverTest {
35
36
37
38
39
40
41 @Test
42 public void testResolvingFilesLenient() throws Exception {
43 final DataSourceResolver dataSourceResolver = new DataSourceUrlResolver(new File("./src/test/resources").toURI().toURL(), true);
44 assertEquals(IMG_SIZE, toByteArray(dataSourceResolver.resolve("images/asf_logo_wide.gif")).length);
45 assertEquals(IMG_SIZE, toByteArray(dataSourceResolver.resolve("./images/asf_logo_wide.gif")).length);
46 assertNull(dataSourceResolver.resolve("./images/does-not-exist.gif"));
47 assertNull(dataSourceResolver.resolve("/images/asf_logo_wide.gif"));
48 }
49
50
51
52
53
54
55 @Test
56 public void testResolvingHttpLenient() throws Exception {
57 final DataSourceResolver dataSourceResolver = new DataSourceUrlResolver(new URL("https://www.apache.org"), true);
58 assertTrue(toByteArray(dataSourceResolver.resolve("https://www.apache.org/images/feather-small.gif")).length > 1);
59 assertTrue(toByteArray(dataSourceResolver.resolve("images/feather-small.gif")).length > 1);
60 assertTrue(toByteArray(dataSourceResolver.resolve("./images/feather-small.gif")).length > 1);
61 assertTrue(toByteArray(dataSourceResolver.resolve("/images/feather-small.gif")).length > 1);
62 }
63
64
65
66
67
68
69 @Test
70 public void testResolvingHttpLenientHost() throws Exception {
71 final DataSourceResolver dataSourceResolver = new DataSourceUrlResolver(new URL("http://does.not.exist"), true);
72 assertNull(toByteArray(dataSourceResolver.resolve("/images/does-not-exist.gif")));
73 }
74
75
76
77
78
79
80 @Test
81 public void testResolvingHttpNonLenient() throws Exception {
82 final DataSourceResolver dataSourceResolver = new DataSourceUrlResolver(new URL("http://does.not.exist"), false);
83 assertThrows(IOException.class, () -> dataSourceResolver.resolve("images/asf_logo_wide.gif"));
84 assertThrows(IOException.class, () -> dataSourceResolver.resolve("images/does-not-exist.gif"));
85 }
86
87 }