Skip to content

Commit

Permalink
Add test for roundtripping numbers in maps through JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Oct 31, 2017
1 parent e2200df commit 3847dc8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
4 changes: 4 additions & 0 deletions java/client/src/org/openqa/selenium/json/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ public <T> 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:
Expand Down
1 change: 1 addition & 0 deletions java/client/test/org/openqa/selenium/json/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ java_test(name = 'small-tests',
srcs = [
'BeanToJsonConverterTest.java',
'JsonToBeanConverterTest.java',
'JsonTest.java',
],
deps = [
'//java/client/src/org/openqa/selenium:selenium',
Expand Down
60 changes: 60 additions & 0 deletions java/client/test/org/openqa/selenium/json/JsonTest.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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<String, Object> 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"));
}
}
}

0 comments on commit 3847dc8

Please sign in to comment.