31 package com.google.protobuf;
33 import static org.junit.Assert.assertEquals;
34 import static org.junit.Assert.assertFalse;
35 import static org.junit.Assert.assertNotEquals;
36 import static org.junit.Assert.fail;
38 import java.io.IOException;
39 import java.nio.ByteBuffer;
40 import java.util.Arrays;
41 import java.util.Collections;
42 import java.util.List;
43 import org.junit.Before;
44 import org.junit.Test;
58 Protobuf.getInstance().registerSchemaOverride(
schema.newInstance().getClass(),
schema);
61 protected abstract Schema<T>
schema();
65 @SuppressWarnings(
"unused")
67 return Collections.emptyList();
78 Reader reader = BinaryReader.newInstance(invalidUtf8Bytes,
true);
80 T newMsg =
schema.newInstance();
83 fail(
"should throw invalid ");
91 if (!Android.isOnAndroidDevice()) {
96 int exceptionCount = 0;
97 for (
int i = 0;
i <=
data.length;
i++) {
98 byte[] truncatedData = Arrays.copyOf(
data,
i);
102 schema.mergeFrom(
message, truncatedData, 0,
i,
new ArrayDecoders.Registers());
105 }
catch (IndexOutOfBoundsException e) {
109 assertNotEquals(0, exceptionCount);
112 protected static final <M extends MessageLite>
void roundtrip(
113 String failureMessage, M msg, Schema<M>
schema)
throws IOException {
115 assertEquals(failureMessage, msg.getSerializedSize(), serializedBytes.length);
118 if (Android.isOnAndroidDevice()) {
120 M newMsg =
schema.newInstance();
122 newMsg, serializedBytes, 0, serializedBytes.length,
new ArrayDecoders.Registers());
123 schema.makeImmutable(newMsg);
124 assertEquals(failureMessage, msg, newMsg);
126 M newMsg =
schema.newInstance();
127 Reader reader = BinaryReader.newInstance(ByteBuffer.wrap(serializedBytes),
true);
129 schema.makeImmutable(newMsg);
131 assertEquals(failureMessage, msg, newMsg);
134 protected final void roundtrip(String failureMessage,
T msg)
throws IOException {
143 return Collections.emptyList();
146 @SuppressWarnings(
"unchecked")
150 if (
schema.isInitialized(msg)) {
151 assertEquals(
"", msg.toString());
152 msg = (
T) msg.toBuilder().build();
154 assertFalse(
schema.isInitialized(msg));