-
-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix JUnit warnings on test runs (#7378)
* init commit * fix firework warning * fix & update purge test * fix backup cleanup * fix imports * change names
- Loading branch information
Showing
7 changed files
with
118 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 56 additions & 30 deletions
86
src/test/java/org/skriptlang/skript/test/tests/files/BackupPurgeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,77 @@ | ||
package org.skriptlang.skript.test.tests.files; | ||
|
||
import ch.njol.skript.util.FileUtils; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertThrows; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class BackupPurgeTest { | ||
|
||
@Test | ||
public void testPurge() throws IOException { | ||
File dir = new File("plugins/Skript/backups/"); | ||
if (!dir.exists()) { | ||
dir.mkdir(); | ||
} | ||
private static final Path FOLDER = Path.of("plugins", "Skript", "backups"); | ||
private static final Path VARIABLES = Path.of("plugins", "Skript", "variables.csv"); | ||
|
||
File vars = new File("plugins/Skript/variables.csv"); | ||
if (!vars.exists()) { | ||
vars.createNewFile(); | ||
@Before | ||
public void setup() throws IOException { | ||
if (Files.exists(FOLDER)) { | ||
clearFolder(); | ||
} else { | ||
Files.createDirectory(FOLDER); | ||
} | ||
|
||
// Create Filler Files to be used for testing | ||
for (int i = 0; i < 100; i++) { | ||
(new File(dir, ("PurgeTest_"+i))).createNewFile(); | ||
Files.createFile(FOLDER.resolve("purge test " + i)); | ||
} | ||
|
||
if (!Files.exists(VARIABLES)) { | ||
Files.createFile(VARIABLES); | ||
} | ||
} | ||
|
||
@Test | ||
public void testPurge() throws IOException { | ||
try (Stream<Path> files = Files.list(FOLDER)) { | ||
assertEquals(100, files.count()); | ||
} | ||
// Test 100 filler files were created | ||
assertEquals("Filler Files != 100", 100, (new ArrayList<File>(Arrays.asList(dir.listFiles()))).size()); | ||
|
||
// Test 'backupPurge' method deleting to 50 | ||
FileUtils.backupPurge(vars, 50); | ||
assertEquals("Backup Purge did not delete down to 50", 50, (new ArrayList<File>(Arrays.asList(dir.listFiles()))).size()); | ||
testBackupPurge(50); | ||
testBackupPurge(20); | ||
testBackupPurge(0); | ||
|
||
assertThrows(IllegalArgumentException.class, () -> FileUtils.backupPurge(VARIABLES.toFile(), -1)); | ||
} | ||
|
||
@After | ||
public void cleanUp() throws IOException { | ||
clearFolder(); | ||
} | ||
|
||
// Test 'backupPurge' method deleting to 20 | ||
FileUtils.backupPurge(vars, 20); | ||
assertEquals("Backup Purge did not delete down to 20", 20, (new ArrayList<File>(Arrays.asList(dir.listFiles()))).size()); | ||
private static void clearFolder() throws IOException { | ||
try (Stream<Path> list = Files.list(FOLDER)) { | ||
list.forEach(path -> { | ||
try { | ||
Files.delete(path); | ||
} catch (IOException ignored) { | ||
|
||
// Test 'backupPurge' method deleting all files | ||
FileUtils.backupPurge(vars, 0); | ||
assertEquals("Backup Purge did not delete all files", 0, (new ArrayList<File>(Arrays.asList(dir.listFiles()))).size()); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
// Test calling with invalid input | ||
assertThrows("Backup Purge did not throw exception for invalid input", IllegalArgumentException.class, () -> FileUtils.backupPurge(vars, -1)); | ||
private static void testBackupPurge(int toKeep) throws IOException { | ||
FileUtils.backupPurge(VARIABLES.toFile(), toKeep); | ||
|
||
try (Stream<Path> files = Files.list(FOLDER)) { | ||
long count = files.count(); | ||
assertNotNull(files); | ||
assertEquals("backup purge did not delete all files", toKeep, count); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters