• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Ekryd / echo-maven-plugin / 3648

14 Nov 2024 01:38PM CUT coverage: 96.795%. Remained the same
3648

push

circleci

web-flow
Update dependency org.codehaus.mojo:versions-maven-plugin to v2.18.0

302 of 312 relevant lines covered (96.79%)

0.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.1
/echo/src/main/java/echo/util/FileUtil.java
1
package echo.util;
2

3
import static echo.exception.FailureException.UNSUPPORTED_ENCODING;
4

5
import echo.exception.FailureException;
6
import echo.output.PluginLog;
7
import echo.parameter.PluginParameters;
8
import java.io.*;
9
import java.nio.charset.UnsupportedCharsetException;
10
import org.apache.commons.io.FileUtils;
11
import org.apache.commons.io.IOUtils;
12

13
/** Used to interface with file system */
14
public class FileUtil {
15
  private static final String READING_INPUT_FROM = "Reading input from ";
16
  private final PluginLog mavenPluginLog;
17
  private final String encoding;
18
  private final String fromFile;
19
  private final File basePath;
20
  private final String toFile;
21
  private final boolean appendToFile;
22
  private final boolean forceOverwrite;
23

24
  /**
25
   * Create a new instance of the FileUtil
26
   *
27
   * @param parameters The user-supplied plugin parameters
28
   * @param mavenPluginLog Wrapper for Maven internal plugin logger
29
   */
30
  public FileUtil(PluginParameters parameters, PluginLog mavenPluginLog) {
1✔
31
    this.mavenPluginLog = mavenPluginLog;
1✔
32
    this.encoding = parameters.getEncoding();
1✔
33
    this.fromFile = parameters.getFromFile();
1✔
34
    this.basePath = parameters.getBasePath();
1✔
35
    this.toFile = parameters.getToFile();
1✔
36
    this.appendToFile = parameters.isAppendToFile();
1✔
37
    this.forceOverwrite = parameters.isForce();
1✔
38
  }
1✔
39

40
  /**
41
   * Saves text output
42
   *
43
   * @param message The text to save
44
   */
45
  public void saveToFile(final String message) {
46
    var saveFile = new File(basePath, toFile);
1✔
47
    var absolutePath = saveFile.getAbsolutePath();
1✔
48
    mavenPluginLog.info("Saving output to " + absolutePath);
1✔
49

50
    try {
51
      checkForNonWritableFile(saveFile);
1✔
52
      makeFileWritable(saveFile);
1✔
53
      FileUtils.write(saveFile, message, encoding, appendToFile);
1✔
54
    } catch (UnsupportedEncodingException | UnsupportedCharsetException ex) {
1✔
55
      throw new FailureException(UNSUPPORTED_ENCODING + ex.getMessage(), ex);
1✔
56
    } catch (IOException ex) {
×
57
      mavenPluginLog.debug(ex);
×
58
      throw new FailureException("Could not save file: " + absolutePath, ex);
×
59
    }
1✔
60
  }
1✔
61

62
  private void checkForNonWritableFile(File saveFile) {
63
    if (saveFile.isDirectory()) {
1✔
64
      throw new FailureException(
1✔
65
          "File " + saveFile.getAbsolutePath() + " exists but is a directory");
1✔
66
    }
67
  }
1✔
68

69
  private void makeFileWritable(File saveFile) {
70
    if (saveFile.isFile() && saveFile.exists() && !saveFile.canWrite()) {
1✔
71
      if (forceOverwrite) {
1✔
72
        var writableStatus = saveFile.setWritable(true);
1✔
73
        if (!writableStatus) {
1✔
74
          throw new FailureException("Could not make file writable " + saveFile.getAbsolutePath());
×
75
        }
76
      } else {
1✔
77
        throw new FailureException("Cannot write to read-only file " + saveFile.getAbsolutePath());
1✔
78
      }
79
    }
80
  }
1✔
81

82
  /**
83
   * Retrieves the message from the location in attribute fromFile
84
   *
85
   * @return Content of the default sort order file
86
   */
87
  public String getFromFile() throws IOException {
88
    var urlWrapper = new UrlWrapper(fromFile);
1✔
89
    try (var inputStream =
90
        urlWrapper.isUrl()
1✔
91
            ? urlWrapper.openStream()
1✔
92
            : getFileFromRelativeOrClassPath(basePath, fromFile)) {
1✔
93
      return IOUtils.toString(inputStream, encoding);
1✔
94
    } catch (UnsupportedEncodingException | UnsupportedCharsetException ex) {
1✔
95
      throw new FailureException(UNSUPPORTED_ENCODING + ex.getMessage(), ex);
1✔
96
    }
97
  }
98

99
  private InputStream getFileFromRelativeOrClassPath(File basePath, String file)
100
      throws IOException {
101
    var findFileInAbsolutePath = new FindFileInAbsolutePath(mavenPluginLog);
1✔
102

103
    findFileInAbsolutePath.openFile(new File(file));
1✔
104
    if (findFileInAbsolutePath.isFound()) {
1✔
105
      mavenPluginLog.debug(READING_INPUT_FROM + findFileInAbsolutePath.getAbsoluteFilePath());
1✔
106

107
      return findFileInAbsolutePath.getInputStream();
1✔
108
    }
109

110
    findFileInAbsolutePath.openFile(new File(basePath, file));
1✔
111
    if (findFileInAbsolutePath.isFound()) {
1✔
112
      mavenPluginLog.debug(READING_INPUT_FROM + findFileInAbsolutePath.getAbsoluteFilePath());
1✔
113

114
      return findFileInAbsolutePath.getInputStream();
1✔
115
    }
116

117
    var findFileInClassPath = new FindFileInClassPath(mavenPluginLog);
1✔
118
    findFileInClassPath.openFile(file);
1✔
119
    if (findFileInClassPath.isFound()) {
1✔
120
      mavenPluginLog.debug(READING_INPUT_FROM + findFileInClassPath.getAbsoluteFilePath());
1✔
121

122
      return findFileInClassPath.getInputStream();
1✔
123
    }
124

125
    throw new FileNotFoundException(
1✔
126
        String.format(
1✔
127
            "Could not find %s, %s or %s in classpath",
128
            new File(file).getAbsolutePath(), new File(basePath, file).getAbsolutePath(), file));
1✔
129
  }
130
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc