Skip to content

Commit

Permalink
minor changes to cli code
Browse files Browse the repository at this point in the history
  • Loading branch information
SingingBush committed Nov 14, 2023
1 parent 76ffa5f commit e2acd30
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright 2002-2004 Jeremias Maerki.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -15,11 +15,9 @@
*/
package org.krysalis.barcode4j.cli;

import org.apache.avalon.framework.ExceptionUtil;

/**
* Abstract base class for an exit handler for the CLI.
*
*
* @author Jeremias Maerki
* @version $Id: AbstractExitHandler.java,v 1.2 2004-09-04 20:25:58 jmaerki Exp $
*/
Expand All @@ -41,7 +39,7 @@ public void failureExit(Main app, String msg, Throwable t, int exitCode) {
app.printAppHeader();
Main.stderr.println(msg);
if (t != null) {
Main.stderr.println(ExceptionUtil.printStackTrace(t));
t.printStackTrace(Main.stderr); // Main.stderr.println(org.apache.avalon.framework.ExceptionUtil.printStackTrace(t));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright 2002-2004 Jeremias Maerki.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -17,19 +17,21 @@

/**
* Default exit handler that calls System.exit().
*
*
* @author Jeremias Maerki
* @version $Id: DefaultExitHandler.java,v 1.2 2004-09-04 20:25:58 jmaerki Exp $
*/
public class DefaultExitHandler extends AbstractExitHandler {

/** {@inheritDoc} */
@Override
public void failureExit(Main app, String msg, Throwable t, int exitCode) {
super.failureExit(app, msg, t, exitCode);
System.exit(exitCode);
}

/** {@inheritDoc} */
@Override
public void successfulExit(Main app) {
super.successfulExit(app);
System.exit(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public class Main {
* Main method.
* The application needs to be called with args. eg:
* <code>-s EAN128 -f "image/svg+xml" 12345679</code>
* For a list of available options use:
* <code>--help</code>
*
* @param args commandline arguments
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright 2002-2004 Jeremias Maerki.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -21,7 +21,6 @@
import java.net.URISyntaxException;
import java.nio.file.Paths;

import org.apache.avalon.framework.ExceptionUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -38,19 +37,21 @@ public class CommandLineTestCase {
private ByteArrayOutputStream err;
private ExitHandlerForTests exitHandler;

private void dumpResults() throws Exception {
System.out.println("Msg: " + this.exitHandler.getLastMsg());
System.out.println("Exit code: " + this.exitHandler.getLastExitCode());
if (this.exitHandler.getLastThrowable() != null) {
System.out.println(ExceptionUtil.printStackTrace(
this.exitHandler.getLastThrowable()));
}
System.out.println("--- stdout (" + this.out.size() + ") ---");
System.out.println(new String(this.out.toByteArray(), "US-ASCII"));
System.out.println("--- stderr (" + this.err.size() + ") ---");
System.out.println(new String(this.err.toByteArray(), "US-ASCII"));
System.out.println("---");
}
// private void dumpResults() throws Exception {
// System.out.println("Msg: " + this.exitHandler.getLastMsg());
// System.out.println("Exit code: " + this.exitHandler.getLastExitCode());
//
// final Throwable lastThrowable = this.exitHandler.getLastThrowable();
// if (lastThrowable != null) {
// lastThrowable.printStackTrace(System.out);
// //System.out.println(org.apache.avalon.framework.ExceptionUtil.printStackTrace(lastThrowable));
// }
// System.out.println("--- stdout (" + this.out.size() + ") ---");
// System.out.println(new String(this.out.toByteArray(), "US-ASCII"));
// System.out.println("--- stderr (" + this.err.size() + ") ---");
// System.out.println(new String(this.err.toByteArray(), "US-ASCII"));
// System.out.println("---");
// }

private void callCLI(String[] args) {
Main app = new Main();
Expand Down Expand Up @@ -174,23 +175,23 @@ void testToFile() throws Exception {
assertTrue(out.length() > 0, "Target file must not be empty");
if (!out.delete()) {
fail("Target file could not be deleted. Not closed?");
}
}
}

@Test
void testDPI() throws Exception {
File out100 = File.createTempFile("krba", ".tmp");
final String[] args100 = {"-s", "ean-13",
final String[] args100 = {"-s", "ean-13",
"-o", out100.getAbsolutePath(),
"-f", "jpeg",
"-f", "jpeg",
"-d", "100", "9771422985503+00006"};
callCLI(args100);

assertEquals(0, this.exitHandler.getLastExitCode(), "Exit code must be 0");
assertTrue(out100.exists(), "Target file does not exist");

File out300 = File.createTempFile("krba", ".tmp");
final String[] args300 = {"-s", "ean-13",
final String[] args300 = {"-s", "ean-13",
"-o", out300.getAbsolutePath(),
"-f", "jpeg",
"--dpi", "300", "9771422985503+00006"};
Expand All @@ -201,10 +202,10 @@ void testDPI() throws Exception {
assertTrue(out300.length() > out100.length(), "300dpi file must be greater than the 100dpi file");
if (!out100.delete()) {
fail("Target file could not be deleted. Not closed?");
}
}
if (!out300.delete()) {
fail("Target file could not be deleted. Not closed?");
}
}
}

/**
Expand Down

0 comments on commit e2acd30

Please sign in to comment.