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 org.junit.Rule;
23  import org.junit.Test;
24  import org.junit.rules.TemporaryFolder;
25  
26  import java.io.ByteArrayInputStream;
27  import java.io.File;
28  import java.io.FileOutputStream;
29  import java.io.InputStream;
30  import java.lang.annotation.ElementType;
31  import java.lang.annotation.Retention;
32  import java.lang.annotation.RetentionPolicy;
33  import java.lang.annotation.Target;
34  import java.net.URL;
35  import java.util.Properties;
36  
37  import static org.hamcrest.CoreMatchers.*;
38  import static org.junit.Assert.assertThat;
39  
40  public class PropertyUtilsTest
41  {
42      @Retention( RetentionPolicy.RUNTIME )
43      @Target( ElementType.METHOD )
44      @interface NeedsTemporaryFolder
45      {
46      }
47  
48      @Rule
49      public TemporaryFolder tempFolder = new TemporaryFolder();
50  
51  
52      @Test
53      // @ReproducesPlexusBug( "Should return null on error like url and file do" )
54      public void loadNullInputStream()
55          throws Exception
56      {
57          assertThat( PropertyUtils.loadProperties( (InputStream) null ), is( new Properties() ) );
58      }
59  
60      @Test
61      public void loadNullURL()
62          throws Exception
63      {
64          assertThat( PropertyUtils.loadProperties( (URL) null ), nullValue( Properties.class ) );
65      }
66  
67      @Test
68      public void loadNullFile()
69          throws Exception
70      {
71          assertThat( PropertyUtils.loadProperties( (File) null ), nullValue( Properties.class ) );
72      }
73  
74      @Test
75      public void loadEmptyInputStream()
76          throws Exception
77      {
78          assertThat( PropertyUtils.loadProperties( new ByteArrayInputStream( new byte[0] ) ), is( new Properties() ) );
79      }
80  
81      @Test
82      @NeedsTemporaryFolder
83      public void loadEmptyFile()
84          throws Exception
85      {
86          assertThat( PropertyUtils.loadProperties( tempFolder.newFile( "empty" ) ), is( new Properties() ) );
87      }
88  
89      @Test
90      @NeedsTemporaryFolder
91      public void loadEmptyURL()
92          throws Exception
93      {
94          assertThat( PropertyUtils.loadProperties( tempFolder.newFile( "empty" ).toURI().toURL() ), is( new Properties() ) );
95      }
96  
97      @Test
98      public void loadValidInputStream()
99          throws Exception
100     {
101         Properties value = new Properties();
102         value.setProperty( "a", "b" );
103         assertThat( PropertyUtils.loadProperties( new ByteArrayInputStream( "a=b".getBytes( "ISO-8859-1" ) ) ),
104                     is( value ) );
105     }
106 
107     @Test
108     @NeedsTemporaryFolder
109     public void loadValidFile()
110         throws Exception
111     {
112         File valid = tempFolder.newFile( "valid" );
113         Properties value = new Properties();
114         value.setProperty( "a", "b" );
115         value.store( new FileOutputStream( valid ), "a test" );
116         assertThat( PropertyUtils.loadProperties( valid ), is( value ) );
117     }
118 
119     @Test
120     @NeedsTemporaryFolder
121     public void loadValidURL()
122         throws Exception
123     {
124         File valid = tempFolder.newFile( "valid" );
125         Properties value = new Properties();
126         value.setProperty( "a", "b" );
127         value.store( new FileOutputStream( valid ), "a test" );
128         assertThat( PropertyUtils.loadProperties( valid.toURI().toURL() ), is( value ) );
129     }
130 
131 
132 }