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.Before;
23  import org.junit.Test;
24  
25  import java.io.IOException;
26  import java.net.URL;
27  
28  /**
29   * JUnit test case for DateSourceResolver.
30   *
31   * @since 1.3
32   */
33  public class DataSourceCompositeResolverTest extends AbstractDataSourceResolverTest
34  {
35  
36      private DataSourceResolver[] dataSourceResolvers;
37  
38      @Before
39      public void setUp() throws Exception
40      {
41          DataSourceUrlResolver urlResolver = new DataSourceUrlResolver(new URL("http://www.apache.org"), false);
42          DataSourceClassPathResolver classPathResolver = new DataSourceClassPathResolver("/images", false);
43          dataSourceResolvers = new DataSourceResolver[] { urlResolver, classPathResolver };
44      }
45  
46      @Test
47      public void testResolvingFilesLenient() throws Exception
48      {
49          DataSourceResolver dataSourceResolver = new DataSourceCompositeResolver(dataSourceResolvers, true);
50  
51          // resolve using HTTP
52          assertTrue(toByteArray(dataSourceResolver.resolve("/images/feather-small.gif")).length > 0);
53  
54          // resolve using class path
55          assertTrue(toByteArray(dataSourceResolver.resolve("/contentTypeTest.gif")).length > 0);
56      }
57  
58      @Test(expected = IOException.class)
59      public void testResolvingFilesNonLenient() throws Exception
60      {
61          DataSourceResolver dataSourceResolver = new DataSourceCompositeResolver(dataSourceResolvers, false);
62  
63          dataSourceResolver.resolve("./image/does-not-exist.gif");
64      }
65  
66      @Test
67      public void testExternalModification() throws Exception
68      {
69          DataSourceCompositeResolver dataSourceResolver = new DataSourceCompositeResolver(dataSourceResolvers, true);
70  
71          DataSourceResolver[] arr = dataSourceResolver.getDataSourceResolvers();
72          
73          // modify an element in the returned array
74          arr[0] = null;
75          
76          DataSourceResolver[] arr2 = dataSourceResolver.getDataSourceResolvers();
77          
78          // assert that the external modification is not propagated to the internal array
79          assertNotNull(arr2[0]);        
80      }
81  
82  }