changes to support required keyword
[lwes-java.git] / src / test / java / org / lwes / db / RequiredTest.java
blobfa64cf5043fa84771104b3079488201158ac264f
1 package org.lwes.db;
2 /**
3 * @author fmaritato
4 */
6 import static org.junit.Assert.assertFalse;
7 import static org.junit.Assert.assertNotNull;
8 import static org.junit.Assert.assertTrue;
9 import org.junit.Test;
10 import org.lwes.AttributeRequiredException;
11 import org.lwes.Event;
12 import org.lwes.EventSystemException;
13 import org.lwes.util.Log;
15 import java.io.File;
16 import java.util.Enumeration;
18 public class RequiredTest {
20 private static final String ESF = "src/test/java/org/lwes/db/RequiredTest.esf";
21 private static final String TEST_EVENT = "TestEvent";
23 @Test
24 public void testRequired() throws EventSystemException {
25 EventTemplateDB template = new EventTemplateDB();
26 template.setESFFile(new File(ESF));
27 assertTrue("Template did not initialize", template.initialize());
28 Enumeration<String> eventNames = template.getEventNames();
29 assertNotNull("Event names enum was null", eventNames);
31 assertTrue("TestEvent was not known to the template",
32 template.checkForEvent(TEST_EVENT));
34 assertTrue("field1 attribute not known to the template",
35 template.checkForAttribute(TEST_EVENT, "field1"));
37 boolean exceptionThrown = false;
39 // Verify that an exception is thrown when a required field is not present.
40 Event evt = new Event("TestEvent", true, template);
41 try {
42 evt.validate();
44 catch (AttributeRequiredException e) {
45 if (Log.isLogDebug()) {
46 Log.debug(e.getMessage());
48 exceptionThrown = true;
50 assertTrue(exceptionThrown);
52 exceptionThrown = false;
53 // Verify no exception when all required fields are set.
54 evt.setString("field1", "value");
55 if (Log.isLogDebug()) {
56 Log.debug(evt.toString());
58 try {
59 evt.validate();
61 catch (AttributeRequiredException e) {
62 if (Log.isLogDebug()) {
63 Log.debug(e.getMessage());
65 exceptionThrown = true;
67 assertFalse(exceptionThrown);