mirror of
https://github.com/RatzzFatzz/MKVAudioSubtitleChanger.git
synced 2026-02-10 17:55:57 +01:00
Migrate from date to instant
This commit is contained in:
@@ -18,7 +18,7 @@ import java.util.regex.Pattern;
|
|||||||
public class FileFilter {
|
public class FileFilter {
|
||||||
private final Set<String> excluded;
|
private final Set<String> excluded;
|
||||||
private final Pattern includePattern;
|
private final Pattern includePattern;
|
||||||
private final Date filterDate;
|
private final Instant filterDate;
|
||||||
private final LastExecutionHandler lastExecutionHandler;
|
private final LastExecutionHandler lastExecutionHandler;
|
||||||
|
|
||||||
private final String EXTENSION_GROUP = "extension";
|
private final String EXTENSION_GROUP = "extension";
|
||||||
@@ -52,10 +52,6 @@ public class FileFilter {
|
|||||||
return includePattern.matcher(pathName.getName()).matches();
|
return includePattern.matcher(pathName.getName()).matches();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isNewer(File pathName, Date date) {
|
|
||||||
return isNewer(pathName, date.toInstant());
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isNewer(File pathName, Instant date) {
|
private boolean isNewer(File pathName, Instant date) {
|
||||||
if (date == null) return true;
|
if (date == null) return true;
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import org.apache.commons.lang3.SystemUtils;
|
|||||||
import picocli.CommandLine;
|
import picocli.CommandLine;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.time.Instant;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import picocli.CommandLine.Option;
|
import picocli.CommandLine.Option;
|
||||||
@@ -56,7 +57,7 @@ public class InputConfig implements CommandLine.IVersionProvider {
|
|||||||
@Option(names = {"-n", "--only-new-files"}, description = "ignores all files unchanged and previously processed")
|
@Option(names = {"-n", "--only-new-files"}, description = "ignores all files unchanged and previously processed")
|
||||||
private boolean onlyNewFiles;
|
private boolean onlyNewFiles;
|
||||||
@Option(names = {"-d", "--filter-date"}, defaultValue = Option.NULL_VALUE, description = "only consider files created newer than entered date (format: \"dd.MM.yyyy-HH:mm:ss\")")
|
@Option(names = {"-d", "--filter-date"}, defaultValue = Option.NULL_VALUE, description = "only consider files created newer than entered date (format: \"dd.MM.yyyy-HH:mm:ss\")")
|
||||||
private Date filterDate;
|
private Instant filterDate;
|
||||||
@Option(names = {"-i", "--include-pattern"}, defaultValue = ".*", description = "include files matching pattern")
|
@Option(names = {"-i", "--include-pattern"}, defaultValue = ".*", description = "include files matching pattern")
|
||||||
private Pattern includePattern;
|
private Pattern includePattern;
|
||||||
@Option(names = {"-e", "--exclude"}, arity = "1..*",
|
@Option(names = {"-e", "--exclude"}, arity = "1..*",
|
||||||
|
|||||||
@@ -1,29 +0,0 @@
|
|||||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.util;
|
|
||||||
|
|
||||||
import java.text.ParseException;
|
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
public class DateUtils {
|
|
||||||
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy-HH:mm:ss");
|
|
||||||
|
|
||||||
public static Date convert(long millis) {
|
|
||||||
return new Date(millis);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert String to date.
|
|
||||||
* @return parsed date, defaultDate if exception occurs
|
|
||||||
*/
|
|
||||||
public static Date convert(String date, Date defaultDate) {
|
|
||||||
try {
|
|
||||||
return dateFormat.parse(date);
|
|
||||||
} catch (ParseException e) {
|
|
||||||
return defaultDate;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String convert(Date date) {
|
|
||||||
return dateFormat.format(date);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
|
package at.pcgamingfreaks.mkvaudiosubtitlechanger.impl;
|
||||||
|
|
||||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.InputConfig;
|
|
||||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ResultStatistic;
|
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ResultStatistic;
|
||||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.DateUtils;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
@@ -14,11 +12,13 @@ import org.mockito.Mockito;
|
|||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.attribute.BasicFileAttributes;
|
import java.nio.file.attribute.BasicFileAttributes;
|
||||||
import java.util.Date;
|
import java.nio.file.attribute.FileTime;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
@@ -71,12 +71,14 @@ class FileFilterTest {
|
|||||||
when(file.toPath()).thenReturn(Path.of(TEST_FILE));
|
when(file.toPath()).thenReturn(Path.of(TEST_FILE));
|
||||||
|
|
||||||
long currentTime = System.currentTimeMillis();
|
long currentTime = System.currentTimeMillis();
|
||||||
FileFilter fileFilter = new FileFilter(excludedDirs, Pattern.compile(pattern), new Date(currentTime + filterDateOffset), null);
|
when(attributes.creationTime()).thenReturn(FileTime.fromMillis(currentTime));
|
||||||
|
when(attributes.lastModifiedTime()).thenReturn(FileTime.fromMillis(currentTime));
|
||||||
|
FileFilter fileFilter = new FileFilter(excludedDirs, Pattern.compile(pattern), Instant.ofEpochMilli(currentTime).plus(filterDateOffset, ChronoUnit.SECONDS), null);
|
||||||
|
|
||||||
try (MockedStatic<DateUtils> mockedFiles = Mockito.mockStatic(DateUtils.class)) {
|
try (MockedStatic<Files> mockedFiles = Mockito.mockStatic(Files.class)) {
|
||||||
mockedFiles
|
mockedFiles
|
||||||
.when(() -> DateUtils.convert(anyLong()))
|
.when(() -> Files.readAttributes(any(), eq(BasicFileAttributes.class)))
|
||||||
.thenReturn(new Date(currentTime));
|
.thenReturn(attributes);
|
||||||
|
|
||||||
assertEquals(acceptanceExpected, fileFilter.accept(file, new HashSet<>(extensions)), "File is accepted");
|
assertEquals(acceptanceExpected, fileFilter.accept(file, new HashSet<>(extensions)), "File is accepted");
|
||||||
assertEquals(excluded, ResultStatistic.getInstance().getExcluded() > 0, "Is counted in excluded statistic");
|
assertEquals(excluded, ResultStatistic.getInstance().getExcluded() > 0, "Is counted in excluded statistic");
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.util;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Disabled;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
|
|
||||||
@Disabled
|
|
||||||
class DateUtilsTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void convert() {
|
|
||||||
Date expectedDate = new Date(0);
|
|
||||||
String expectedString = "01.01.1970-01:00:00";
|
|
||||||
|
|
||||||
assertEquals(expectedDate, DateUtils.convert(0));
|
|
||||||
assertEquals(expectedDate, DateUtils.convert(expectedString, expectedDate));
|
|
||||||
assertEquals(expectedDate, DateUtils.convert("1234;15", expectedDate));
|
|
||||||
assertEquals(expectedString, DateUtils.convert(expectedDate));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user