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.mail.resolver;
18  
19  import static org.junit.Assert.*;
20  
21  import org.apache.commons.mail.DataSourceResolver;
22  import org.junit.Test;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.net.URL;
27  
28  /**
29   * JUnit test case for DataSourceUrlResolver.
30   *
31   * @since 1.3
32   */
33  public class DataSourceUrlResolverTest extends AbstractDataSourceResolverTest
34  {
35  
36      /**
37       * Shows how the DataSourceUrlResolver can resolve files as well but this should
38       * be done using a DataSourceFileResolver.
39       *
40       * @throws Exception the test failed
41       */
42      @Test
43      public void testResolvingFilesLenient() throws Exception
44      {
45          DataSourceResolver dataSourceResolver = new DataSourceUrlResolver(new File("./src/test/resources").toURI().toURL(), true);
46          assertTrue(toByteArray(dataSourceResolver.resolve("images/asf_logo_wide.gif")).length == IMG_SIZE);
47          assertTrue(toByteArray(dataSourceResolver.resolve("./images/asf_logo_wide.gif")).length == IMG_SIZE);
48          assertNull(dataSourceResolver.resolve("./images/does-not-exist.gif"));
49          assertNull(dataSourceResolver.resolve("/images/asf_logo_wide.gif"));
50      }
51  
52      /**
53       * Tests resolving resources over HTTP.
54       *
55       * @throws Exception the test failed
56       */
57      @Test
58      public void testResolvingHttpLenient() throws Exception
59      {
60          DataSourceResolver dataSourceResolver = new DataSourceUrlResolver(new URL("http://www.apache.org"), true);
61          assertTrue(toByteArray(dataSourceResolver.resolve("http://www.apache.org/images/feather-small.gif")).length > 1);
62          assertTrue(toByteArray(dataSourceResolver.resolve("images/feather-small.gif")).length > 1);
63          assertTrue(toByteArray(dataSourceResolver.resolve("./images/feather-small.gif")).length > 1);
64          assertTrue(toByteArray(dataSourceResolver.resolve("/images/feather-small.gif")).length > 1);
65          assertNull(toByteArray(dataSourceResolver.resolve("/images/does-not-exist.gif")));
66      }
67  
68      /**
69       * Tests resolving resources over HTTP.
70       *
71       * @throws Exception the test failed
72       */
73      @Test(expected = IOException.class)
74      public void testResolvingHttpNonLenient() throws Exception
75      {
76          DataSourceResolver dataSourceResolver = new DataSourceUrlResolver(new URL("http://www.apache.org"), false);
77          assertNotNull(dataSourceResolver.resolve("images/asf_logo_wide.gif"));
78  
79          dataSourceResolver.resolve("images/does-not-exist.gif");
80      }
81  
82  }