boolean and byte array support added
[lwes-java.git] / src / test / java / org / lwes / db / ArrayTest.java
blob4d7696f05ec308583419da2195cf530dbf3fec36
1 package org.lwes.db;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6 import org.junit.Before;
7 import org.junit.Test;
8 import org.lwes.Event;
9 import org.lwes.EventAttributeSizeException;
10 import org.lwes.EventSystemException;
11 import org.lwes.util.Log;
13 import java.io.File;
14 import java.util.Enumeration;
17 /**
18 * @author fmaritato
21 public class ArrayTest {
23 private static final String ESF = "src/test/java/org/lwes/db/ArrayTest.esf";
24 private static final String TEST_EVENT = "TestEvent";
26 private EventTemplateDB template = null;
28 @Before
29 public void setUp() {
30 template = new EventTemplateDB();
31 template.setESFFile(new File(ESF));
32 template.initialize();
35 @Test
36 public void testArrayParse() {
38 EventTemplateDB template = new EventTemplateDB();
39 template.setESFFile(new File(ESF));
40 assertTrue("Template did not initialize", template.initialize());
42 Enumeration<String> eventNames = template.getEventNames();
43 assertNotNull("Event names enum was null", eventNames);
45 assertTrue("TestEvent was not known to the template",
46 template.checkForEvent(TEST_EVENT));
48 assertTrue("field1 attribute not known to the template",
49 template.checkForAttribute(TEST_EVENT, "field1"));
53 @Test
54 public void testStringArray() throws EventSystemException {
56 // First try to set a valid string array
57 Event evt = new Event("TestEvent", true, template);
58 evt.setStringArray("field1", new String[]{"1", "2", "3"});
59 String[] ar = evt.getStringArray("field1");
60 assertNotNull("string array was null", ar);
61 assertEquals("length was incorrect", 3, ar.length);
63 // Make sure we can serialize/deserialize it
64 byte[] serializedEvent = evt.serialize();
65 Event evt2 = new Event(serializedEvent, true, template);
66 assertNotNull(evt2);
67 String[] a2 = evt2.getStringArray("field1");
68 assertNotNull(a2);
69 assertEquals("deserialized array length was incorrect", 3, a2.length);
71 // Now try to set an invalid string array and make
72 // sure an exception is thrown.
73 boolean exceptionThrown = false;
74 try {
75 evt.setStringArray("field1", new String[]{"1", "2", "3",
76 "4", "5", "6",
77 "7", "8", "9",
78 "10", "11", "12"});
80 catch (EventAttributeSizeException e) {
81 if (Log.isLogDebug()) {
82 Log.debug(e.getMessage());
84 exceptionThrown = true;
86 assertTrue("No exception was thrown for array length", exceptionThrown);
89 @Test
90 public void testInt16() throws EventSystemException {
91 // First try to set a valid string array
92 Event evt = new Event("TestEvent", true, template);
93 evt.setInt16Array("field2", new short[]{1, 2, 3});
94 short[] ar = evt.getInt16Array("field2");
95 assertNotNull("string array was null", ar);
96 assertEquals("length was incorrect", 3, ar.length);
98 // Make sure we can serialize/deserialize it
99 byte[] serializedEvent = evt.serialize();
100 Event evt2 = new Event(serializedEvent, true, template);
101 assertNotNull(evt2);
102 short[] a2 = evt2.getInt16Array("field2");
103 assertNotNull(a2);
104 assertEquals("deserialized array length was incorrect", 3, a2.length);
106 // Now try to set an invalid string array and make
107 // sure an exception is thrown.
108 boolean exceptionThrown = false;
109 try {
110 evt.setInt16Array("field2", new short[]{1, 2, 3,
111 4, 5, 6,
112 7, 8, 9,
113 10, 11, 12});
115 catch (EventAttributeSizeException e) {
116 if (Log.isLogDebug()) {
117 Log.debug(e.getMessage());
119 exceptionThrown = true;
121 assertTrue("No exception was thrown for array length", exceptionThrown);
124 @Test
125 public void testInt32() throws EventSystemException {
126 // First try to set a valid string array
127 Event evt = new Event("TestEvent", true, template);
128 evt.setInt32Array("field3", new int[]{1234567890, 234567890, 345678901});
129 int[] ar = evt.getInt32Array("field3");
130 assertNotNull("string array was null", ar);
131 assertEquals("length was incorrect", 3, ar.length);
133 // Make sure we can serialize/deserialize it
134 byte[] serializedEvent = evt.serialize();
135 Event evt2 = new Event(serializedEvent, true, template);
136 assertNotNull(evt2);
137 int[] a2 = evt2.getInt32Array("field3");
138 assertNotNull(a2);
139 assertEquals("deserialized array length was incorrect", 3, a2.length);
140 assertEquals("a2[0]", a2[0], 1234567890);
141 assertEquals("a2[1]", a2[1], 234567890);
142 assertEquals("a2[2]", a2[2], 345678901);
144 // Now try to set an invalid string array and make
145 // sure an exception is thrown.
146 boolean exceptionThrown = false;
147 try {
148 evt.setInt32Array("field3", new int[]{1, 2, 3,
149 4, 5, 6,
150 7, 8, 9,
151 10, 11, 12});
153 catch (EventAttributeSizeException e) {
154 if (Log.isLogDebug()) {
155 Log.debug(e.getMessage());
157 exceptionThrown = true;
159 assertTrue("No exception was thrown for array length", exceptionThrown);
162 @Test
163 public void testInt64() throws EventSystemException {
164 // First try to set a valid string array
165 Event evt = new Event("TestEvent", true, template);
166 evt.setInt64Array("field4", new long[]{123456789012l, 234567890123l});
167 long[] ar = evt.getInt64Array("field4");
168 assertNotNull("string array was null", ar);
169 assertEquals("length was incorrect", 2, ar.length);
171 // Make sure we can serialize/deserialize it
172 byte[] serializedEvent = evt.serialize();
173 Event evt2 = new Event(serializedEvent, true, template);
174 assertNotNull(evt2);
175 long[] a2 = evt2.getInt64Array("field4");
176 assertNotNull(a2);
177 assertEquals("deserialized array length was incorrect", 2, a2.length);
178 assertEquals("a2[0]", a2[0], 123456789012l);
179 assertEquals("a2[1]", a2[1], 234567890123l);
181 // Now try to set an invalid string array and make
182 // sure an exception is thrown.
183 boolean exceptionThrown = false;
184 try {
185 evt.setInt64Array("field4", new long[]{1, 2, 3,
186 4, 5, 6,
187 7, 8, 9,
188 10, 11, 12});
190 catch (EventAttributeSizeException e) {
191 if (Log.isLogDebug()) {
192 Log.debug(e.getMessage());
194 exceptionThrown = true;
196 assertTrue("No exception was thrown for array length", exceptionThrown);
199 @Test
200 public void testUInt16() throws EventSystemException {
201 // First try to set a valid string array
202 Event evt = new Event("TestEvent", true, template);
203 evt.setUInt16Array("field5", new int[]{1, 2, 3});
204 int[] ar = evt.getUInt16Array("field5");
205 assertNotNull("string array was null", ar);
206 assertEquals("length was incorrect", 3, ar.length);
208 // Make sure we can serialize/deserialize it
209 byte[] serializedEvent = evt.serialize();
210 Event evt2 = new Event(serializedEvent, true, template);
211 assertNotNull(evt2);
212 int[] a2 = evt2.getUInt16Array("field5");
213 assertNotNull(a2);
214 assertEquals("deserialized array length was incorrect", 3, a2.length);
216 // Now try to set an invalid string array and make
217 // sure an exception is thrown.
218 boolean exceptionThrown = false;
219 try {
220 evt.setUInt16Array("field5", new int[]{1, 2, 3,
221 4, 5, 6,
222 7, 8, 9,
223 10, 11, 12});
225 catch (EventAttributeSizeException e) {
226 if (Log.isLogDebug()) {
227 Log.debug(e.getMessage());
229 exceptionThrown = true;
231 assertTrue("No exception was thrown for array length", exceptionThrown);
234 @Test
235 public void testUInt32() throws EventSystemException {
236 // First try to set a valid string array
237 Event evt = new Event("TestEvent", true, template);
238 evt.setUInt32Array("field6", new long[]{1234567890, 234567890, 345678901});
239 long[] ar = evt.getUInt32Array("field6");
240 assertNotNull("string array was null", ar);
241 assertEquals("length was incorrect", 3, ar.length);
243 // Make sure we can serialize/deserialize it
244 byte[] serializedEvent = evt.serialize();
245 Event evt2 = new Event(serializedEvent, true, template);
246 assertNotNull(evt2);
247 long[] a2 = evt2.getUInt32Array("field6");
248 assertNotNull(a2);
249 assertEquals("deserialized array length was incorrect", 3, a2.length);
250 assertEquals("a2[0]", a2[0], 1234567890);
251 assertEquals("a2[1]", a2[1], 234567890);
252 assertEquals("a2[2]", a2[2], 345678901);
254 // Now try to set an invalid string array and make
255 // sure an exception is thrown.
256 boolean exceptionThrown = false;
257 try {
258 evt.setUInt32Array("field6", new long[]{1, 2, 3,
259 4, 5, 6,
260 7, 8, 9,
261 10, 11, 12});
263 catch (EventAttributeSizeException e) {
264 if (Log.isLogDebug()) {
265 Log.debug(e.getMessage());
267 exceptionThrown = true;
269 assertTrue("No exception was thrown for array length", exceptionThrown);
272 @Test
273 public void testUInt64() throws EventSystemException {
274 // First try to set a valid string array
275 Event evt = new Event("TestEvent", true, template);
276 evt.setUInt64Array("field7", new long[]{123456789012l, 234567890123l});
277 long[] ar = evt.getUInt64Array("field7");
278 assertNotNull("string array was null", ar);
279 assertEquals("length was incorrect", 2, ar.length);
281 // Make sure we can serialize/deserialize it
282 byte[] serializedEvent = evt.serialize();
283 Event evt2 = new Event(serializedEvent, true, template);
284 assertNotNull(evt2);
285 long[] a2 = evt2.getUInt64Array("field7");
286 assertNotNull(a2);
287 assertEquals("deserialized array length was incorrect", 2, a2.length);
288 assertEquals("a2[0]", a2[0], 123456789012l);
289 assertEquals("a2[1]", a2[1], 234567890123l);
291 // Now try to set an invalid string array and make
292 // sure an exception is thrown.
293 boolean exceptionThrown = false;
294 try {
295 evt.setUInt64Array("field7", new long[]{1, 2, 3,
296 4, 5, 6,
297 7, 8, 9,
298 10, 11, 12});
300 catch (EventAttributeSizeException e) {
301 if (Log.isLogDebug()) {
302 Log.debug(e.getMessage());
304 exceptionThrown = true;
306 assertTrue("No exception was thrown for array length", exceptionThrown);
309 @Test
310 public void testBooleanArray() throws EventSystemException {
311 Event evt = new Event("TestEvent", true, template);
312 evt.setBooleanArray("field8", new boolean[]{true, false});
313 boolean[] ar = evt.getBooleanArray("field8");
314 assertNotNull("boolean array was null", ar);
315 assertEquals("length was incorrect", 2, ar.length);
317 // Make sure we can serialize/deserialize it
318 byte[] serializedEvent = evt.serialize();
319 Event evt2 = new Event(serializedEvent, true, template);
320 assertNotNull(evt2);
321 boolean[] a2 = evt2.getBooleanArray("field8");
322 assertNotNull(a2);
323 assertEquals("deserialized array length was incorrect", 2, a2.length);
324 assertEquals("a2[0]", a2[0], true);
325 assertEquals("a2[1]", a2[1], false);
328 @Test
329 public void testByteArray() throws EventSystemException {
330 Event evt = new Event("TestEvent", true, template);
331 evt.setByteArray("field9", new byte[]{(byte) 0x1, (byte) 0x2});
332 byte[] ar = evt.getByteArray("field9");
333 assertNotNull("boolean array was null", ar);
334 assertEquals("length was incorrect", 2, ar.length);
336 // Make sure we can serialize/deserialize it
337 byte[] serializedEvent = evt.serialize();
338 Event evt2 = new Event(serializedEvent, true, template);
339 assertNotNull(evt2);
340 byte[] a2 = evt2.getByteArray("field9");
341 assertNotNull(a2);
342 assertEquals("deserialized array length was incorrect", 2, a2.length);
343 assertEquals("a2[0]", a2[0], (byte) 0x1);
344 assertEquals("a2[1]", a2[1], (byte) 0x2);