View Javadoc
1   package org.apache.maven.shared.utils;
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.ByteArrayInputStream;
23  import java.io.File;
24  import java.io.FileOutputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.OutputStream;
28  import java.io.UnsupportedEncodingException;
29  import java.lang.annotation.ElementType;
30  import java.lang.annotation.Retention;
31  import java.lang.annotation.RetentionPolicy;
32  import java.lang.annotation.Target;
33  import java.net.URL;
34  import java.util.Properties;
35  
36  import org.junit.Rule;
37  import org.junit.Test;
38  import org.junit.rules.TemporaryFolder;
39  
40  import static org.hamcrest.CoreMatchers.is;
41  import static org.hamcrest.CoreMatchers.nullValue;
42  import static org.hamcrest.MatcherAssert.assertThat;
43  
44  public class PropertyUtilsTest
45  {
46  
47      @Retention( RetentionPolicy.RUNTIME )
48      @Target( ElementType.METHOD )
49      @interface NeedsTemporaryFolder
50      {
51      }
52  
53      @Rule
54      public TemporaryFolder tempFolder = new TemporaryFolder();
55  
56      @Test
57      @SuppressWarnings( "deprecation" )
58      // @ReproducesPlexusBug( "Should return null on error like url and file do" )
59      public void loadNullInputStream()
60          throws Exception
61      {
62          assertThat( PropertyUtils.loadProperties( (InputStream) null ), is( new Properties() ) );
63      }
64  
65      @Test
66      public void loadOptionalNullInputStream()
67          throws Exception
68      {
69          assertThat( PropertyUtils.loadOptionalProperties( (InputStream) null ), is( new Properties() ) );
70      }
71      
72  
73      @Test
74      public void loadOptionalProperties_ioException()
75          throws Exception
76      {
77          URL url = new URL( "https://nonesuch12344.foo.bar.com" );
78          assertThat( PropertyUtils.loadOptionalProperties( url ), is( new Properties() ) );
79      }
80  
81      @Test
82      @SuppressWarnings( "deprecation" )
83      public void loadNullURL()
84          throws Exception
85      {
86          assertThat( PropertyUtils.loadProperties( (URL) null ), nullValue( Properties.class ) );
87      }
88  
89      @Test
90      public void loadOptionalNullURL()
91          throws Exception
92      {
93          assertThat( PropertyUtils.loadOptionalProperties( (URL) null ), is( new Properties() ) );
94      }
95  
96      @Test
97      @SuppressWarnings( "deprecation" )
98      public void loadNullFile()
99          throws Exception
100     {
101         assertThat( PropertyUtils.loadProperties( (File) null ), nullValue( Properties.class ) );
102     }
103 
104     @Test
105     public void loadOptionalNullFile()
106         throws Exception
107     {
108         assertThat( PropertyUtils.loadOptionalProperties( (File) null ), is( new Properties() ) );
109     }
110 
111     @Test
112     @SuppressWarnings( "deprecation" )
113     public void loadEmptyInputStream()
114         throws Exception
115     {
116         assertThat( PropertyUtils.loadProperties( new ByteArrayInputStream( new byte[ 0 ] ) ),
117                     is( new Properties() ) );
118 
119         assertThat( PropertyUtils.loadOptionalProperties( new ByteArrayInputStream( new byte[ 0 ] ) ),
120                     is( new Properties() ) );
121 
122     }
123 
124     @Test
125     @NeedsTemporaryFolder
126     @SuppressWarnings( "deprecation" )
127     public void loadEmptyFile()
128         throws Exception
129     {
130         assertThat( PropertyUtils.loadProperties( tempFolder.newFile( "empty" ) ), is( new Properties() ) );
131         assertThat( PropertyUtils.loadOptionalProperties( tempFolder.newFile( "optional" ) ), is( new Properties() ) );
132     }
133 
134     @Test
135     @NeedsTemporaryFolder
136     @SuppressWarnings( "deprecation" )
137     public void loadEmptyURL()
138         throws Exception
139     {
140         assertThat( PropertyUtils.loadProperties( tempFolder.newFile( "empty" ).toURI().toURL() ),
141                     is( new Properties() ) );
142 
143         assertThat( PropertyUtils.loadOptionalProperties( tempFolder.newFile( "optional" ).toURI().toURL() ),
144                     is( new Properties() ) );
145 
146     }
147 
148     @Test
149     @SuppressWarnings( "deprecation" )
150     public void loadValidInputStream() throws UnsupportedEncodingException
151     {
152         Properties value = new Properties();
153         value.setProperty( "a", "b" );
154 
155         assertThat( PropertyUtils.loadProperties( new ByteArrayInputStream( "a=b".getBytes( "ISO-8859-1" ) ) ),
156                     is( value ) );
157 
158         assertThat( PropertyUtils.loadOptionalProperties( new ByteArrayInputStream( "a=b".getBytes( "ISO-8859-1" ) ) ),
159                     is( value ) );
160 
161     }
162 
163     @Test
164     @NeedsTemporaryFolder
165     @SuppressWarnings( "deprecation" )
166     public void loadValidFile() throws IOException
167     {
168         File valid = tempFolder.newFile( "valid" );
169         Properties value = new Properties();
170         value.setProperty( "a", "b" );
171         try ( OutputStream out = new FileOutputStream( valid ) )
172         {
173             value.store( out, "a test" );
174             assertThat( PropertyUtils.loadProperties( valid ), is( value ) );
175             assertThat( PropertyUtils.loadOptionalProperties( valid ), is( value ) );
176         }
177     }
178 
179     @Test
180     @NeedsTemporaryFolder
181     @SuppressWarnings( "deprecation" )
182     public void loadValidURL() throws IOException
183     {
184         File valid = tempFolder.newFile( "valid" );
185         Properties value = new Properties();
186         value.setProperty( "a", "b" );
187         try ( OutputStream out = new FileOutputStream( valid ) )
188         {
189           value.store( out, "a test" );
190           assertThat( PropertyUtils.loadProperties( valid.toURI().toURL() ), is( value ) );
191           assertThat( PropertyUtils.loadOptionalProperties( valid.toURI().toURL() ), is( value ) );
192         }
193     }
194 
195 }