| 1 | package be.nikiroo.utils.test_code; |
| 2 | |
| 3 | import be.nikiroo.utils.Version; |
| 4 | import be.nikiroo.utils.test.TestCase; |
| 5 | import be.nikiroo.utils.test.TestLauncher; |
| 6 | |
| 7 | class VersionTest extends TestLauncher { |
| 8 | public VersionTest(String[] args) { |
| 9 | super("Version test", args); |
| 10 | |
| 11 | addTest(new TestCase("String <-> int") { |
| 12 | @Override |
| 13 | public void test() throws Exception { |
| 14 | assertEquals("Cannot parse version 1.2.3 from int to String", |
| 15 | "1.2.3", new Version(1, 2, 3).toString()); |
| 16 | assertEquals( |
| 17 | "Cannot parse major version \"1.2.3\" from String to int", |
| 18 | 1, new Version("1.2.3").getMajor()); |
| 19 | assertEquals( |
| 20 | "Cannot parse minor version \"1.2.3\" from String to int", |
| 21 | 2, new Version("1.2.3").getMinor()); |
| 22 | assertEquals( |
| 23 | "Cannot parse patch version \"1.2.3\" from String to int", |
| 24 | 3, new Version("1.2.3").getPatch()); |
| 25 | } |
| 26 | }); |
| 27 | |
| 28 | addTest(new TestCase("Bad input") { |
| 29 | @Override |
| 30 | public void test() throws Exception { |
| 31 | assertEquals( |
| 32 | "Bad input should return an empty version", |
| 33 | true, |
| 34 | new Version( |
| 35 | "Doors 98 SE Special Deluxe Edition Pro++ Not-Home") |
| 36 | .isEmpty()); |
| 37 | |
| 38 | assertEquals( |
| 39 | "Bad input should return [unknown]", |
| 40 | "[unknown]", |
| 41 | new Version( |
| 42 | "Doors 98 SE Special Deluxe Edition Pro++ Not-Home") |
| 43 | .toString()); |
| 44 | } |
| 45 | }); |
| 46 | |
| 47 | addTest(new TestCase("Read current version") { |
| 48 | @Override |
| 49 | public void test() throws Exception { |
| 50 | assertNotNull("The version should not be NULL (in any case!)", |
| 51 | Version.getCurrentVersion()); |
| 52 | assertEquals("The current version should not be empty", false, |
| 53 | Version.getCurrentVersion().isEmpty()); |
| 54 | } |
| 55 | }); |
| 56 | |
| 57 | addTest(new TestCase("Tag version") { |
| 58 | @Override |
| 59 | public void test() throws Exception { |
| 60 | Version version = new Version("1.0.0-debian0"); |
| 61 | assertEquals("debian", version.getTag()); |
| 62 | assertEquals(0, version.getTagVersion()); |
| 63 | version = new Version("1.0.0-debian.0"); |
| 64 | assertEquals("debian.", version.getTag()); |
| 65 | assertEquals(0, version.getTagVersion()); |
| 66 | version = new Version("1.0.0-debian-0"); |
| 67 | assertEquals("debian-", version.getTag()); |
| 68 | assertEquals(0, version.getTagVersion()); |
| 69 | version = new Version("1.0.0-debian-12"); |
| 70 | assertEquals("debian-", version.getTag()); |
| 71 | assertEquals(12, version.getTagVersion()); |
| 72 | |
| 73 | // tag with no tag version |
| 74 | version = new Version("1.0.0-dev"); |
| 75 | assertEquals(1, version.getMajor()); |
| 76 | assertEquals(0, version.getMinor()); |
| 77 | assertEquals(0, version.getPatch()); |
| 78 | assertEquals("dev", version.getTag()); |
| 79 | assertEquals(-1, version.getTagVersion()); |
| 80 | } |
| 81 | }); |
| 82 | |
| 83 | addTest(new TestCase("Comparing versions") { |
| 84 | @Override |
| 85 | public void test() throws Exception { |
| 86 | assertEquals(true, |
| 87 | new Version(1, 1, 1).isNewerThan(new Version(1, 1, 0))); |
| 88 | assertEquals(true, |
| 89 | new Version(2, 0, 0).isNewerThan(new Version(1, 1, 1))); |
| 90 | assertEquals(true, |
| 91 | new Version(10, 7, 8).isNewerThan(new Version(9, 9, 9))); |
| 92 | assertEquals(true, |
| 93 | new Version(0, 0, 0).isOlderThan(new Version(0, 0, 1))); |
| 94 | assertEquals(1, |
| 95 | new Version(1, 1, 1).compareTo(new Version(0, 1, 1))); |
| 96 | assertEquals(-1, |
| 97 | new Version(0, 0, 1).compareTo(new Version(0, 1, 1))); |
| 98 | assertEquals(0, |
| 99 | new Version(0, 0, 1).compareTo(new Version(0, 0, 1))); |
| 100 | assertEquals(true, |
| 101 | new Version(0, 0, 1).equals(new Version(0, 0, 1))); |
| 102 | assertEquals(false, |
| 103 | new Version(0, 2, 1).equals(new Version(0, 0, 1))); |
| 104 | |
| 105 | assertEquals(true, |
| 106 | new Version(1, 0, 1, "my.tag.", 2).equals(new Version( |
| 107 | 1, 0, 1, "my.tag.", 2))); |
| 108 | assertEquals(false, |
| 109 | new Version(1, 0, 1, "my.tag.", 2).equals(new Version( |
| 110 | 1, 0, 0, "my.tag.", 2))); |
| 111 | assertEquals(false, |
| 112 | new Version(1, 0, 1, "my.tag.", 2).equals(new Version( |
| 113 | 1, 0, 1, "not-my.tag.", 2))); |
| 114 | } |
| 115 | }); |
| 116 | |
| 117 | addTest(new TestCase("toString") { |
| 118 | @Override |
| 119 | public void test() throws Exception { |
| 120 | // Check leading 0s: |
| 121 | Version version = new Version("01.002.4"); |
| 122 | assertEquals("Leading 0s not working", "1.2.4", |
| 123 | version.toString()); |
| 124 | |
| 125 | // Check spacing |
| 126 | version = new Version("1 . 2.4 "); |
| 127 | assertEquals("Additional spaces not working", "1.2.4", |
| 128 | version.toString()); |
| 129 | |
| 130 | String[] tests = new String[] { "1.0.0", "1.2.3", "1.0.0-dev", |
| 131 | "1.1.2-niki0" }; |
| 132 | for (String test : tests) { |
| 133 | version = new Version(test); |
| 134 | assertEquals("toString and back conversion failed", test, |
| 135 | version.toString()); |
| 136 | } |
| 137 | } |
| 138 | }); |
| 139 | } |
| 140 | } |