Skip to content

Commit 32c7251

Browse files
committed
Add jacoco & increase code coverage
1 parent 3f81efb commit 32c7251

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

build.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ plugins {
22
// Apply the java-library plugin for API and implementation separation.
33
`java-library`
44
`maven-publish` // Add ./gradlew publishToMavenLocal
5+
jacoco
56
}
67

78
version = "0.0.3"
@@ -47,6 +48,11 @@ tasks.named<Test>("test") {
4748
events("passed", "skipped", "failed")
4849
showStandardStreams = true
4950
}
51+
52+
finalizedBy(tasks.jacocoTestReport) // report is always generated after tests run
53+
}
54+
tasks.jacocoTestReport {
55+
dependsOn(tasks.test) // tests are required to run before generating the report
5056
}
5157

5258

src/main/java/fr/formiko/utils/FLUFiles.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ private FLUFiles() {} // hide constructor
5151

5252
public static boolean openWebLinkInBrowser(String url) { return false; }
5353

54+
// Internal class to hide implementation
5455
private static class FLUFilesInternal {
5556
private FLUFilesInternal() {} // hide constructor
5657

src/main/java/fr/formiko/utils/FLUOS.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ public class FLUOS {
1515

1616
/**
1717
* Singleton pattern.
18-
* Private constructor to prevent instantiation.
18+
* Private constructor to prevent instantiation. (Protected for testing)
1919
*/
20-
private FLUOS() {
21-
String osName = System.getProperty("os.name").toLowerCase();
20+
protected FLUOS(String osName) {
21+
instance = this;
2222
if (osName.contains("win")) {
2323
os = OS.WINDOWS;
2424
} else if (osName.contains("mac")) {
@@ -29,6 +29,7 @@ private FLUOS() {
2929
os = OS.OTHER;
3030
}
3131
}
32+
private FLUOS() { this(System.getProperty("os.name").toLowerCase()); }
3233

3334
public static @Nonnull OS getOS() { return instance.os; }
3435
public static boolean isWindows() { return instance.os == OS.WINDOWS; }

src/test/java/fr/formiko/utils/FLUFilesTest.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ void testCreateFiles(String path, boolean shouldWork) {
4040
private static Stream<Arguments> testCreateFilesSource() {
4141
return Stream.of(Arguments.of(TEST_PATH_TEMPORARY + "testCreateFiles1.txt", true),
4242
Arguments.of(TEST_PATH_TEMPORARY + "testCreateFiles2.png", true), Arguments.of(TEST_PATH_TEMPORARY + "éà@--", true),
43-
Arguments.of(TEST_PATH + "existingFile.x", false), Arguments.of(TEST_PATH_TEMPORARY + "/2/3/4/t", true));
43+
Arguments.of(TEST_PATH + "existingFile.x", false), Arguments.of(TEST_PATH_TEMPORARY + "/2/3/4/t", true),
44+
Arguments.of(null, false));
4445
}
4546

4647
@ParameterizedTest
@@ -54,7 +55,7 @@ void testCreateDirectory(String path, boolean shouldWork) {
5455
private static Stream<Arguments> testCreateDirectorySource() {
5556
return Stream.of(Arguments.of(TEST_PATH_TEMPORARY + "testCreateDirectory1", true),
5657
Arguments.of(TEST_PATH_TEMPORARY + "testCreateDirectory2", true), Arguments.of(TEST_PATH_TEMPORARY + "éàOP%u%", true),
57-
Arguments.of(TEST_PATH, false), Arguments.of(TEST_PATH_TEMPORARY + "DIR/2/3/4/t/", true));
58+
Arguments.of(TEST_PATH, false), Arguments.of(TEST_PATH_TEMPORARY + "DIR/2/3/4/t/", true), Arguments.of(null, false));
5859
}
5960

6061
@ParameterizedTest
@@ -68,7 +69,8 @@ void testDelete(String path, boolean shouldWork, String pathToCreate) {
6869

6970
private static Stream<Arguments> testDeleteSource() {
7071
return Stream.of(Arguments.of(TEST_PATH_TEMPORARY, true, TEST_PATH_TEMPORARY + "P/2/3/4"),
71-
Arguments.of(TEST_PATH_TEMPORARY, false, null), Arguments.of(TEST_PATH + "unexistingDirectory", false, null));
72+
Arguments.of(TEST_PATH_TEMPORARY, false, null), Arguments.of(TEST_PATH + "unexistingDirectory", false, null),
73+
Arguments.of(null, false, null));
7274
}
7375

7476
@ParameterizedTest
@@ -84,21 +86,25 @@ private static Stream<Arguments> testCopySource() {
8486
return Stream.of(Arguments.of(TEST_PATH + "existingFile.x", true, TEST_PATH_TEMPORARY + "existingFile.x"), // normal copy
8587
Arguments.of(TEST_PATH + "unexistingFile.x", false, TEST_PATH_TEMPORARY + "unexistingFile.x"), // copy of missing file
8688
Arguments.of(TEST_PATH_TEMPORARY + "existingFile.x", false, TEST_PATH_TEMPORARY + "existingFile.x"), // don't exist here
87-
Arguments.of(TEST_PATH + "existingFile.x", false, TEST_PATH + "existingFile.x")); // same location
89+
Arguments.of(TEST_PATH + "existingFile.x", false, TEST_PATH + "existingFile.x"), // same location
90+
Arguments.of(null, false, TEST_PATH), Arguments.of(TEST_PATH + "existingFile.x", false, ""));
8891
}
8992

9093
@ParameterizedTest
9194
@MethodSource("testMoveSource")
9295
void testMove(String source, boolean shouldWork, String destination) {
93-
assertEquals(true, FLUFiles.createFile(source));
96+
if (source != null) {
97+
assertEquals(true, FLUFiles.createFile(source));
98+
}
9499
assertEquals(shouldWork, FLUFiles.move(source, destination));
95100
if (shouldWork) {
96101
assertEquals(true, FLUFiles.delete(destination));
97102
}
98103
}
99104

100105
private static Stream<Arguments> testMoveSource() {
101-
return Stream.of(Arguments.of(TEST_PATH_TEMPORARY + "RTYUIFile.x", true, TEST_PATH_TEMPORARY + "DTCFile.x"));
106+
return Stream.of(Arguments.of(TEST_PATH_TEMPORARY + "RTYUIFile.x", true, TEST_PATH_TEMPORARY + "DTCFile.x"),
107+
Arguments.of(null, false, TEST_PATH), Arguments.of(TEST_PATH_TEMPORARY + "exzbnkistingFile.x", false, null));
102108
}
103109

104110
@ParameterizedTest
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
package fr.formiko.utils;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
34
import static org.junit.jupiter.api.Assertions.assertNotNull;
45
import static org.junit.jupiter.api.Assertions.assertTrue;
56
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.CsvSource;
69

710
public class FLUOSTest {
811
@Test
912
void testNull() { assertNotNull(FLUOS.getOS()); }
1013

1114
@Test
1215
void testIsX() { assertTrue(FLUOS.isWindows() || FLUOS.isLinux() || FLUOS.isMac() || FLUOS.isOther()); }
16+
17+
@ParameterizedTest
18+
@CsvSource({"Windowsx64, true, false, false, false", "Debian-Linux, false, true, false, false", "MacOS56, false, false, true, false",
19+
"X, false, false, false, true", "Onix, false, true, false, false", "maix, false, true, false, false"})
20+
void testOS(String osName, boolean isWindows, boolean isLinux, boolean isMac, boolean isOther) {
21+
assertEquals(isWindows, new FLUOSTestInstance(osName).isWindows());
22+
assertEquals(isLinux, new FLUOSTestInstance(osName).isLinux());
23+
assertEquals(isMac, new FLUOSTestInstance(osName).isMac());
24+
assertEquals(isOther, new FLUOSTestInstance(osName).isOther());
25+
}
26+
27+
class FLUOSTestInstance extends FLUOS {
28+
public FLUOSTestInstance(String osName) { super(osName.toLowerCase()); }
29+
}
1330
}

0 commit comments

Comments
 (0)