View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.util;
15  
16  import static org.junit.Assert.assertEquals;
17  
18  import java.io.BufferedReader;
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.InputStreamReader;
23  import java.io.PrintWriter;
24  import java.net.MalformedURLException;
25  import java.net.URL;
26  
27  import org.junit.Test;
28  
29  import ch.qos.logback.core.util.LocationUtil;
30  
31  /**
32   * Unit tests for {@link LocationUtil}.
33   *
34   * @author Carl Harris
35   */
36  public class LocationUtilTest {
37  
38      private static final String TEST_CLASSPATH_RESOURCE = "util/testResource.txt";
39      private static final String TEST_PATTERN = "TEST RESOURCE";
40  
41      @Test
42      public void testImplicitClasspathUrl() throws Exception {
43          URL url = LocationUtil.urlForResource(TEST_CLASSPATH_RESOURCE);
44          validateResource(url);
45      }
46  
47      @Test
48      public void testExplicitClasspathUrl() throws Exception {
49          URL url = LocationUtil.urlForResource(LocationUtil.CLASSPATH_SCHEME + TEST_CLASSPATH_RESOURCE);
50          validateResource(url);
51      }
52  
53      @Test
54      public void testExplicitClasspathUrlWithLeadingSlash() throws Exception {
55          URL url = LocationUtil.urlForResource(LocationUtil.CLASSPATH_SCHEME + "/" + TEST_CLASSPATH_RESOURCE);
56          validateResource(url);
57      }
58  
59      @Test(expected = MalformedURLException.class)
60      public void testExplicitClasspathUrlEmptyPath() throws Exception {
61          LocationUtil.urlForResource(LocationUtil.CLASSPATH_SCHEME);
62      }
63  
64      @Test(expected = MalformedURLException.class)
65      public void testExplicitClasspathUrlWithRootPath() throws Exception {
66          LocationUtil.urlForResource(LocationUtil.CLASSPATH_SCHEME + "/");
67      }
68  
69      @Test
70      public void testFileUrl() throws Exception {
71          File file = File.createTempFile("testResource", ".txt");
72          file.deleteOnExit();
73          PrintWriter writer = new PrintWriter(file);
74          writer.println(TEST_PATTERN);
75          writer.close();
76          URL url = file.toURI().toURL();
77          validateResource(url);
78      }
79  
80      private void validateResource(URL url) throws IOException {
81          InputStream inputStream = url.openStream();
82          try {
83              BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
84              String line = reader.readLine();
85              assertEquals(TEST_PATTERN, line);
86          } finally {
87              try {
88                  inputStream.close();
89              } catch (IOException ex) {
90                  // ignore close exception
91                  ex.printStackTrace(System.err);
92              }
93          }
94      }
95  
96  }