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

ljacqu / wordeval / 14560448446

20 Apr 2025 02:30PM UTC coverage: 55.102% (+1.4%) from 53.745%
14560448446

push

github

ljacqu
Springify processing of evaluators

291 of 596 branches covered (48.83%)

51 of 63 new or added lines in 8 files covered. (80.95%)

57 existing lines in 6 files now uncovered.

756 of 1372 relevant lines covered (55.1%)

3.13 hits per line

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

53.85
/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 org.springframework.stereotype.Component;
8

9
import java.io.File;
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
3✔
25
  private String root = "";
26
  
27
  @Getter
3✔
28
  private boolean usePrettyPrint = false;
29
  
30
  @Getter(lazy = true)
5✔
31
  private final Gson gson = createGson();
32
  
33
  /**
34
   * Initializes an instance and sets whether to use JSON Pretty Print or not.
35
   *
36
   * @param usePrettyPrint JSON pretty print setting
37
   */
38
  public DataUtils(boolean usePrettyPrint) {
2✔
39
    this.usePrettyPrint = usePrettyPrint;
3✔
40
  }
1✔
41
  
42
  /**
43
   * Sets the root path (the path to append to file references).
44
   *
45
   * @param root the root to append to file paths
46
   */
47
  public void setRoot(String root) {
48
    if (!root.isEmpty() && !root.endsWith("/") && !root.endsWith(File.separator)) {
11!
49
      this.root = root + File.separator;
6✔
50
    } else {
51
      this.root = root; 
3✔
52
    }
53
  }
1✔
54
  
55
  /**
56
   * Writes the content to the given file.
57
   *
58
   * @param filename the name of the file to write to
59
   * @param content the content to write
60
   */
61
  public void writeToFile(String filename, String content) {
62
    try {
63
      Files.write(Paths.get(root + filename), content.getBytes());
×
64
    } catch (IOException e) {
×
65
      throw new UncheckedIOException("Could not write to file '" + filename + "'", e);
×
UNCOV
66
    }
×
UNCOV
67
  }
×
68
  
69
  /**
70
   * Reads a file's contents as UTF-8.
71
   *
72
   * @param filename the name of the file to read
73
   * @return the contents of the file
74
   */
75
  public String readFile(String filename) {
76
    try {
77
      return Files.readString(Paths.get(filename));
×
UNCOV
78
    } catch (IOException e) {
×
UNCOV
79
      throw new UncheckedIOException("Could not read file '" + filename + "'", e);
×
80
    }
81
  }
82

83
  /**
84
   * Reads all lines of a file as UTF-8.
85
   *
86
   * @param filename the name of the file to read
87
   * @return the file's contents by line
88
   */
89
  public static List<String> readAllLines(String filename) {
90
    try {
91
      return Files.readAllLines(Paths.get(filename));
×
UNCOV
92
    } catch (IOException e) {
×
UNCOV
93
      throw new UncheckedIOException("Could not read from file '" + filename + "'", e);
×
94
    }
95
  }
96
  
97
  /**
98
   * Converts an object to its JSON representation.
99
   *
100
   * @param o the object to convert
101
   * @return the generated JSON
102
   */
103
  public String toJson(Object o) {
104
    return getGson().toJson(o);
5✔
105
  }
106

107
  /**
108
   * Deserializes JSON to the specified type.
109
   *
110
   * @param <T> the result type
111
   * @param json the JSON text to deserialize
112
   * @param typeOfT the type of the resulting object
113
   * @return resulting object
114
   */
115
  public <T> T fromJson(String json, Type typeOfT) {
UNCOV
116
    return getGson().fromJson(json, typeOfT);
×
117
  }
118
  
119
  private Gson createGson() {
120
    if (usePrettyPrint) {
3✔
121
      return new GsonBuilder().setPrettyPrinting().create();
6✔
122
    }
123
    return new Gson();
4✔
124
  }
125

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