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 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
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 }