rev version
[lwes-java.git] / tests / org / lwes / db / EventTemplateDBTest.java
blob8e3b6a12a19cfa73ee469afb3607cb1d1bf4019b
1 package org.lwes.db;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertNotNull;
6 import static org.junit.Assert.assertTrue;
7 import static org.junit.Assert.fail;
8 import org.junit.Test;
9 import org.lwes.BaseType;
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.FileNotFoundException;
14 import java.util.Enumeration;
15 import java.util.Map;
17 /**
18 * @author fmaritato
21 public class EventTemplateDBTest {
23 private static final String ESF = "tests/org/lwes/db/EventTemplateDBTest.esf";
24 private static final String TEST_EVENT = "TestEvent";
26 @Test
27 public void testTemplateFromFile() {
28 EventTemplateDB template = new EventTemplateDB();
29 template.setESFFile(new File(ESF));
30 assertTrue("Template did not initialize", template.initialize());
32 Enumeration<String> eventNames = template.getEventNames();
33 assertNotNull("Event names enum was null", eventNames);
35 assertTrue("TestEvent was not known to the template",
36 template.checkForEvent(TEST_EVENT));
38 assertTrue("field1 attribute not known to the template",
39 template.checkForAttribute(TEST_EVENT, "field1"));
41 // Check a metadata attribute
42 assertTrue("SenderIP attribute not known to the template",
43 template.checkForAttribute(TEST_EVENT, "SenderIP"));
45 BaseType bt = template.getBaseTypeForObjectAttribute(TEST_EVENT, "field2", 100l);
46 assertNotNull(bt);
47 assertEquals("Wrong BaseType returned", "int64", bt.getTypeName());
49 assertTrue("Wrong type for attribute field2",
50 template.checkTypeForAttribute(TEST_EVENT, "field2", bt));
52 assertFalse("template allowed string for a numeric type",
53 template.checkTypeForAttribute(TEST_EVENT, "field2", "wrong"));
55 Map<String, BaseType> bts = template.getBaseTypesForEvent(TEST_EVENT);
56 assertNotNull(bts);
57 assertEquals("Number of types in the map is wrong", 7, bts.size());
58 BaseType field2BT = bts.get("field2");
59 assertNotNull(field2BT);
60 assertEquals("int64", field2BT.getTypeName());
62 Object obj = template.parseAttribute(TEST_EVENT, "field2", "100");
63 assertNotNull(obj);
65 String testHtmlString =
66 new StringBuilder().append("<table>\n")
67 .append("<tr><th>MetaEventInfo</th><th>Type</th><th>Name</th></tr>\n")
68 .append("<tr><td></td><td>uint16</td><td>SiteID</td></tr>\n")
69 .append("<tr><td></td><td>int16</td><td>enc</td></tr>\n")
70 .append("<tr><td></td><td>ip_addr</td><td>SenderIP</td></tr>\n")
71 .append("<tr><td></td><td>int64</td><td>ReceiptTime</td></tr>\n")
72 .append("<tr><td></td><td>uint16</td><td>SenderPort</td></tr>\n")
73 .append("<tr><th>TestEvent</th><th>Type</th><th>Name</th></tr>\n")
74 .append("<tr><td></td><td>uint16</td><td>SiteID</td></tr>\n")
75 .append("<tr><td></td><td>int16</td><td>enc</td></tr>\n")
76 .append("<tr><td></td><td>string</td><td>field1</td></tr>\n")
77 .append("<tr><td></td><td>int64</td><td>field2</td></tr>\n")
78 .append("<tr><td></td><td>ip_addr</td><td>SenderIP</td></tr>\n")
79 .append("<tr><td></td><td>int64</td><td>ReceiptTime</td></tr>\n")
80 .append("<tr><td></td><td>uint16</td><td>SenderPort</td></tr>\n")
81 .append("</table>\n")
82 .toString();
83 String htmlString = template.toHtmlString();
84 assertNotNull("html string was null", htmlString);
85 assertEquals("html string did not match", testHtmlString, htmlString);
87 String testString =
88 new StringBuilder().append("\nMetaEventInfo\n")
89 .append("{\n")
90 .append("\tint64 ReceiptTime;\n")
91 .append("\tip_addr SenderIP;\n")
92 .append("\tuint16 SenderPort;\n")
93 .append("\tuint16 SiteID;\n")
94 .append("\tint16 enc;\n")
95 .append("}\n")
96 .append("TestEvent\n")
97 .append("{\n")
98 .append("\tint64 ReceiptTime;\n")
99 .append("\tip_addr SenderIP;\n")
100 .append("\tuint16 SenderPort;\n")
101 .append("\tuint16 SiteID;\n")
102 .append("\tint16 enc;\n")
103 .append("\tstring field1;\n")
104 .append("\tint64 field2;\n")
105 .append("}\n")
106 .toString();
107 String toString = template.toString();
108 assertNotNull("toString was null", toString);
109 assertEquals("test string did not match", testString, toString);
112 @Test
113 public void testTemplateFromStream() {
114 EventTemplateDB template = new EventTemplateDB();
115 try {
116 template.setESFInputStream(new FileInputStream(ESF));
118 catch (FileNotFoundException e) {
119 fail(e.getMessage());
121 assertTrue(template.initialize());