Search in sources :

Example 21 with JsonReader

use of com.google.gson.stream.JsonReader in project buck by facebook.

the class WorkerProcessProtocolZeroTest method testReceiveHandshakeWithIncorrectID.

@Test
public void testReceiveHandshakeWithIncorrectID() throws IOException {
    int handshakeID = 123;
    int differentHandshakeID = 456;
    JsonReader jsonReader = createMockJsonReaderForReceiveHandshake(differentHandshakeID, "handshake", "0");
    WorkerProcessProtocol protocol = new WorkerProcessProtocolZero(fakeProcessExecutor, fakeLaunchedProcess, dummyJsonWriter, jsonReader, newTempFile());
    try {
        protocol.receiveHandshake(handshakeID);
    } catch (HumanReadableException e) {
        assertThat(e.getMessage(), Matchers.containsString(String.format("Expected handshake response's \"id\" value to be \"%d\"", handshakeID)));
    }
}
Also used : HumanReadableException(com.facebook.buck.util.HumanReadableException) JsonReader(com.google.gson.stream.JsonReader) Test(org.junit.Test)

Example 22 with JsonReader

use of com.google.gson.stream.JsonReader in project buck by facebook.

the class WorkerProcessProtocolZeroTest method testReceiveCommandResponseWithInvalidType.

@Test
public void testReceiveCommandResponseWithInvalidType() throws IOException {
    int messageID = 123;
    JsonReader jsonReader = createMockJsonReaderForReceiveCommandResponse(messageID, "INVALID RESPONSE TYPE", 0);
    WorkerProcessProtocol protocol = new WorkerProcessProtocolZero(fakeProcessExecutor, fakeLaunchedProcess, dummyJsonWriter, jsonReader, newTempFile());
    try {
        protocol.receiveCommandResponse(messageID);
    } catch (HumanReadableException e) {
        assertThat(e.getMessage(), Matchers.containsString("Expected response's \"type\" to be one of"));
    }
}
Also used : HumanReadableException(com.facebook.buck.util.HumanReadableException) JsonReader(com.google.gson.stream.JsonReader) Test(org.junit.Test)

Example 23 with JsonReader

use of com.google.gson.stream.JsonReader in project buck by facebook.

the class WorkerProcessProtocolZeroTest method testReceiveHandshake.

@Test
public void testReceiveHandshake() throws IOException {
    int handshakeID = 123;
    JsonReader jsonReader = createMockJsonReaderForReceiveHandshake(handshakeID, "handshake", "0");
    WorkerProcessProtocol protocol = new WorkerProcessProtocolZero(fakeProcessExecutor, fakeLaunchedProcess, dummyJsonWriter, jsonReader, newTempFile());
    protocol.receiveHandshake(handshakeID);
}
Also used : JsonReader(com.google.gson.stream.JsonReader) Test(org.junit.Test)

Example 24 with JsonReader

use of com.google.gson.stream.JsonReader in project buck by facebook.

the class WorkerProcessProtocolZeroTest method testReceiveCommandWithInvalidType.

@Test
public void testReceiveCommandWithInvalidType() throws IOException {
    int messageID = 123;
    JsonReader jsonReader = createMockJsonReaderForReceiveCommand(messageID, "INVALID RESPONSE TYPE", "/path/to/args", "/path/to/stdout", "/path/to/stderr");
    WorkerProcessProtocol protocol = new WorkerProcessProtocolZero(fakeProcessExecutor, fakeLaunchedProcess, dummyJsonWriter, jsonReader, newTempFile());
    try {
        protocol.receiveCommand(messageID);
    } catch (HumanReadableException e) {
        assertThat(e.getMessage(), Matchers.containsString("Expected command's \"type\" to be \"command\""));
    }
}
Also used : HumanReadableException(com.facebook.buck.util.HumanReadableException) JsonReader(com.google.gson.stream.JsonReader) Test(org.junit.Test)

Example 25 with JsonReader

use of com.google.gson.stream.JsonReader in project Minechem by iopleke.

the class ElementHandler method readFromStream.

private static void readFromStream(InputStream stream) {
    JsonReader jReader = new JsonReader(new InputStreamReader(stream));
    JsonParser parser = new JsonParser();
    Set<Map.Entry<String, JsonElement>> elementsSet = parser.parse(jReader).getAsJsonObject().entrySet();
    int count = 0;
    for (Map.Entry<String, JsonElement> elementEntry : elementsSet) {
        if (!elementEntry.getValue().isJsonObject()) {
            continue;
        }
        JsonObject elementObject = elementEntry.getValue().getAsJsonObject();
        ElementRegistry.getInstance().registerElement(Integer.parseInt(elementEntry.getKey()), elementObject.get("longName").getAsString(), elementObject.get("shortName").getAsString(), elementObject.get("form").getAsString(), elementObject.get("type").getAsString(), Integer.parseInt(elementObject.get("neutrons").getAsString()));
        count++;
    }
    LogHelper.info("Total of " + count + " elements registered");
}
Also used : InputStreamReader(java.io.InputStreamReader) JsonElement(com.google.gson.JsonElement) JsonReader(com.google.gson.stream.JsonReader) JsonObject(com.google.gson.JsonObject) Map(java.util.Map) JsonParser(com.google.gson.JsonParser)

Aggregations

JsonReader (com.google.gson.stream.JsonReader)95 StringReader (java.io.StringReader)36 JsonElement (com.google.gson.JsonElement)30 Test (org.junit.Test)19 JsonObject (com.google.gson.JsonObject)17 IOException (java.io.IOException)17 InputStreamReader (java.io.InputStreamReader)17 JsonParser (com.google.gson.JsonParser)11 HumanReadableException (com.facebook.buck.util.HumanReadableException)10 Gson (com.google.gson.Gson)9 TypeToken (com.google.gson.reflect.TypeToken)8 JsonWriter (com.google.gson.stream.JsonWriter)8 Map (java.util.Map)7 JsonToken (com.google.gson.stream.JsonToken)6 HashMap (java.util.HashMap)6 InputStream (java.io.InputStream)5 StringWriter (java.io.StringWriter)5 Type (java.lang.reflect.Type)5 ArrayList (java.util.ArrayList)5 BufferedReader (java.io.BufferedReader)4