Skip to content

Commit 7cd16dd

Browse files
committed
Fix json de/serialization of java.util.logging.Level.FINE
1 parent 29b6339 commit 7cd16dd

File tree

5 files changed

+74
-25
lines changed

5 files changed

+74
-25
lines changed

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

+35-13
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,20 @@
1616

1717
package org.openqa.selenium.logging;
1818

19-
import java.util.Map;
20-
import java.util.logging.Level;
19+
import static com.google.common.base.Strings.isNullOrEmpty;
2120

2221
import com.google.common.collect.ImmutableMap;
2322

23+
import java.util.logging.Level;
24+
2425
public class LogLevelMapping {
2526

26-
private static Map<Long, Level> levelMap;
27+
/**
28+
* WebDriver log level DEBUG which is mapped to Level.FINE.
29+
*/
30+
private static final String DEBUG = "DEBUG";
31+
32+
private static ImmutableMap<Integer, Level> levelMap;
2733

2834
static {
2935
Level[] supportedLevels = new Level[] {
@@ -34,31 +40,47 @@ public class LogLevelMapping {
3440
Level.SEVERE,
3541
Level.OFF
3642
};
37-
ImmutableMap.Builder<Long, Level> builder = ImmutableMap.builder();
43+
ImmutableMap.Builder<Integer, Level> builder = ImmutableMap.builder();
3844
for (Level level : supportedLevels) {
39-
builder.put((long)level.intValue(), level);
45+
builder.put(level.intValue(), level);
4046
}
4147
levelMap = builder.build();
4248
}
43-
49+
4450
/**
45-
* WebDriver log level DEBUG which is mapped to Level.FINE.
51+
* Normalizes the given level to one of those supported by Selenium.
4652
*/
47-
private static final String DEBUG = "DEBUG";
48-
49-
public static Level toLevel(long longValue) {
50-
return levelMap.get(longValue);
53+
public static Level normalize(Level level) {
54+
if (levelMap.containsKey(level.intValue())) {
55+
return levelMap.get(level.intValue());
56+
} else if (level.intValue() >= Level.SEVERE.intValue()) {
57+
return Level.SEVERE;
58+
} else if (level.intValue() >= Level.WARNING.intValue()) {
59+
return Level.WARNING;
60+
} else if (level.intValue() >= Level.INFO.intValue()) {
61+
return Level.INFO;
62+
} else {
63+
return Level.FINE;
64+
}
65+
}
66+
67+
/**
68+
* Converts the JDK level to a name supported by Selenium.
69+
*/
70+
public static String getName(Level level) {
71+
Level normalized = normalize(level);
72+
return normalized == Level.FINE ? DEBUG : normalized.getName();
5173
}
5274

5375
public static Level toLevel(String logLevelName) {
54-
if (logLevelName == null || "".equals(logLevelName)) {
76+
if (isNullOrEmpty(logLevelName)) {
5577
// Default the log level to info.
5678
return Level.INFO;
5779
}
5880

5981
if (logLevelName.equals(DEBUG)) {
6082
return Level.FINE;
6183
}
62-
return levelMap.get((long)Level.parse(logLevelName).intValue());
84+
return levelMap.get(Level.parse(logLevelName).intValue());
6385
}
6486
}

java/client/src/org/openqa/selenium/remote/BeanToJsonConverter.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.openqa.selenium.Cookie;
2929
import org.openqa.selenium.WebDriverException;
3030
import org.openqa.selenium.logging.LogEntries;
31+
import org.openqa.selenium.logging.LogLevelMapping;
3132
import org.openqa.selenium.logging.LoggingPreferences;
3233
import org.openqa.selenium.logging.SessionLogs;
3334

@@ -106,7 +107,7 @@ private JsonElement convertObject(Object toConvert, int maxDepth) throws Excepti
106107
}
107108

108109
if (toConvert instanceof Level) {
109-
return new JsonPrimitive(toConvert.toString());
110+
return new JsonPrimitive(LogLevelMapping.getName((Level) toConvert));
110111
}
111112

112113
if (toConvert.getClass().isEnum() || toConvert instanceof Enum) {
@@ -117,7 +118,7 @@ private JsonElement convertObject(Object toConvert, int maxDepth) throws Excepti
117118
LoggingPreferences prefs = (LoggingPreferences) toConvert;
118119
JsonObject converted = new JsonObject();
119120
for (String logType : prefs.getEnabledLogTypes()) {
120-
converted.addProperty(logType, prefs.getLevel(logType).toString());
121+
converted.addProperty(logType, LogLevelMapping.getName(prefs.getLevel(logType)));
121122
}
122123
return converted;
123124
}

java/client/src/org/openqa/selenium/remote/JsonToBeanConverter.java

+2-7
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,8 @@ private <T> T convert(Class<T> clazz, Object text, int depth) {
120120
JsonObject json = text instanceof JsonElement
121121
? ((JsonElement) text).getAsJsonObject()
122122
: new JsonParser().parse(text.toString()).getAsJsonObject();
123-
DesiredCapabilities caps = new DesiredCapabilities();
124-
125-
for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
126-
caps.setCapability(entry.getKey(), convert(Object.class, entry.getValue(), depth + 1));
127-
}
128-
129-
return (T) caps;
123+
Map<String, Object> map = convertMap(json.getAsJsonObject(), depth);
124+
return (T) new DesiredCapabilities(map);
130125
}
131126

132127
if (Date.class.equals(clazz)) {

java/client/test/org/openqa/selenium/remote/BeanToJsonConverterTest.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -370,15 +370,19 @@ public void testProperlyConvertsNulls() {
370370
@Test
371371
public void testConvertLoggingPreferencesToJson() {
372372
LoggingPreferences prefs = new LoggingPreferences();
373+
prefs.enable(LogType.BROWSER, Level.WARNING);
373374
prefs.enable(LogType.CLIENT, Level.FINE);
374375
prefs.enable(LogType.DRIVER, Level.ALL);
376+
prefs.enable(LogType.SERVER, Level.OFF);
375377

376378
String json = new BeanToJsonConverter().convert(prefs);
377379

378380
JsonObject converted = new JsonParser().parse(json).getAsJsonObject();
379381

380-
assertEquals("FINE", converted.get(LogType.CLIENT).getAsString());
382+
assertEquals("WARNING", converted.get(LogType.BROWSER).getAsString());
383+
assertEquals("DEBUG", converted.get(LogType.CLIENT).getAsString());
381384
assertEquals("ALL", converted.get(LogType.DRIVER).getAsString());
385+
assertEquals("OFF", converted.get(LogType.SERVER).getAsString());
382386
}
383387

384388
@Test

java/client/test/org/openqa/selenium/remote/JsonToBeanConverterTest.java

+29-2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@
2222
import org.openqa.selenium.Capabilities;
2323
import org.openqa.selenium.Cookie;
2424
import org.openqa.selenium.Platform;
25+
import org.openqa.selenium.logging.LogType;
26+
import org.openqa.selenium.logging.LoggingPreferences;
2527

2628
import java.util.Collections;
2729
import java.util.Date;
2830
import java.util.HashMap;
2931
import java.util.List;
3032
import java.util.Map;
3133
import java.util.concurrent.TimeUnit;
34+
import java.util.logging.Level;
3235

3336
import static org.hamcrest.Matchers.equalTo;
3437
import static org.hamcrest.Matchers.hasEntry;
@@ -159,7 +162,8 @@ public void testShouldUseAMapToRepresentComplexObjects() throws Exception {
159162
toModel.addProperty("thing", "hairy");
160163
toModel.addProperty("hairy", "true");
161164

162-
Map<?,?> modelled = (Map<?,?>) new JsonToBeanConverter().convert(Object.class, toModel.toString());
165+
Map<?,?> modelled = (Map<?,?>) new JsonToBeanConverter().convert(Object.class,
166+
toModel.toString());
163167
assertEquals(2, modelled.size());
164168
}
165169

@@ -244,7 +248,7 @@ public void testShouldConvertObjectsInArraysToMaps() throws Exception {
244248
private void assertMapEntry(Map<?,?> map, String key, Object expected) {
245249
assertTrue("Missing key: " + key, map.containsKey(key));
246250
assertEquals("Wrong value for key: " + key + ": " + map.get(key).getClass().getName(),
247-
expected, map.get(key));
251+
expected, map.get(key));
248252
}
249253

250254
@Test
@@ -296,6 +300,29 @@ public void testShouldConvertCapabilitiesToAMapAndIncludeCustomValues() throws E
296300
assertEquals("fishy", converted.getCapability("furrfu"));
297301
}
298302

303+
@Test
304+
public void testShouldParseCapabilitiesWithLoggingPreferences() throws Exception {
305+
JsonObject prefs = new JsonObject();
306+
prefs.addProperty("browser", "WARNING");
307+
prefs.addProperty("client", "DEBUG");
308+
prefs.addProperty("driver", "ALL");
309+
prefs.addProperty("server", "OFF");
310+
311+
JsonObject caps = new JsonObject();
312+
caps.add(CapabilityType.LOGGING_PREFS, prefs);
313+
314+
Capabilities converted = new JsonToBeanConverter()
315+
.convert(Capabilities.class, caps.toString());
316+
317+
LoggingPreferences lp =
318+
(LoggingPreferences) converted.getCapability(CapabilityType.LOGGING_PREFS);
319+
assertNotNull(lp);
320+
assertEquals(Level.WARNING, lp.getLevel(LogType.BROWSER));
321+
assertEquals(Level.FINE, lp.getLevel(LogType.CLIENT));
322+
assertEquals(Level.ALL, lp.getLevel(LogType.DRIVER));
323+
assertEquals(Level.OFF, lp.getLevel(LogType.SERVER));
324+
}
325+
299326
@Test
300327
public void testShouldNotParseQuotedJsonObjectsAsActualJsonObjects() {
301328
JsonObject inner = new JsonObject();

0 commit comments

Comments
 (0)