/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.mail.resolver;

import static org.junit.Assert.*;

import org.apache.commons.mail.DataSourceResolver;
import org.junit.Test;

import java.io.IOException;
import java.net.URL;

/**
 * JUnit test case for DateSourceResolver.
 *
 * @since 1.3
 */
public class DataSourceCompositeResolverTest extends AbstractDataSourceResolverTest
{
    @Test
    public void testResolvingFilesLenient() throws Exception
    {
        DataSourceResolver urlResolver = new DataSourceUrlResolver(new URL("http://www.apache.org"), false);
        DataSourceResolver classPathResolver = new DataSourceClassPathResolver("/images", false);
        DataSourceResolver[] dataSourceResolvers = new DataSourceResolver[] { urlResolver, classPathResolver };
        DataSourceResolver dataSourceResolver = new DataSourceCompositeResolver(dataSourceResolvers, true);

        // resolve using HTTP
        assertTrue(toByteArray(dataSourceResolver.resolve("/images/feather-small.gif")).length > 0);

        // resolve using class path
        assertTrue(toByteArray(dataSourceResolver.resolve("/contentTypeTest.gif")).length > 0);
    }

    @Test
    public void testResolvingFilesNonLenient() throws Exception
    {
        DataSourceResolver urlResolver = new DataSourceUrlResolver(new URL("http://www.apache.org"), false);
        DataSourceResolver classPathResolver = new DataSourceClassPathResolver("/images", false);
        DataSourceResolver[] dataSourceResolvers = new DataSourceResolver[] { urlResolver, classPathResolver };
        DataSourceResolver dataSourceResolver = new DataSourceCompositeResolver(dataSourceResolvers, false);

        try
        {
            dataSourceResolver.resolve("./image/does-not-exist.gif");
            fail("Expected an IOException");
        }
        catch(IOException e)
        {
            // expected
            return;
        }
    }

    @Test
    public void testExternalModification() throws Exception
    {
        DataSourceResolver urlResolver = new DataSourceUrlResolver(new URL("http://www.apache.org"), false);
        DataSourceResolver classPathResolver = new DataSourceClassPathResolver("/images", false);
        DataSourceResolver[] dataSourceResolvers = new DataSourceResolver[] { urlResolver, classPathResolver };
        DataSourceCompositeResolver dataSourceResolver = new DataSourceCompositeResolver(dataSourceResolvers, true);

        DataSourceResolver[] arr = dataSourceResolver.getDataSourceResolvers();
        
        // modify an element in the returned array
        arr[0] = null;
        
        DataSourceResolver[] arr2 = dataSourceResolver.getDataSourceResolvers();
        
        // assert that the external modification is not propagated to the internal array
        assertNotNull(arr2[0]);        
    }

}

@@ -924,16 +924,41 @@
 solver;%0A
+import org.junit.Before;%0A
 import o
@@ -1181,59 +1181,97 @@
 t%0A%7B%0A
+%0A
     
-@Test%0A    public void testResolvingFilesLenient
+private DataSourceResolver%5B%5D dataSourceResolvers;%0A%0A    @Before%0A    public void setUp
 () t
@@ -1302,32 +1302,35 @@
       DataSource
+Url
 Resolver urlReso
@@ -1414,32 +1414,41 @@
       DataSource
+ClassPath
 Resolver classPa
@@ -1518,37 +1518,16 @@
 %0A       
- DataSourceResolver%5B%5D
  dataSou
@@ -1594,32 +1594,116 @@
 PathResolver %7D;%0A
+    %7D%0A%0A    @Test%0A    public void testResolvingFilesLenient() throws Exception%0A    %7B%0A
         DataSour
@@ -2150,327 +2150,8 @@
   %7B%0A
-        DataSourceResolver urlResolver = new DataSourceUrlResolver(new URL(%22http://www.apache.org%22), false);%0A        DataSourceResolver classPathResolver = new DataSourceClassPathResolver(%22/images%22, false);%0A        DataSourceResolver%5B%5D dataSourceResolvers = new DataSourceResolver%5B%5D %7B urlResolver, classPathResolver %7D;%0A
     
@@ -2583,327 +2583,8 @@
   %7B%0A
-        DataSourceResolver urlResolver = new DataSourceUrlResolver(new URL(%22http://www.apache.org%22), false);%0A        DataSourceResolver classPathResolver = new DataSourceClassPathResolver(%22/images%22, false);%0A        DataSourceResolver%5B%5D dataSourceResolvers = new DataSourceResolver%5B%5D %7B urlResolver, classPathResolver %7D;%0A
     

Move initialization to set up method