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  import org.codehaus.plexus.util.IOUtil;
30  
31  /**
32   * Base class for propertyutils test case
33   */
34  public abstract class AbstractPropertyUtilsTest extends AbstractMojoTestCase {
35      protected File propertyFile;
36  
37      protected File validationFile;
38  
39      protected Properties validationProp;
40  
41      protected abstract File getPropertyFile();
42  
43      protected abstract File getValidationFile();
44  
45      protected void setUp() throws Exception {
46          super.setUp();
47  
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(File validationPropFile) {
82          validationProp = new Properties();
83          InputStream in = null;
84  
85          try {
86              in = Files.newInputStream(validationPropFile.toPath());
87              validationProp.load(in);
88              in.close();
89              in = null;
90          } catch (IOException ex) {
91              // TODO: do error handling
92          } finally {
93              IOUtil.close(in);
94          }
95      }
96  }