Skip to content

Commit 724ffdd

Browse files
EventTypeSupplier Improvement
Improving null handling by defaulting a null constructor parameter to Logger.EVENT_UNSPECIFIED. This then prevents the get() call from ever returning an empty String. Test case updated to be parameterized and validates each of the define EventType cases, along with the null EventType case.
1 parent fc4fd1e commit 724ffdd

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

src/main/java/org/owasp/esapi/logging/appender/EventTypeLogSupplier.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.function.Supplier;
1919

20+
import org.owasp.esapi.Logger;
2021
import org.owasp.esapi.Logger.EventType;
2122

2223
/**
@@ -34,11 +35,11 @@ public class EventTypeLogSupplier implements Supplier<String> {
3435
* @param evtyp EventType reference to supply log representation for
3536
*/
3637
public EventTypeLogSupplier(EventType evtyp) {
37-
this.eventType = evtyp;
38+
this.eventType = evtyp == null ? Logger.EVENT_UNSPECIFIED : evtyp;
3839
}
3940

4041
@Override
4142
public String get() {
42-
return eventType == null ? "" : eventType.toString();
43+
return eventType.toString();
4344
}
4445
}

src/test/java/org/owasp/esapi/logging/appender/EventTypeLogSupplierTest.java

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,45 @@
22

33
import static org.junit.Assert.assertEquals;
44

5+
import java.util.ArrayList;
6+
import java.util.Collection;
7+
import java.util.List;
8+
59
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
import org.junit.runners.Parameterized;
12+
import org.junit.runners.Parameterized.Parameters;
613
import org.owasp.esapi.Logger;
714
import org.owasp.esapi.Logger.EventType;
815

16+
@RunWith(Parameterized.class)
917
public class EventTypeLogSupplierTest {
1018

19+
@Parameters (name="{0} -> {1}")
20+
public static Collection<Object[]> assembleTests() {
21+
List<Object[]> paramSets = new ArrayList<>();
22+
paramSets.add(new Object[] {Logger.EVENT_FAILURE,Logger.EVENT_FAILURE.toString()});
23+
paramSets.add(new Object[] {Logger.EVENT_SUCCESS,Logger.EVENT_SUCCESS.toString()});
24+
paramSets.add(new Object[] {Logger.EVENT_UNSPECIFIED,Logger.EVENT_UNSPECIFIED.toString()});
25+
paramSets.add(new Object[] {Logger.SECURITY_AUDIT,Logger.SECURITY_AUDIT.toString()});
26+
paramSets.add(new Object[] {Logger.SECURITY_FAILURE,Logger.SECURITY_FAILURE.toString()});
27+
paramSets.add(new Object[] {Logger.SECURITY_SUCCESS,Logger.SECURITY_SUCCESS.toString()});
28+
paramSets.add(new Object[] {null, Logger.EVENT_UNSPECIFIED.toString()});
29+
30+
return paramSets;
31+
}
32+
33+
private final EventType eventType;
34+
private final String expectedResult;
35+
36+
public EventTypeLogSupplierTest(EventType eventType, String result) {
37+
this.eventType = eventType;
38+
this.expectedResult = result;
39+
}
1140
@Test
1241
public void testEventTypeLog() {
13-
EventType eventType = Logger.EVENT_UNSPECIFIED;
1442
EventTypeLogSupplier supplier = new EventTypeLogSupplier(eventType);
15-
16-
assertEquals(eventType.toString(), supplier.get());
43+
assertEquals(expectedResult, supplier.get());
1744
}
1845

19-
@Test
20-
public void testNullEventTypeLog() {
21-
22-
EventTypeLogSupplier supplier = new EventTypeLogSupplier(null);
23-
24-
assertEquals("", supplier.get());
25-
}
2646
}

0 commit comments

Comments
 (0)