From 3847dc8fd90621cbce2d9f8c739ef2380eb34943 Mon Sep 17 00:00:00 2001 From: Simon Stewart Date: Tue, 31 Oct 2017 15:23:06 +0000 Subject: [PATCH] Add test for roundtripping numbers in maps through JSON --- .../src/org/openqa/selenium/json/Json.java | 4 ++ .../client/test/org/openqa/selenium/json/BUCK | 1 + .../org/openqa/selenium/json/JsonTest.java | 60 +++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 java/client/test/org/openqa/selenium/json/JsonTest.java diff --git a/java/client/src/org/openqa/selenium/json/Json.java b/java/client/src/org/openqa/selenium/json/Json.java index f0e413815c180..294126ac297a8 100644 --- a/java/client/src/org/openqa/selenium/json/Json.java +++ b/java/client/src/org/openqa/selenium/json/Json.java @@ -81,7 +81,11 @@ public T toType(Object source, Type typeOfT) { private static Object readValue(JsonReader in, Gson gson) throws IOException { switch (in.peek()) { case BEGIN_ARRAY: + return gson.fromJson(in, List.class); + case BEGIN_OBJECT: + return gson.fromJson(in, Map.class); + case BOOLEAN: case NULL: case STRING: diff --git a/java/client/test/org/openqa/selenium/json/BUCK b/java/client/test/org/openqa/selenium/json/BUCK index a075c125ddace..31cedb2915d50 100644 --- a/java/client/test/org/openqa/selenium/json/BUCK +++ b/java/client/test/org/openqa/selenium/json/BUCK @@ -2,6 +2,7 @@ java_test(name = 'small-tests', srcs = [ 'BeanToJsonConverterTest.java', 'JsonToBeanConverterTest.java', + 'JsonTest.java', ], deps = [ '//java/client/src/org/openqa/selenium:selenium', diff --git a/java/client/test/org/openqa/selenium/json/JsonTest.java b/java/client/test/org/openqa/selenium/json/JsonTest.java new file mode 100644 index 0000000000000..6f1634bf6499e --- /dev/null +++ b/java/client/test/org/openqa/selenium/json/JsonTest.java @@ -0,0 +1,60 @@ +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.openqa.selenium.json; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.openqa.selenium.json.Json.MAP_TYPE; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; + +import org.junit.Test; + +import java.io.IOException; +import java.io.StringReader; +import java.util.Map; + +public class JsonTest { + + @Test + public void canRoundTripNumbers() { + Map original = ImmutableMap.of( + "options", ImmutableMap.of("args", ImmutableList.of(1L, "hello"))); + + Json json = new Json(); + String converted = json.toJson(original); + Object remade = json.toType(converted, MAP_TYPE); + + assertEquals(original, remade); + } + + @Test + public void roundTripAFirefoxOptions() throws IOException { + Map caps = ImmutableMap.of( + "moz:firefoxOptions", ImmutableMap.of( + "prefs", ImmutableMap.of("foo.bar", 1))); + String json = new Json().toJson(caps); + assertFalse(json, json.contains("1.0")); + + try (JsonInput input = new Json().newInput(new StringReader(json))) { + json = new Json().toJson(input.read(Json.MAP_TYPE)); + assertFalse(json, json.contains("1.0")); + } + } +}