Version 1.2.3: getVersion, openResource, fixes
authorNiki Roo <niki@nikiroo.be>
Mon, 27 Feb 2017 05:56:22 +0000 (06:56 +0100)
committerNiki Roo <niki@nikiroo.be>
Mon, 27 Feb 2017 05:56:22 +0000 (06:56 +0100)
- IOUtils: getVerion, openResource
- fixes in configure

VERSION
changelog
configure.sh
src/be/nikiroo/utils/IOUtils.java
src/be/nikiroo/utils/Progress.java
src/be/nikiroo/utils/test/IOUtilsTest.java [new file with mode: 0644]
src/be/nikiroo/utils/test/Test.java
src/be/nikiroo/utils/test/TestCase.java
src/be/nikiroo/utils/test/TestLauncher.java

diff --git a/VERSION b/VERSION
index 23aa8390630cc1ae7f102ddd472e4ba48b689844..0495c4a88caed0f036ffab0948c17d4b5fdc96c1 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-1.2.2
+1.2.3
index 4bcb5a44661c067f2059617ed9a73b0f6985037d..1d6aa51a16c04bdff9fca759e74f30e9d3ed3b71 100644 (file)
--- 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
 -------------
 
index 0ac4e2e423e10bceb187396910a9d4701c531bc7..713c9f46fe52430a99e25af94298fde825ae661e 100755 (executable)
@@ -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
index 92e312874599c6652454551bdd096cbc7b531226..1b70b0e783cee077af91067a07e792b6eb5af993 100644 (file)
@@ -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;
index 8b6e10e5fc97ca6e5b2748aeb58c4829d80a3287..473e80662a4a816a1fb8af21d82421c654f6ee3d 100644 (file)
@@ -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 (file)
index 0000000..5046940
--- /dev/null
@@ -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());
+                       }
+               });
+       }
+}
index 4340f5f8ac18db11d08a7425a043d7aa4436c571..9833225ed2d1e9f61338cf5d824930b5fb045dbb 100644 (file)
@@ -11,6 +11,7 @@ public class Test extends TestLauncher {
 
                addSeries(new ProgressTest(args));
                addSeries(new BundleTest(args));
+               addSeries(new IOUtilsTest(args));
        }
 
        /**
index e4860fa11f51e0f5e4cda5d366fffa8db0eef285..b37ea987ffca27677a32eb5d31d05e2632974c52 100644 (file)
@@ -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.
index b6118df103ee68711273e30b0ae33d3517883855..10a6e2794811036a12f21239d254f7dd3e2c483b 100644 (file)
@@ -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);
                        }