View Javadoc

1   /*
2    * Copyright 2011-2012 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.util.lang;
17  
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.allOf;
20  import static org.hamcrest.Matchers.equalTo;
21  import static org.hamcrest.Matchers.hasItem;
22  import static org.hamcrest.Matchers.is;
23  
24  import java.net.URL;
25  import java.net.URLClassLoader;
26  import java.util.List;
27  
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  import de.smartics.testdoc.annotations.Uut;
32  import de.smartics.testdoc.categories.type.Coverage;
33  
34  /**
35   * Tests {@link ClassPathDirectoryListing}.
36   */
37  @Uut(type = ClassPathDirectoryListing.class)
38  @SuppressWarnings("unchecked")
39  public class ClassPathDirectoryListingTest
40  {
41    // ********************************* Fields *********************************
42  
43    // --- constants ------------------------------------------------------------
44  
45    // --- members --------------------------------------------------------------
46  
47    // ****************************** Inner Classes *****************************
48  
49    // ********************************* Methods ********************************
50  
51    // --- prepare --------------------------------------------------------------
52  
53    // --- helper ---------------------------------------------------------------
54  
55    // --- tests ----------------------------------------------------------------
56  
57    @Test
58    public void readsFilesFromTheFileSystem()
59    {
60      final ClassLoader loader = getClass().getClassLoader(); // NOPMD
61      final ClassPathDirectoryListing uut = new ClassPathDirectoryListing(loader);
62      final String resourcePath = "de/smartics/util/lang/testdir";
63  
64      final List<String> listing = uut.list(resourcePath);
65      assertThat(listing.size(), is(equalTo(3)));
66      assertThat(listing,
67          allOf(hasItem("subdir"), hasItem("file-1.txt"), hasItem("file-2.txt")));
68    }
69  
70    @Test
71    public void readsFilesFromAJarArchive()
72    {
73      final URL jarUrl = getClass().getResource("test-archive.jar");
74      final ClassLoader loader = new URLClassLoader(new URL[] { jarUrl }, null);
75  
76      final ClassPathDirectoryListing uut = new ClassPathDirectoryListing(loader);
77      final String resourcePath = "testdir";
78  
79      final List<String> listing = uut.list(resourcePath);
80      assertThat(listing.size(), is(equalTo(3)));
81      assertThat(listing,
82          allOf(hasItem("subdir"), hasItem("file-1.txt"), hasItem("file-2.txt")));
83    }
84  
85    @Test
86    public void readsFromJarRoot()
87    {
88      final URL jarUrl = getClass().getResource("test-archive.jar");
89      final ClassLoader loader = new URLClassLoader(new URL[] { jarUrl });
90  
91      final ClassPathDirectoryListing uut = new ClassPathDirectoryListing(loader);
92      final String resourcePath = "";
93  
94      final List<String> listing = uut.list(resourcePath);
95      assertThat(listing.size(), is(equalTo(1)));
96      assertThat(listing, allOf(hasItem("de")));
97    }
98  
99    @Test(expected = NullPointerException.class)
100   @Category(Coverage.class)
101   public void doesNotAllowNullAsResourcePath()
102   {
103     final ClassLoader loader = getClass().getClassLoader(); // NOPMD
104     final ClassPathDirectoryListing uut = new ClassPathDirectoryListing(loader);
105     final String resourcePath = null;
106 
107     uut.list(resourcePath);
108   }
109 
110   @Test(expected = IllegalArgumentException.class)
111   @Category(Coverage.class)
112   public void throwsIllegalArgumentExceptionIfResourceNotFoundOnClassPath()
113   {
114     final ClassLoader loader = getClass().getClassLoader(); // NOPMD
115     final ClassPathDirectoryListing uut = new ClassPathDirectoryListing(loader);
116     final String resourcePath = "unknown";
117 
118     uut.list(resourcePath);
119   }
120 }