1   package org.apache.maven.plugin.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.IOException;
24  import java.util.Properties;
25  
26  /**
27   * Tests {@link PropertyUtils#loadPropertyFile(File, Properties)}.
28   *
29   * @author William Ferguson
30   */
31  public class EnhancedPropertyUtilsTest
32      extends AbstractPropertyUtilsTest
33  {
34      private static final String validationFileName =
35          "/target/test-classes/unit/propertiesutils-test/enhanced_validation.properties";
36  
37      private static final String propFileName = "/target/test-classes/unit/propertiesutils-test/enhanced.properties";
38  
39      private final Properties baseProps = new Properties();
40  
41      protected void setUp()
42          throws Exception
43      {
44          super.setUp();
45          this.baseProps.setProperty( "prop1", "valueOfProperty1" );
46      }
47  
48      protected File getPropertyFile()
49      {
50          final File propFile = new File( getBasedir(), propFileName );
51  
52          if ( !propFile.exists() )
53          {
54              return null;
55          }
56  
57          return propFile;
58      }
59  
60      protected File getValidationFile()
61      {
62          final File file = new File( getBasedir(), validationFileName );
63  
64          if ( !file.exists() )
65          {
66              return null;
67          }
68  
69          return file;
70      }
71  
72      /**
73       * Load property test case can be adjusted by modifying the enhanced.properties and enhanced_validation properties.
74       *
75       * @throws Exception
76       */
77      public void testBasicLoadProperty()
78          throws Exception
79      {
80          final Properties props = PropertyUtils.loadPropertyFile( this.propertyFile, this.baseProps );
81  
82          assertNotNull( props );
83          assertTrue( validateProperties( props ) );
84      }
85  
86      /**
87       * Load property test case can be adjusted by modifying the enhanced.properties and enhanced_validation properties.
88       *
89       * @throws Exception
90       */
91      public void testNonExistentProperty()
92          throws Exception
93      {
94          final Properties props = PropertyUtils.loadPropertyFile( this.propertyFile, this.baseProps );
95  
96          assertNotNull( props );
97          assertNull( props.getProperty( "does_not_exist" ) );
98      }
99  
100     /**
101      * Load property test case can be adjusted by modifying the enhanced.properties and enhanced_validation properties.
102      *
103      * @throws Exception
104      */
105     public void testException()
106         throws Exception
107     {
108         try
109         {
110             PropertyUtils.loadPropertyFile( new File( "NON_EXISTENT_FILE" ), this.baseProps );
111             fail( "Should not have been able to load properties from a non-existent file" );
112         }
113         catch ( IOException e )
114         {
115             // as expected.
116         }
117     }
118 }