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

ljacqu / wordeval / 14890732001

07 May 2025 06:33PM UTC coverage: 60.614% (-0.8%) from 61.437%
14890732001

push

github

ljacqu
Fix line processor tests not to depend on the dictionary files

392 of 702 branches covered (55.84%)

0 of 2 new or added lines in 1 file covered. (0.0%)

26 existing lines in 5 files now uncovered.

968 of 1597 relevant lines covered (60.61%)

3.41 hits per line

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

21.05
/src/main/java/ch/jalu/wordeval/DataUtils.java
1
package ch.jalu.wordeval;
2

3
import com.google.gson.Gson;
4
import com.google.gson.GsonBuilder;
5
import lombok.Getter;
6
import lombok.NoArgsConstructor;
7
import lombok.Setter;
8
import org.springframework.stereotype.Component;
9

10
import java.io.IOException;
11
import java.io.UncheckedIOException;
12
import java.lang.reflect.Type;
13
import java.nio.file.Files;
14
import java.nio.file.Paths;
15
import java.util.List;
16
import java.util.stream.Stream;
17

18
/**
19
 * Wrapper for interaction with the file system and JSON operations.
20
 */
21
@Component
22
@NoArgsConstructor
23
public class DataUtils {
24
  
25
  @Getter
26
  @Setter
27
  private boolean jsonPrettyPrint;
28
  
29
  @Getter(lazy = true)
30
  private final Gson gson = createGson();
31
  
32
  /**
33
   * Writes the content to the given file.
34
   *
35
   * @param filename the name of the file to write to
36
   * @param content the content to write
37
   */
38
  public void writeToFile(String filename, String content) {
39
    try {
40
      Files.write(Paths.get(filename), content.getBytes());
×
41
    } catch (IOException e) {
×
42
      throw new UncheckedIOException("Could not write to file '" + filename + "'", e);
×
43
    }
×
UNCOV
44
  }
×
45
  
46
  /**
47
   * Reads a file's contents as UTF-8.
48
   *
49
   * @param filename the name of the file to read
50
   * @return the contents of the file
51
   */
52
  public String readFile(String filename) {
53
    try {
54
      return Files.readString(Paths.get(filename));
×
55
    } catch (IOException e) {
×
UNCOV
56
      throw new UncheckedIOException("Could not read file '" + filename + "'", e);
×
57
    }
58
  }
59

60
  /**
61
   * Reads all lines of a file as UTF-8.
62
   *
63
   * @param filename the name of the file to read
64
   * @return the file's contents by line
65
   */
66
  public List<String> readAllLines(String filename) {
67
    try {
68
      return Files.readAllLines(Paths.get(filename));
×
69
    } catch (IOException e) {
×
UNCOV
70
      throw new UncheckedIOException("Could not read from file '" + filename + "'", e);
×
71
    }
72
  }
73

74
  /**
75
   * Returns all lines of the file read as UTF-8 in a stream that must be closed.
76
   *
77
   * @param filename the name of the file to read
78
   * @return stream of the file's lines
79
   */
80
  public Stream<String> lines(String filename) {
81
    try {
UNCOV
82
      return Files.lines(Paths.get(filename));
×
UNCOV
83
    } catch (IOException e) {
×
UNCOV
84
      throw new UncheckedIOException("Could not read from file '" + filename + "'", e);
×
85
    }
86
  }
87

88
  /**
89
   * Converts an object to its JSON representation.
90
   *
91
   * @param o the object to convert
92
   * @return the generated JSON
93
   */
94
  public String toJson(Object o) {
95
    return getGson().toJson(o);
5✔
96
  }
97

98
  /**
99
   * Deserializes JSON to the specified type.
100
   *
101
   * @param <T> the result type
102
   * @param json the JSON text to deserialize
103
   * @param typeOfT the type of the resulting object
104
   * @return resulting object
105
   */
106
  public <T> T fromJson(String json, Type typeOfT) {
UNCOV
107
    return getGson().fromJson(json, typeOfT);
×
108
  }
109
  
110
  private Gson createGson() {
111
    if (jsonPrettyPrint) {
3✔
112
      return new GsonBuilder().setPrettyPrinting().create();
6✔
113
    }
114
    return new Gson();
4✔
115
  }
116

117
}
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

© 2026 Coveralls, Inc