From 051d9cbfe2d611816bd9b1eda84539f75d907721 Mon Sep 17 00:00:00 2001 From: Frank Maritato Date: Thu, 16 Jul 2009 21:00:11 +0000 Subject: [PATCH] started adding some unit tests git-svn-id: https://lwes.svn.sourceforge.net/svnroot/lwes/lwes-java/trunk@135 a2f82657-cdd2-4550-bd36-68a8e7111808 --- tests/org/lwes/db/EventTemplateDBTest.esf | 15 +++ tests/org/lwes/db/EventTemplateDBTest.java | 123 +++++++++++++++++++++++++ tests/org/lwes/util/CharacterEncodingTest.java | 55 +++++++++++ tests/org/lwes/util/NumberCodecTest.java | 89 ++++++++++++++++++ 4 files changed, 282 insertions(+) create mode 100644 tests/org/lwes/db/EventTemplateDBTest.esf create mode 100644 tests/org/lwes/db/EventTemplateDBTest.java create mode 100644 tests/org/lwes/util/CharacterEncodingTest.java create mode 100644 tests/org/lwes/util/NumberCodecTest.java diff --git a/tests/org/lwes/db/EventTemplateDBTest.esf b/tests/org/lwes/db/EventTemplateDBTest.esf new file mode 100644 index 0000000..6fde0ce --- /dev/null +++ b/tests/org/lwes/db/EventTemplateDBTest.esf @@ -0,0 +1,15 @@ +MetaEventInfo +{ + ip_addr SenderIP; # IP address of Sender + uint16 SenderPort; # IP port of Sender + int64 ReceiptTime; # time this event was received, in + # milliseconds since epoch + int16 enc; # encoding of strings in the event + uint16 SiteID; # id of site sending the event +} + +TestEvent +{ + string field1; + int64 field2; +} \ No newline at end of file diff --git a/tests/org/lwes/db/EventTemplateDBTest.java b/tests/org/lwes/db/EventTemplateDBTest.java new file mode 100644 index 0000000..8e3b6a1 --- /dev/null +++ b/tests/org/lwes/db/EventTemplateDBTest.java @@ -0,0 +1,123 @@ +package org.lwes.db; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import org.junit.Test; +import org.lwes.BaseType; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.util.Enumeration; +import java.util.Map; + +/** + * @author fmaritato + */ + +public class EventTemplateDBTest { + + private static final String ESF = "tests/org/lwes/db/EventTemplateDBTest.esf"; + private static final String TEST_EVENT = "TestEvent"; + + @Test + public void testTemplateFromFile() { + EventTemplateDB template = new EventTemplateDB(); + template.setESFFile(new File(ESF)); + assertTrue("Template did not initialize", template.initialize()); + + Enumeration eventNames = template.getEventNames(); + assertNotNull("Event names enum was null", eventNames); + + assertTrue("TestEvent was not known to the template", + template.checkForEvent(TEST_EVENT)); + + assertTrue("field1 attribute not known to the template", + template.checkForAttribute(TEST_EVENT, "field1")); + + // Check a metadata attribute + assertTrue("SenderIP attribute not known to the template", + template.checkForAttribute(TEST_EVENT, "SenderIP")); + + BaseType bt = template.getBaseTypeForObjectAttribute(TEST_EVENT, "field2", 100l); + assertNotNull(bt); + assertEquals("Wrong BaseType returned", "int64", bt.getTypeName()); + + assertTrue("Wrong type for attribute field2", + template.checkTypeForAttribute(TEST_EVENT, "field2", bt)); + + assertFalse("template allowed string for a numeric type", + template.checkTypeForAttribute(TEST_EVENT, "field2", "wrong")); + + Map bts = template.getBaseTypesForEvent(TEST_EVENT); + assertNotNull(bts); + assertEquals("Number of types in the map is wrong", 7, bts.size()); + BaseType field2BT = bts.get("field2"); + assertNotNull(field2BT); + assertEquals("int64", field2BT.getTypeName()); + + Object obj = template.parseAttribute(TEST_EVENT, "field2", "100"); + assertNotNull(obj); + + String testHtmlString = + new StringBuilder().append("\n") + .append("\n") + .append("\n") + .append("\n") + .append("\n") + .append("\n") + .append("\n") + .append("\n") + .append("\n") + .append("\n") + .append("\n") + .append("\n") + .append("\n") + .append("\n") + .append("\n") + .append("
MetaEventInfoTypeName
uint16SiteID
int16enc
ip_addrSenderIP
int64ReceiptTime
uint16SenderPort
TestEventTypeName
uint16SiteID
int16enc
stringfield1
int64field2
ip_addrSenderIP
int64ReceiptTime
uint16SenderPort
\n") + .toString(); + String htmlString = template.toHtmlString(); + assertNotNull("html string was null", htmlString); + assertEquals("html string did not match", testHtmlString, htmlString); + + String testString = + new StringBuilder().append("\nMetaEventInfo\n") + .append("{\n") + .append("\tint64 ReceiptTime;\n") + .append("\tip_addr SenderIP;\n") + .append("\tuint16 SenderPort;\n") + .append("\tuint16 SiteID;\n") + .append("\tint16 enc;\n") + .append("}\n") + .append("TestEvent\n") + .append("{\n") + .append("\tint64 ReceiptTime;\n") + .append("\tip_addr SenderIP;\n") + .append("\tuint16 SenderPort;\n") + .append("\tuint16 SiteID;\n") + .append("\tint16 enc;\n") + .append("\tstring field1;\n") + .append("\tint64 field2;\n") + .append("}\n") + .toString(); + String toString = template.toString(); + assertNotNull("toString was null", toString); + assertEquals("test string did not match", testString, toString); + } + + @Test + public void testTemplateFromStream() { + EventTemplateDB template = new EventTemplateDB(); + try { + template.setESFInputStream(new FileInputStream(ESF)); + } + catch (FileNotFoundException e) { + fail(e.getMessage()); + } + assertTrue(template.initialize()); + } +} diff --git a/tests/org/lwes/util/CharacterEncodingTest.java b/tests/org/lwes/util/CharacterEncodingTest.java new file mode 100644 index 0000000..044917e --- /dev/null +++ b/tests/org/lwes/util/CharacterEncodingTest.java @@ -0,0 +1,55 @@ +package org.lwes.util; +/** + * @author fmaritato + */ + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; + +public class CharacterEncodingTest { + + @Test + public void testCharEncodeInstance() { + CharacterEncoding utf8 = null; + try { + utf8 = CharacterEncoding.getInstance("UTF-8"); + } + catch (UnsupportedEncodingException e) { + fail(e.getMessage()); + } + assertNotNull(utf8); + + CharacterEncoding iso88591 = null; + try { + iso88591 = CharacterEncoding.getInstance("ISO-8859-1"); + } + catch (UnsupportedEncodingException e) { + fail(e.getMessage()); + } + assertNotNull(iso88591); + + assertFalse("utf8 = iso-8859-1", utf8.equals(iso88591)); + + } + + @Test + public void testCharEncodeStatics() { + CharacterEncoding utf8 = CharacterEncoding.UTF_8; + assertEquals(utf8.getEncodingString(), "UTF-8"); + CharacterEncoding ascii = CharacterEncoding.ASCII; + assertEquals(ascii.getEncodingString(), "ASCII"); + CharacterEncoding jp = CharacterEncoding.EUC_JP; + assertEquals(jp.getEncodingString(), "EUC_JP"); + CharacterEncoding kr = CharacterEncoding.EUC_KR; + assertEquals(kr.getEncodingString(), "EUC_KR"); + CharacterEncoding iso8859 = CharacterEncoding.ISO_8859_1; + assertEquals(iso8859.getEncodingString(), "ISO-8859-1"); + CharacterEncoding jis = CharacterEncoding.SHIFT_JIS; + assertEquals(jis.getEncodingString(), "SJIS"); + } +} diff --git a/tests/org/lwes/util/NumberCodecTest.java b/tests/org/lwes/util/NumberCodecTest.java new file mode 100644 index 0000000..6193b39 --- /dev/null +++ b/tests/org/lwes/util/NumberCodecTest.java @@ -0,0 +1,89 @@ +package org.lwes.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import org.junit.Test; + +import java.math.BigInteger; + +/** + * @author fmaritato + */ + +public class NumberCodecTest { + + @Test + public void testToHexString() { + String s = NumberCodec.toHexString((byte) 1); + assertNotNull(s); + assertEquals("Byte to hex incorrect", "01", s); + + s = NumberCodec.toHexString((short) 1); + assertNotNull(s); + assertEquals("Short to hex incorrect", "0001", s); + + s = NumberCodec.toHexString(1); + assertNotNull(s); + assertEquals("Int to hex incorrect", "00000001", s); + + s = NumberCodec.toHexString((long) 1); + assertNotNull(s); + assertEquals("Long to hex incorrect", "0000000000000001", s); + + s = NumberCodec.toHexString(new BigInteger("1000")); + assertNotNull(s); + assertEquals("BigInteger to hex incorrect", "3e8", s); + } + + @Test + public void testWriteHexStringToBuffer() { + StringBuffer buf = new StringBuffer(16); + + NumberCodec.writeHexString((byte) 1, buf); + assertEquals("01", buf.toString()); + buf.delete(0, buf.length()); + + NumberCodec.writeHexString((short) 1, buf); + assertEquals("0001", buf.toString()); + buf.delete(0, buf.length()); + + NumberCodec.writeHexString(1, buf); + assertEquals("00000001", buf.toString()); + buf.delete(0, buf.length()); + + NumberCodec.writeHexString((long) 1, buf); + assertEquals("0000000000000001", buf.toString()); + buf.delete(0, buf.length()); + } + + @Test + public void testByteArrayToHexString() { + byte[] buf = NumberCodec.hexStringToByteArray("011e"); + String hex1 = NumberCodec.byteArrayToHexString(buf); + assertEquals("byteArrayToHexString failed", "011e", hex1); + String hex2 = NumberCodec.byteArrayToHexString(buf, 0, buf.length); + assertEquals("byteArrayToHexString failed", "011e", hex2); + + byte b = NumberCodec.byteFromHexString("0a"); + assertEquals("byteFromHexString fail", 10, b); + } + + @Test + public void testEncodeDecode() { + + // encodeLong and decodeLong call the unchecked versions underneath. + byte[] buf = new byte[64]; + NumberCodec.encodeLong(123l, buf, 0, buf.length); + long lval = NumberCodec.decodeLong(buf, 0, buf.length); + assertEquals("encode/decode long unchecked fail", 123l, lval); + + NumberCodec.encodeInt(123, buf, 0, buf.length); + int ival = NumberCodec.decodeInt(buf, 0, buf.length); + assertEquals("encode/decode int unchecked fail", 123, ival); + + NumberCodec.encodeShort((short) 12, buf, 0, buf.length); + short sval = NumberCodec.decodeShort(buf, 0, buf.length); + assertEquals("encode/decode short unchecked fail", 12, sval); + + } +} -- 2.11.4.GIT