1 package org.apache.maven.shared.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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 }