View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.properties;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  import static org.junit.jupiter.params.provider.Arguments.arguments;
23  
24  import java.io.BufferedReader;
25  import java.io.FileInputStream;
26  import java.nio.file.Files;
27  import java.nio.file.Paths;
28  import java.util.Properties;
29  import java.util.stream.Stream;
30  
31  import org.apache.commons.collections4.BulkTest;
32  import org.junit.jupiter.api.Assumptions;
33  import org.junit.jupiter.api.Test;
34  import org.junit.jupiter.params.ParameterizedTest;
35  import org.junit.jupiter.params.provider.Arguments;
36  import org.junit.jupiter.params.provider.MethodSource;
37  
38  public abstract class AbstractPropertiesFactoryTest<T extends Properties> {
39  
40      public static Stream<Arguments> getParameters() {
41          return Stream.of(
42                  arguments(".properties"),
43                  arguments(".xml")
44          );
45      }
46  
47      private final AbstractPropertiesFactory<T> factory;
48  
49      protected AbstractPropertiesFactoryTest(final AbstractPropertiesFactory<T> factory) {
50          this.factory = factory;
51      }
52  
53      private void assertContents(final T properties) {
54          assertEquals("value1", properties.getProperty("key1"));
55          assertEquals("value2", properties.getProperty("key2"));
56          assertEquals("value3", properties.getProperty("key3"));
57          assertEquals("value4", properties.getProperty("key4"));
58          assertEquals("value5", properties.getProperty("key5"));
59          assertEquals("value6", properties.getProperty("key6"));
60          assertEquals("value7", properties.getProperty("key7"));
61          assertEquals("value8", properties.getProperty("key8"));
62          assertEquals("value9", properties.getProperty("key9"));
63          assertEquals("value10", properties.getProperty("key10"));
64          assertEquals("value11", properties.getProperty("key11"));
65      }
66  
67      private String getPathString(final String fileExtension) {
68          return BulkTest.TEST_PROPERTIES_PATH + "test" + fileExtension;
69      }
70  
71      private boolean isXmlTest(final String fileExtension) {
72          return ".xml".equals(fileExtension);
73      }
74  
75      @Test
76      public void testInstance() {
77          assertNotNull(PropertiesFactory.INSTANCE);
78      }
79  
80      @ParameterizedTest
81      @MethodSource(value = "getParameters")
82      public void testLoadClassLoaderMissingResource(final String fileExtension) throws Exception {
83          assertNull(factory.load(ClassLoader.getSystemClassLoader(), "missing/test" + fileExtension));
84      }
85  
86      @ParameterizedTest
87      @MethodSource(value = "getParameters")
88      public void testLoadClassLoaderResource(final String fileExtension) throws Exception {
89          assertContents(factory.load(ClassLoader.getSystemClassLoader(), "org/apache/commons/collections4/properties/test" + fileExtension));
90      }
91  
92      @ParameterizedTest
93      @MethodSource(value = "getParameters")
94      public void testLoadFile(final String fileExtension) throws Exception {
95          assertContents(factory.load(Paths.get(getPathString(fileExtension)).toFile()));
96      }
97  
98      @ParameterizedTest
99      @MethodSource(value = "getParameters")
100     public void testLoadFileName(final String fileExtension) throws Exception {
101         assertContents(factory.load(getPathString(fileExtension)));
102     }
103 
104     @ParameterizedTest
105     @MethodSource(value = "getParameters")
106     public void testLoadInputStream(final String fileExtension) throws Exception {
107         // Can't tell what we are reading
108         Assumptions.assumeFalse(isXmlTest(fileExtension));
109         //
110         try (FileInputStream inputStream = new FileInputStream(getPathString(fileExtension))) {
111             assertContents(factory.load(inputStream));
112         }
113     }
114 
115     @ParameterizedTest
116     @MethodSource(value = "getParameters")
117     public void testLoadPath(final String fileExtension) throws Exception {
118         assertContents(factory.load(Paths.get(getPathString(fileExtension))));
119     }
120 
121     @ParameterizedTest
122     @MethodSource(value = "getParameters")
123     public void testLoadReader(final String fileExtension) throws Exception {
124         // Can't tell what we are reading
125         Assumptions.assumeFalse(isXmlTest(fileExtension));
126         //
127         try (BufferedReader inputStream = Files.newBufferedReader(Paths.get(getPathString(fileExtension)))) {
128             assertContents(factory.load(inputStream));
129         }
130     }
131 
132     @ParameterizedTest
133     @MethodSource(value = "getParameters")
134     public void testLoadUri(final String fileExtension) throws Exception {
135         assertContents(factory.load(Paths.get(getPathString(fileExtension)).toUri()));
136     }
137 
138     @ParameterizedTest
139     @MethodSource(value = "getParameters")
140     public void testLoadUrl(final String fileExtension) throws Exception {
141         assertContents(factory.load(Paths.get(getPathString(fileExtension)).toUri().toURL()));
142     }
143 
144 }