[REFACTOR] search mkvtoolnix path

This commit is contained in:
RatzzFatzz
2019-11-22 22:49:20 +01:00
parent 25e9fd6f3f
commit 6744597363
4 changed files with 84 additions and 45 deletions

View File

@@ -162,6 +162,14 @@
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<!-- endregion -->
</dependencies>
</project>

View File

@@ -1,42 +1,11 @@
import config.MKVToolProperties;
import lombok.extern.log4j.Log4j2;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
@Log4j2
public class Main {
public static void main(String[] args) {
File path = new File("mkvDirectoryPath");
MKVToolProperties prop;
if(!path.exists()) {
while(true) {
readPath();
prop = new MKVToolProperties();
if(prop.pathsAreValid()) {
break;
}
}
log.info("Path is valid!");
}else if(path.exists()) {
prop = new MKVToolProperties();
if(!prop.pathsAreValid()) {
readPath();
}
log.info("Path is valid!");
}
MKVToolProperties.getInstance().defineMKVToolNixPath();
}
private static void readPath() {
System.out.println("Please enter a valid path to mkvtoolnix!");
Scanner input = new Scanner(System.in);
try(PrintWriter out = new PrintWriter("mkvDirectoryPath", "UTF-8")){
out.print(input.nextLine());
}catch(FileNotFoundException | UnsupportedEncodingException e) {
log.error("File not found!");
}
}
}

View File

@@ -6,6 +6,7 @@ import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -15,25 +16,72 @@ public class MKVToolProperties {
private String mkvmergePath;
private String mkvpropeditPath;
public MKVToolProperties() {
try(Stream<String> stream = Files.lines(Paths.get("mkvDirectoryPath"))) {
directoryPath = stream.collect(Collectors.joining("\n"));
}catch(IOException e) {
log.fatal(e.getMessage());
private static MKVToolProperties instance = null;
private MKVToolProperties() {
}
if(!directoryPath.endsWith("\\")) {
directoryPath += "\\";
public static MKVToolProperties getInstance() {
if(instance == null){
instance = new MKVToolProperties();
}
return instance;
}
public void defineMKVToolNixPath() {
searchWithFilePath();
if(pathIsValid()){
log.info("MKVToolNix found!");
return;
}
log.debug("MKVToolNix not found in file!");
searchInDefaultPath();
if(pathIsValid()){
log.info("MKVToolNix found!");
return;
}
log.debug("MKVToolNix not found in default path!");
Scanner input = new Scanner(System.in);
while(true){
searchWithUserPath(input);
if(pathIsValid()){
log.info("MKVToolNix found!");
break;
}
}
log.error("MKVToolNix not found anywhere!");
}
private boolean pathIsValid() {
checkForSeparator();
setMKVmergeAndPropEditPath();
return new File(mkvmergePath).exists() && new File(mkvpropeditPath).exists();
}
private void checkForSeparator() {
if(! (directoryPath.endsWith("/") || directoryPath.endsWith("\\"))){
directoryPath += File.separator;
}
}
private void setMKVmergeAndPropEditPath() {
mkvmergePath = directoryPath + "mkvmerge.exe";
mkvpropeditPath = directoryPath + "mkvpropedit.exe";
}
private void searchWithFilePath() {
try(Stream<String> stream = Files.lines(Paths.get("mkvDirectoryPath"))){
directoryPath = stream.collect(Collectors.joining("\n"));
}catch(IOException e){
log.fatal(e.getMessage());
}
}
public boolean pathsAreValid() {
File mkvmergeFile = new File(mkvmergePath);
File mkvpropeditFile = new File(mkvpropeditPath);
private void searchInDefaultPath() {
return mkvmergeFile.exists() && mkvpropeditFile.exists();
}
private void searchWithUserPath(Scanner input) {
directoryPath = input.nextLine();
}
}

View File

@@ -2,15 +2,20 @@ package config;
import lombok.extern.log4j.Log4j2;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
@Log4j2
public class MKVToolPropertiesTest {
@Mock
Scanner input;
@Test
public void testPathIsValid() {
try(PrintWriter out = new PrintWriter("mkvDirectoryPath", "UTF-8")){
@@ -28,4 +33,13 @@ public class MKVToolPropertiesTest {
}
}
@Test
public void testCreateFilePath() {
// input = mock(Scanner.class);
// when(input.nextLine()).thenReturn("test\\resources\\");
// MKVToolProperties.createFilePath();
// MKVToolProperties prop = new MKVToolProperties();
// assertEquals(prop.getMkvmergePath(), "test\\resources\\mkvmerge.exe");
}
}