added a test for isSet
[lwes-java.git] / src / test / java / org / lwes / EventTest.java
blobc1567ac6bf72f2235d4aad043bbc508886bd379b
1 package org.lwes;
2 /**
3 * @author fmaritato
4 */
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;
17 import java.io.File;
18 import java.math.BigInteger;
19 import java.net.InetAddress;
20 import java.net.UnknownHostException;
22 public class EventTest {
24 private EventTemplateDB eventTemplate;
26 @Before
27 public void setUp() {
28 eventTemplate = new EventTemplateDB();
29 eventTemplate.setESFFile(new File("src/test/java/org/lwes/EventTest.esf"));
30 eventTemplate.initialize();
33 @Test
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"));
44 @Test
45 public void testNullValue() throws EventSystemException {
46 Event evt = new Event("Test", false, eventTemplate);
47 Short s = evt.getInt16("a");
48 assertNull(s);
49 evt.setInt16("a", (short) 1);
50 s = evt.getInt16("a");
51 assertNotNull(s);
52 assertEquals("short value incorrect", (short) 1, s.shortValue());
55 @Test
56 public void testUnsignedTypesValidate() throws EventSystemException {
57 Event evt = new Event("Test", false, eventTemplate);
58 try {
59 evt.setUInt16("SiteID", 0);
60 evt.validate();
62 catch (EventSystemException e) {
63 fail(e.getMessage());
67 @Test
68 public void testValidateEventName() throws EventSystemException {
69 boolean exceptionThrown = false;
70 Event evt = new Event("Test2", false, eventTemplate);
71 try {
72 evt.validate();
74 catch (NoSuchEventException e) {
75 exceptionThrown = true;
77 assertTrue("No exception for invalid event", exceptionThrown);
80 @Test
81 public void testValidateField() throws EventSystemException {
82 Event evt = new Event("Test", false, eventTemplate);
83 try {
84 evt.setString("field1", "avalue");
85 evt.validate();
87 catch (EventSystemException e) {
88 fail(e.getMessage());
92 @Test
93 public void testValidateBadTypeField() throws EventSystemException {
94 boolean exceptionThrown = false;
95 Event evt = new Event("Test", false, eventTemplate);
96 try {
97 evt.setInt16("field1", (short) 15);
98 evt.validate();
100 catch (NoSuchAttributeTypeException e) {
101 exceptionThrown = true;
102 System.out.println(e);
104 assertTrue("No Exception for wrong type set", exceptionThrown);
107 @Test
108 public void testValidateBadField() throws EventSystemException {
109 boolean exceptionThrown = false;
110 Event evt = new Event("Test", false, eventTemplate);
111 try {
112 evt.setInt16("field3", (short) 15);
113 evt.validate();
115 catch (NoSuchAttributeException e) {
116 exceptionThrown = true;
118 assertTrue("No exception for invalid field", exceptionThrown);
121 @Test
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.
134 @Test
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");
149 assertNotNull(s);
150 assertEquals("int16 wrong", 1, s.shortValue());
151 Integer i = evt.getInt32("int32");
152 assertNotNull(i);
153 assertEquals("int32 wrong", 1337, i.intValue());
154 Long l = evt.getInt64("int64");
155 assertNotNull(l);
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");
160 assertNotNull(i);
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"));
170 @Test
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;
183 try {
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);