changing license to BSD, assigning Yahoo copyrights where appropriate
[lwes-java.git] / src / test / java / org / lwes / EventTest.java
blobb6fcc57a5dc56955aef8c0a304d0ec8f9ce3822b
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.List;
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.assertFalse;
33 import static org.junit.Assert.assertNotNull;
34 import static org.junit.Assert.assertNull;
35 import static org.junit.Assert.assertTrue;
36 import static org.junit.Assert.fail;
38 public class EventTest {
40 private static transient final Log log = LogFactory.getLog(EventTest.class);
42 private EventTemplateDB eventTemplate;
44 @Before
45 public void setUp() {
46 eventTemplate = new EventTemplateDB();
47 eventTemplate.setESFFile(new File("src/test/java/org/lwes/EventTest.esf"));
48 eventTemplate.initialize();
51 @Test
52 public void testIPV4() throws EventSystemException, UnknownHostException {
53 Event evt = new Event("Test", true, eventTemplate);
54 evt.setIPV4Address("userIP", InetAddress.getByName("www.yahoo.com"));
55 InetAddress a = evt.getIPV4Address("userIP");
56 assertNotNull(a);
58 byte[] bytes = evt.serialize();
59 Event evt2 = new Event(bytes, true, eventTemplate);
60 assertNotNull(evt2);
61 InetAddress b = evt2.getIPV4Address("userIP");
62 assertNotNull(b);
63 // Can't test the exact hostname b/c you actually get a vip hostname back...
64 assertTrue("Not a yahoo.com address", b.getHostName().endsWith(".yahoo.com"));
67 @Test
68 public void testGetInetAddress() throws EventSystemException, UnknownHostException {
69 Event evt = new Event("Test", false, eventTemplate);
70 evt.setIPAddress("ip", InetAddress.getByName("www.yahoo.com"));
71 InetAddress a = evt.getInetAddress("ip");
72 assertNotNull(a);
75 @Test
76 public void testIsSet() throws EventSystemException {
77 Event evt = new Event("Test", false, eventTemplate);
78 assertFalse(evt.isSet("notset"));
80 evt.setInt32("set", 32);
81 assertTrue(evt.isSet("set"));
84 @Test
85 public void testNullValue() throws EventSystemException {
86 Event evt = new Event("Test", false, eventTemplate);
87 Short s = evt.getInt16("a");
88 assertNull(s);
89 evt.setInt16("a", (short) 1);
90 s = evt.getInt16("a");
91 assertNotNull(s);
92 assertEquals("short value incorrect", (short) 1, s.shortValue());
95 @Test
96 public void testUnsignedTypesValidate() throws EventSystemException {
97 Event evt = new Event("Test", false, eventTemplate);
98 try {
99 evt.setUInt16("SiteID", 0);
100 evt.validate();
102 catch (EventSystemException e) {
103 fail(e.getMessage());
107 @Test
108 public void testValidateEventName() throws EventSystemException {
109 boolean exceptionThrown = false;
110 Event evt = new Event("Test2", false, eventTemplate);
111 try {
112 evt.validate();
114 catch (ValidationExceptions e) {
115 exceptionThrown = true;
117 assertTrue("No exception for invalid event", exceptionThrown);
120 @Test
121 public void testValidateField() throws EventSystemException {
122 Event evt = new Event("Test", false, eventTemplate);
123 try {
124 evt.setString("field1", "avalue");
125 evt.validate();
127 catch (EventSystemException e) {
128 fail(e.getMessage());
132 @Test
133 public void testValidateBadTypeField() throws EventSystemException {
134 Event evt = new Event("Test", false, eventTemplate);
135 try {
136 evt.setInt16("field1", (short) 15);
137 evt.validate();
139 catch (ValidationExceptions e) {
140 List<EventSystemException> exc = e.getAllExceptions();
141 assertEquals("Wrong num of exceptions", 1, exc.size());
142 assertEquals("Wrong exception",
143 "org.lwes.NoSuchAttributeTypeException",
144 exc.get(0).getClass().getName());
148 @Test
149 public void testValidateBadField() throws EventSystemException {
150 Event evt = new Event("Test", false, eventTemplate);
151 try {
152 evt.setInt16("field3", (short) 15);
153 evt.validate();
155 catch (ValidationExceptions e) {
156 List<EventSystemException> exc = e.getAllExceptions();
157 assertEquals("Wrong num of exceptions", 1, exc.size());
158 assertEquals("Wrong exception",
159 "org.lwes.NoSuchAttributeException",
160 exc.get(0).getClass().getName());
164 @Test
165 public void testSerialize() throws EventSystemException {
166 Event evt = new Event("Test", false, eventTemplate);
167 evt.setString("attr_s", "str_value");
168 evt.setInt32("attr_i", 1);
169 byte[] bytes = evt.serialize();
170 String str = new String(bytes);
171 byte[] encoded = Base64.encodeBase64(bytes);
172 // TODO this test not finished yet.
175 @Test
176 public void testEventAccessors() throws EventSystemException, UnknownHostException {
177 Event evt = new Event("Test", false, eventTemplate);
179 evt.setInt16("int16", (short) 1);
180 evt.setInt32("int32", 1337);
181 evt.setInt64("int64", 1337133713371337l);
182 evt.setBoolean("bool", true);
183 evt.setString("str", "string");
184 evt.setUInt16("uint16", 1337); // uint16 in java is just an int
185 evt.setUInt32("uint32", 1337133713371337l); // uint32 in java is a long
186 evt.setUInt64("uint64", 1337133713371337l); // uint64 is a BigInteger
187 evt.setIPAddress("ipaddr", InetAddress.getByName("localhost"));
189 Short s = evt.getInt16("int16");
190 assertNotNull(s);
191 assertEquals("int16 wrong", 1, s.shortValue());
192 Integer i = evt.getInt32("int32");
193 assertNotNull(i);
194 assertEquals("int32 wrong", 1337, i.intValue());
195 Long l = evt.getInt64("int64");
196 assertNotNull(l);
197 assertEquals("int64 wrong", 1337133713371337l, l.longValue());
198 assertEquals("bool wrong", true, evt.getBoolean("bool"));
199 assertEquals("str wrong", "string", evt.getString("str"));
200 i = evt.getUInt16("uint16");
201 assertNotNull(i);
202 assertEquals("uint16 wrong", 1337, i.intValue());
203 l = evt.getUInt32("uint32");
204 assertEquals("uint32 wrong", 1337133713371337l, l.longValue());
205 assertEquals("uint64 wrong",
206 BigInteger.valueOf(1337133713371337l),
207 evt.getUInt64("uint64"));
211 @Test
212 public void testEventSize() throws EventSystemException {
214 Event evt = new Event("Test", false, eventTemplate);
216 for (int i = 0; i < 5000; i++) {
217 evt.setInt32("" + i, i);
220 byte[] bytes = evt.serialize();
221 assertEquals("number of bytes wrong?", 48904, bytes.length);
223 boolean exceptionThrown = false;
224 try {
225 for (int i = 5001; i < 10000; i++) {
226 evt.setInt32("" + i, i);
229 catch (Exception e) {
230 exceptionThrown = true;
231 assertEquals("Different exception",
232 "org.lwes.EventSystemException",
233 e.getClass().getName());
235 assertTrue("Size exception was not thrown", exceptionThrown);