001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.util;
015
016import static org.junit.Assert.assertEquals;
017
018import java.io.BufferedReader;
019import java.io.File;
020import java.io.IOException;
021import java.io.InputStream;
022import java.io.InputStreamReader;
023import java.io.PrintWriter;
024import java.net.MalformedURLException;
025import java.net.URL;
026
027import org.junit.Test;
028
029import ch.qos.logback.core.util.LocationUtil;
030
031/**
032 * Unit tests for {@link LocationUtil}.
033 *
034 * @author Carl Harris
035 */
036public class LocationUtilTest {
037
038    private static final String TEST_CLASSPATH_RESOURCE = "util/testResource.txt";
039    private static final String TEST_PATTERN = "TEST RESOURCE";
040
041    @Test
042    public void testImplicitClasspathUrl() throws Exception {
043        URL url = LocationUtil.urlForResource(TEST_CLASSPATH_RESOURCE);
044        validateResource(url);
045    }
046
047    @Test
048    public void testExplicitClasspathUrl() throws Exception {
049        URL url = LocationUtil.urlForResource(LocationUtil.CLASSPATH_SCHEME + TEST_CLASSPATH_RESOURCE);
050        validateResource(url);
051    }
052
053    @Test
054    public void testExplicitClasspathUrlWithLeadingSlash() throws Exception {
055        URL url = LocationUtil.urlForResource(LocationUtil.CLASSPATH_SCHEME + "/" + TEST_CLASSPATH_RESOURCE);
056        validateResource(url);
057    }
058
059    @Test(expected = MalformedURLException.class)
060    public void testExplicitClasspathUrlEmptyPath() throws Exception {
061        LocationUtil.urlForResource(LocationUtil.CLASSPATH_SCHEME);
062    }
063
064    @Test(expected = MalformedURLException.class)
065    public void testExplicitClasspathUrlWithRootPath() throws Exception {
066        LocationUtil.urlForResource(LocationUtil.CLASSPATH_SCHEME + "/");
067    }
068
069    @Test
070    public void testFileUrl() throws Exception {
071        File file = File.createTempFile("testResource", ".txt");
072        file.deleteOnExit();
073        PrintWriter writer = new PrintWriter(file);
074        writer.println(TEST_PATTERN);
075        writer.close();
076        URL url = file.toURI().toURL();
077        validateResource(url);
078    }
079
080    private void validateResource(URL url) throws IOException {
081        InputStream inputStream = url.openStream();
082        try {
083            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
084            String line = reader.readLine();
085            assertEquals(TEST_PATTERN, line);
086        } finally {
087            try {
088                inputStream.close();
089            } catch (IOException ex) {
090                // ignore close exception
091                ex.printStackTrace(System.err);
092            }
093        }
094    }
095
096}