Skip to content

Commit 4d02e6b

Browse files
mk868VietND96
andauthored
[java] Add nullness for logging (#15108)
Co-authored-by: Viet Nguyen Duc <[email protected]>
1 parent 3c16d81 commit 4d02e6b

File tree

5 files changed

+20
-5
lines changed

5 files changed

+20
-5
lines changed

java/src/org/openqa/selenium/logging/LogEntries.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
import java.util.Iterator;
2424
import java.util.List;
2525
import java.util.stream.StreamSupport;
26+
import org.jspecify.annotations.NullMarked;
2627
import org.openqa.selenium.Beta;
2728

2829
/**
2930
* Represent a pool of {@link LogEntry}. This class also provides filtering mechanisms based on
3031
* levels.
3132
*/
3233
@Beta
34+
@NullMarked
3335
public class LogEntries implements Iterable<LogEntry> {
3436

3537
private final List<LogEntry> entries;

java/src/org/openqa/selenium/logging/LogEntry.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
import java.util.HashMap;
2323
import java.util.Map;
2424
import java.util.logging.Level;
25+
import org.jspecify.annotations.NullMarked;
2526

2627
/** Represents a single log statement. */
28+
@NullMarked
2729
public class LogEntry {
2830

2931
private final Level level;

java/src/org/openqa/selenium/logging/LogLevelMapping.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,20 @@
2020
import java.util.Collections;
2121
import java.util.HashMap;
2222
import java.util.Map;
23+
import java.util.Optional;
2324
import java.util.logging.Level;
25+
import org.jspecify.annotations.NullMarked;
26+
import org.jspecify.annotations.Nullable;
2427

28+
@NullMarked
2529
public class LogLevelMapping {
2630

2731
/** WebDriver log level DEBUG which is mapped to Level.FINE. */
2832
private static final String DEBUG = "DEBUG";
2933

34+
// Default the log level to info.
35+
private static final Level DEFAULT_LEVEL = Level.INFO;
36+
3037
private static final Map<Integer, Level> levelMap;
3138

3239
static {
@@ -70,15 +77,15 @@ public static String getName(Level level) {
7077
return normalized == Level.FINE ? DEBUG : normalized.getName();
7178
}
7279

73-
public static Level toLevel(String logLevelName) {
80+
public static Level toLevel(@Nullable String logLevelName) {
7481
if (logLevelName == null || logLevelName.isEmpty()) {
75-
// Default the log level to info.
76-
return Level.INFO;
82+
return DEFAULT_LEVEL;
7783
}
7884

7985
if (logLevelName.equals(DEBUG)) {
8086
return Level.FINE;
8187
}
82-
return levelMap.get(Level.parse(logLevelName).intValue());
88+
return Optional.ofNullable(levelMap.get(Level.parse(logLevelName).intValue()))
89+
.orElse(DEFAULT_LEVEL);
8390
}
8491
}

java/src/org/openqa/selenium/logging/SessionLogs.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@
2424
import java.util.List;
2525
import java.util.Map;
2626
import java.util.Set;
27+
import org.jspecify.annotations.NullMarked;
2728
import org.openqa.selenium.Beta;
29+
import org.openqa.selenium.internal.Require;
2830

2931
/** Contains the logs for a session divided by supported log types. */
3032
@Beta
33+
@NullMarked
3134
public class SessionLogs {
3235
private final Map<String, LogEntries> logTypeToEntriesMap;
3336

@@ -66,7 +69,7 @@ public static SessionLogs fromJSON(Map<String, Object> rawSessionLogs) {
6669
logEntries.add(
6770
new LogEntry(
6871
LogLevelMapping.toLevel(String.valueOf(rawEntry.get("level"))),
69-
((Number) rawEntry.get("timestamp")).longValue(),
72+
Require.nonNull("timestamp", (Number) rawEntry.get("timestamp")).longValue(),
7073
String.valueOf(rawEntry.get("message"))));
7174
}
7275
sessionLogs.addLog(logType, new LogEntries(logEntries));

java/test/org/openqa/selenium/logging/LoggingTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ void testLogLevelConversions() {
4040
assertThat(toLevel("WARNING")).isEqualTo(WARNING);
4141
assertThat(toLevel("SEVERE")).isEqualTo(SEVERE);
4242
assertThat(toLevel("OFF")).isEqualTo(OFF);
43+
assertThat(toLevel(null)).isEqualTo(INFO);
4344
}
4445

4546
@Test

0 commit comments

Comments
 (0)