View Javadoc

1   /*
2    * Copyright 2012-2013 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 test.de.smartics.properties.spi.core.util;
17  
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.endsWith;
20  
21  import org.junit.Before;
22  import org.junit.Test;
23  
24  import de.smartics.properties.spi.core.util.ClassLoaderUtils;
25  import de.smartics.testdoc.annotations.Uut;
26  
27  /**
28   * Tests {@link ClassLoaderUtils}.
29   */
30  @Uut(type = ClassLoaderUtils.class)
31  public class ClassLoaderUtilsTest
32  {
33    // ********************************* Fields *********************************
34  
35    // --- constants ------------------------------------------------------------
36  
37    /**
38     * A reference to a resource that does exist on the class path.
39     */
40    private static final String EXISTING_RESOURCE =
41        "test/de/smartics/properties/spi/core/util/aresource.properties";
42  
43    /**
44     * A reference to a resource that does not exist on the class path.
45     */
46    private static final String NON_EXISTING_RESOURCE = "unknown-resource";
47  
48    // --- members --------------------------------------------------------------
49  
50    private ClassLoader loader;
51  
52    // ****************************** Inner Classes *****************************
53  
54    // ********************************* Methods ********************************
55  
56    // --- prepare --------------------------------------------------------------
57  
58    @Before
59    public void setUp()
60    {
61      this.loader = ClassLoaderUtilsTest.class.getClassLoader(); // NOPMD
62    }
63  
64    // --- helper ---------------------------------------------------------------
65  
66    // --- tests ----------------------------------------------------------------
67  
68    @Test
69    public void calculatesThePathToTheRoot()
70    {
71      final String root =
72          ClassLoaderUtils.calcFirstArchivePath(loader, EXISTING_RESOURCE);
73  
74      assertThat(root, endsWith("target/test-classes/"));
75    }
76  
77    @Test(expected = IllegalArgumentException.class)
78    public void signalsMissingResourceWithException()
79    {
80      ClassLoaderUtils.calcFirstArchivePath(loader, NON_EXISTING_RESOURCE);
81    }
82  }