Added TestMailPersistable as a test case and equals/hashCode to ProsePackage and StringId

This commit is contained in:
Obique PSWG
2017-05-24 01:47:08 -05:00
parent 4abc684753
commit d7e20a56b3
6 changed files with 168 additions and 2 deletions
@@ -261,6 +261,19 @@ public class ProsePackage implements OutOfBandData {
return "ProsePackage[base=" + base + ", grammarFlag=" + grammarFlag + ", actor=" + actor + ", target=" + target + ", other=" + other + ", di=" + di + ", df=" + df + "]";
}
@Override
public boolean equals(Object o) {
if (!(o instanceof ProsePackage))
return false;
ProsePackage pp = (ProsePackage) o;
return base.equals(pp.base) && actor.equals(pp.actor) && target.equals(pp.target) && other.equals(pp.other) && grammarFlag == pp.grammarFlag && di == pp.di && df == pp.df;
}
@Override
public int hashCode() {
return base.hashCode() * 3 + actor.hashCode() * 7 + target.hashCode() * 13 + other.hashCode() * 17 + (grammarFlag ? 1 : 0) + di * 19 + ((int) (df * 23));
}
private static class Prose implements Encodable, Persistable {
private long objectId;
@@ -323,6 +336,19 @@ public class ProsePackage implements OutOfBandData {
objectId = stream.getLong();
text = stream.getUnicode();
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Prose))
return false;
Prose p = (Prose) o;
return stringId.equals(p.stringId) && objectId == p.objectId && text.equals(p.text);
}
@Override
public int hashCode() {
return stringId.hashCode() * 3 + Long.hashCode(objectId) * 7 + text.hashCode() * 13;
}
@Override
public String toString() {
+12
View File
@@ -125,4 +125,16 @@ public class StringId implements OutOfBandData, Persistable {
public String toString() {
return "@" + file + ":" + key;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof StringId))
return false;
return ((StringId) o).getKey().equals(getKey()) && ((StringId) o).getFile().equals(getFile());
}
@Override
public int hashCode() {
return key.hashCode() * 67 + file.hashCode();
}
}
@@ -101,6 +101,10 @@ public class Mail implements Encodable, Persistable {
public void setTimestamp(int timestamp) {
this.timestamp = timestamp;
}
public OutOfBandPackage getOutOfBandPackage() {
return outOfBandPackage;
}
public void setOutOfBandPackage(OutOfBandPackage outOfBandPackage) {
this.outOfBandPackage = outOfBandPackage;
+2 -1
View File
@@ -34,7 +34,8 @@ public enum SpecificObject {
SO_TICKET_COLLETOR ("object/tangible/travel/ticket_collector/shared_ticket_collector.iff"),
SO_TRANSPORT_SHUTTLE ("object/creature/npc/theme_park/shared_player_shuttle.iff"),
SO_TRANSPORT_STARPORT ("object/creature/npc/theme_park/shared_player_transport.iff"),
SO_TRANSPORT_STARPORT_THEED ("object/creature/npc/theme_park/shared_player_transport_theed_hangar.iff");
SO_TRANSPORT_STARPORT_THEED ("object/creature/npc/theme_park/shared_player_transport_theed_hangar.iff"),
SO_WAYPOINT_BLUE ("object/waypoint/shared_world_waypoint_blue.iff");
private final String template;
@@ -0,0 +1,122 @@
/************************************************************************************
* Copyright (c) 2015 /// Project SWG /// www.projectswg.com *
* *
* ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on *
* July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. *
* Our goal is to create an emulator which will provide a server for players to *
* continue playing a game similar to the one they used to play. We are basing *
* it on the final publish of the game prior to end-game events. *
* *
* This file is part of Holocore. *
* *
* -------------------------------------------------------------------------------- *
* *
* Holocore is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* Holocore is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with Holocore. If not, see <http://www.gnu.org/licenses/>. *
* *
***********************************************************************************/
package resources.persistable;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import com.projectswg.common.persistable.InputPersistenceStream;
import com.projectswg.common.persistable.OutputPersistenceStream;
import resources.encodables.OutOfBandPackage;
import resources.encodables.ProsePackage;
import resources.encodables.StringId;
import resources.encodables.player.Mail;
import resources.objects.SpecificObject;
import resources.objects.waypoint.WaypointObject;
import services.objects.ObjectCreator;
@RunWith(JUnit4.class)
public class TestMailPersistable {
@Test
public void testMailStringIdPersistable() throws IOException {
Mail mail = new Mail("sender", "subject", "message", 100);
mail.setOutOfBandPackage(new OutOfBandPackage(new StringId("key", "value")));
test(mail);
mail.setOutOfBandPackage(new OutOfBandPackage(new StringId("key", "value"), new StringId("key2", "value2")));
test(mail);
}
@Test
public void testMailProsePackagePersistable() throws IOException {
Mail mail = new Mail("sender", "subject", "message", 100);
mail.setOutOfBandPackage(new OutOfBandPackage(new ProsePackage(new StringId("key", "value"), "DI", 50)));
test(mail);
mail.setOutOfBandPackage(new OutOfBandPackage(new ProsePackage(new StringId("key", "value"), "DF", 10f)));
test(mail);
mail.setOutOfBandPackage(new OutOfBandPackage(new ProsePackage(new StringId("key", "value"), "DI", 50), new ProsePackage(new StringId("key", "value"), "DI", 25)));
test(mail);
mail.setOutOfBandPackage(new OutOfBandPackage(new ProsePackage(new StringId("key", "value"), "DF", 10f), new ProsePackage(new StringId("key", "value"), "DF", 20f)));
test(mail);
mail.setOutOfBandPackage(new OutOfBandPackage(new ProsePackage(new StringId("key", "value"), "DI", 50), new ProsePackage(new StringId("key", "value"), "DF", 25f)));
test(mail);
mail.setOutOfBandPackage(new OutOfBandPackage(new ProsePackage(new StringId("key", "value"), "DF", 10f), new ProsePackage(new StringId("key", "value"), "DI", 20)));
test(mail);
}
@Test
public void testMailWaypointPersistable() throws IOException {
Mail mail = new Mail("sender", "subject", "message", 100);
WaypointObject waypoint1 = (WaypointObject) ObjectCreator.createObjectFromTemplate(SpecificObject.SO_WAYPOINT_BLUE.getTemplate());
WaypointObject waypoint2 = (WaypointObject) ObjectCreator.createObjectFromTemplate(SpecificObject.SO_WAYPOINT_BLUE.getTemplate());
mail.setOutOfBandPackage(new OutOfBandPackage(waypoint1));
test(mail);
mail.setOutOfBandPackage(new OutOfBandPackage(waypoint1, waypoint2));
test(mail);
}
private void test(Mail mail) throws IOException {
byte [] written = write(mail);
Mail read = read(written);
test(mail, read);
}
private void test(Mail write, Mail read) {
Assert.assertEquals(write.getSender(), read.getSender());
Assert.assertEquals(write.getSubject(), read.getSubject());
Assert.assertEquals(write.getReceiverId(), read.getReceiverId());
Assert.assertEquals(write.getMessage(), read.getMessage());
Assert.assertEquals(write.getLength(), read.getLength());
Assert.assertEquals(write.getOutOfBandPackage().getLength(), read.getOutOfBandPackage().getLength());
Assert.assertEquals(write.getOutOfBandPackage().getPackages(), read.getOutOfBandPackage().getPackages());
}
private byte [] write(Mail mail) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputPersistenceStream os = new OutputPersistenceStream(baos);
os.write(mail, Mail::save);
os.close();
return baos.toByteArray();
}
private Mail read(byte [] data) throws IOException {
try (InputPersistenceStream is = new InputPersistenceStream(new ByteArrayInputStream(data))) {
Mail mail = is.read(Mail::create);
Assert.assertEquals(0, is.available());
return mail;
}
}
}
@@ -34,7 +34,8 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({
TestSimplePersistable.class,
TestSWGPersistable.class
TestSWGPersistable.class,
TestMailPersistable.class
})
public class TestPersistable {