/google-http-client-protobuf/src/test/java/com/google/api/client/protobuf/ProtocolBuffersTest.java

https://code.google.com/p/google-http-java-client/ · Java · 44 lines · 18 code · 5 blank · 21 comment · 0 complexity · a61c7fb8362ef8d03378449d4e65d24f MD5 · raw file

  1. /*
  2. * Copyright (c) 2011 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  5. * in compliance with the License. You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License
  10. * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  11. * or implied. See the License for the specific language governing permissions and limitations under
  12. * the License.
  13. */
  14. package com.google.api.client.protobuf;
  15. import junit.framework.TestCase;
  16. import java.io.ByteArrayInputStream;
  17. /**
  18. * Tests {@link ProtocolBuffers}.
  19. *
  20. * @author Yaniv Inbar
  21. */
  22. public class ProtocolBuffersTest extends TestCase {
  23. public void testParseAndClose() throws Exception {
  24. SimpleProto.TestMessage mockResponse = SimpleProto.TestMessage.newBuilder()
  25. .setStatus(SimpleProto.TestStatus.SUCCESS)
  26. .setName("This is a test!")
  27. .setValue(123454321)
  28. .build();
  29. // Create the parser and test it with our mock response
  30. SimpleProto.TestMessage parsedResponse = ProtocolBuffers.parseAndClose(
  31. new ByteArrayInputStream(mockResponse.toByteArray()), SimpleProto.TestMessage.class);
  32. // Validate the parser properly parsed the response
  33. // (i.e. it matches the original mock response)
  34. assertEquals(mockResponse.getSerializedSize(), parsedResponse.getSerializedSize());
  35. assertEquals(mockResponse.getStatus(), parsedResponse.getStatus());
  36. assertEquals(mockResponse.getName(), parsedResponse.getName());
  37. assertEquals(mockResponse.getValue(), parsedResponse.getValue());
  38. }
  39. }