Version 1.3.1: new Version class
authorNiki Roo <niki@nikiroo.be>
Mon, 27 Feb 2017 07:07:21 +0000 (08:07 +0100)
committerNiki Roo <niki@nikiroo.be>
Mon, 27 Feb 2017 07:07:21 +0000 (08:07 +0100)
VERSION
changelog
src/be/nikiroo/utils/IOUtils.java
src/be/nikiroo/utils/Version.java [new file with mode: 0644]
src/be/nikiroo/utils/test/BundleTest.java
src/be/nikiroo/utils/test/IOUtilsTest.java
src/be/nikiroo/utils/test/ProgressTest.java
src/be/nikiroo/utils/test/Test.java
src/be/nikiroo/utils/test/VersionTest.java [new file with mode: 0644]

diff --git a/VERSION b/VERSION
index 0495c4a88caed0f036ffab0948c17d4b5fdc96c1..3a3cd8cc8b079cb410a465d2925b9cbd703115cb 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-1.2.3
+1.3.1
index 1d6aa51a16c04bdff9fca759e74f30e9d3ed3b71..02cb917e49246eb029452e019d5bc5760dce805a 100644 (file)
--- a/changelog
+++ b/changelog
@@ -1,3 +1,9 @@
+Version 1.3.1
+-------------
+
+New Version class
+       Which can parse versions from the running program
+
 Version 1.2.3
 -------------
 
index 1b70b0e783cee077af91067a07e792b6eb5af993..eb105e2698535e325ed602fc0b3ed50f7e0b16b4 100644 (file)
@@ -5,7 +5,6 @@ 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;
@@ -330,31 +329,6 @@ 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).
         * 
diff --git a/src/be/nikiroo/utils/Version.java b/src/be/nikiroo/utils/Version.java
new file mode 100644 (file)
index 0000000..550c5e9
--- /dev/null
@@ -0,0 +1,140 @@
+package be.nikiroo.utils;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * This class describe a program {@link Version}.
+ * 
+ * @author niki
+ */
+public class Version {
+       private String version;
+       private int major;
+       private int minor;
+       private int patch;
+
+       /**
+        * Create a new {@link Version} with the given values.
+        * 
+        * @param major
+        *            the major version
+        * @param minor
+        *            the minor version
+        * @param patch
+        *            the patch version
+        */
+       public Version(int major, int minor, int patch) {
+               this.major = major;
+               this.minor = minor;
+               this.patch = patch;
+               this.version = String.format("%d.%d.%d", major, minor, patch);
+       }
+
+       /**
+        * Create a new {@link Version} with the given value, which must be in the
+        * form <tt>MAJOR.MINOR.PATCH</tt>.
+        * 
+        * @param version
+        *            the version (<tt>MAJOR.MINOR.PATCH</tt>)
+        */
+       public Version(String version) {
+               this.version = version;
+               try {
+                       String[] tab = version.split("\\.");
+                       this.major = Integer.parseInt(tab[0]);
+                       this.minor = Integer.parseInt(tab[1]);
+                       this.patch = Integer.parseInt(tab[2]);
+               } catch (Exception e) {
+                       this.major = 0;
+                       this.minor = 0;
+                       this.patch = 0;
+               }
+       }
+
+       /**
+        * The 'major' version.
+        * <p>
+        * This version should only change when API-incompatible changes are made to
+        * the program.
+        * 
+        * @return the major version
+        */
+       public int getMajor() {
+               return major;
+       }
+
+       /**
+        * The 'minor' version.
+        * <p>
+        * This version should only change when new, backwards-compatible
+        * functionality has been added to the program.
+        * 
+        * @return the minor version
+        */
+       public int getMinor() {
+               return minor;
+       }
+
+       /**
+        * The 'patch' version.
+        * <p>
+        * This version should change when backwards-compatible bugfixes have been
+        * added to the program.
+        * 
+        * @return the patch version
+        */
+       public int getPatch() {
+               return patch;
+       }
+
+       /**
+        * Check if this {@link Version} is "empty" (i.e., the version was not
+        * parse-able or not given).
+        * <p>
+        * An empty {@link Version} is always <tt>0.0.0</tt>.
+        * 
+        * @return TRUS if it is empty
+        */
+       public boolean isEmpty() {
+               return major == 0 && minor == 0 && patch == 0;
+       }
+
+       /**
+        * Return the version of the running program if it follows the VERSION
+        * convention (i.e., if it has a file called VERSION containing the version
+        * as a {@link String} in its binary root, and if this {@link String}
+        * follows the Major/Minor/Patch convention).
+        * <p>
+        * If it does not, return an empty {@link Version} object.
+        * 
+        * @return the {@link Version} of the program, or an empty {@link Version}
+        *         (does not return NULL)
+        */
+       public static String getCurrentVersion() {
+               String version = null;
+
+               InputStream in = IOUtils.openResource("VERSION");
+               if (in != null) {
+                       try {
+                               ByteArrayOutputStream ba = new ByteArrayOutputStream();
+                               IOUtils.write(in, ba);
+                               in.close();
+
+                               version = ba.toString("UTF-8").trim();
+                       } catch (IOException e) {
+                       }
+               }
+
+               return version;
+       }
+
+       /**
+        * Return a user-readable form of this {@link Version}.
+        */
+       @Override
+       public String toString() {
+               return version == null ? "[unknown]" : version;
+       }
+}
index 6a2fdaa5847b331f648ad356dc787750c00ce807..119f3b9d6e496a305df52467ea0e9ff2650468f5 100644 (file)
@@ -7,7 +7,7 @@ import be.nikiroo.utils.resources.Bundle;
 import be.nikiroo.utils.resources.Bundles;
 import be.nikiroo.utils.resources.Meta;
 
-public class BundleTest extends TestLauncher {
+class BundleTest extends TestLauncher {
        private File tmp;
        private B b = new B();
 
index 50469400f87e7746aabcbe1ec53a50749a5c8f69..c62d44d3603624fc7407ab6bf5d6e691a194fd51 100644 (file)
@@ -4,8 +4,7 @@ import java.io.InputStream;
 
 import be.nikiroo.utils.IOUtils;
 
-public class IOUtilsTest extends TestLauncher {
-
+class IOUtilsTest extends TestLauncher {
        public IOUtilsTest(String[] args) {
                super("IOUtils test", args);
 
@@ -19,13 +18,5 @@ public class IOUtilsTest extends TestLauncher {
                                in.close();
                        }
                });
-
-               addTest(new TestCase("getVersion") {
-                       @Override
-                       public void test() throws Exception {
-                               assertNotNull("The VERSION is not defined",
-                                               IOUtils.getVersion());
-                       }
-               });
        }
 }
index 2194c237b625434422dc342654cf93ef5ac2e175..ff46bd6944d6df4721858c2d012c3a3b2ea3fb40 100644 (file)
@@ -2,7 +2,7 @@ package be.nikiroo.utils.test;
 
 import be.nikiroo.utils.Progress;
 
-public class ProgressTest extends TestLauncher {
+class ProgressTest extends TestLauncher {
        public ProgressTest(String[] args) {
                super("Progress reporting", args);
 
index 9833225ed2d1e9f61338cf5d824930b5fb045dbb..df53648ce125ae32be26eba468df310189056fb8 100644 (file)
@@ -12,6 +12,7 @@ public class Test extends TestLauncher {
                addSeries(new ProgressTest(args));
                addSeries(new BundleTest(args));
                addSeries(new IOUtilsTest(args));
+               addSeries(new VersionTest(args));
        }
 
        /**
diff --git a/src/be/nikiroo/utils/test/VersionTest.java b/src/be/nikiroo/utils/test/VersionTest.java
new file mode 100644 (file)
index 0000000..8dc3ba3
--- /dev/null
@@ -0,0 +1,48 @@
+package be.nikiroo.utils.test;
+
+import be.nikiroo.utils.Version;
+
+class VersionTest extends TestLauncher {
+       public VersionTest(String[] args) {
+               super("Version test", args);
+
+               addTest(new TestCase("String <-> int") {
+                       @Override
+                       public void test() throws Exception {
+                               assertEquals("Cannot parse version 1.2.3 from int to String",
+                                               "1.2.3", new Version(1, 2, 3).toString());
+                               assertEquals(
+                                               "Cannot parse major version \"1.2.3\" from String to int",
+                                               1, new Version("1.2.3").getMajor());
+                               assertEquals(
+                                               "Cannot parse minor version \"1.2.3\" from String to int",
+                                               2, new Version("1.2.3").getMinor());
+                               assertEquals(
+                                               "Cannot parse patch version \"1.2.3\" from String to int",
+                                               3, new Version("1.2.3").getPatch());
+                       }
+               });
+
+               addTest(new TestCase("Bad input") {
+                       @Override
+                       public void test() throws Exception {
+                               assertEquals(
+                                               "Bad input should return an empty version",
+                                               true,
+                                               new Version(
+                                                               "Doors 98 SE Special Deluxe Edition Pro++ Not-Home")
+                                                               .isEmpty());
+                       }
+               });
+
+               addTest(new TestCase("Read current version") {
+                       @Override
+                       public void test() throws Exception {
+                               assertNotNull("The version should not be NULL (in any case!)",
+                                               Version.getCurrentVersion());
+                               assertEquals("The current version should not be empty", false,
+                                               Version.getCurrentVersion().isEmpty());
+                       }
+               });
+       }
+}