Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/main/java/com/nlstn/jmediaOrganizer/JMediaOrganizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.logging.log4j.Logger;

import com.nlstn.jmediaOrganizer.gui.Window;
import com.nlstn.jmediaOrganizer.properties.InvalidLaunchConfigurationException;
import com.nlstn.jmediaOrganizer.properties.LaunchConfiguration;
import com.nlstn.jmediaOrganizer.properties.ProjectProperties;
import com.nlstn.jmediaOrganizer.properties.Settings;
Expand Down Expand Up @@ -59,15 +60,26 @@ public class JMediaOrganizer {
private static File inputFolder = null;

public static void main(String[] args) {
Settings.loadSettings();
ProjectProperties.loadProjectProperties();
LaunchConfiguration config = LaunchConfiguration.parse(args);
log.info("Starting " + ProjectProperties.getName() + " v" + ProjectProperties.getVersion());
if (config.isHeadlessModeEnabled())
headlessHandlerFactory.get();
else
window = new Window();
}
Settings.loadSettings();
ProjectProperties.loadProjectProperties();
try {
LaunchConfiguration config = LaunchConfiguration.parse(args);
log.info("Starting " + ProjectProperties.getName() + " v" + ProjectProperties.getVersion());
if (config.isHeadlessModeEnabled())
headlessHandlerFactory.get();
else
window = new Window();
}
catch (InvalidLaunchConfigurationException e) {
log.error(e.getMessage());
System.err.println(e.getMessage());
String helpText = e.getHelpText();
if (helpText != null && !helpText.isBlank()) {
System.err.println(helpText);
}
System.exit(1);
}
}

static void setHeadlessHandlerFactory(Supplier<HeadlessHandler> factory) {
headlessHandlerFactory = factory == null ? HeadlessHandler::new : factory;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.nlstn.jmediaOrganizer.properties;

/**
* Exception representing invalid launch configuration arguments.
*/
public class InvalidLaunchConfigurationException extends IllegalArgumentException {

private static final long serialVersionUID = 1L;

private final String helpText;

public InvalidLaunchConfigurationException(String message, String helpText) {
super(message);
this.helpText = helpText;
}

public InvalidLaunchConfigurationException(String message, String helpText, Throwable cause) {
super(message, cause);
this.helpText = helpText;
}

public String getHelpText() {
return helpText;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.nlstn.jmediaOrganizer.properties;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;

import org.apache.commons.cli.CommandLine;
Expand Down Expand Up @@ -67,35 +69,33 @@ private LaunchConfiguration(String[] args) {
CommandLineParser parser = new DefaultParser();
help = new HelpFormatter();

try {
cmd = parser.parse(options, args);
}
catch (ParseException e) {
System.out.println(e.getMessage());
help.printHelp("JMediaOrganizer", options);
Runtime.getRuntime().exit(-1);
}
try {
cmd = parser.parse(options, args);
}
catch (ParseException e) {
throw new InvalidLaunchConfigurationException(e.getMessage(), buildHelpText(), e);
}
processArgs();
}

private void processArgs() {
headlessMode = cmd.hasOption("h");

// If headless mode is enabled, input folder has to be supplied via command args
if (headlessMode && !cmd.hasOption("i")) {
System.out.println("You need to specify an input folder (-i), if you run in headless mode!");
help.printHelp("JMediaOrganizer", options);
Runtime.getRuntime().exit(-1);
}
if (headlessMode) {
log.debug("Enabled headlessMode");
}
if (cmd.hasOption("i")) {
String inputFolderString = cmd.getOptionValue("i");
File inputFolder = new File(inputFolderString);
if (!(inputFolder.exists() && inputFolder.isDirectory())) {
log.error("Invalid input folder!");
help.printHelp("JMediaOrganizer", options);
private void processArgs() {
headlessMode = cmd.hasOption("h");

// If headless mode is enabled, input folder has to be supplied via command args
if (headlessMode && !cmd.hasOption("i")) {
throw new InvalidLaunchConfigurationException(
"You need to specify an input folder (-i), if you run in headless mode!",
buildHelpText());
}
if (headlessMode) {
log.debug("Enabled headlessMode");
}
if (cmd.hasOption("i")) {
String inputFolderString = cmd.getOptionValue("i");
File inputFolder = new File(inputFolderString);
if (!(inputFolder.exists() && inputFolder.isDirectory())) {
log.error("Invalid input folder!");
help.printHelp("JMediaOrganizer", options);
}
else {
JMediaOrganizer.setInputFolder(inputFolder);
Expand Down Expand Up @@ -137,11 +137,20 @@ private void processArgs() {
log.debug("Setting outputFolder to {}", outFile.getAbsolutePath());
}
}
if (cmd.hasOption("t")) {
Settings.setInvalidTypes(Arrays.asList(cmd.getOptionValue("t").split(";")));
log.debug("Setting invalidTypes to {}", cmd.getOptionValue("t"));
}
}
if (cmd.hasOption("t")) {
Settings.setInvalidTypes(Arrays.asList(cmd.getOptionValue("t").split(";")));
log.debug("Setting invalidTypes to {}", cmd.getOptionValue("t"));
}
}

private String buildHelpText() {
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
help.printHelp(printWriter, HelpFormatter.DEFAULT_WIDTH, "JMediaOrganizer", null, options,
HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD, null, false);
printWriter.flush();
return writer.toString();
}

public boolean isHeadlessModeEnabled() {
return headlessMode;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.nlstn.jmediaOrganizer.properties;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class LaunchConfigurationTest {

@Test
public void parseThrowsWhenHeadlessInputMissing() {
InvalidLaunchConfigurationException exception = assertThrows(
InvalidLaunchConfigurationException.class,
() -> LaunchConfiguration.parse(new String[] { "-h" }));

assertTrue(exception.getMessage().contains("input folder"));
assertNotNull(exception.getHelpText());
assertFalse(exception.getHelpText().isBlank());
}

@Test
public void parseThrowsWhenUnknownOptionSupplied() {
InvalidLaunchConfigurationException exception = assertThrows(
InvalidLaunchConfigurationException.class,
() -> LaunchConfiguration.parse(new String[] { "--unknown" }));

assertTrue(exception.getMessage().contains("Unrecognized option"));
assertNotNull(exception.getHelpText());
assertFalse(exception.getHelpText().isBlank());
}
}