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