mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-03 08:39:11 +00:00
Default apollo.
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package org.apollo.net.codec.game;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import net.burtleburtle.bob.rand.IsaacRandom;
|
||||
|
||||
import org.apollo.net.meta.PacketType;
|
||||
import org.jboss.netty.buffer.ChannelBuffer;
|
||||
import org.jboss.netty.buffer.ChannelBuffers;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* A test for the {@link GamePacketEncoder} class.
|
||||
* @author Graham
|
||||
*/
|
||||
public class TestGamePacketEncoder {
|
||||
|
||||
/**
|
||||
* Tests the {@link GamePacketEncoder#encode(org.jboss.netty.channel.ChannelHandlerContext, org.jboss.netty.channel.Channel, Object)}
|
||||
* method.
|
||||
* @throws Exception if an error occurs.
|
||||
*/
|
||||
@Test
|
||||
public void testEncode() throws Exception {
|
||||
// generates 243, 141, 34, -223, 121...
|
||||
IsaacRandom random = new IsaacRandom(new int[] { 0, 0, 0, 0 });
|
||||
GamePacketEncoder encoder = new GamePacketEncoder(random);
|
||||
|
||||
ChannelBuffer payload = ChannelBuffers.wrappedBuffer("Hello".getBytes());
|
||||
GamePacket packet = new GamePacket(10, PacketType.FIXED, payload.copy());
|
||||
ChannelBuffer buf = (ChannelBuffer) encoder.encode(null, null, packet);
|
||||
|
||||
assertEquals(6, buf.readableBytes());
|
||||
assertEquals(253, buf.readUnsignedByte());
|
||||
assertEquals('H', buf.readUnsignedByte());
|
||||
assertEquals('e', buf.readUnsignedByte());
|
||||
assertEquals('l', buf.readUnsignedByte());
|
||||
assertEquals('l', buf.readUnsignedByte());
|
||||
assertEquals('o', buf.readUnsignedByte());
|
||||
|
||||
packet = new GamePacket(9, PacketType.VARIABLE_BYTE, payload.copy());
|
||||
buf = (ChannelBuffer) encoder.encode(null, null, packet);
|
||||
|
||||
assertEquals(7, buf.readableBytes());
|
||||
assertEquals(150, buf.readUnsignedByte());
|
||||
assertEquals(5, buf.readUnsignedByte());
|
||||
assertEquals('H', buf.readUnsignedByte());
|
||||
assertEquals('e', buf.readUnsignedByte());
|
||||
assertEquals('l', buf.readUnsignedByte());
|
||||
assertEquals('l', buf.readUnsignedByte());
|
||||
assertEquals('o', buf.readUnsignedByte());
|
||||
|
||||
packet = new GamePacket(0, PacketType.VARIABLE_SHORT, payload.copy());
|
||||
buf = (ChannelBuffer) encoder.encode(null, null, packet);
|
||||
|
||||
assertEquals(8, buf.readableBytes());
|
||||
assertEquals(34, buf.readUnsignedByte());
|
||||
assertEquals(5, buf.readUnsignedShort());
|
||||
assertEquals('H', buf.readUnsignedByte());
|
||||
assertEquals('e', buf.readUnsignedByte());
|
||||
assertEquals('l', buf.readUnsignedByte());
|
||||
assertEquals('l', buf.readUnsignedByte());
|
||||
assertEquals('o', buf.readUnsignedByte());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package org.apollo.util;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.apollo.net.NetworkConstants;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* A test for the {@link ByteBufferUtil} class.
|
||||
* @author Graham
|
||||
*/
|
||||
public class TestByteBufferUtil {
|
||||
|
||||
/**
|
||||
* Tests the {@link ByteBufferUtil#readUnsignedTriByte(ByteBuffer)} method.
|
||||
*/
|
||||
@Test
|
||||
public void testReadUnsignedTriByte() {
|
||||
ByteBuffer buf = ByteBuffer.allocate(3);
|
||||
buf.put((byte) 123);
|
||||
buf.put((byte) 45);
|
||||
buf.put((byte) 67);
|
||||
buf.flip();
|
||||
|
||||
assertEquals(8072515, ByteBufferUtil.readUnsignedTriByte(buf));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the {@link ByteBufferUtil#readString(ByteBuffer)} method.
|
||||
*/
|
||||
@Test
|
||||
public void testReadString() {
|
||||
ByteBuffer buf = ByteBuffer.allocate(8);
|
||||
buf.put((byte) 'h');
|
||||
buf.put((byte) 'e');
|
||||
buf.put((byte) 'l');
|
||||
buf.put((byte) 'l');
|
||||
buf.put((byte) 'o');
|
||||
buf.put((byte) NetworkConstants.STRING_TERMINATOR);
|
||||
buf.put((byte) 66);
|
||||
buf.put((byte) 6);
|
||||
buf.flip();
|
||||
|
||||
assertEquals("hello", ByteBufferUtil.readString(buf));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.apollo.util;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.apollo.util.ChannelBufferUtil;
|
||||
import org.jboss.netty.buffer.ChannelBuffer;
|
||||
import org.jboss.netty.buffer.ChannelBuffers;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* A test for the {@link ChannelBufferUtil} class.
|
||||
* @author Graham
|
||||
*/
|
||||
public final class TestChannelBufferUtil {
|
||||
|
||||
/**
|
||||
* Test the {@link ChannelBufferUtil#readString(ChannelBuffer)}
|
||||
* method.
|
||||
*/
|
||||
@Test
|
||||
public void testReadString() {
|
||||
ChannelBuffer buf = ChannelBuffers.buffer(6);
|
||||
buf.writeBytes(new byte[] { 'H', 'e', 'l', 'l', 'o', 10 });
|
||||
String str = ChannelBufferUtil.readString(buf);
|
||||
assertEquals("Hello", str);
|
||||
|
||||
buf = ChannelBuffers.buffer(5);
|
||||
buf.writeBytes(new byte[] { 'W', 'o', 'r', 'l', 'd' });
|
||||
str = ChannelBufferUtil.readString(buf);
|
||||
assertEquals("World", str);
|
||||
|
||||
buf = ChannelBuffers.buffer(3);
|
||||
buf.writeByte('!');
|
||||
buf.writeByte(10);
|
||||
buf.writeByte('.');
|
||||
|
||||
str = ChannelBufferUtil.readString(buf);
|
||||
assertEquals("!", str);
|
||||
|
||||
str = ChannelBufferUtil.readString(buf);
|
||||
assertEquals(".", str);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.apollo.util;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* A test for the {@link CompressionUtil} class.
|
||||
* @author Graham
|
||||
*/
|
||||
public class TestCompressionUtil {
|
||||
|
||||
/**
|
||||
* Tests the {@link CompressionUtil#gzip(byte[])} and
|
||||
* {@link CompressionUtil#ungzip(byte[], byte[])} methods.
|
||||
* @throws IOException if an I/O error occurs.
|
||||
*/
|
||||
@Test
|
||||
public void testGzip() throws IOException {
|
||||
String str = "Hello, World!";
|
||||
byte[] data = str.getBytes();
|
||||
byte[] compressed = CompressionUtil.gzip(data);
|
||||
CompressionUtil.ungzip(compressed, data);
|
||||
assertEquals(str, new String(data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the {@link CompressionUtil#bzip2(byte[])} and
|
||||
* {@link CompressionUtil#unbzip2(byte[], byte[])} methods.
|
||||
* @throws IOException if an I/O error occurs.
|
||||
*/
|
||||
@Test
|
||||
public void testBzip2() throws IOException {
|
||||
String str = "Hello, World!";
|
||||
byte[] data = str.getBytes();
|
||||
byte[] compressed = CompressionUtil.bzip2(data);
|
||||
CompressionUtil.unbzip2(compressed, data);
|
||||
assertEquals(str, new String(data));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.apollo.util;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* A test for the {@link LanguageUtil} class.
|
||||
* @author Graham
|
||||
*/
|
||||
public class TestLanguageUtil {
|
||||
|
||||
/**
|
||||
* Tests the {@link LanguageUtil#getIndefiniteArticle(String)} method.
|
||||
*/
|
||||
@Test
|
||||
public void testIndefiniteArticle() {
|
||||
assertEquals("an", LanguageUtil.getIndefiniteArticle("apple"));
|
||||
assertEquals("an", LanguageUtil.getIndefiniteArticle("urn"));
|
||||
assertEquals("a", LanguageUtil.getIndefiniteArticle("nose"));
|
||||
assertEquals("a", LanguageUtil.getIndefiniteArticle("foot"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.apollo.util;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* A test for the {@link TextUtil} class.
|
||||
* @author Graham
|
||||
*/
|
||||
public class TestTextUtil {
|
||||
|
||||
/**
|
||||
* Tests the {@link TextUtil#compress(String, byte[])} and
|
||||
* {@link TextUtil#uncompress(byte[], int)} methods.
|
||||
*/
|
||||
@Test
|
||||
public void testCompression() {
|
||||
String str = "hello, world!";
|
||||
|
||||
byte[] compressed = new byte[128];
|
||||
int len = TextUtil.compress(str, compressed);
|
||||
String uncompressed = TextUtil.uncompress(compressed, len);
|
||||
|
||||
assertEquals(str, uncompressed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the {@link TextUtil#filterInvalidCharacters(String)} method.
|
||||
*/
|
||||
@Test
|
||||
public void testFilter() {
|
||||
String str = "this contains <<< invalid characters";
|
||||
String filtered = "this contains invalid characters";
|
||||
assertEquals(filtered, TextUtil.filterInvalidCharacters(str));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tets the {@link TextUtil#capitalize(String)} method.
|
||||
*/
|
||||
@Test
|
||||
public void testCapitalize() {
|
||||
String str = "tHiS is CRAP capitAliZation. do You AGreE? YES!";
|
||||
String capitalized = "This is crap capitalization. Do you agree? Yes!";
|
||||
assertEquals(capitalized, TextUtil.capitalize(str));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package org.apollo.util.xml;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
/**
|
||||
* A test for the {@link XmlParser} class.
|
||||
* @author Graham
|
||||
*/
|
||||
public final class TestXmlParser {
|
||||
|
||||
/**
|
||||
* A test for the {@link XmlParser#parse(java.io.InputStream)} method.
|
||||
* @throws SAXException if a SAX error occurs.
|
||||
* @throws IOException if an I/O error occurs.
|
||||
*/
|
||||
@Test
|
||||
public void testParseInputStream() throws SAXException, IOException {
|
||||
XmlParser parser = new XmlParser();
|
||||
InputStream is = new ByteArrayInputStream("<root a='1' b='2' c='3'><z><y><x></x></y></z></root>".getBytes());
|
||||
XmlNode root = parser.parse(is);
|
||||
|
||||
assertEquals(root.getName(), "root");
|
||||
assertEquals(root.getAttributeCount(), 3);
|
||||
assertEquals(root.getChildCount(), 1);
|
||||
assertFalse(root.hasValue());
|
||||
|
||||
Set<String> attributeNames = root.getAttributeNames();
|
||||
assertTrue(attributeNames.contains("a"));
|
||||
assertTrue(attributeNames.contains("b"));
|
||||
assertTrue(attributeNames.contains("c"));
|
||||
assertFalse(attributeNames.contains("z"));
|
||||
assertFalse(attributeNames.contains("y"));
|
||||
assertFalse(attributeNames.contains("x"));
|
||||
|
||||
assertEquals("1", root.getAttribute("a"));
|
||||
assertEquals("2", root.getAttribute("b"));
|
||||
assertEquals("3", root.getAttribute("c"));
|
||||
assertNull(root.getAttribute("z"));
|
||||
assertNull(root.getAttribute("y"));
|
||||
assertNull(root.getAttribute("x"));
|
||||
|
||||
XmlNode[] firstChild = root.getChildren().toArray(new XmlNode[1]);
|
||||
assertEquals(1, firstChild.length);
|
||||
assertEquals("z", firstChild[0].getName());
|
||||
|
||||
XmlNode[] secondChild = firstChild[0].getChildren().toArray(new XmlNode[1]);
|
||||
assertEquals(1, secondChild.length);
|
||||
assertEquals("y", secondChild[0].getName());
|
||||
|
||||
XmlNode[] thirdChild = secondChild[0].getChildren().toArray(new XmlNode[1]);
|
||||
assertEquals(1, thirdChild.length);
|
||||
assertEquals("x", thirdChild[0].getName());
|
||||
|
||||
assertEquals(0, thirdChild[0].getChildCount());
|
||||
}
|
||||
|
||||
/**
|
||||
* A test for the {@link XmlParser#parse(java.io.Reader)} method.
|
||||
* @throws SAXException if a SAX error occurs.
|
||||
* @throws IOException if an I/O error occurs.
|
||||
*/
|
||||
@Test
|
||||
public void testParseReader() throws SAXException, IOException {
|
||||
XmlParser parser = new XmlParser();
|
||||
Reader reader = new StringReader("<alphabet><a>1</a><b>2</b><c>3</c></alphabet>");
|
||||
XmlNode root = parser.parse(reader);
|
||||
|
||||
assertEquals(root.getName(), "alphabet");
|
||||
assertEquals(root.getAttributeCount(), 0);
|
||||
assertEquals(root.getChildCount(), 3);
|
||||
assertFalse(root.hasValue());
|
||||
|
||||
XmlNode[] children = root.getChildren().toArray(new XmlNode[3]);
|
||||
|
||||
assertEquals(children[0].getName(), "a");
|
||||
assertEquals(children[1].getName(), "b");
|
||||
assertEquals(children[2].getName(), "c");
|
||||
|
||||
assertEquals(children[0].getValue(), "1");
|
||||
assertEquals(children[1].getValue(), "2");
|
||||
assertEquals(children[2].getValue(), "3");
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
assertTrue(children[i].hasValue());
|
||||
assertEquals(children[i].getAttributeCount(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user