test(file-cleaner): added unit tests

This commit is contained in:
Begerad, Stefan 2021-08-09 01:41:21 -04:00
parent 57ee8288da
commit b2e61d7397
6 changed files with 105 additions and 77 deletions

View File

@ -9,21 +9,39 @@
<version>0.0.1-SNAPSHOT</version>
<properties>
<!-- https://maven.apache.org/general.html#encoding-warning -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- junit 5, unit test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.9.0</version>
<version>2.14.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--The default maven-surefire-plugin is outdated,
make sure update to the latest.-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
@ -45,9 +63,11 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8</source>
<target>8</target>

View File

@ -1,38 +1,53 @@
package de.begerad.fileagerm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import static de.begerad.fileagerm.Main.LOG;
public class FileCleaner {
public final static Logger LOG = LoggerFactory.getLogger(FileCleaner.class);
/**
* Clean-up files older than a certain time
*
* @param age age of files to be cleaned up
* @param dirPath directory path of files to be cleaned up
*/
public static void deleteFilesByAge(int age, String dirPath) {
public static int deleteFilesByAge(int age, String dirPath) {
int count = 0;
LOG.debug("dirPath: {}", dirPath);
File directory = new File(dirPath);
if (directory.exists()) {
File[] listFiles = FileExplorer.listFiles(dirPath);
LOG.debug("listFiles.length: {}", listFiles.length);
long purgeTime = System.currentTimeMillis() - (age * 24L * 60L * 60L * 1000L);
for (File listFile : listFiles) {
if (listFile.lastModified() < purgeTime) {
if (!listFile.delete()) {
LOG.warn("file could NOT be deleted: {}", listFile.getName());
if (listFiles.length > 0) {
long purgeTime = System.currentTimeMillis()
- (age * 24L * 60L * 60L * 1000L);
for (File listFile : listFiles) {
if (listFile.lastModified() < purgeTime) {
if (!listFile.delete()) {
LOG.warn("file could NOT be deleted: {}",
listFile.getName());
} else {
LOG.debug("file deleted: {}",
listFile.getName());
count++;
}
} else {
LOG.debug("file deleted: {}", listFile.getName());
LOG.debug("file is too young to be deleted: {}",
listFile.getName());
}
} else {
LOG.debug("file is too young to be deleted: {}", listFile.getName());
}
} else {
LOG.debug("NO files present");
}
} else {
LOG.warn("directory does not exist: {}", directory);
}
return count;
}
}

View File

@ -2,42 +2,8 @@ package de.begerad.fileagerm;
import java.io.File;
import java.io.FileFilter;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class FileExplorer {
private final String directory;
public FileExplorer(String directory) {
this.directory = directory;
}
/**
* @return set containing list of file paths
*/
public Set<String> listFilesUsingJavaIO() {
return Stream.of(new File(directory).listFiles())
.filter(file -> !file.isDirectory())
.map(File::getName)
.collect(Collectors.toSet());
}
/**
* @return array containing list of File objects
*/
public File[] listFilesUsingFileFilter() {
//Creating a File object for directory
File dir = new File(directory);
//Creating filter for directory files
FileFilter fileFilter = new FileFilter() {
public boolean accept(File dir) {
return dir.isFile();
}
};
return dir.listFiles(fileFilter);
}
/**
* @return array containing list of File objects

View File

@ -6,7 +6,6 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Set;
import static de.begerad.fileagerm.FileCleaner.deleteFilesByAge;
@ -31,39 +30,14 @@ public class Main {
+ " is NOT a valid directory.");
return;
}
try {
System.out.println("dir path: "
+ (new File(dirPath).getCanonicalPath()));
LOG.debug("dir path: {}",
(new File(dirPath).getCanonicalPath()));
} catch (IOException e) {
e.printStackTrace();
}
FileExplorer fileList = new FileExplorer(dirPath);
File[] list = fileList.listFilesUsingFileFilter();
LOG.debug("List of files in the specified directory:");
if (list != null) {
for (File file : list) {
LOG.debug("file name: {}", file.getName());
LOG.debug("file path: {}", file.getPath());
LOG.debug("file abs path: {}", file.getAbsolutePath());
try {
LOG.debug("file can path: {}", file.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
LOG.warn("List of files is empty");
}
Set<String> setList = fileList.listFilesUsingJavaIO();
LOG.debug("List of files in the specified directory:");
for (String file : setList) {
LOG.debug("{}", file);
}
int age = 1;
long purgeTime = System.currentTimeMillis() - (age * 24L * 60L * 60L * 1000L);
LOG.debug("age in ms: {}", purgeTime);
@ -73,7 +47,8 @@ public class Main {
purgeTime = cal.getTimeInMillis();
LOG.debug("age in ms: {}", purgeTime);
deleteFilesByAge(age, dirPath);
int filesDeleted = deleteFilesByAge(age, dirPath);
LOG.debug("# files deleted: " + filesDeleted);
LOG.debug("main() done.");
}

View File

@ -1,4 +1,50 @@
package de.begerad.fileagerm;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestFileCleaner {
private final static int AGE_YEAR = 365;
private final static int AGE_HALF_YEAR = 183;
private final static int AGE_ZERO = 0;
private final static String DIR_PATH_TMP = "/tmp";
private final static String DIR_PATH_VAR = "/var";
private final static String DIR_PATH_DOT_NPM = "/root/.npm";
private final static String DIR_PATH_TMP_DEL = "/tmp/del";
@Test
public void testZeroAge() {
int count = FileCleaner.deleteFilesByAge(AGE_YEAR,
DIR_PATH_TMP);
assertEquals(0, count);
}
@Test
public void testZeroFiles() {
int count = FileCleaner.deleteFilesByAge(AGE_YEAR,
DIR_PATH_VAR);
assertEquals(0, count);
}
@Test
public void testDirDoesNotExist() {
int count = FileCleaner.deleteFilesByAge(AGE_YEAR,
DIR_PATH_DOT_NPM);
assertEquals(0, count);
}
@Test
public void testRootDir() {
int count = FileCleaner.deleteFilesByAge(AGE_HALF_YEAR,
"/usr/local/share/fonts/");
assertEquals(0, count);
}
@Test
public void testFileDeleted() {
int count = FileCleaner.deleteFilesByAge(AGE_ZERO,
DIR_PATH_TMP_DEL);
assertEquals(1, count);
}
}

View File

@ -11,11 +11,17 @@ public class TestFileExplorer {
@Test
public void testEmptyListFiles() {
File[] files = FileExplorer.listFiles("/mnt");
assertTrue(files.length == 0);
assertEquals(0, files.length);
}
@Test
public void testNullListFiles() {
assertNull(FileExplorer.listFiles("/etc/hosts"));
}
@Test
public void testLengthListFiles() {
File[] files = FileExplorer.listFiles("/var/www/test/public_html");
assertEquals(1, files.length);
}
}