1 /*======================================================================*
2 * Copyright (c) 2010, Frank Maritato All rights reserved. *
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 *======================================================================*/
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
;
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
;
46 eventTemplate
= new EventTemplateDB();
47 eventTemplate
.setESFFile(new File("src/test/java/org/lwes/EventTest.esf"));
48 eventTemplate
.initialize();
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");
58 byte[] bytes
= evt
.serialize();
59 Event evt2
= new Event(bytes
, true, eventTemplate
);
61 InetAddress b
= evt2
.getIPV4Address("userIP");
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"));
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");
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"));
85 public void testNullValue() throws EventSystemException
{
86 Event evt
= new Event("Test", false, eventTemplate
);
87 Short s
= evt
.getInt16("a");
89 evt
.setInt16("a", (short) 1);
90 s
= evt
.getInt16("a");
92 assertEquals("short value incorrect", (short) 1, s
.shortValue());
96 public void testUnsignedTypesValidate() throws EventSystemException
{
97 Event evt
= new Event("Test", false, eventTemplate
);
99 evt
.setUInt16("SiteID", 0);
102 catch (EventSystemException e
) {
103 fail(e
.getMessage());
108 public void testValidateEventName() throws EventSystemException
{
109 boolean exceptionThrown
= false;
110 Event evt
= new Event("Test2", false, eventTemplate
);
114 catch (ValidationExceptions e
) {
115 exceptionThrown
= true;
117 assertTrue("No exception for invalid event", exceptionThrown
);
121 public void testValidateField() throws EventSystemException
{
122 Event evt
= new Event("Test", false, eventTemplate
);
124 evt
.setString("field1", "avalue");
127 catch (EventSystemException e
) {
128 fail(e
.getMessage());
133 public void testValidateBadTypeField() throws EventSystemException
{
134 Event evt
= new Event("Test", false, eventTemplate
);
136 evt
.setInt16("field1", (short) 15);
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());
149 public void testValidateBadField() throws EventSystemException
{
150 Event evt
= new Event("Test", false, eventTemplate
);
152 evt
.setInt16("field3", (short) 15);
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());
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.
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");
191 assertEquals("int16 wrong", 1, s
.shortValue());
192 Integer i
= evt
.getInt32("int32");
194 assertEquals("int32 wrong", 1337, i
.intValue());
195 Long l
= evt
.getInt64("int64");
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");
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"));
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;
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
);