From: Niki Roo Date: Mon, 27 Feb 2017 05:56:22 +0000 (+0100) Subject: Version 1.2.3: getVersion, openResource, fixes X-Git-Tag: nikiroo-utils-1.2.3^0 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=16d593780fa5a4c39cc36b29382da610eae951da;p=nikiroo-utils.git Version 1.2.3: getVersion, openResource, fixes - IOUtils: getVerion, openResource - fixes in configure --- diff --git a/VERSION b/VERSION index 23aa839..0495c4a 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.2.2 +1.2.3 diff --git a/changelog b/changelog index 4bcb5a4..1d6aa51 100644 --- a/changelog +++ b/changelog @@ -1,3 +1,18 @@ +Version 1.2.3 +------------- + +Add openResource and getVersion in IOUtils + The file VERSION is supposed to exist + +Give more informartion on AssertErrors + The TestCase were not always helpful in case of AssertExceptions; they + now print the stacktrace (they only used to do it for non-assert + exceptions) + +Fix configure.sh + The VERSION file was not added, the Main method was not the correct + one (so it was not producing working runnable JAR, yet it stated so) + Version 1.2.2 ------------- diff --git a/configure.sh b/configure.sh index 0ac4e2e..713c9f4 100755 --- a/configure.sh +++ b/configure.sh @@ -44,13 +44,13 @@ else fi; -echo "MAIN = be/nikiroo/utils/resources/TransBundle" > Makefile -echo "MORE = be/nikiroo/utils/StringUtils be/nikiroo/utils/IOUtils be/nikiroo/utils/MarkableFileInputStream be/nikiroo/utils/ui/UIUtils be/nikiroo/utils/ui/WrapLayout be/nikiroo/utils/ui/ProgressBar be/nikiroo/utils/test/TestLauncher" >> Makefile +echo "MAIN = be/nikiroo/utils/test/Test" > Makefile +echo "MORE = be/nikiroo/utils/StringUtils be/nikiroo/utils/IOUtils be/nikiroo/utils/MarkableFileInputStream be/nikiroo/utils/ui/UIUtils be/nikiroo/utils/ui/WrapLayout be/nikiroo/utils/ui/ProgressBar be/nikiroo/utils/resources/TransBundle" >> Makefile echo "TEST = be/nikiroo/utils/test/Test" >> Makefile echo "TEST_PARAMS = $cols $ok $ko" >> Makefile echo "NAME = nikiroo-utils" >> Makefile echo "PREFIX = $PREFIX" >> Makefile -echo "JAR_FLAGS += -C bin/ be" >> Makefile +echo "JAR_FLAGS += -C bin/ be -C bin/ VERSION" >> Makefile echo "SJAR_FLAGS += -C src/ be" >> Makefile cat Makefile.base >> Makefile diff --git a/src/be/nikiroo/utils/IOUtils.java b/src/be/nikiroo/utils/IOUtils.java index 92e3128..1b70b0e 100644 --- a/src/be/nikiroo/utils/IOUtils.java +++ b/src/be/nikiroo/utils/IOUtils.java @@ -5,6 +5,7 @@ import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -232,7 +233,7 @@ public class IOUtils { * @throws IOException * in case of IO error */ - static public BufferedImage toImage(InputStream in) throws IOException { + public static BufferedImage toImage(InputStream in) throws IOException { MarkableFileInputStream tmpIn = null; File tmp = null; try { @@ -329,6 +330,48 @@ public class IOUtils { return image; } + /** + * Return the version of the program if it follows the VERSION convention + * (i.e., if it has a file called VERSION containing the version as a + * {@link String} on its binary root). + * + * @return the version, or NULL + */ + public static String getVersion() { + String version = null; + + InputStream in = openResource("VERSION"); + if (in != null) { + try { + ByteArrayOutputStream ba = new ByteArrayOutputStream(); + write(in, ba); + in.close(); + + version = ba.toString("UTF-8"); + } catch (IOException e) { + } + } + + return version; + } + + /** + * Open the given /-separated resource (from the binary root). + * + * @param name + * the resource name + * + * @return the opened resource if found, NLL if not + */ + public static InputStream openResource(String name) { + ClassLoader loader = IOUtils.class.getClassLoader(); + if (loader == null) { + loader = ClassLoader.getSystemClassLoader(); + } + + return loader.getResourceAsStream(name); + } + /** * Return the EXIF transformation flag of this image if any. * @@ -344,7 +387,7 @@ public class IOUtils { * @throws IOException * in case of IO error */ - static private int getExifTransorm(InputStream in) throws IOException { + private static int getExifTransorm(InputStream in) throws IOException { int[] exif_data = new int[100]; int set_flag = 0; int is_motorola = 0; diff --git a/src/be/nikiroo/utils/Progress.java b/src/be/nikiroo/utils/Progress.java index 8b6e10e..473e806 100644 --- a/src/be/nikiroo/utils/Progress.java +++ b/src/be/nikiroo/utils/Progress.java @@ -270,7 +270,7 @@ public class Progress { name = Progress.this.name; } - setTotalProgress(name, (int) (total * (max - min))); + setTotalProgress(name, (int) Math.round(total * (max - min))); } }); diff --git a/src/be/nikiroo/utils/test/IOUtilsTest.java b/src/be/nikiroo/utils/test/IOUtilsTest.java new file mode 100644 index 0000000..5046940 --- /dev/null +++ b/src/be/nikiroo/utils/test/IOUtilsTest.java @@ -0,0 +1,31 @@ +package be.nikiroo.utils.test; + +import java.io.InputStream; + +import be.nikiroo.utils.IOUtils; + +public class IOUtilsTest extends TestLauncher { + + public IOUtilsTest(String[] args) { + super("IOUtils test", args); + + addTest(new TestCase("openResource") { + @Override + public void test() throws Exception { + InputStream in = IOUtils.openResource("VERSION"); + assertNotNull( + "The VERSION file is supposed to be present in the binaries", + in); + in.close(); + } + }); + + addTest(new TestCase("getVersion") { + @Override + public void test() throws Exception { + assertNotNull("The VERSION is not defined", + IOUtils.getVersion()); + } + }); + } +} diff --git a/src/be/nikiroo/utils/test/Test.java b/src/be/nikiroo/utils/test/Test.java index 4340f5f..9833225 100644 --- a/src/be/nikiroo/utils/test/Test.java +++ b/src/be/nikiroo/utils/test/Test.java @@ -11,6 +11,7 @@ public class Test extends TestLauncher { addSeries(new ProgressTest(args)); addSeries(new BundleTest(args)); + addSeries(new IOUtilsTest(args)); } /** diff --git a/src/be/nikiroo/utils/test/TestCase.java b/src/be/nikiroo/utils/test/TestCase.java index e4860fa..b37ea98 100644 --- a/src/be/nikiroo/utils/test/TestCase.java +++ b/src/be/nikiroo/utils/test/TestCase.java @@ -233,6 +233,29 @@ abstract public class TestCase { assertEquals(errorMessage, new Double(expected), new Double(actual)); } + /** + * Check that given {@link Object} is not NULL. + * + * @param the + * error message to display if it is NULL + * @param actual + * the actual value + * + * @throws AssertException + * in case they differ + */ + public void assertNotNull(String errorMessage, Object actual) + throws AssertException { + if (actual == null) { + if (errorMessage == null) { + errorMessage = String.format("" // + + "Assertion failed!\n" // + + "Object should have been NULL: [%s]", actual); + } + throw new AssertException(errorMessage); + } + } + /** * Generate the default assert message for 2 different values that were * supposed to be equals. diff --git a/src/be/nikiroo/utils/test/TestLauncher.java b/src/be/nikiroo/utils/test/TestLauncher.java index b6118df..10a6e27 100644 --- a/src/be/nikiroo/utils/test/TestLauncher.java +++ b/src/be/nikiroo/utils/test/TestLauncher.java @@ -5,8 +5,6 @@ import java.io.StringWriter; import java.util.ArrayList; import java.util.List; -import be.nikiroo.utils.test.TestCase.AssertException; - /** * A {@link TestLauncher} starts a series of {@link TestCase}s and displays the * result to the user. @@ -305,13 +303,10 @@ public class TestLauncher { private void print(int depth, Exception error) { if (error != null) { System.out.println(" " + koString); - String lines = error.getMessage() + ""; - if (!(error instanceof AssertException)) { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - error.printStackTrace(pw); - lines = sw.toString(); - } + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + error.printStackTrace(pw); + String lines = sw.toString(); for (String line : lines.split("\n")) { System.out.println(prefix(depth, false) + "\t\t" + line); }