001/*
002 * ModeShape (http://www.modeshape.org)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.modeshape.common.util;
017
018import static org.junit.Assert.assertNotNull;
019import static org.junit.Assert.assertNull;
020import org.junit.Before;
021import org.junit.Test;
022import java.net.URL;
023import java.util.Arrays;
024import java.util.Collections;
025
026/**
027 * Unit test for {@link StringURLClassLoader}
028 *
029 * @author Horia Chiorean
030 */
031public class StringURLClassLoaderTest {
032
033    private static final String RESOURCE_PATH = StringURLClassLoaderTest.class.getPackage().getName().replaceAll("\\.", "/") + "/additionalmime.types";
034    private static final URL RESOURCE_URL = StringURLClassLoaderTest.class.getClassLoader().getResource(RESOURCE_PATH);
035
036    @Before
037    public void setUp() throws Exception {
038        assertNotNull(RESOURCE_URL);
039    }
040
041    @Test
042    public void shouldNotResolveURLsIfEmptyListProvided() throws Exception {
043        StringURLClassLoader classLoader = new StringURLClassLoader(Collections.<String>emptyList());
044        assertNull(classLoader.getResource(RESOURCE_PATH));
045    }
046
047    @Test(expected = IllegalArgumentException.class)
048    public void shouldNotAllowNullArgument() throws Exception {
049        new StringURLClassLoader(null);
050    }
051
052    @Test
053    public void shouldResolveResourceIfURLProvided() throws Exception {
054        String folderUrl = RESOURCE_URL.toString().substring(0, RESOURCE_URL.toString().indexOf(RESOURCE_PATH));
055        StringURLClassLoader classLoader = new StringURLClassLoader(Arrays.asList("invalidURL", folderUrl));
056        assertNotNull(classLoader.getResource(RESOURCE_PATH));
057    }
058}