From d50509001a04751166b290e6ba6bc99cf6133458 Mon Sep 17 00:00:00 2001 From: atomicint Date: Sun, 7 Feb 2016 03:49:19 -0500 Subject: [PATCH 1/4] Fix String attribute encoding delimiter --- .../org/apollo/game/model/entity/attr/StringAttribute.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/game/src/main/org/apollo/game/model/entity/attr/StringAttribute.java b/game/src/main/org/apollo/game/model/entity/attr/StringAttribute.java index c2f0454c..1158c09d 100644 --- a/game/src/main/org/apollo/game/model/entity/attr/StringAttribute.java +++ b/game/src/main/org/apollo/game/model/entity/attr/StringAttribute.java @@ -1,6 +1,7 @@ package org.apollo.game.model.entity.attr; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Arrays; /** @@ -31,11 +32,11 @@ public final class StringAttribute extends Attribute { @Override public byte[] encode() { - byte[] bytes = value.getBytes(Charset.forName("UTF-8")); + byte[] bytes = value.getBytes(StandardCharsets.UTF_8); int length = bytes.length; bytes = Arrays.copyOf(bytes, length + 1); - bytes[length - 1] = 0; + bytes[length] = 0; return bytes; } From 6c2edb8efd57b8cb7fe125736c48d36e55a2f87b Mon Sep 17 00:00:00 2001 From: atomicint Date: Sun, 7 Feb 2016 14:40:47 -0500 Subject: [PATCH 2/4] Add forLong attribute definition --- .../game/model/entity/attr/AttributeDefinition.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/game/src/main/org/apollo/game/model/entity/attr/AttributeDefinition.java b/game/src/main/org/apollo/game/model/entity/attr/AttributeDefinition.java index 9f08acbb..6a47996f 100644 --- a/game/src/main/org/apollo/game/model/entity/attr/AttributeDefinition.java +++ b/game/src/main/org/apollo/game/model/entity/attr/AttributeDefinition.java @@ -42,6 +42,17 @@ public final class AttributeDefinition { return new AttributeDefinition<>(defaultValue, persistence, AttributeType.LONG); } + /** + * Creates an AttributeDefinition for a {@code long}. + * + * @param defaultValue The default value of the definition. + * @param persistence The {@link AttributePersistence} of the definition. + * @return The AttributeDefinition. + */ + public static AttributeDefinition forLong(long defaultValue, AttributePersistence persistence) { + return new AttributeDefinition<>(defaultValue, persistence, AttributeType.LONG); + } + /** * Creates an AttributeDefinition for a String. * From 0c10acafdd2666637246dff86722a279905dfcee Mon Sep 17 00:00:00 2001 From: atomicint Date: Sun, 7 Feb 2016 14:41:22 -0500 Subject: [PATCH 3/4] Fix class cast exception when creating int attributes --- .../main/org/apollo/game/model/entity/attr/AttributeMap.java | 2 +- .../main/org/apollo/game/model/entity/attr/StringAttribute.java | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/game/src/main/org/apollo/game/model/entity/attr/AttributeMap.java b/game/src/main/org/apollo/game/model/entity/attr/AttributeMap.java index 5937274b..0ee12779 100644 --- a/game/src/main/org/apollo/game/model/entity/attr/AttributeMap.java +++ b/game/src/main/org/apollo/game/model/entity/attr/AttributeMap.java @@ -113,7 +113,7 @@ public final class AttributeMap { private Attribute createAttribute(T value, AttributeType type) { switch (type) { case LONG: - return new NumericalAttribute((Long) value); + return new NumericalAttribute(((Number) value).longValue()); case DOUBLE: return new NumericalAttribute((Double) value); case STRING: diff --git a/game/src/main/org/apollo/game/model/entity/attr/StringAttribute.java b/game/src/main/org/apollo/game/model/entity/attr/StringAttribute.java index 1158c09d..2f6062bf 100644 --- a/game/src/main/org/apollo/game/model/entity/attr/StringAttribute.java +++ b/game/src/main/org/apollo/game/model/entity/attr/StringAttribute.java @@ -1,6 +1,5 @@ package org.apollo.game.model.entity.attr; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.Arrays; From 38486f9e3e83a78930f68ab6b29b46d3e6393d2c Mon Sep 17 00:00:00 2001 From: atomicint Date: Sun, 7 Feb 2016 14:41:48 -0500 Subject: [PATCH 4/4] Add unit test for Attribute#encode Fix typo in Attribute unit test Ensure test method names are consistent with others in Apollo More clear tests --- .../model/entity/attr/AttributeTests.java | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 game/src/test/org/apollo/game/model/entity/attr/AttributeTests.java diff --git a/game/src/test/org/apollo/game/model/entity/attr/AttributeTests.java b/game/src/test/org/apollo/game/model/entity/attr/AttributeTests.java new file mode 100644 index 00000000..bbd656f8 --- /dev/null +++ b/game/src/test/org/apollo/game/model/entity/attr/AttributeTests.java @@ -0,0 +1,78 @@ +package org.apollo.game.model.entity.attr; + +import java.nio.charset.StandardCharsets; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.google.common.primitives.Longs; + +/** + * Tests for different {@link Attribute}s. + */ +public final class AttributeTests { + + /** + * Defines Attributes with their default values before any tests are run. + */ + @Before + public void setup() { + AttributeMap.define("string_test", AttributeDefinition.forString("Hello world", AttributePersistence.TRANSIENT)); + AttributeMap.define("boolean_test", AttributeDefinition.forBoolean(true, AttributePersistence.TRANSIENT)); + AttributeMap.define("int_test", AttributeDefinition.forInt(Integer.MAX_VALUE, AttributePersistence.TRANSIENT)); + AttributeMap.define("long_test", AttributeDefinition.forLong(Long.MAX_VALUE, AttributePersistence.TRANSIENT)); + AttributeMap.define("double_test", AttributeDefinition.forDouble(Double.MAX_VALUE, AttributePersistence.TRANSIENT)); + } + + /** + * Tests Attribute String encoding. + */ + @Test + public void stringEncode() { + AttributeMap map = new AttributeMap(); + byte[] expected = "Hello world\0".getBytes(StandardCharsets.UTF_8); + Assert.assertArrayEquals(expected, map.get("string_test").encode()); + } + + /** + * Tests Attribute boolean encoding. + */ + @Test + public void booleanEncode() { + AttributeMap map = new AttributeMap(); + byte[] expected = { 1 }; + Assert.assertArrayEquals(expected, map.get("boolean_test").encode()); + } + + /** + * Tests Attribute int encoding. + */ + @Test + public void intEncode() { + AttributeMap map = new AttributeMap(); + byte[] expected = Longs.toByteArray(Integer.MAX_VALUE); + Assert.assertArrayEquals(expected, map.get("int_test").encode()); + } + + /** + * Tests Attribute long encoding. + */ + @Test + public void longEncode() { + AttributeMap map = new AttributeMap(); + byte[] expected = Longs.toByteArray(Long.MAX_VALUE); + Assert.assertArrayEquals(expected, map.get("long_test").encode()); + } + + /** + * Tests Attribute double encoding. + */ + @Test + public void doubleEncode() { + AttributeMap map = new AttributeMap(); + byte[] expected = Longs.toByteArray(Double.doubleToLongBits(Double.MAX_VALUE)); + Assert.assertArrayEquals(expected, map.get("double_test").encode()); + } + +}