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

ljacqu / wordeval / 14562965679

20 Apr 2025 08:11PM UTC coverage: 55.116% (+1.7%) from 53.367%
14562965679

push

github

ljacqu
Merge evaluator handlers to EvaluatorService

269 of 568 branches covered (47.36%)

4 of 6 new or added lines in 2 files covered. (66.67%)

87 existing lines in 6 files now uncovered.

738 of 1339 relevant lines covered (55.12%)

3.27 hits per line

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

25.0
/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

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

59
  /**
60
   * Reads all lines of a file as UTF-8.
61
   *
62
   * @param filename the name of the file to read
63
   * @return the file's contents by line
64
   */
65
  public List<String> readAllLines(String filename) {
66
    try {
UNCOV
67
      return Files.readAllLines(Paths.get(filename));
×
UNCOV
68
    } catch (IOException e) {
×
UNCOV
69
      throw new UncheckedIOException("Could not read from file '" + filename + "'", e);
×
70
    }
71
  }
72
  
73
  /**
74
   * Converts an object to its JSON representation.
75
   *
76
   * @param o the object to convert
77
   * @return the generated JSON
78
   */
79
  public String toJson(Object o) {
80
    return getGson().toJson(o);
5✔
81
  }
82

83
  /**
84
   * Deserializes JSON to the specified type.
85
   *
86
   * @param <T> the result type
87
   * @param json the JSON text to deserialize
88
   * @param typeOfT the type of the resulting object
89
   * @return resulting object
90
   */
91
  public <T> T fromJson(String json, Type typeOfT) {
UNCOV
92
    return getGson().fromJson(json, typeOfT);
×
93
  }
94
  
95
  private Gson createGson() {
96
    if (jsonPrettyPrint) {
3✔
97
      return new GsonBuilder().setPrettyPrinting().create();
6✔
98
    }
99
    return new Gson();
4✔
100
  }
101

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