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.File;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.nio.file.Files;
25  import java.util.Enumeration;
26  import java.util.Properties;
27  
28  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
29  
30  /**
31   * Base class for propertyutils test case
32   */
33  public abstract class AbstractPropertyUtilsTest extends AbstractMojoTestCase {
34      protected File propertyFile;
35  
36      protected File validationFile;
37  
38      protected Properties validationProp;
39  
40      protected abstract File getPropertyFile();
41  
42      protected abstract File getValidationFile();
43  
44      protected void setUp() throws Exception {
45          super.setUp();
46  
47          // load data
48          propertyFile = getPropertyFile();
49          assertNotNull(propertyFile);
50  
51          validationFile = getValidationFile();
52          assertNotNull(validationFile);
53  
54          loadValidationProperties(validationFile);
55      }
56  
57      protected boolean validateProperties(Properties prop) {
58          boolean bRetVal = false;
59  
60          Enumeration<?> propKeys = prop.keys();
61          String key;
62  
63          while (propKeys.hasMoreElements()) {
64              key = (String) propKeys.nextElement();
65              bRetVal = prop.getProperty(key).equals(validationProp.getProperty(key));
66              if (!bRetVal) {
67                  break;
68              }
69          }
70  
71          return bRetVal;
72      }
73  
74      /**
75       * load the property file for cross checking the
76       * values in the processed property file
77       *
78       * @param validationPropFile
79       */
80      private void loadValidationProperties(File validationPropFile) throws IOException {
81          validationProp = new Properties();
82          try (InputStream in = Files.newInputStream(validationPropFile.toPath())) {
83              validationProp.load(in);
84          }
85      }
86  }