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  
18  package org.apache.juneau.common.internal;
19  
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import java.io.*;
23  import java.nio.charset.*;
24  import java.nio.file.*;
25  import java.util.*;
26  
27  import org.apache.juneau.*;
28  import org.apache.juneau.common.utils.*;
29  import org.junit.jupiter.api.*;
30  /**
31   * Tests {@link PathReaderBuilder}.
32   */
33  class PathReaderBuilderTest extends TestBase {
34  
35  	private static final Path PATH = Paths.get("src/test/resources/files/Test3.properties");
36  
37  	@Test void a01_allowNoFile() throws IOException {
38  		final var p = new Properties();
39  		try (Reader r = PathReaderBuilder.create().allowNoFile().build()) {
40  			p.load(new StringReader(IOUtils.read(r, Files.size(PATH))));
41  		}
42  		assertNull(p.get("file"));
43  		p.clear();
44  		try (Reader r = PathReaderBuilder.create().allowNoFile().path("this file does not exist, at all.").build()) {
45  			p.load(new StringReader(IOUtils.read(r, Files.size(PATH))));
46  		}
47  		assertNull(p.get("file"));
48  	}
49  
50  	@Test void a02_allowNoFileException() {
51  		assertThrows(IllegalStateException.class, () -> PathReaderBuilder.create().build());  // NOSONAR
52  		assertThrows(NoSuchFileException.class, () -> PathReaderBuilder.create().path("this file does not exist, at all.").build());
53  	}
54  
55  	@Test void a03_charsetCharset() throws IOException {
56  		final var p = new Properties();
57  		try (Reader r = PathReaderBuilder.create().path(PATH).charset(StandardCharsets.UTF_8).build()) {
58  			p.load(new StringReader(IOUtils.read(r, Files.size(PATH))));
59  		}
60  		assertEquals("files/Test3.properties", p.get("file"));
61  		p.clear();
62  		try (Reader r = PathReaderBuilder.create().path(PATH).charset((Charset) null).build()) {
63  			p.load(new StringReader(IOUtils.read(r, Files.size(PATH))));
64  		}
65  		assertEquals("files/Test3.properties", p.get("file"));
66  	}
67  
68  	@Test void a04_charsetString() throws IOException {
69  		final var p = new Properties();
70  		try (Reader r = PathReaderBuilder.create().path(PATH).charset(StandardCharsets.UTF_8.name()).build()) {
71  			p.load(new StringReader(IOUtils.read(r, Files.size(PATH))));
72  		}
73  		assertEquals("files/Test3.properties", p.get("file"));
74  		p.clear();
75  		try (Reader r = PathReaderBuilder.create().path(PATH).charset((String) null).build()) {
76  			p.load(new StringReader(IOUtils.read(r, Files.size(PATH))));
77  		}
78  		assertEquals("files/Test3.properties", p.get("file"));
79  	}
80  
81  	@Test void a05_create() throws IOException {
82  		final var p = new Properties();
83  		try (Reader r = PathReaderBuilder.create(PATH).build()) {
84  			p.load(new StringReader(IOUtils.read(r, Files.size(PATH))));
85  		}
86  		assertEquals("files/Test3.properties", p.get("file"));
87  	}
88  
89  	@Test void a06_pathPath() throws IOException {
90  		final var p = new Properties();
91  		try (Reader r = PathReaderBuilder.create().path(PATH).build()) {
92  			p.load(new StringReader(IOUtils.read(r, Files.size(PATH))));
93  		}
94  		assertEquals("files/Test3.properties", p.get("file"));
95  	}
96  
97  	@Test void a07_pathString() throws IOException {
98  		final var p = new Properties();
99  		try (Reader r = PathReaderBuilder.create().path(PATH.toString()).build()) {
100 			p.load(new StringReader(IOUtils.read(r, Files.size(PATH))));
101 		}
102 		assertEquals("files/Test3.properties", p.get("file"));
103 	}
104 }