aboutsummaryrefslogtreecommitdiff
path: root/protobuf-nano
diff options
context:
space:
mode:
authorEric Anderson <ejona@google.com>2015-10-27 19:53:18 -0700
committerEric Anderson <ejona@google.com>2015-10-28 10:00:41 -0700
commitcea8e8e4a799aaecede746c5235acd6dd8323b72 (patch)
treec34bc96691d317f38c4cb472a35c224771096203 /protobuf-nano
parent122f93c26d1f5f88045cb3c50f5ce3c8ec3b47f3 (diff)
downloadgrpc-grpc-java-cea8e8e4a799aaecede746c5235acd6dd8323b72.tar.gz
Add tests for nano proto
Since some of the tests would need only minor modification to work for normal proto, normal proto got a few more tests as well.
Diffstat (limited to 'protobuf-nano')
-rw-r--r--protobuf-nano/build.gradle27
-rw-r--r--protobuf-nano/src/generated/test/javanano/io/grpc/protobuf/nano/Messages.java136
-rw-r--r--protobuf-nano/src/test/java/io/grpc/protobuf/nano/NanoUtilsTest.java133
-rw-r--r--protobuf-nano/src/test/proto/messages.proto42
4 files changed, 338 insertions, 0 deletions
diff --git a/protobuf-nano/build.gradle b/protobuf-nano/build.gradle
index 1313e088c..de4f95a74 100644
--- a/protobuf-nano/build.gradle
+++ b/protobuf-nano/build.gradle
@@ -1,3 +1,13 @@
+// Add dependency on the protobuf plugin
+buildscript {
+ repositories {
+ mavenCentral()
+ }
+ dependencies {
+ classpath libraries.protobuf_plugin
+ }
+}
+
plugins {
id "be.insaneprogramming.gradle.animalsniffer" version "1.4.0"
}
@@ -13,3 +23,20 @@ dependencies {
animalsniffer {
signature = "org.codehaus.mojo.signature:java16:+@signature"
}
+
+configureProtoCompilation()
+
+if (project.hasProperty('protobuf')) {
+ protobuf {
+ generateProtoTasks {
+ all().each { task ->
+ task.builtins {
+ remove java
+ javanano {
+ option 'ignore_services=true'
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/protobuf-nano/src/generated/test/javanano/io/grpc/protobuf/nano/Messages.java b/protobuf-nano/src/generated/test/javanano/io/grpc/protobuf/nano/Messages.java
new file mode 100644
index 000000000..04c525d8d
--- /dev/null
+++ b/protobuf-nano/src/generated/test/javanano/io/grpc/protobuf/nano/Messages.java
@@ -0,0 +1,136 @@
+// Generated by the protocol buffer compiler. DO NOT EDIT!
+
+package io.grpc.protobuf.nano;
+
+@SuppressWarnings("hiding")
+public interface Messages {
+
+ public static final class Message extends
+ com.google.protobuf.nano.MessageNano {
+
+ private static volatile Message[] _emptyArray;
+ public static Message[] emptyArray() {
+ // Lazily initializes the empty array
+ if (_emptyArray == null) {
+ synchronized (
+ com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) {
+ if (_emptyArray == null) {
+ _emptyArray = new Message[0];
+ }
+ }
+ }
+ return _emptyArray;
+ }
+
+ // optional int32 i = 1;
+ public int i;
+
+ // optional bool b = 2;
+ public boolean b;
+
+ // optional string s = 3;
+ public java.lang.String s;
+
+ // optional bytes bs = 4;
+ public byte[] bs;
+
+ public Message() {
+ clear();
+ }
+
+ public Message clear() {
+ i = 0;
+ b = false;
+ s = "";
+ bs = com.google.protobuf.nano.WireFormatNano.EMPTY_BYTES;
+ cachedSize = -1;
+ return this;
+ }
+
+ @Override
+ public void writeTo(com.google.protobuf.nano.CodedOutputByteBufferNano output)
+ throws java.io.IOException {
+ if (this.i != 0) {
+ output.writeInt32(1, this.i);
+ }
+ if (this.b != false) {
+ output.writeBool(2, this.b);
+ }
+ if (!this.s.equals("")) {
+ output.writeString(3, this.s);
+ }
+ if (!java.util.Arrays.equals(this.bs, com.google.protobuf.nano.WireFormatNano.EMPTY_BYTES)) {
+ output.writeBytes(4, this.bs);
+ }
+ super.writeTo(output);
+ }
+
+ @Override
+ protected int computeSerializedSize() {
+ int size = super.computeSerializedSize();
+ if (this.i != 0) {
+ size += com.google.protobuf.nano.CodedOutputByteBufferNano
+ .computeInt32Size(1, this.i);
+ }
+ if (this.b != false) {
+ size += com.google.protobuf.nano.CodedOutputByteBufferNano
+ .computeBoolSize(2, this.b);
+ }
+ if (!this.s.equals("")) {
+ size += com.google.protobuf.nano.CodedOutputByteBufferNano
+ .computeStringSize(3, this.s);
+ }
+ if (!java.util.Arrays.equals(this.bs, com.google.protobuf.nano.WireFormatNano.EMPTY_BYTES)) {
+ size += com.google.protobuf.nano.CodedOutputByteBufferNano
+ .computeBytesSize(4, this.bs);
+ }
+ return size;
+ }
+
+ @Override
+ public Message mergeFrom(
+ com.google.protobuf.nano.CodedInputByteBufferNano input)
+ throws java.io.IOException {
+ while (true) {
+ int tag = input.readTag();
+ switch (tag) {
+ case 0:
+ return this;
+ default: {
+ if (!com.google.protobuf.nano.WireFormatNano.parseUnknownField(input, tag)) {
+ return this;
+ }
+ break;
+ }
+ case 8: {
+ this.i = input.readInt32();
+ break;
+ }
+ case 16: {
+ this.b = input.readBool();
+ break;
+ }
+ case 26: {
+ this.s = input.readString();
+ break;
+ }
+ case 34: {
+ this.bs = input.readBytes();
+ break;
+ }
+ }
+ }
+ }
+
+ public static Message parseFrom(byte[] data)
+ throws com.google.protobuf.nano.InvalidProtocolBufferNanoException {
+ return com.google.protobuf.nano.MessageNano.mergeFrom(new Message(), data);
+ }
+
+ public static Message parseFrom(
+ com.google.protobuf.nano.CodedInputByteBufferNano input)
+ throws java.io.IOException {
+ return new Message().mergeFrom(input);
+ }
+ }
+}
diff --git a/protobuf-nano/src/test/java/io/grpc/protobuf/nano/NanoUtilsTest.java b/protobuf-nano/src/test/java/io/grpc/protobuf/nano/NanoUtilsTest.java
new file mode 100644
index 000000000..621f11cff
--- /dev/null
+++ b/protobuf-nano/src/test/java/io/grpc/protobuf/nano/NanoUtilsTest.java
@@ -0,0 +1,133 @@
+/*
+ * Copyright 2015, Google Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package io.grpc.protobuf.nano;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import com.google.protobuf.nano.MessageNano;
+
+import io.grpc.MethodDescriptor.Marshaller;
+import io.grpc.Status;
+import io.grpc.StatusRuntimeException;
+import io.grpc.protobuf.nano.Messages.Message;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/** Unit tests for {@link NanoUtils}. */
+@RunWith(JUnit4.class)
+public class NanoUtilsTest {
+ private Marshaller<Message> marshaller = NanoUtils.marshaller(new MessageNanoFactory<Message>() {
+ @Override
+ public Message newInstance() {
+ return new Message();
+ }
+ });
+
+ @Test
+ public void testRoundTrip() {
+ Message m = new Message();
+ m.i = 2;
+ m.b = true;
+ m.s = "string";
+ Message m2 = marshaller.parse(marshaller.stream(m));
+ assertNotSame(m, m2);
+ assertEquals(2, m2.i);
+ assertEquals(true, m2.b);
+ assertEquals("string", m2.s);
+ assertTrue(MessageNano.messageNanoEquals(m, m2));
+ }
+
+ @Test
+ public void testIoException() {
+ final IOException ioException = new IOException();
+ InputStream is = new InputStream() {
+ @Override
+ public int read() throws IOException {
+ throw ioException;
+ }
+ };
+ try {
+ marshaller.parse(is);
+ fail("Expected exception");
+ } catch (StatusRuntimeException ex) {
+ assertEquals(Status.Code.INTERNAL, ex.getStatus().getCode());
+ assertSame(ioException, ex.getCause());
+ }
+ }
+
+ @Test
+ public void testLarge() {
+ Message m = new Message();
+ // The default limit is 64MB. Using a larger proto to verify that the limit is not enforced.
+ m.bs = new byte[70 * 1024 * 1024];
+ Message m2 = marshaller.parse(marshaller.stream(m));
+ assertNotSame(m, m2);
+ assertArrayEquals(m.bs, m2.bs);
+ }
+
+ @Test
+ public void testAvailable() throws Exception {
+ Message m = new Message();
+ m.s = "string";
+ InputStream is = marshaller.stream(m);
+ assertEquals(m.getSerializedSize(), is.available());
+ is.read();
+ assertEquals(m.getSerializedSize() - 1, is.available());
+ while (is.read() != -1) {}
+ assertEquals(-1, is.read());
+ assertEquals(0, is.available());
+ }
+
+ @Test
+ public void testEmpty() throws IOException {
+ InputStream is = marshaller.stream(new Message());
+ assertEquals(0, is.available());
+ byte[] b = new byte[10];
+ assertEquals(-1, is.read(b));
+ assertArrayEquals(new byte[10], b);
+ // Do the same thing again, because the internal state may be different
+ assertEquals(-1, is.read(b));
+ assertArrayEquals(new byte[10], b);
+ assertEquals(-1, is.read());
+ assertEquals(0, is.available());
+ }
+}
diff --git a/protobuf-nano/src/test/proto/messages.proto b/protobuf-nano/src/test/proto/messages.proto
new file mode 100644
index 000000000..59d64e973
--- /dev/null
+++ b/protobuf-nano/src/test/proto/messages.proto
@@ -0,0 +1,42 @@
+
+// Copyright 2015, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+syntax = "proto3";
+
+package grpc;
+
+option java_package = "io.grpc.protobuf";
+
+message Message {
+ int32 i = 1;
+ bool b = 2;
+ string s = 3;
+ bytes bs = 4;
+}