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 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   * JUnit test case for DataSourceClassPathResolver..
29   *
30   * @since 1.3
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      // Start of Tests
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  }