Skip to content

Commit

Permalink
Make the bean to json test use JsonOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed May 14, 2018
1 parent ade980b commit 70121d3
Showing 1 changed file with 49 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@
import org.openqa.selenium.remote.SessionId;

import java.awt.*;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
Expand All @@ -71,11 +74,11 @@
import java.util.stream.Stream;


public class BeanToJsonConverterTest {
public class JsonOutputTest {

@Test
public void testShouldBeAbleToConvertASimpleString() {
String json = new BeanToJsonConverter().convert("cheese");
String json = convert("cheese");

assertThat(json, is("\"cheese\""));
}
Expand All @@ -86,15 +89,15 @@ public void testShouldConvertAMapIntoAJsonObject() {
toConvert.put("cheese", "cheddar");
toConvert.put("fish", "nice bit of haddock");

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

JsonObject converted = new JsonParser().parse(json).getAsJsonObject();
assertThat(converted.get("cheese").getAsString(), is("cheddar"));
}

@Test
public void testShouldConvertASimpleJavaBean() {
String json = new BeanToJsonConverter().convert(new SimpleBean());
String json = convert(new SimpleBean());

JsonObject converted = new JsonParser().parse(json).getAsJsonObject();
assertThat(converted.get("foo").getAsString(), is("bar"));
Expand All @@ -104,7 +107,7 @@ public void testShouldConvertASimpleJavaBean() {

@Test
public void testShouldConvertArrays() {
String json = new BeanToJsonConverter().convert(new BeanWithArray());
String json = convert(new BeanWithArray());

JsonObject converted = new JsonParser().parse(json).getAsJsonObject();
JsonArray allNames = converted.get("names").getAsJsonArray();
Expand All @@ -113,7 +116,7 @@ public void testShouldConvertArrays() {

@Test
public void testShouldConvertCollections() {
String json = new BeanToJsonConverter().convert(new BeanWithCollection());
String json = convert(new BeanWithCollection());

JsonObject converted = new JsonParser().parse(json).getAsJsonObject();
JsonArray allNames = converted.get("something").getAsJsonArray();
Expand All @@ -122,7 +125,7 @@ public void testShouldConvertCollections() {

@Test
public void testShouldConvertNumbersAsLongs() {
String json = new BeanToJsonConverter().convert(new Exception());
String json = convert(new Exception());
Map<?,?> map = new Json().toType(json, Map.class);

List<?> stack = (List<?>) map.get("stackTrace");
Expand All @@ -135,7 +138,7 @@ public void testShouldConvertNumbersAsLongs() {
@Test
public void testShouldNotChokeWhenCollectionIsNull() {
try {
new BeanToJsonConverter().convert(new BeanWithNullCollection());
convert(new BeanWithNullCollection());
} catch (Exception e) {
e.printStackTrace();
fail("That shouldn't have happened");
Expand All @@ -145,13 +148,13 @@ public void testShouldNotChokeWhenCollectionIsNull() {
@Test
public void testShouldConvertEnumsToStrings() {
// If this doesn't hang indefinitely, we're all good
new BeanToJsonConverter().convert(State.INDIFFERENT);
convert(State.INDIFFERENT);
}

@Test
public void testShouldConvertEnumsWithMethods() {
// If this doesn't hang indefinitely, we're all good
new BeanToJsonConverter().convert(WithMethods.CHEESE);
convert(WithMethods.CHEESE);
}

@Test
Expand All @@ -170,15 +173,15 @@ public void testShouldBeAbleToConvertAPoint() {
Point point = new Point(65, 75);

try {
new BeanToJsonConverter().convert(point);
convert(point);
} catch (StackOverflowError e) {
fail("This should never happen");
}
}

@Test
public void testShouldEncodeClassNameAsClassProperty() {
String json = new BeanToJsonConverter().convert(new SimpleBean());
String json = convert(new SimpleBean());

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

Expand All @@ -188,7 +191,7 @@ public void testShouldEncodeClassNameAsClassProperty() {
@Test
public void testShouldBeAbleToConvertASessionId() {
SessionId sessionId = new SessionId("some id");
String json = new BeanToJsonConverter().convert(sessionId);
String json = convert(sessionId);

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

Expand All @@ -199,7 +202,7 @@ public void testShouldBeAbleToConvertASessionId() {
public void testShouldBeAbleToConvertAJsonObject() {
JsonObject obj = new JsonObject();
obj.addProperty("key", "value");
String json = new BeanToJsonConverter().convert(obj);
String json = convert(obj);

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

Expand All @@ -210,7 +213,7 @@ public void testShouldBeAbleToConvertAJsonObject() {
public void testShouldBeAbleToConvertACapabilityObject() {
Capabilities caps = new ImmutableCapabilities("key", "alpha");

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

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

Expand All @@ -227,7 +230,7 @@ public void testShouldConvertAProxyCorrectly() {
Map<String, ?> asMap = ImmutableMap.of("desiredCapabilities", caps);
Command command = new Command(new SessionId("empty"), DriverCommand.NEW_SESSION, asMap);

String json = new BeanToJsonConverter().convert(command.getParameters());
String json = convert(command.getParameters());

JsonObject converted = new JsonParser().parse(json).getAsJsonObject();
JsonObject capsAsMap = converted.get("desiredCapabilities").getAsJsonObject();
Expand All @@ -239,13 +242,13 @@ public void testShouldConvertAProxyCorrectly() {

@Test
public void testShouldCallToJsonMethodIfPresent() {
String json = new BeanToJsonConverter().convert(new JsonAware("converted"));
String json = convert(new JsonAware("converted"));
assertEquals("\"converted\"", json);
}

@Test
public void testShouldPreferToJsonMethodToToMapMethod() {
String json = new BeanToJsonConverter().convert(new MappableJsonAware("converted"));
String json = convert(new MappableJsonAware("converted"));
assertEquals("\"converted\"", json);
}

Expand All @@ -258,7 +261,7 @@ public Map<String, Object> toJson() {
}
}

String json = new BeanToJsonConverter().convert(new ToJsonReturnsMap());
String json = convert(new ToJsonReturnsMap());
JsonObject converted = new JsonParser().parse(json).getAsJsonObject();

assertEquals(1, converted.entrySet().size());
Expand All @@ -274,7 +277,7 @@ public Set<String> toJson() {
}
}

String json = new BeanToJsonConverter().convert(new ToJsonReturnsCollection());
String json = convert(new ToJsonReturnsCollection());
JsonArray converted = new JsonParser().parse(json).getAsJsonArray();

assertEquals(2, converted.size());
Expand All @@ -286,13 +289,13 @@ public Set<String> toJson() {

@Test
public void testShouldCallAsMapMethodIfPresent() {
String json = new BeanToJsonConverter().convert(new Mappable1("a key", "a value"));
String json = convert(new Mappable1("a key", "a value"));
assertEquals("{\"a key\":\"a value\"}", json);
}

@Test
public void testShouldCallToMapMethodIfPresent() {
String json = new BeanToJsonConverter().convert(new Mappable2("a key", "a value"));
String json = convert(new Mappable2("a key", "a value"));
assertEquals("{\"a key\":\"a value\"}", json);
}

Expand All @@ -311,7 +314,7 @@ public void testConvertsToJsonMethodResultToPrimitiveIfItIsNotJson() {
} catch (JsonSyntaxException expected) {
}

String json = new BeanToJsonConverter().convert(new JsonAware(raw));
String json = convert(new JsonAware(raw));
assertEquals("\"gnu/linux\"", json);
}

Expand Down Expand Up @@ -341,7 +344,7 @@ private void verifyStackTraceInJson(String json, StackTraceElement[] stackTrace)
public void testShouldBeAbleToConvertARuntimeException() {
RuntimeException clientError = new RuntimeException("foo bar baz!");
StackTraceElement[] stackTrace = clientError.getStackTrace();
String json = new BeanToJsonConverter().convert(clientError);
String json = convert(clientError);
assertTrue(json.contains("\"message\":\"foo bar baz!\""));
assertTrue(json.contains("\"class\":\"java.lang.RuntimeException\""));
assertTrue(json.contains("\"stackTrace\""));
Expand All @@ -352,7 +355,7 @@ public void testShouldBeAbleToConvertARuntimeException() {
public void testShouldBeAbleToConvertAWebDriverException() {
RuntimeException clientError = new WebDriverException("foo bar baz!");
StackTraceElement[] stackTrace = clientError.getStackTrace();
String raw = new BeanToJsonConverter().convert(clientError);
String raw = convert(clientError);

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

Expand All @@ -372,15 +375,15 @@ public void testShouldBeAbleToConvertAWebDriverException() {
public void testShouldConvertUnhandledAlertException() {
RuntimeException clientError = new UnhandledAlertException("unhandled alert", "cheese!");
Map<?, ?> obj = new Gson()
.fromJson(new StringReader(new BeanToJsonConverter().convert(clientError)), Map.class);
.fromJson(new StringReader(convert(clientError)), Map.class);
assertTrue(obj.containsKey("alert"));
assertEquals(ImmutableMap.of("text", "cheese!"), obj.get("alert"));
}


@Test
public void testShouldConvertDatesToMillisecondsInUtcTime() {
String jsonStr = new BeanToJsonConverter().convert(new Date(0));
String jsonStr = convert(new Date(0));
assertEquals(0, Integer.valueOf(jsonStr).intValue());
}

Expand All @@ -401,7 +404,7 @@ public Date getDate() {

Date date = new Date(123456789L);
Bean bean = new Bean(date);
String jsonStr = new BeanToJsonConverter().convert(bean);
String jsonStr = convert(bean);

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

Expand All @@ -414,7 +417,7 @@ public void testShouldBeAbleToConvertACookie() {
Date expiry = new Date();
Cookie cookie = new Cookie("name", "value", "domain", "/path", expiry, true, true);

String jsonStr = new BeanToJsonConverter().convert(cookie);
String jsonStr = convert(cookie);

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

Expand All @@ -431,7 +434,7 @@ public void testShouldBeAbleToConvertACookie() {
@Test
public void testUnsetCookieFieldsAreUndefined() {
Cookie cookie = new Cookie("name", "value");
String jsonStr = new BeanToJsonConverter().convert(cookie);
String jsonStr = convert(cookie);
// assertThat(jsonStr, not(containsString("path")));
assertThat(jsonStr, not(containsString("domain")));
assertThat(jsonStr, not(containsString("expiry")));
Expand All @@ -441,7 +444,7 @@ public void testUnsetCookieFieldsAreUndefined() {
public void testProperlyConvertsNulls() {
Map<String, Object> frameId = new HashMap<>();
frameId.put("id", null);
String payload = new BeanToJsonConverter().convert(frameId);
String payload = convert(frameId);
assertEquals("{\"id\":null}", payload);
}

Expand All @@ -453,7 +456,7 @@ public void testConvertLoggingPreferencesToJson() {
prefs.enable(LogType.DRIVER, Level.ALL);
prefs.enable(LogType.SERVER, Level.OFF);

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

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

Expand All @@ -465,7 +468,7 @@ public void testConvertLoggingPreferencesToJson() {

@Test
public void testConvertsLogEntryToJson() {
String raw = new BeanToJsonConverter().convert(new LogEntry(Level.OFF, 17, "foo"));
String raw = convert(new LogEntry(Level.OFF, 17, "foo"));

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

Expand All @@ -481,7 +484,7 @@ public void testConvertLogEntriesToJson() {
final LogEntry entry2 = new LogEntry(Level.WARNING, timestamp, "entry2");
LogEntries entries = new LogEntries(asList(entry1, entry2));

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

JsonArray converted = new JsonParser().parse(json).getAsJsonArray();

Expand All @@ -504,7 +507,7 @@ public void testShouldBeAbleToConvertACommand() {
parameters.put("param2", "value2");
Command command = new Command(sessionId, commandName, parameters);

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

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

Expand Down Expand Up @@ -534,7 +537,7 @@ public void shouldConvertAUrlToAString() throws MalformedURLException {

@Test
public void shouldNotIncludePropertiesFromJavaLangObjectOtherThanClass() {
String json = new BeanToJsonConverter().convert(new SimpleBean());
String json = convert(new SimpleBean());

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

Expand All @@ -544,6 +547,16 @@ public void shouldNotIncludePropertiesFromJavaLangObjectOtherThanClass() {
.forEach(name -> assertFalse(name, converted.keySet().contains(name)));
}

private String convert(Object toConvert) {
try (Writer writer = new StringWriter();
JsonOutput jsonOutput = new Json().newOutput(writer)) {
jsonOutput.write(toConvert);
return writer.toString();
} catch (IOException e) {
throw new JsonException(e);
}
}

@SuppressWarnings("unused")
private static class SimpleBean {

Expand Down

0 comments on commit 70121d3

Please sign in to comment.