View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * JUnit test case for DataSourceUrlResolver.
33   */
34  public class DataSourceUrlResolverTest extends AbstractDataSourceResolverTest {
35  
36      /**
37       * Shows how the DataSourceUrlResolver can resolve files as well but this should be done using a DataSourceFileResolver.
38       *
39       * @throws Exception the test failed
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       * Tests resolving resources over HTTP.
52       *
53       * @throws Exception the test failed
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       * Tests resolving resources over HTTP.
66       *
67       * @throws Exception the test failed
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       * Tests resolving resources over HTTP.
77       *
78       * @throws Exception the test failed
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  }