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

ljacqu / wordeval / 14562498097

20 Apr 2025 07:08PM UTC coverage: 54.64% (-0.6%) from 55.233%
14562498097

push

github

ljacqu
Create tests for appData, prevent AppData from being initialized directly

270 of 570 branches covered (47.37%)

8 of 9 new or added lines in 3 files covered. (88.89%)

26 existing lines in 5 files now uncovered.

736 of 1347 relevant lines covered (54.64%)

3.24 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 {
NEW
39
      Files.write(Paths.get(filename), content.getBytes());
×
40
    } catch (IOException e) {
×
41
      throw new UncheckedIOException("Could not write to file '" + filename + "'", e);
×
42
    }
×
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 {
53
      return Files.readString(Paths.get(filename));
×
54
    } catch (IOException e) {
×
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
  // todo: should not be static
66
  public static List<String> readAllLines(String filename) {
67
    try {
68
      return Files.readAllLines(Paths.get(filename));
×
69
    } catch (IOException e) {
×
70
      throw new UncheckedIOException("Could not read from file '" + filename + "'", e);
×
71
    }
72
  }
73
  
74
  /**
75
   * Converts an object to its JSON representation.
76
   *
77
   * @param o the object to convert
78
   * @return the generated JSON
79
   */
80
  public String toJson(Object o) {
81
    return getGson().toJson(o);
5✔
82
  }
83

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

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