Skip to content

Commit

Permalink
Fix json de/serialization of java.util.logging.Level.FINE
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyba committed Oct 30, 2014
1 parent 29b6339 commit 7cd16dd
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 25 deletions.
48 changes: 35 additions & 13 deletions java/client/src/org/openqa/selenium/logging/LogLevelMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@

package org.openqa.selenium.logging;

import java.util.Map;
import java.util.logging.Level;
import static com.google.common.base.Strings.isNullOrEmpty;

import com.google.common.collect.ImmutableMap;

import java.util.logging.Level;

public class LogLevelMapping {

private static Map<Long, Level> levelMap;
/**
* WebDriver log level DEBUG which is mapped to Level.FINE.
*/
private static final String DEBUG = "DEBUG";

private static ImmutableMap<Integer, Level> levelMap;

static {
Level[] supportedLevels = new Level[] {
Expand All @@ -34,31 +40,47 @@ public class LogLevelMapping {
Level.SEVERE,
Level.OFF
};
ImmutableMap.Builder<Long, Level> builder = ImmutableMap.builder();
ImmutableMap.Builder<Integer, Level> builder = ImmutableMap.builder();
for (Level level : supportedLevels) {
builder.put((long)level.intValue(), level);
builder.put(level.intValue(), level);
}
levelMap = builder.build();
}

/**
* WebDriver log level DEBUG which is mapped to Level.FINE.
* Normalizes the given level to one of those supported by Selenium.
*/
private static final String DEBUG = "DEBUG";

public static Level toLevel(long longValue) {
return levelMap.get(longValue);
public static Level normalize(Level level) {
if (levelMap.containsKey(level.intValue())) {
return levelMap.get(level.intValue());
} else if (level.intValue() >= Level.SEVERE.intValue()) {
return Level.SEVERE;
} else if (level.intValue() >= Level.WARNING.intValue()) {
return Level.WARNING;
} else if (level.intValue() >= Level.INFO.intValue()) {
return Level.INFO;
} else {
return Level.FINE;
}
}

/**
* Converts the JDK level to a name supported by Selenium.
*/
public static String getName(Level level) {
Level normalized = normalize(level);
return normalized == Level.FINE ? DEBUG : normalized.getName();
}

public static Level toLevel(String logLevelName) {
if (logLevelName == null || "".equals(logLevelName)) {
if (isNullOrEmpty(logLevelName)) {
// Default the log level to info.
return Level.INFO;
}

if (logLevelName.equals(DEBUG)) {
return Level.FINE;
}
return levelMap.get((long)Level.parse(logLevelName).intValue());
return levelMap.get(Level.parse(logLevelName).intValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogLevelMapping;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.logging.SessionLogs;

Expand Down Expand Up @@ -106,7 +107,7 @@ private JsonElement convertObject(Object toConvert, int maxDepth) throws Excepti
}

if (toConvert instanceof Level) {
return new JsonPrimitive(toConvert.toString());
return new JsonPrimitive(LogLevelMapping.getName((Level) toConvert));
}

if (toConvert.getClass().isEnum() || toConvert instanceof Enum) {
Expand All @@ -117,7 +118,7 @@ private JsonElement convertObject(Object toConvert, int maxDepth) throws Excepti
LoggingPreferences prefs = (LoggingPreferences) toConvert;
JsonObject converted = new JsonObject();
for (String logType : prefs.getEnabledLogTypes()) {
converted.addProperty(logType, prefs.getLevel(logType).toString());
converted.addProperty(logType, LogLevelMapping.getName(prefs.getLevel(logType)));
}
return converted;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,8 @@ private <T> T convert(Class<T> clazz, Object text, int depth) {
JsonObject json = text instanceof JsonElement
? ((JsonElement) text).getAsJsonObject()
: new JsonParser().parse(text.toString()).getAsJsonObject();
DesiredCapabilities caps = new DesiredCapabilities();

for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
caps.setCapability(entry.getKey(), convert(Object.class, entry.getValue(), depth + 1));
}

return (T) caps;
Map<String, Object> map = convertMap(json.getAsJsonObject(), depth);
return (T) new DesiredCapabilities(map);
}

if (Date.class.equals(clazz)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,15 +370,19 @@ public void testProperlyConvertsNulls() {
@Test
public void testConvertLoggingPreferencesToJson() {
LoggingPreferences prefs = new LoggingPreferences();
prefs.enable(LogType.BROWSER, Level.WARNING);
prefs.enable(LogType.CLIENT, Level.FINE);
prefs.enable(LogType.DRIVER, Level.ALL);
prefs.enable(LogType.SERVER, Level.OFF);

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

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

assertEquals("FINE", converted.get(LogType.CLIENT).getAsString());
assertEquals("WARNING", converted.get(LogType.BROWSER).getAsString());
assertEquals("DEBUG", converted.get(LogType.CLIENT).getAsString());
assertEquals("ALL", converted.get(LogType.DRIVER).getAsString());
assertEquals("OFF", converted.get(LogType.SERVER).getAsString());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.Platform;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
Expand Down Expand Up @@ -159,7 +162,8 @@ public void testShouldUseAMapToRepresentComplexObjects() throws Exception {
toModel.addProperty("thing", "hairy");
toModel.addProperty("hairy", "true");

Map<?,?> modelled = (Map<?,?>) new JsonToBeanConverter().convert(Object.class, toModel.toString());
Map<?,?> modelled = (Map<?,?>) new JsonToBeanConverter().convert(Object.class,
toModel.toString());
assertEquals(2, modelled.size());
}

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

@Test
Expand Down Expand Up @@ -296,6 +300,29 @@ public void testShouldConvertCapabilitiesToAMapAndIncludeCustomValues() throws E
assertEquals("fishy", converted.getCapability("furrfu"));
}

@Test
public void testShouldParseCapabilitiesWithLoggingPreferences() throws Exception {
JsonObject prefs = new JsonObject();
prefs.addProperty("browser", "WARNING");
prefs.addProperty("client", "DEBUG");
prefs.addProperty("driver", "ALL");
prefs.addProperty("server", "OFF");

JsonObject caps = new JsonObject();
caps.add(CapabilityType.LOGGING_PREFS, prefs);

Capabilities converted = new JsonToBeanConverter()
.convert(Capabilities.class, caps.toString());

LoggingPreferences lp =
(LoggingPreferences) converted.getCapability(CapabilityType.LOGGING_PREFS);
assertNotNull(lp);
assertEquals(Level.WARNING, lp.getLevel(LogType.BROWSER));
assertEquals(Level.FINE, lp.getLevel(LogType.CLIENT));
assertEquals(Level.ALL, lp.getLevel(LogType.DRIVER));
assertEquals(Level.OFF, lp.getLevel(LogType.SERVER));
}

@Test
public void testShouldNotParseQuotedJsonObjectsAsActualJsonObjects() {
JsonObject inner = new JsonObject();
Expand Down

0 comments on commit 7cd16dd

Please sign in to comment.