View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.commons.io;
18  
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  import java.io.*;
22  import java.nio.charset.*;
23  import java.nio.file.*;
24  
25  import org.apache.juneau.*;
26  import org.junit.jupiter.api.*;
27  
28  class FileWriterBuilder_Test extends TestBase {
29  
30  	private static final Path TEST_FILE = Paths.get("target/test-output/FileWriterBuilder_Test.txt");
31  
32  	@BeforeEach
33  	void setUp() throws IOException {
34  		Files.createDirectories(TEST_FILE.getParent());
35  		if (Files.exists(TEST_FILE)) {
36  			Files.delete(TEST_FILE);
37  		}
38  	}
39  
40  	@AfterEach
41  	void tearDown() throws IOException {
42  		if (Files.exists(TEST_FILE)) {
43  			Files.delete(TEST_FILE);
44  		}
45  	}
46  
47  	//====================================================================================================
48  	// create() tests
49  	//====================================================================================================
50  	@Test void a01_create() {
51  		var builder = FileWriterBuilder.create();
52  		assertNotNull(builder);
53  	}
54  
55  	@Test void a02_createWithFile() {
56  		var file = TEST_FILE.toFile();
57  		var builder = FileWriterBuilder.create(file);
58  		assertNotNull(builder);
59  	}
60  
61  	@Test void a03_createWithPath() {
62  		var builder = FileWriterBuilder.create(TEST_FILE.toString());
63  		assertNotNull(builder);
64  	}
65  
66  	//====================================================================================================
67  	// build() tests
68  	//====================================================================================================
69  	@Test void b01_build() throws IOException {
70  		var file = TEST_FILE.toFile();
71  		try (var writer = FileWriterBuilder.create(file).build()) {
72  			writer.write("test");
73  		}
74  		var content = Files.readString(TEST_FILE);
75  		assertEquals("test", content);
76  	}
77  
78  	@Test void b02_build_noFile() {
79  		assertThrows(IllegalArgumentException.class, () -> {
80  			FileWriterBuilder.create().build();
81  		});
82  	}
83  
84  	//====================================================================================================
85  	// append() tests
86  	//====================================================================================================
87  	@Test void c01_append() throws IOException {
88  		var file = TEST_FILE.toFile();
89  		// Write initial content
90  		try (var writer = FileWriterBuilder.create(file).build()) {
91  			writer.write("initial");
92  		}
93  		// Append more content
94  		try (var writer = FileWriterBuilder.create(file).append().build()) {
95  			writer.write("appended");
96  		}
97  		var content = Files.readString(TEST_FILE);
98  		assertEquals("initialappended", content);
99  	}
100 
101 	@Test void c02_append_newFile() throws IOException {
102 		var file = TEST_FILE.toFile();
103 		try (var writer = FileWriterBuilder.create(file).append().build()) {
104 			writer.write("appended");
105 		}
106 		var content = Files.readString(TEST_FILE);
107 		assertEquals("appended", content);
108 	}
109 
110 	//====================================================================================================
111 	// buffered() tests
112 	//====================================================================================================
113 	@Test void d01_buffered() throws IOException {
114 		var file = TEST_FILE.toFile();
115 		try (var writer = FileWriterBuilder.create(file).buffered().build()) {
116 			writer.write("test");
117 		}
118 		var content = Files.readString(TEST_FILE);
119 		assertEquals("test", content);
120 	}
121 
122 	@Test void d02_bufferedAndAppend() throws IOException {
123 		var file = TEST_FILE.toFile();
124 		try (var writer = FileWriterBuilder.create(file).build()) {
125 			writer.write("initial");
126 		}
127 		try (var writer = FileWriterBuilder.create(file).buffered().append().build()) {
128 			writer.write("appended");
129 		}
130 		var content = Files.readString(TEST_FILE);
131 		assertEquals("initialappended", content);
132 	}
133 
134 	//====================================================================================================
135 	// charset(Charset) tests
136 	//====================================================================================================
137 	@Test void e01_charsetCharset() throws IOException {
138 		var file = TEST_FILE.toFile();
139 		try (var writer = FileWriterBuilder.create(file)
140 			.charset(StandardCharsets.UTF_8)
141 			.build()) {
142 			writer.write("test");
143 		}
144 		var content = Files.readString(TEST_FILE, StandardCharsets.UTF_8);
145 		assertEquals("test", content);
146 	}
147 
148 	@Test void e02_charsetCharset_null() throws IOException {
149 		var file = TEST_FILE.toFile();
150 		try (var writer = FileWriterBuilder.create(file)
151 			.charset((Charset)null)
152 			.build()) {
153 			writer.write("test");
154 		}
155 		var content = Files.readString(TEST_FILE);
156 		assertEquals("test", content);
157 	}
158 
159 	@Test void e03_charsetCharset_iso8859_1() throws IOException {
160 		var file = TEST_FILE.toFile();
161 		try (var writer = FileWriterBuilder.create(file)
162 			.charset(StandardCharsets.ISO_8859_1)
163 			.build()) {
164 			writer.write("test");
165 		}
166 		var content = Files.readString(TEST_FILE, StandardCharsets.ISO_8859_1);
167 		assertEquals("test", content);
168 	}
169 
170 	//====================================================================================================
171 	// charset(String) tests
172 	//====================================================================================================
173 	@Test void f01_charsetString() throws IOException {
174 		var file = TEST_FILE.toFile();
175 		try (var writer = FileWriterBuilder.create(file)
176 			.charset("UTF-8")
177 			.build()) {
178 			writer.write("test");
179 		}
180 		var content = Files.readString(TEST_FILE, StandardCharsets.UTF_8);
181 		assertEquals("test", content);
182 	}
183 
184 	@Test void f02_charsetString_null() {
185 		assertThrows(IllegalArgumentException.class, () -> {
186 			FileWriterBuilder.create(TEST_FILE.toFile())
187 				.charset((String)null)
188 				.build();
189 		});
190 	}
191 
192 	@Test void f03_charsetString_invalid() {
193 		assertThrows(UnsupportedCharsetException.class, () -> {
194 			FileWriterBuilder.create(TEST_FILE.toFile())
195 				.charset("INVALID-CHARSET-NAME")
196 				.build();
197 		});
198 	}
199 
200 	//====================================================================================================
201 	// file() tests
202 	//====================================================================================================
203 	@Test void g01_fileFile() throws IOException {
204 		var file = TEST_FILE.toFile();
205 		try (var writer = FileWriterBuilder.create()
206 			.file(file)
207 			.build()) {
208 			writer.write("test");
209 		}
210 		var content = Files.readString(TEST_FILE);
211 		assertEquals("test", content);
212 	}
213 
214 	@Test void g02_fileString() throws IOException {
215 		try (var writer = FileWriterBuilder.create()
216 			.file(TEST_FILE.toString())
217 			.build()) {
218 			writer.write("test");
219 		}
220 		var content = Files.readString(TEST_FILE);
221 		assertEquals("test", content);
222 	}
223 
224 	//====================================================================================================
225 	// Chaining tests
226 	//====================================================================================================
227 	@Test void h01_chaining() throws IOException {
228 		var file = TEST_FILE.toFile();
229 		try (var writer = FileWriterBuilder.create()
230 			.file(file)
231 			.charset(StandardCharsets.UTF_8)
232 			.buffered()
233 			.append()
234 			.build()) {
235 			writer.write("test");
236 		}
237 		var content = Files.readString(TEST_FILE, StandardCharsets.UTF_8);
238 		assertEquals("test", content);
239 	}
240 }
241