diff --git a/pom.xml b/pom.xml index 7128918..ad32318 100644 --- a/pom.xml +++ b/pom.xml @@ -162,6 +162,14 @@ 5.4.2 test + + + org.mockito + mockito-all + 1.9.5 + test + + \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java index b9befe0..c2f130a 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -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!"); - } - } + } diff --git a/src/main/java/config/MKVToolProperties.java b/src/main/java/config/MKVToolProperties.java index c4953ed..0f515c8 100644 --- a/src/main/java/config/MKVToolProperties.java +++ b/src/main/java/config/MKVToolProperties.java @@ -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 stream = Files.lines(Paths.get("mkvDirectoryPath"))) { - directoryPath = stream.collect(Collectors.joining("\n")); - }catch(IOException e) { - log.fatal(e.getMessage()); - } + private static MKVToolProperties instance = null; - if(!directoryPath.endsWith("\\")) { - directoryPath += "\\"; + private MKVToolProperties() { + } + + 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 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(); } } diff --git a/src/test/java/config/MKVToolPropertiesTest.java b/src/test/java/config/MKVToolPropertiesTest.java index cd07add..850f2e9 100644 --- a/src/test/java/config/MKVToolPropertiesTest.java +++ b/src/test/java/config/MKVToolPropertiesTest.java @@ -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"); + } }