6 import org
.apache
.commons
.codec
.binary
.Base64
;
7 import static org
.junit
.Assert
.assertEquals
;
8 import static org
.junit
.Assert
.assertFalse
;
9 import static org
.junit
.Assert
.assertNotNull
;
10 import static org
.junit
.Assert
.assertNull
;
11 import static org
.junit
.Assert
.assertTrue
;
12 import static org
.junit
.Assert
.fail
;
13 import org
.junit
.Before
;
14 import org
.junit
.Test
;
15 import org
.lwes
.db
.EventTemplateDB
;
18 import java
.math
.BigInteger
;
19 import java
.net
.InetAddress
;
20 import java
.net
.UnknownHostException
;
22 public class EventTest
{
24 private EventTemplateDB eventTemplate
;
28 eventTemplate
= new EventTemplateDB();
29 eventTemplate
.setESFFile(new File("src/test/java/org/lwes/EventTest.esf"));
30 eventTemplate
.initialize();
34 public void testIsSet() throws EventSystemException
{
36 Event evt
= new Event("Test", false, eventTemplate
);
37 assertFalse(evt
.isSet("notset"));
39 evt
.setInt32("set", 32);
40 assertTrue(evt
.isSet("set"));
45 public void testNullValue() throws EventSystemException
{
46 Event evt
= new Event("Test", false, eventTemplate
);
47 Short s
= evt
.getInt16("a");
49 evt
.setInt16("a", (short) 1);
50 s
= evt
.getInt16("a");
52 assertEquals("short value incorrect", (short) 1, s
.shortValue());
56 public void testUnsignedTypesValidate() throws EventSystemException
{
57 Event evt
= new Event("Test", false, eventTemplate
);
59 evt
.setUInt16("SiteID", 0);
62 catch (EventSystemException e
) {
68 public void testValidateEventName() throws EventSystemException
{
69 boolean exceptionThrown
= false;
70 Event evt
= new Event("Test2", false, eventTemplate
);
74 catch (NoSuchEventException e
) {
75 exceptionThrown
= true;
77 assertTrue("No exception for invalid event", exceptionThrown
);
81 public void testValidateField() throws EventSystemException
{
82 Event evt
= new Event("Test", false, eventTemplate
);
84 evt
.setString("field1", "avalue");
87 catch (EventSystemException e
) {
93 public void testValidateBadTypeField() throws EventSystemException
{
94 boolean exceptionThrown
= false;
95 Event evt
= new Event("Test", false, eventTemplate
);
97 evt
.setInt16("field1", (short) 15);
100 catch (NoSuchAttributeTypeException e
) {
101 exceptionThrown
= true;
102 System
.out
.println(e
);
104 assertTrue("No Exception for wrong type set", exceptionThrown
);
108 public void testValidateBadField() throws EventSystemException
{
109 boolean exceptionThrown
= false;
110 Event evt
= new Event("Test", false, eventTemplate
);
112 evt
.setInt16("field3", (short) 15);
115 catch (NoSuchAttributeException e
) {
116 exceptionThrown
= true;
118 assertTrue("No exception for invalid field", exceptionThrown
);
122 public void testSerialize() throws EventSystemException
{
123 Event evt
= new Event("Test", false, eventTemplate
);
124 evt
.setString("attr_s", "str_value");
125 evt
.setInt32("attr_i", 1);
126 byte[] bytes
= evt
.serialize();
127 String str
= new String(bytes
);
128 //System.out.println("as string: "+str);
129 byte[] encoded
= Base64
.encodeBase64(bytes
);
130 //System.out.println(new String(encoded));
131 // TODO this test not finished yet.
135 public void testEventAccessors() throws EventSystemException
, UnknownHostException
{
136 Event evt
= new Event("Test", false, eventTemplate
);
138 evt
.setInt16("int16", (short) 1);
139 evt
.setInt32("int32", 1337);
140 evt
.setInt64("int64", 1337133713371337l);
141 evt
.setBoolean("bool", true);
142 evt
.setString("str", "string");
143 evt
.setUInt16("uint16", 1337); // uint16 in java is just an int
144 evt
.setUInt32("uint32", 1337133713371337l); // uint32 in java is a long
145 evt
.setUInt64("uint64", 1337133713371337l); // uint64 is a BigInteger
146 evt
.setIPAddress("ipaddr", InetAddress
.getByName("localhost"));
148 Short s
= evt
.getInt16("int16");
150 assertEquals("int16 wrong", 1, s
.shortValue());
151 Integer i
= evt
.getInt32("int32");
153 assertEquals("int32 wrong", 1337, i
.intValue());
154 Long l
= evt
.getInt64("int64");
156 assertEquals("int64 wrong", 1337133713371337l, l
.longValue());
157 assertEquals("bool wrong", true, evt
.getBoolean("bool"));
158 assertEquals("str wrong", "string", evt
.getString("str"));
159 i
= evt
.getUInt16("uint16");
161 assertEquals("uint16 wrong", 1337, i
.intValue());
162 l
= evt
.getUInt32("uint32");
163 assertEquals("uint32 wrong", 1337133713371337l, l
.longValue());
164 assertEquals("uint64 wrong",
165 BigInteger
.valueOf(1337133713371337l),
166 evt
.getUInt64("uint64"));
171 public void testEventSize() throws EventSystemException
{
173 Event evt
= new Event("Test", false, eventTemplate
);
175 for (int i
= 0; i
< 5000; i
++) {
176 evt
.setInt32("" + i
, i
);
179 byte[] bytes
= evt
.serialize();
180 assertEquals("number of bytes wrong?", 48904, bytes
.length
);
182 boolean exceptionThrown
= false;
184 for (int i
= 5001; i
< 10000; i
++) {
185 evt
.setInt32("" + i
, i
);
188 catch (Exception e
) {
189 exceptionThrown
= true;
190 assertEquals("Different exception",
191 "org.lwes.EventSystemException",
192 e
.getClass().getName());
194 assertTrue("Size exception was not thrown", exceptionThrown
);