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.classpath;
17  
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.hasItems;
20  import static org.hamcrest.Matchers.is;
21  
22  import help.de.smartics.properties.core.SimpleProperties;
23  
24  import java.io.IOException;
25  import java.net.URL;
26  import java.util.ArrayList;
27  import java.util.Arrays;
28  import java.util.HashSet;
29  import java.util.Set;
30  
31  import org.hamcrest.Matcher;
32  import org.junit.Before;
33  import org.junit.Test;
34  import org.junit.experimental.categories.Category;
35  
36  import de.smartics.properties.spi.core.classpath.PropertiesFilesLoader;
37  import de.smartics.testdoc.annotations.Uut;
38  import de.smartics.testdoc.categories.Technical;
39  import de.smartics.testdoc.categories.type.Coverage;
40  
41  /**
42   * Tests {@link PropertiesFilesLoader}.
43   */
44  public class PropertiesFilesLoaderTest
45  {
46    // ********************************* Fields *********************************
47  
48    // --- constants ------------------------------------------------------------
49  
50    // --- members --------------------------------------------------------------
51  
52    @Uut
53    private PropertiesFilesLoader uut;
54  
55    // ****************************** Inner Classes *****************************
56  
57    // ********************************* Methods ********************************
58  
59    // --- prepare --------------------------------------------------------------
60  
61    @Before
62    public void setUp()
63    {
64      uut = new PropertiesFilesLoader();
65    }
66  
67    // --- helper ---------------------------------------------------------------
68  
69    // --- tests ----------------------------------------------------------------
70  
71    @Test
72    @Category(Technical.class)
73    public void allowsNullAsCollectionOfClassPathRoots()
74    {
75      final Set<String> propertiesFiles = uut.getPropertiesFiles(null);
76      assertThat(propertiesFiles.isEmpty(), is(true));
77    }
78  
79    @Test
80    @Category(Technical.class)
81    public void allowsAnEmptyCollectionOfClassPathRoots()
82    {
83      final Set<String> propertiesFiles =
84          uut.getPropertiesFiles(new HashSet<URL>());
85      assertThat(propertiesFiles.isEmpty(), is(true));
86    }
87  
88    @Test
89    @Category(Technical.class)
90    @SuppressWarnings({ "unchecked", "rawtypes" })
91    public void returnsOnlyPropertiesFiles() throws IOException
92    {
93      final String propertiesFileInClassPath =
94          "example/de/smartics/properties/simple/configuration.properties";
95  
96      final ClassLoader loader = SimpleProperties.class.getClassLoader(); // NOPMD
97      final URL url = loader.getResource("");
98      final Set<String> propertiesFiles =
99          uut.getPropertiesFiles(Arrays.asList(url));
100     assertThat(propertiesFiles.isEmpty(), is(false));
101     assertThat(propertiesFiles, (Matcher) hasItems(propertiesFileInClassPath));
102   }
103 
104   @Test
105   @Category(Technical.class)
106   @SuppressWarnings({ "unchecked", "rawtypes" })
107   public void returnsBootPropertiesFile() throws IOException
108   {
109     final String bootPropertiesInClassPath =
110         "META-INF/smartics-properties/boot.properties";
111 
112     final ClassLoader loader = SimpleProperties.class.getClassLoader(); // NOPMD
113     final URL url = loader.getResource("example/de/smartics/properties/boot");
114     final Set<String> propertiesFiles =
115         uut.getBootPropertiesFiles(Arrays.asList(url));
116     assertThat(propertiesFiles.isEmpty(), is(false));
117     assertThat(propertiesFiles, (Matcher) hasItems(bootPropertiesInClassPath));
118   }
119 
120   @Test
121   @Category(Technical.class)
122   public void returnsNoBootPropertiesFileIfNoneIsProvided() throws IOException
123   {
124     final ClassLoader loader = SimpleProperties.class.getClassLoader(); // NOPMD
125     final URL url = loader.getResource("example/de/smartics/properties/simple");
126     final Set<String> propertiesFiles =
127         uut.getBootPropertiesFiles(Arrays.asList(url));
128     assertThat(propertiesFiles.isEmpty(), is(true));
129   }
130 
131   @Test
132   @Category(Coverage.class)
133   public void fetchingBootPropertiesAllowsUrlsToBeNull() throws IOException
134   {
135     final Set<String> propertiesFiles = uut.getBootPropertiesFiles(null);
136     assertThat(propertiesFiles.isEmpty(), is(true));
137   }
138 
139   @Test
140   @Category(Coverage.class)
141   public void fetchingBootPropertiesAllowsUrlsToBeEmpty() throws IOException
142   {
143     final Set<String> propertiesFiles =
144         uut.getBootPropertiesFiles(new ArrayList<URL>(0));
145     assertThat(propertiesFiles.isEmpty(), is(true));
146   }
147 }