View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.resources;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.util.Enumeration;
26  import java.util.Properties;
27  
28  import org.junit.jupiter.api.BeforeEach;
29  
30  import static org.junit.jupiter.api.Assertions.assertNotNull;
31  
32  /**
33   * Base class for propertyutils test case
34   */
35  public abstract class AbstractPropertyUtilsTest {
36      protected Path propertyFile;
37  
38      protected Path validationFile;
39  
40      protected Properties validationProp;
41  
42      protected abstract Path getPropertyFile();
43  
44      protected abstract Path getValidationFile();
45  
46      @BeforeEach
47      protected void setUp() throws Exception {
48          // load data
49          propertyFile = getPropertyFile();
50          assertNotNull(propertyFile);
51  
52          validationFile = getValidationFile();
53          assertNotNull(validationFile);
54  
55          loadValidationProperties(validationFile);
56      }
57  
58      protected boolean validateProperties(Properties prop) {
59          boolean bRetVal = false;
60  
61          Enumeration<?> propKeys = prop.keys();
62          String key;
63  
64          while (propKeys.hasMoreElements()) {
65              key = (String) propKeys.nextElement();
66              bRetVal = prop.getProperty(key).equals(validationProp.getProperty(key));
67              if (!bRetVal) {
68                  break;
69              }
70          }
71  
72          return bRetVal;
73      }
74  
75      /**
76       * load the property file for cross checking the
77       * values in the processed property file
78       *
79       * @param validationPropFile
80       */
81      private void loadValidationProperties(Path validationPropFile) {
82          validationProp = new Properties();
83          try (InputStream in = Files.newInputStream(validationPropFile)) {
84              validationProp.load(in);
85          } catch (IOException ex) {
86              // TODO: do error handling
87          }
88      }
89  }