|
10 | 10 | *
|
11 | 11 | * SPDX-License-Identifier: EPL-2.0
|
12 | 12 | */
|
13 |
| -package org.openhab.binding.fmiweather; |
| 13 | +package org.openhab.binding.fmiweather.internal; |
14 | 14 |
|
15 |
| -import static org.junit.jupiter.api.Assertions.fail; |
16 |
| - |
17 |
| -import java.io.BufferedReader; |
18 | 15 | import java.io.IOException;
|
19 |
| -import java.lang.reflect.InvocationTargetException; |
20 |
| -import java.lang.reflect.Method; |
21 |
| -import java.net.URISyntaxException; |
| 16 | +import java.io.InputStream; |
22 | 17 | import java.nio.charset.StandardCharsets;
|
23 |
| -import java.nio.file.Files; |
24 |
| -import java.nio.file.Path; |
25 |
| -import java.nio.file.Paths; |
26 | 18 | import java.util.HashSet;
|
27 |
| -import java.util.Objects; |
28 | 19 | import java.util.Set;
|
29 | 20 |
|
| 21 | +import javax.xml.xpath.XPathExpressionException; |
| 22 | + |
30 | 23 | import org.eclipse.jdt.annotation.NonNullByDefault;
|
31 | 24 | import org.eclipse.jdt.annotation.Nullable;
|
32 | 25 | import org.hamcrest.Description;
|
|
37 | 30 | import org.openhab.binding.fmiweather.internal.client.Data;
|
38 | 31 | import org.openhab.binding.fmiweather.internal.client.FMIResponse;
|
39 | 32 | import org.openhab.binding.fmiweather.internal.client.Location;
|
| 33 | +import org.openhab.binding.fmiweather.internal.client.exception.FMIExceptionReportException; |
| 34 | +import org.openhab.binding.fmiweather.internal.client.exception.FMIUnexpectedResponseException; |
| 35 | +import org.xml.sax.SAXException; |
40 | 36 |
|
41 | 37 | /**
|
42 | 38 | * Base class for response parsing tests
|
|
47 | 43 | public class AbstractFMIResponseParsingTest {
|
48 | 44 |
|
49 | 45 | @NonNullByDefault({})
|
50 |
| - protected Client client; |
| 46 | + protected ClientExposed client; |
51 | 47 |
|
52 | 48 | @BeforeEach
|
53 | 49 | public void setUpClient() {
|
54 |
| - client = new Client(); |
55 |
| - } |
56 |
| - |
57 |
| - protected Path getTestResource(String filename) { |
58 |
| - try { |
59 |
| - return Paths.get(getClass().getResource(filename).toURI()); |
60 |
| - } catch (URISyntaxException e) { |
61 |
| - fail(e.getMessage()); |
62 |
| - // Make the compiler happy by throwing here, fails already above |
63 |
| - throw new IllegalStateException(); |
64 |
| - } |
| 50 | + client = new ClientExposed(); |
65 | 51 | }
|
66 | 52 |
|
67 |
| - protected String readTestResourceUtf8(String filename) { |
68 |
| - return readTestResourceUtf8(getTestResource(filename)); |
69 |
| - } |
70 |
| - |
71 |
| - protected String readTestResourceUtf8(Path path) { |
72 |
| - try { |
73 |
| - BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8); |
74 |
| - StringBuilder content = new StringBuilder(); |
75 |
| - char[] buffer = new char[1024]; |
76 |
| - int read = -1; |
77 |
| - while ((read = reader.read(buffer)) != -1) { |
78 |
| - content.append(buffer, 0, read); |
| 53 | + protected String readTestResourceUtf8(String filename) throws IOException { |
| 54 | + try (InputStream inputStream = AbstractFMIResponseParsingTest.class.getResourceAsStream(filename)) { |
| 55 | + if (inputStream == null) { |
| 56 | + throw new IOException("Input stream is null"); |
79 | 57 | }
|
80 |
| - return content.toString(); |
81 |
| - } catch (IOException e) { |
82 |
| - fail(e.getMessage()); |
83 |
| - // Make the compiler happy by throwing here, fails already above |
84 |
| - throw new IllegalStateException(); |
| 58 | + byte[] bytes = inputStream.readAllBytes(); |
| 59 | + if (bytes == null) { |
| 60 | + throw new IOException("Resulting byte-array empty"); |
| 61 | + } |
| 62 | + return new String(bytes, StandardCharsets.UTF_8); |
85 | 63 | }
|
86 | 64 | }
|
87 | 65 |
|
@@ -143,42 +121,15 @@ protected void describeMismatchSafely(Data dataValues, @Nullable Description mis
|
143 | 121 | };
|
144 | 122 | }
|
145 | 123 |
|
146 |
| - /** |
147 |
| - * |
148 |
| - * @param content |
149 |
| - * @return |
150 |
| - * @throws Throwable exception raised by parseMultiPointCoverageXml |
151 |
| - * @throws AssertionError exception raised when parseMultiPointCoverageXml method signature does not match excepted |
152 |
| - * (test & implementation is out-of-sync) |
153 |
| - */ |
154 |
| - protected FMIResponse parseMultiPointCoverageXml(String content) throws Throwable { |
155 |
| - try { |
156 |
| - Method parseMethod = Client.class.getDeclaredMethod("parseMultiPointCoverageXml", String.class); |
157 |
| - parseMethod.setAccessible(true); |
158 |
| - return Objects.requireNonNull((FMIResponse) parseMethod.invoke(client, content)); |
159 |
| - } catch (InvocationTargetException e) { |
160 |
| - throw e.getTargetException(); |
161 |
| - } catch (Exception e) { |
162 |
| - fail(String.format("Unexpected reflection error (code changed?) %s: %s", e.getClass().getName(), |
163 |
| - e.getMessage())); |
164 |
| - // Make the compiler happy by throwing here, fails already above |
165 |
| - throw new IllegalStateException(); |
| 124 | + protected class ClientExposed extends Client { |
| 125 | + public FMIResponse parseMultiPointCoverageXml(String response) throws FMIUnexpectedResponseException, |
| 126 | + FMIExceptionReportException, SAXException, IOException, XPathExpressionException { |
| 127 | + return super.parseMultiPointCoverageXml(response); |
166 | 128 | }
|
167 |
| - } |
168 | 129 |
|
169 |
| - @SuppressWarnings("unchecked") |
170 |
| - protected Set<Location> parseStations(String content) { |
171 |
| - try { |
172 |
| - Method parseMethod = Objects.requireNonNull(Client.class.getDeclaredMethod("parseStations", String.class)); |
173 |
| - parseMethod.setAccessible(true); |
174 |
| - return Objects.requireNonNull((Set<Location>) parseMethod.invoke(client, content)); |
175 |
| - } catch (InvocationTargetException e) { |
176 |
| - throw new RuntimeException(e.getTargetException()); |
177 |
| - } catch (Exception e) { |
178 |
| - fail(String.format("Unexpected reflection error (code changed?) %s: %s", e.getClass().getName(), |
179 |
| - e.getMessage())); |
180 |
| - // Make the compiler happy by throwing here, fails already above |
181 |
| - throw new IllegalStateException(); |
| 130 | + public Set<Location> parseStations(String response) throws FMIExceptionReportException, |
| 131 | + FMIUnexpectedResponseException, SAXException, IOException, XPathExpressionException { |
| 132 | + return super.parseStations(response); |
182 | 133 | }
|
183 | 134 | }
|
184 | 135 | }
|
0 commit comments