1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.shared.utils;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.File;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.io.UnsupportedEncodingException;
28 import java.lang.annotation.ElementType;
29 import java.lang.annotation.Retention;
30 import java.lang.annotation.RetentionPolicy;
31 import java.lang.annotation.Target;
32 import java.net.URL;
33 import java.util.Properties;
34
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.rules.TemporaryFolder;
38
39 import static org.hamcrest.CoreMatchers.is;
40 import static org.hamcrest.CoreMatchers.nullValue;
41 import static org.hamcrest.MatcherAssert.assertThat;
42
43 public class PropertyUtilsTest {
44
45 @Retention(RetentionPolicy.RUNTIME)
46 @Target(ElementType.METHOD)
47 @interface NeedsTemporaryFolder {}
48
49 @Rule
50 public TemporaryFolder tempFolder = new TemporaryFolder();
51
52 @Test
53 @SuppressWarnings("deprecation")
54
55 public void loadNullInputStream() throws Exception {
56 assertThat(PropertyUtils.loadProperties((InputStream) null), is(new Properties()));
57 }
58
59 @Test
60 public void loadOptionalNullInputStream() throws Exception {
61 assertThat(PropertyUtils.loadOptionalProperties((InputStream) null), is(new Properties()));
62 }
63
64 @Test
65 public void loadOptionalProperties_ioException() throws Exception {
66 URL url = new URL("https://nonesuch12344.foo.bar.com");
67 assertThat(PropertyUtils.loadOptionalProperties(url), is(new Properties()));
68 }
69
70 @Test
71 @SuppressWarnings("deprecation")
72 public void loadNullURL() throws Exception {
73 assertThat(PropertyUtils.loadProperties((URL) null), nullValue(Properties.class));
74 }
75
76 @Test
77 public void loadOptionalNullURL() throws Exception {
78 assertThat(PropertyUtils.loadOptionalProperties((URL) null), is(new Properties()));
79 }
80
81 @Test
82 @SuppressWarnings("deprecation")
83 public void loadNullFile() throws Exception {
84 assertThat(PropertyUtils.loadProperties((File) null), nullValue(Properties.class));
85 }
86
87 @Test
88 public void loadOptionalNullFile() throws Exception {
89 assertThat(PropertyUtils.loadOptionalProperties((File) null), is(new Properties()));
90 }
91
92 @Test
93 @SuppressWarnings("deprecation")
94 public void loadEmptyInputStream() throws Exception {
95 assertThat(PropertyUtils.loadProperties(new ByteArrayInputStream(new byte[0])), is(new Properties()));
96
97 assertThat(PropertyUtils.loadOptionalProperties(new ByteArrayInputStream(new byte[0])), is(new Properties()));
98 }
99
100 @Test
101 @NeedsTemporaryFolder
102 @SuppressWarnings("deprecation")
103 public void loadEmptyFile() throws Exception {
104 assertThat(PropertyUtils.loadProperties(tempFolder.newFile("empty")), is(new Properties()));
105 assertThat(PropertyUtils.loadOptionalProperties(tempFolder.newFile("optional")), is(new Properties()));
106 }
107
108 @Test
109 @NeedsTemporaryFolder
110 @SuppressWarnings("deprecation")
111 public void loadEmptyURL() throws Exception {
112 assertThat(
113 PropertyUtils.loadProperties(tempFolder.newFile("empty").toURI().toURL()), is(new Properties()));
114
115 assertThat(
116 PropertyUtils.loadOptionalProperties(
117 tempFolder.newFile("optional").toURI().toURL()),
118 is(new Properties()));
119 }
120
121 @Test
122 @SuppressWarnings("deprecation")
123 public void loadValidInputStream() throws UnsupportedEncodingException {
124 Properties value = new Properties();
125 value.setProperty("a", "b");
126
127 assertThat(PropertyUtils.loadProperties(new ByteArrayInputStream("a=b".getBytes("ISO-8859-1"))), is(value));
128
129 assertThat(
130 PropertyUtils.loadOptionalProperties(new ByteArrayInputStream("a=b".getBytes("ISO-8859-1"))),
131 is(value));
132 }
133
134 @Test
135 @NeedsTemporaryFolder
136 @SuppressWarnings("deprecation")
137 public void loadValidFile() throws IOException {
138 File valid = tempFolder.newFile("valid");
139 Properties value = new Properties();
140 value.setProperty("a", "b");
141 try (OutputStream out = new FileOutputStream(valid)) {
142 value.store(out, "a test");
143 assertThat(PropertyUtils.loadProperties(valid), is(value));
144 assertThat(PropertyUtils.loadOptionalProperties(valid), is(value));
145 }
146 }
147
148 @Test
149 @NeedsTemporaryFolder
150 @SuppressWarnings("deprecation")
151 public void loadValidURL() throws IOException {
152 File valid = tempFolder.newFile("valid");
153 Properties value = new Properties();
154 value.setProperty("a", "b");
155 try (OutputStream out = new FileOutputStream(valid)) {
156 value.store(out, "a test");
157 assertThat(PropertyUtils.loadProperties(valid.toURI().toURL()), is(value));
158 assertThat(PropertyUtils.loadOptionalProperties(valid.toURI().toURL()), is(value));
159 }
160 }
161 }