Version 1.3.4
authorNiki Roo <niki@nikiroo.be>
Sun, 5 Mar 2017 11:25:28 +0000 (12:25 +0100)
committerNiki Roo <niki@nikiroo.be>
Sun, 5 Mar 2017 11:25:28 +0000 (12:25 +0100)
- Improve TestCase error reporting: we know display the full stack trace
  even for AssertionErrors
- Extends Version with new methods: isOlderThan(Version) and
  isNewerThan(Version)

changelog
src/be/nikiroo/utils/Version.java
src/be/nikiroo/utils/test/VersionTest.java

index 3e55a30dd25c348f600cde975252fd06fc6f21e7..588ef48a977107d8fb7d9e073c949c4dd8257c3b 100644 (file)
--- a/changelog
+++ b/changelog
@@ -1,3 +1,12 @@
+Version 1.3.4
+-------------
+
+Improve TestCase error reporting
+       We know display the full stack trace even for AssertionErrors
+
+Extends Version
+       ...with new methods: isOlderThan(Version) and isNewerThan(Version)
+
 Version 1.3.3
 -------------
 
index 613fcfe54130ef04d4df068091d7e52a26879002..94387373e980daf30b3ca4de01cfa72e490a0307 100644 (file)
@@ -9,12 +9,19 @@ import java.io.InputStream;
  * 
  * @author niki
  */
-public class Version {
+public class Version implements Comparable<Version> {
        private String version;
        private int major;
        private int minor;
        private int patch;
 
+       /**
+        * Create a new, empty {@link Version}.
+        * 
+        */
+       public Version() {
+       }
+
        /**
         * Create a new {@link Version} with the given values.
         * 
@@ -96,12 +103,46 @@ public class Version {
         * <p>
         * An empty {@link Version} is always <tt>0.0.0</tt>.
         * 
-        * @return TRUS if it is empty
+        * @return TRUE if it is empty
         */
        public boolean isEmpty() {
                return major == 0 && minor == 0 && patch == 0;
        }
 
+       /**
+        * Check if we are more recent than the given {@link Version}.
+        * 
+        * @param o
+        *            the other {@link Version}
+        * @return TRUE if this {@link Version} is more recent than the given one
+        */
+       public boolean isNewerThan(Version o) {
+               if (major > o.major) {
+                       return true;
+               }
+
+               if (major == o.major && minor > o.minor) {
+                       return true;
+               }
+
+               if (major == o.major && minor == o.minor && patch > o.patch) {
+                       return true;
+               }
+
+               return false;
+       }
+
+       /**
+        * Check if we are older than the given {@link Version}.
+        * 
+        * @param o
+        *            the other {@link Version}
+        * @return TRUE if this {@link Version} is older than the given one
+        */
+       public boolean isOlderThan(Version o) {
+               return !equals(o) && !isNewerThan(o);
+       }
+
        /**
         * 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
@@ -131,6 +172,31 @@ public class Version {
                return new Version(version);
        }
 
+       public int compareTo(Version o) {
+               if (equals(o)) {
+                       return 0;
+               } else if (isNewerThan(o)) {
+                       return 1;
+               } else {
+                       return -1;
+               }
+       }
+
+       @Override
+       public boolean equals(Object obj) {
+               if (obj instanceof Version) {
+                       Version o = (Version) obj;
+                       return o.major == major && o.minor == minor && o.patch == patch;
+               }
+
+               return false;
+       }
+
+       @Override
+       public int hashCode() {
+               return version == null ? 0 : version.hashCode();
+       }
+
        /**
         * Return a user-readable form of this {@link Version}.
         */
index 75e2cdf04fecff6afce1d2bf5a935aae5d4b231b..376bd4428e5e16151e3bdd7940c4f9ed203f1d32 100644 (file)
@@ -51,5 +51,29 @@ class VersionTest extends TestLauncher {
                                                Version.getCurrentVersion().isEmpty());
                        }
                });
+
+               addTest(new TestCase("Comparing versions") {
+                       @Override
+                       public void test() throws Exception {
+                               assertEquals(true,
+                                               new Version(1, 1, 1).isNewerThan(new Version(1, 1, 0)));
+                               assertEquals(true,
+                                               new Version(2, 0, 0).isNewerThan(new Version(1, 1, 1)));
+                               assertEquals(true,
+                                               new Version(10, 7, 8).isNewerThan(new Version(9, 9, 9)));
+                               assertEquals(true,
+                                               new Version(0, 0, 0).isOlderThan(new Version(0, 0, 1)));
+                               assertEquals(1,
+                                               new Version(1, 1, 1).compareTo(new Version(0, 1, 1)));
+                               assertEquals(-1,
+                                               new Version(0, 0, 1).compareTo(new Version(0, 1, 1)));
+                               assertEquals(0,
+                                               new Version(0, 0, 1).compareTo(new Version(0, 0, 1)));
+                               assertEquals(true,
+                                               new Version(0, 0, 1).equals(new Version(0, 0, 1)));
+                               assertEquals(false,
+                                               new Version(0, 2, 1).equals(new Version(0, 0, 1)));
+                       }
+               });
        }
 }