+ * We have to create a new instance of SimpleDateFormat every time as it is not thread safe. + * @return The date formatter. + */ + private static SimpleDateFormat getDateFormat() { + SimpleDateFormat format = new SimpleDateFormat("yy'w'ww", Locale.US); + format.setLenient(false); + return format; + } + + /** + * Retrieve the snapshot version within a week, starting at zero. + * @return The weekly version + */ + public int getSnapshotWeekVersion() { + return snapshotWeekVersion; + } + + /** + * Retrieve the week this snapshot was released. + * @return The week. + */ + public Date getSnapshotDate() { + return snapshotDate; + } + + /** + * Retrieve the raw snapshot string (yy'w'ww[a-z]). + * @return The snapshot string. + */ + public String getSnapshotString() { + return rawString; + } + + @Override + public int compareTo(SnapshotVersion o) { + if (o == null) + return 1; + + return ComparisonChain.start(). + compare(snapshotDate, o.getSnapshotDate()). + compare(snapshotWeekVersion, o.getSnapshotWeekVersion()). + result(); + } + + @Override + public boolean equals(Object obj) { + if (obj == this) + return true; + if (obj instanceof SnapshotVersion) { + SnapshotVersion other = (SnapshotVersion) obj; + return Objects.equal(snapshotDate, other.getSnapshotDate()) && + snapshotWeekVersion == other.getSnapshotWeekVersion(); + } + return false; + } + + @Override + public int hashCode() { + return Objects.hashCode(snapshotDate, snapshotWeekVersion); + } + + @Override + public String toString() { + return rawString; + } +} diff --git a/ProtocolLib/src/test/java/com/comphenix/protocol/MinecraftVersionTest.java b/ProtocolLib/src/test/java/com/comphenix/protocol/MinecraftVersionTest.java index 2d127c23..aa3a3691 100644 --- a/ProtocolLib/src/test/java/com/comphenix/protocol/MinecraftVersionTest.java +++ b/ProtocolLib/src/test/java/com/comphenix/protocol/MinecraftVersionTest.java @@ -22,9 +22,9 @@ import static org.junit.Assert.*; import org.junit.Test; import com.comphenix.protocol.utility.MinecraftVersion; +import com.comphenix.protocol.utility.SnapshotVersion; public class MinecraftVersionTest { - @Test public void testComparision() { MinecraftVersion within = new MinecraftVersion(1, 2, 5); @@ -38,6 +38,12 @@ public class MinecraftVersionTest { assertFalse(outside.compareTo(within) < 0 && outside.compareTo(highest) < 0); } + @Test + public void testSnapshotVersion() { + MinecraftVersion version = MinecraftVersion.fromServerVersion("git-Spigot-1119 (MC: 13w39b)"); + assertEquals(version.getSnapshot(), new SnapshotVersion("13w39b")); + } + public void testParsing() { assertEquals(MinecraftVersion.extractVersion("CraftBukkit R3.0 (MC: 1.4.3)"), "1.4.3"); assertEquals(MinecraftVersion.extractVersion("CraftBukkit Test Beta 1 (MC: 1.10.01 )"), "1.10.01"); diff --git a/ProtocolLib/src/test/java/com/comphenix/protocol/utility/SnapshotVersionTest.java b/ProtocolLib/src/test/java/com/comphenix/protocol/utility/SnapshotVersionTest.java new file mode 100644 index 00000000..534a5d2d --- /dev/null +++ b/ProtocolLib/src/test/java/com/comphenix/protocol/utility/SnapshotVersionTest.java @@ -0,0 +1,41 @@ +package com.comphenix.protocol.utility; + +import static org.junit.Assert.*; + +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; + +import org.junit.Test; + +public class SnapshotVersionTest { + @Test + public void testDates() { + SnapshotVersion a = new SnapshotVersion("12w50b"); + SnapshotVersion b = new SnapshotVersion("13w05a"); + + expect(a.getSnapshotDate(), 12, 50); + expect(b.getSnapshotDate(), 13, 5); + + // Test equality + assertEquals(a, new SnapshotVersion("12w50b")); + } + + @Test(expected=IllegalArgumentException.class) + public void testDateParsingProblem() { + // This date is not valid + new SnapshotVersion("12w80a"); + } + + @Test(expected=IllegalArgumentException.class) + public void testMissingWeekVersion() { + new SnapshotVersion("13w05"); + } + + private void expect(Date date, int year, int week) { + Calendar calendar = Calendar.getInstance(Locale.US); + calendar.setTime(date); + assertEquals(year, calendar.get(Calendar.YEAR) % 100); + assertEquals(week, calendar.get(Calendar.WEEK_OF_YEAR)); + } +}