removed a debug
[lwes-java.git] / src / test / java / org / lwes / EventTest.java
blob1c92caaa8b8b6469a0b812b91e2a611c63d017cc
1 /*======================================================================*
2 * Copyright (c) 2010, Frank Maritato All rights reserved. *
3 * *
4 * Licensed under the New BSD License (the "License"); you may not use *
5 * this file except in compliance with the License. Unless required *
6 * by applicable law or agreed to in writing, software distributed *
7 * under the License is distributed on an "AS IS" BASIS, WITHOUT *
8 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
9 * See the License for the specific language governing permissions and *
10 * limitations under the License. See accompanying LICENSE file. *
11 *======================================================================*/
13 package org.lwes;
14 /**
15 * @author fmaritato
18 import org.apache.commons.codec.binary.Base64;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.lwes.db.EventTemplateDB;
25 import java.io.File;
26 import java.math.BigInteger;
27 import java.net.InetAddress;
28 import java.net.UnknownHostException;
29 import java.util.Enumeration;
30 import java.util.List;
32 import static org.junit.Assert.assertEquals;
33 import static org.junit.Assert.assertFalse;
34 import static org.junit.Assert.assertNotNull;
35 import static org.junit.Assert.assertNull;
36 import static org.junit.Assert.assertTrue;
37 import static org.junit.Assert.fail;
39 public class EventTest {
41 private static transient final Log log = LogFactory.getLog(EventTest.class);
43 private EventTemplateDB eventTemplate;
45 @Before
46 public void setUp() {
47 eventTemplate = new EventTemplateDB();
48 eventTemplate.setESFFile(new File("src/test/java/org/lwes/EventTest.esf"));
49 eventTemplate.initialize();
52 @Test
53 public void testGetAttributeNames() throws EventSystemException {
54 Event evt = new Event("Test", false, eventTemplate);
55 evt.setString("str", "string");
57 boolean success = false;
58 Enumeration en = evt.getEventAttributeNames();
59 while (en.hasMoreElements()) {
60 String name = (String) en.nextElement();
61 if ("str".equals(name)) {
62 success = true;
65 assertTrue(success);
68 @Test
69 public void testIPV4() throws EventSystemException, UnknownHostException {
70 Event evt = new Event("Test", true, eventTemplate);
71 evt.setIPV4Address("userIP", InetAddress.getByName("www.yahoo.com"));
72 InetAddress a = evt.getIPV4Address("userIP");
73 assertNotNull(a);
75 byte[] bytes = evt.serialize();
76 Event evt2 = new Event(bytes, true, eventTemplate);
77 assertNotNull(evt2);
78 InetAddress b = evt2.getIPV4Address("userIP");
79 assertNotNull(b);
80 // Can't test the exact hostname b/c you actually get a vip hostname back...
81 assertTrue("Not a yahoo.com address", b.getHostName().endsWith(".yahoo.com"));
84 @Test
85 public void testGetInetAddress() throws EventSystemException, UnknownHostException {
86 Event evt = new Event("Test", false, eventTemplate);
87 evt.setIPAddress("ip", InetAddress.getByName("www.yahoo.com"));
88 InetAddress a = evt.getInetAddress("ip");
89 assertNotNull(a);
92 @Test
93 public void testIsSet() throws EventSystemException {
94 Event evt = new Event("Test", false, eventTemplate);
95 assertFalse(evt.isSet("notset"));
97 evt.setInt32("set", 32);
98 assertTrue(evt.isSet("set"));
101 @Test
102 public void testNullValue() throws EventSystemException {
103 Event evt = new Event("Test", false, eventTemplate);
104 Short s = evt.getInt16("a");
105 assertNull(s);
106 evt.setInt16("a", (short) 1);
107 s = evt.getInt16("a");
108 assertNotNull(s);
109 assertEquals("short value incorrect", (short) 1, s.shortValue());
112 @Test
113 public void testUnsignedTypesValidate() throws EventSystemException {
114 Event evt = new Event("Test", false, eventTemplate);
115 try {
116 evt.setUInt16("SiteID", 0);
117 evt.validate();
119 catch (EventSystemException e) {
120 fail(e.getMessage());
124 @Test
125 public void testValidateEventName() throws EventSystemException {
126 boolean exceptionThrown = false;
127 Event evt = new Event("Test2", false, eventTemplate);
128 try {
129 evt.validate();
131 catch (ValidationExceptions e) {
132 exceptionThrown = true;
134 assertTrue("No exception for invalid event", exceptionThrown);
137 @Test
138 public void testValidateField() throws EventSystemException {
139 Event evt = new Event("Test", false, eventTemplate);
140 try {
141 evt.setString("field1", "avalue");
142 evt.validate();
144 catch (EventSystemException e) {
145 fail(e.getMessage());
149 @Test
150 public void testValidateBadTypeField() throws EventSystemException {
151 Event evt = new Event("Test", false, eventTemplate);
152 try {
153 evt.setInt16("field1", (short) 15);
154 evt.validate();
156 catch (ValidationExceptions e) {
157 List<EventSystemException> exc = e.getAllExceptions();
158 assertEquals("Wrong num of exceptions", 1, exc.size());
159 assertEquals("Wrong exception",
160 "org.lwes.NoSuchAttributeTypeException",
161 exc.get(0).getClass().getName());
165 @Test
166 public void testValidateBadField() throws EventSystemException {
167 Event evt = new Event("Test", false, eventTemplate);
168 try {
169 evt.setInt16("field3", (short) 15);
170 evt.validate();
172 catch (ValidationExceptions e) {
173 List<EventSystemException> exc = e.getAllExceptions();
174 assertEquals("Wrong num of exceptions", 1, exc.size());
175 assertEquals("Wrong exception",
176 "org.lwes.NoSuchAttributeException",
177 exc.get(0).getClass().getName());
181 @Test
182 public void testSerialize() throws EventSystemException {
183 Event evt = new Event("Test", false, eventTemplate);
184 evt.setString("attr_s", "str_value");
185 evt.setInt32("attr_i", 1);
186 byte[] bytes = evt.serialize();
187 String str = new String(bytes);
188 byte[] encoded = Base64.encodeBase64(bytes);
189 // TODO this test not finished yet.
192 @Test
193 public void testEventAccessors() throws EventSystemException, UnknownHostException {
194 Event evt = new Event("Test", false, eventTemplate);
196 evt.setInt16("int16", (short) 1);
197 evt.setInt32("int32", 1337);
198 evt.setInt64("int64", 1337133713371337l);
199 evt.setBoolean("bool", true);
200 evt.setString("str", "string");
201 evt.setUInt16("uint16", 1337); // uint16 in java is just an int
202 evt.setUInt32("uint32", 1337133713371337l); // uint32 in java is a long
203 evt.setUInt64("uint64", 1337133713371337l); // uint64 is a BigInteger
204 evt.setIPAddress("ipaddr", InetAddress.getByName("localhost"));
206 Short s = evt.getInt16("int16");
207 assertNotNull(s);
208 assertEquals("int16 wrong", 1, s.shortValue());
209 Integer i = evt.getInt32("int32");
210 assertNotNull(i);
211 assertEquals("int32 wrong", 1337, i.intValue());
212 Long l = evt.getInt64("int64");
213 assertNotNull(l);
214 assertEquals("int64 wrong", 1337133713371337l, l.longValue());
215 assertEquals("bool wrong", true, evt.getBoolean("bool"));
216 assertEquals("str wrong", "string", evt.getString("str"));
217 i = evt.getUInt16("uint16");
218 assertNotNull(i);
219 assertEquals("uint16 wrong", 1337, i.intValue());
220 l = evt.getUInt32("uint32");
221 assertEquals("uint32 wrong", 1337133713371337l, l.longValue());
222 assertEquals("uint64 wrong",
223 BigInteger.valueOf(1337133713371337l),
224 evt.getUInt64("uint64"));
228 @Test
229 public void testEventSize() throws EventSystemException {
231 Event evt = new Event("Test", false, eventTemplate);
233 for (int i = 0; i < 5000; i++) {
234 evt.setInt32("" + i, i);
237 byte[] bytes = evt.serialize();
238 assertEquals("number of bytes wrong?", 48904, bytes.length);
240 boolean exceptionThrown = false;
241 try {
242 for (int i = 5001; i < 10000; i++) {
243 evt.setInt32("" + i, i);
246 catch (Exception e) {
247 exceptionThrown = true;
248 assertEquals("Different exception",
249 "org.lwes.EventSystemException",
250 e.getClass().getName());
252 assertTrue("Size exception was not thrown", exceptionThrown);