Search in sources :

Example 66 with JsonReader

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

the class WorkerProcessProtocolZeroTest method testReceiveCommandWithIncorrectMessageID.

@Test
public void testReceiveCommandWithIncorrectMessageID() throws IOException {
    int messageID = 123;
    int differentMessageID = 456;
    JsonReader jsonReader = createMockJsonReaderForReceiveCommand(differentMessageID, "command", "/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(String.format("Expected command's \"id\" value to be \"%d\"", messageID)));
    }
}
Also used : HumanReadableException(com.facebook.buck.util.HumanReadableException) JsonReader(com.google.gson.stream.JsonReader) Test(org.junit.Test)

Example 67 with JsonReader

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

the class WorkerProcessProtocolZeroTest method testReceiveCommandResponseWithMalformedJSON.

@Test
public void testReceiveCommandResponseWithMalformedJSON() throws IOException {
    String malformedJson = "><(((('> blub";
    WorkerProcessProtocol protocol = new WorkerProcessProtocolZero(fakeProcessExecutor, fakeLaunchedProcess, dummyJsonWriter, new JsonReader(new StringReader(malformedJson)), newTempFile());
    try {
        protocol.receiveCommandResponse(123);
    } catch (HumanReadableException e) {
        assertThat(e.getMessage(), Matchers.containsString("Error receiving command response"));
    }
}
Also used : HumanReadableException(com.facebook.buck.util.HumanReadableException) StringReader(java.io.StringReader) JsonReader(com.google.gson.stream.JsonReader) Test(org.junit.Test)

Example 68 with JsonReader

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

the class WorkerProcessProtocolZeroTest method setUp.

@Before
public void setUp() throws IOException {
    ProcessExecutorParams fakeParams = ProcessExecutorParams.ofCommand("");
    fakeProcess = new FakeProcess(0);
    fakeProcessExecutor = new FakeProcessExecutor(ImmutableMap.of(fakeParams, fakeProcess));
    fakeLaunchedProcess = fakeProcessExecutor.launchProcess(fakeParams);
    dummyJsonWriter = new JsonWriter(new StringWriter());
    dummyJsonReader = new JsonReader(new StringReader(""));
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) StringWriter(java.io.StringWriter) FakeProcessExecutor(com.facebook.buck.util.FakeProcessExecutor) StringReader(java.io.StringReader) FakeProcess(com.facebook.buck.util.FakeProcess) JsonReader(com.google.gson.stream.JsonReader) JsonWriter(com.google.gson.stream.JsonWriter) Before(org.junit.Before)

Example 69 with JsonReader

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

the class WorkerProcessProtocolZeroTest method testReceiveHandshakeWithIncorrectType.

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

Example 70 with JsonReader

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

the class WorkerProcessProtocolZeroTest method testClose.

@Test
public void testClose() throws IOException {
    StringWriter jsonSentToWorkerProcess = new StringWriter();
    JsonWriter writer = new JsonWriter(jsonSentToWorkerProcess);
    // write an opening bracket now, so the writer doesn't throw due to invalid JSON when it goes
    // to write the closing bracket
    writer.beginArray();
    // add an opening bracket and consume it now, so that the reader doesn't throw due to invalid
    // JSON when it goes to read the closing bracket
    JsonReader reader = new JsonReader(new StringReader("[]"));
    reader.beginArray();
    WorkerProcessProtocol protocol = new WorkerProcessProtocolZero(fakeProcessExecutor, fakeLaunchedProcess, writer, reader, newTempFile());
    protocol.close();
    String expectedJson = "]";
    assertThat(jsonSentToWorkerProcess.toString(), Matchers.endsWith(expectedJson));
    assertTrue(fakeProcess.isDestroyed());
}
Also used : StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) JsonReader(com.google.gson.stream.JsonReader) JsonWriter(com.google.gson.stream.JsonWriter) Test(org.junit.Test)

Aggregations

JsonReader (com.google.gson.stream.JsonReader)100 StringReader (java.io.StringReader)38 JsonElement (com.google.gson.JsonElement)31 IOException (java.io.IOException)20 JsonObject (com.google.gson.JsonObject)19 Test (org.junit.Test)19 InputStreamReader (java.io.InputStreamReader)18 JsonParser (com.google.gson.JsonParser)12 HumanReadableException (com.facebook.buck.util.HumanReadableException)10 Gson (com.google.gson.Gson)10 JsonWriter (com.google.gson.stream.JsonWriter)10 TypeToken (com.google.gson.reflect.TypeToken)8 Map (java.util.Map)8 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 GsonBuilder (com.google.gson.GsonBuilder)4