1 /*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2013-2026 The Coveralls Maven Plugin Project Contributors:
5 * https://github.com/hazendaz/coveralls-maven-plugin/graphs/contributors
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25 package org.eluder.coveralls.maven.plugin.util;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.io.PrintWriter;
30 import java.net.URISyntaxException;
31 import java.net.URL;
32 import java.nio.charset.StandardCharsets;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.util.Locale;
36
37 import org.apache.commons.codec.digest.DigestUtils;
38
39 /**
40 * The Class TestIoUtil.
41 */
42 public final class TestIoUtil {
43
44 /**
45 * Write file content.
46 *
47 * @param content
48 * the content
49 * @param file
50 * the file
51 *
52 * @throws IOException
53 * Signals that an I/O exception has occurred.
54 */
55 public static void writeFileContent(final String content, final File file) throws IOException {
56 try (var writer = new PrintWriter(file, StandardCharsets.UTF_8)) {
57 writer.write(content);
58 }
59 }
60
61 /**
62 * Read file content.
63 *
64 * @param file
65 * the file
66 *
67 * @return the string
68 *
69 * @throws IOException
70 * Signals that an I/O exception has occurred.
71 */
72 public static String readFileContent(final File file) throws IOException {
73 return Files.readString(file.toPath(), StandardCharsets.UTF_8);
74 }
75
76 /**
77 * Gets the file.
78 *
79 * @param resource
80 * the resource
81 *
82 * @return the file
83 */
84 public static File getFile(final String resource) {
85 try {
86 var local = resource;
87 if (local.lastIndexOf("/") > 0) {
88 local = local.substring(local.lastIndexOf('/'));
89 }
90 if (!local.startsWith("/")) {
91 local = "/" + local;
92 }
93 return Path.of(TestIoUtil.getResourceUrl(local).toURI()).toFile();
94 } catch (final URISyntaxException e) {
95 throw new IllegalArgumentException(e);
96 }
97 }
98
99 /**
100 * Gets the sha 512 digest hex.
101 *
102 * @param content
103 * the content
104 *
105 * @return the sha 512 digest hex
106 */
107 public static String getSha512DigestHex(final String content) {
108 return DigestUtils.sha512Hex(content).toUpperCase(Locale.ENGLISH);
109 }
110
111 /**
112 * Gets the resource url.
113 *
114 * @param resource
115 * the resource
116 *
117 * @return the resource url
118 */
119 private static URL getResourceUrl(final String resource) {
120 return TestIoUtil.class.getResource(resource);
121 }
122
123 /**
124 * Instantiates a new test io util.
125 */
126 private TestIoUtil() {
127 // Do Nothing
128 }
129
130 }