Author: bryanduxbury
Date: Fri Mar 26 05:58:34 2010
New Revision: 927699
URL: http://svn.apache.org/viewvc?rev=927699&view=rev
Log:
java: Convert Binary and Compact protocol tests to JUnit
Added:
incubator/thrift/trunk/lib/java/test/org/apache/thrift/protocol/
incubator/thrift/trunk/lib/java/test/org/apache/thrift/protocol/ProtocolTestBase.java
(contents, props changed)
- copied, changed from r927642,
incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/ProtocolTestBase.java
incubator/thrift/trunk/lib/java/test/org/apache/thrift/protocol/TestTBinaryProtocol.java
(contents, props changed)
- copied, changed from r927642,
incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/TBinaryProtocolTest.java
incubator/thrift/trunk/lib/java/test/org/apache/thrift/protocol/TestTCompactProtocol.java
(contents, props changed)
- copied, changed from r927642,
incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/TCompactProtocolTest.java
Removed:
incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/ProtocolTestBase.java
incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/TBinaryProtocolTest.java
incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/TCompactProtocolTest.java
Modified:
incubator/thrift/trunk/lib/java/src/org/apache/thrift/protocol/TMessage.java
Modified:
incubator/thrift/trunk/lib/java/src/org/apache/thrift/protocol/TMessage.java
URL:
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/java/src/org/apache/thrift/protocol/TMessage.java?rev=927699&r1=927698&r2=927699&view=diff
==============================================================================
---
incubator/thrift/trunk/lib/java/src/org/apache/thrift/protocol/TMessage.java
(original)
+++
incubator/thrift/trunk/lib/java/src/org/apache/thrift/protocol/TMessage.java
Fri Mar 26 05:58:34 2010
@@ -41,7 +41,14 @@ public final class TMessage {
public String toString() {
return "<TMessage name:'" + name + "' type: " + type + " seqid:" + seqid +
">";
}
-
+
+ public boolean equals(Object other) {
+ if (other instanceof TMessage) {
+ return equals((TMessage) other);
+ }
+ return false;
+ }
+
public boolean equals(TMessage other) {
return name.equals(other.name) && type == other.type && seqid ==
other.seqid;
}
Copied:
incubator/thrift/trunk/lib/java/test/org/apache/thrift/protocol/ProtocolTestBase.java
(from r927642,
incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/ProtocolTestBase.java)
URL:
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/java/test/org/apache/thrift/protocol/ProtocolTestBase.java?p2=incubator/thrift/trunk/lib/java/test/org/apache/thrift/protocol/ProtocolTestBase.java&p1=incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/ProtocolTestBase.java&r1=927642&r2=927699&rev=927699&view=diff
==============================================================================
---
incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/ProtocolTestBase.java
(original)
+++
incubator/thrift/trunk/lib/java/test/org/apache/thrift/protocol/ProtocolTestBase.java
Fri Mar 26 05:58:34 2010
@@ -16,23 +16,18 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.thrift.test;
+package org.apache.thrift.protocol;
import java.util.Arrays;
import java.util.List;
+import junit.framework.TestCase;
+
import org.apache.thrift.TBase;
import org.apache.thrift.TDeserializer;
import org.apache.thrift.TException;
import org.apache.thrift.TSerializer;
-import org.apache.thrift.protocol.TBinaryProtocol;
-import org.apache.thrift.protocol.TField;
-import org.apache.thrift.protocol.TMessage;
-import org.apache.thrift.protocol.TMessageType;
-import org.apache.thrift.protocol.TProtocol;
-import org.apache.thrift.protocol.TProtocolFactory;
-import org.apache.thrift.protocol.TStruct;
-import org.apache.thrift.protocol.TType;
+import org.apache.thrift.test.Fixtures;
import org.apache.thrift.transport.TMemoryBuffer;
import thrift.test.CompactProtoTestStruct;
@@ -41,256 +36,199 @@ import thrift.test.Nesting;
import thrift.test.OneOfEach;
import thrift.test.Srv;
-public abstract class ProtocolTestBase {
-
+public abstract class ProtocolTestBase extends TestCase {
+
protected abstract TProtocolFactory getFactory();
- public void main() throws Exception {
- testNakedByte();
+ public void testProtocol() throws Exception {
+ internalTestNakedByte();
for (int i = 0; i < 128; i++) {
- testByteField((byte)i);
- testByteField((byte)-i);
+ internalTestByteField((byte)i);
+ internalTestByteField((byte)-i);
}
-
+
for (int s : Arrays.asList(0, 1, 7, 150, 15000, 0x7fff, -1, -7, -150,
-15000, -0x7fff)) {
- testNakedI16((short)s);
- testI16Field((short)s);
+ internalTestNakedI16((short)s);
+ internalTestI16Field((short)s);
}
for (int i : Arrays.asList(0, 1, 7, 150, 15000, 31337, 0xffff, 0xffffff,
-1, -7, -150, -15000, -0xffff, -0xffffff)) {
- testNakedI32(i);
- testI32Field(i);
+ internalTestNakedI32(i);
+ internalTestI32Field(i);
}
- testNakedI64(0);
- testI64Field(0);
+ internalTestNakedI64(0);
+ internalTestI64Field(0);
for (int i = 0; i < 62; i++) {
- testNakedI64(1L << i);
- testNakedI64(-(1L << i));
- testI64Field(1L << i);
- testI64Field(-(1L << i));
+ internalTestNakedI64(1L << i);
+ internalTestNakedI64(-(1L << i));
+ internalTestI64Field(1L << i);
+ internalTestI64Field(-(1L << i));
}
- testDouble();
+ internalTestDouble();
for (String s : Arrays.asList("", "short", "borderlinetiny", "a bit longer
than the smallest possible")) {
- testNakedString(s);
- testStringField(s);
+ internalTestNakedString(s);
+ internalTestStringField(s);
}
for (byte[] b : Arrays.asList(new byte[0], new
byte[]{0,1,2,3,4,5,6,7,8,9,10}, new byte[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14},
new byte[128])) {
- testNakedBinary(b);
- testBinaryField(b);
+ internalTestNakedBinary(b);
+ internalTestBinaryField(b);
}
- testSerialization(OneOfEach.class, Fixtures.oneOfEach);
- testSerialization(Nesting.class, Fixtures.nesting);
- testSerialization(HolyMoley.class, Fixtures.holyMoley);
- testSerialization(CompactProtoTestStruct.class,
Fixtures.compactProtoTestStruct);
+ internalTestSerialization(OneOfEach.class, Fixtures.oneOfEach);
+ internalTestSerialization(Nesting.class, Fixtures.nesting);
+ internalTestSerialization(HolyMoley.class, Fixtures.holyMoley);
+ internalTestSerialization(CompactProtoTestStruct.class,
Fixtures.compactProtoTestStruct);
- testMessage();
+ internalTestMessage();
- testServerRequest();
+ internalTestServerRequest();
- testTDeserializer();
+ internalTestTDeserializer();
}
- public void testNakedByte() throws Exception {
+ private void internalTestNakedByte() throws Exception {
TMemoryBuffer buf = new TMemoryBuffer(0);
TProtocol proto = getFactory().getProtocol(buf);
proto.writeByte((byte)123);
- byte out = proto.readByte();
- if (out != 123) {
- throw new RuntimeException("Byte was supposed to be " + (byte)123 + "
but was " + out);
- }
+ assertEquals((byte) 123, proto.readByte());
}
- public void testByteField(final byte b) throws Exception {
- testStructField(new StructFieldTestCase(TType.BYTE, (short)15) {
+ private void internalTestByteField(final byte b) throws Exception {
+ internalTestStructField(new StructFieldTestCase(TType.BYTE, (short)15) {
public void writeMethod(TProtocol proto) throws TException {
proto.writeByte(b);
}
-
+
public void readMethod(TProtocol proto) throws TException {
- byte result = proto.readByte();
- if (result != b) {
- throw new RuntimeException("Byte was supposed to be " + (byte)b + "
but was " + result);
- }
+ assertEquals((byte)b, proto.readByte());
}
});
}
- public void testNakedI16(short n) throws Exception {
+ private void internalTestNakedI16(short n) throws Exception {
TMemoryBuffer buf = new TMemoryBuffer(0);
TProtocol proto = getFactory().getProtocol(buf);
proto.writeI16(n);
- // System.out.println(buf.inspect());
- int out = proto.readI16();
- if (out != n) {
- throw new RuntimeException("I16 was supposed to be " + n + " but was " +
out);
- }
+ assertEquals(n, proto.readI16());
}
- public void testI16Field(final short n) throws Exception {
- testStructField(new StructFieldTestCase(TType.I16, (short)15) {
+ private void internalTestI16Field(final short n) throws Exception {
+ internalTestStructField(new StructFieldTestCase(TType.I16, (short)15) {
public void writeMethod(TProtocol proto) throws TException {
proto.writeI16(n);
}
public void readMethod(TProtocol proto) throws TException {
- short result = proto.readI16();
- if (result != n) {
- throw new RuntimeException("I16 was supposed to be " + n + " but was
" + result);
- }
+ assertEquals(n, proto.readI16());
}
});
}
- public void testNakedI32(int n) throws Exception {
+ private void internalTestNakedI32(int n) throws Exception {
TMemoryBuffer buf = new TMemoryBuffer(0);
TProtocol proto = getFactory().getProtocol(buf);
proto.writeI32(n);
- // System.out.println(buf.inspect());
- int out = proto.readI32();
- if (out != n) {
- throw new RuntimeException("I32 was supposed to be " + n + " but was " +
out);
- }
+ assertEquals(n, proto.readI32());
}
- public void testI32Field(final int n) throws Exception {
- testStructField(new StructFieldTestCase(TType.I32, (short)15) {
+ private void internalTestI32Field(final int n) throws Exception {
+ internalTestStructField(new StructFieldTestCase(TType.I32, (short)15) {
public void writeMethod(TProtocol proto) throws TException {
proto.writeI32(n);
}
public void readMethod(TProtocol proto) throws TException {
- int result = proto.readI32();
- if (result != n) {
- throw new RuntimeException("I32 was supposed to be " + n + " but was
" + result);
- }
+ assertEquals(n, proto.readI32());
}
});
}
- public void testNakedI64(long n) throws Exception {
+ private void internalTestNakedI64(long n) throws Exception {
TMemoryBuffer buf = new TMemoryBuffer(0);
TProtocol proto = getFactory().getProtocol(buf);
proto.writeI64(n);
- // System.out.println(buf.inspect());
- long out = proto.readI64();
- if (out != n) {
- throw new RuntimeException("I64 was supposed to be " + n + " but was " +
out);
- }
+ assertEquals(n, proto.readI64());
}
- public void testI64Field(final long n) throws Exception {
- testStructField(new StructFieldTestCase(TType.I64, (short)15) {
+ private void internalTestI64Field(final long n) throws Exception {
+ internalTestStructField(new StructFieldTestCase(TType.I64, (short)15) {
public void writeMethod(TProtocol proto) throws TException {
proto.writeI64(n);
}
public void readMethod(TProtocol proto) throws TException {
- long result = proto.readI64();
- if (result != n) {
- throw new RuntimeException("I64 was supposed to be " + n + " but was
" + result);
- }
+ assertEquals(n, proto.readI64());
}
});
}
- public void testDouble() throws Exception {
+ private void internalTestDouble() throws Exception {
TMemoryBuffer buf = new TMemoryBuffer(1000);
TProtocol proto = getFactory().getProtocol(buf);
proto.writeDouble(123.456);
- double out = proto.readDouble();
- if (out != 123.456) {
- throw new RuntimeException("Double was supposed to be " + 123.456 + "
but was " + out);
- }
+ assertEquals(123.456, proto.readDouble());
}
- public void testNakedString(String str) throws Exception {
+ private void internalTestNakedString(String str) throws Exception {
TMemoryBuffer buf = new TMemoryBuffer(0);
TProtocol proto = getFactory().getProtocol(buf);
proto.writeString(str);
- // System.out.println(buf.inspect());
- String out = proto.readString();
- if (!str.equals(out)) {
- throw new RuntimeException("String was supposed to be '" + str + "' but
was '" + out + "'");
- }
+ assertEquals(str, proto.readString());
}
-
- public void testStringField(final String str) throws Exception {
- testStructField(new StructFieldTestCase(TType.STRING, (short)15) {
+
+ private void internalTestStringField(final String str) throws Exception {
+ internalTestStructField(new StructFieldTestCase(TType.STRING, (short)15) {
public void writeMethod(TProtocol proto) throws TException {
proto.writeString(str);
}
-
+
public void readMethod(TProtocol proto) throws TException {
- String result = proto.readString();
- if (!result.equals(str)) {
- throw new RuntimeException("String was supposed to be " + str + "
but was " + result);
- }
+ assertEquals(str, proto.readString());
}
});
}
- public void testNakedBinary(byte[] data) throws Exception {
+ private void internalTestNakedBinary(byte[] data) throws Exception {
TMemoryBuffer buf = new TMemoryBuffer(0);
TProtocol proto = getFactory().getProtocol(buf);
proto.writeBinary(data);
- // System.out.println(buf.inspect());
- byte[] out = proto.readBinary();
- if (!Arrays.equals(data, out)) {
- throw new RuntimeException("Binary was supposed to be '" + data + "' but
was '" + out + "'");
- }
+ assertTrue(Arrays.equals(data, proto.readBinary()));
}
- public void testBinaryField(final byte[] data) throws Exception {
- testStructField(new StructFieldTestCase(TType.STRING, (short)15) {
+ private void internalTestBinaryField(final byte[] data) throws Exception {
+ internalTestStructField(new StructFieldTestCase(TType.STRING, (short)15) {
public void writeMethod(TProtocol proto) throws TException {
proto.writeBinary(data);
}
-
+
public void readMethod(TProtocol proto) throws TException {
- byte[] result = proto.readBinary();
- if (!Arrays.equals(data, result)) {
- throw new RuntimeException("Binary was supposed to be '" +
bytesToString(data) + "' but was '" + bytesToString(result) + "'");
- }
+ assertTrue(Arrays.equals(data, proto.readBinary()));
}
});
-
}
- public <T extends TBase> void testSerialization(Class<T> klass, T obj)
throws Exception {
+ private <T extends TBase> void internalTestSerialization(Class<T> klass, T
expected) throws Exception {
TMemoryBuffer buf = new TMemoryBuffer(0);
TBinaryProtocol binproto = new TBinaryProtocol(buf);
- try {
- obj.write(binproto);
- // System.out.println("Size in binary protocol: " + buf.length());
+ expected.write(binproto);
- buf = new TMemoryBuffer(0);
- TProtocol proto = getFactory().getProtocol(buf);
+ buf = new TMemoryBuffer(0);
+ TProtocol proto = getFactory().getProtocol(buf);
- obj.write(proto);
- System.out.println("Size in " + proto.getClass().getSimpleName() + ": "
+ buf.length());
- // System.out.println(buf.inspect());
-
- T objRead = klass.newInstance();
- objRead.read(proto);
- if (!obj.equals(objRead)) {
- System.out.println("Expected: " + obj.toString());
- System.out.println("Actual: " + objRead.toString());
- // System.out.println(buf.inspect());
- throw new RuntimeException("Objects didn't match!");
- }
- } catch (Exception e) {
- System.out.println(buf.inspect());
- throw e;
- }
+ expected.write(proto);
+ System.out.println("Size in " + proto.getClass().getSimpleName() + ": " +
buf.length());
+
+ T actual = klass.newInstance();
+ actual.read(proto);
+ assertEquals(expected, actual);
}
- public void testMessage() throws Exception {
+ private void internalTestMessage() throws Exception {
List<TMessage> msgs = Arrays.asList(new TMessage[]{
new TMessage("short message name", TMessageType.CALL, 0),
new TMessage("1", TMessageType.REPLY, 12345),
@@ -308,13 +246,11 @@ public abstract class ProtocolTestBase {
output = proto.readMessageBegin();
- if (!msg.equals(output)) {
- throw new RuntimeException("Message was supposed to be " + msg + " but
was " + output);
- }
+ assertEquals(msg, output);
}
}
- public void testServerRequest() throws Exception {
+ private void internalTestServerRequest() throws Exception {
Srv.Iface handler = new Srv.Iface() {
public int Janky(int i32arg) throws TException {
return i32arg * 2;
@@ -348,13 +284,10 @@ public abstract class ProtocolTestBase {
// System.out.println(clientOutTrans.inspect());
testProcessor.process(clientOutProto, clientInProto);
// System.out.println(clientInTrans.inspect());
- int result = testClient.recv_Janky();
- if (result != 2) {
- throw new RuntimeException("Got an unexpected result: " + result);
- }
+ assertEquals(2, testClient.recv_Janky());
}
- private void testTDeserializer() throws TException {
+ private void internalTestTDeserializer() throws TException {
TSerializer ser = new TSerializer(getFactory());
byte[] bytes = ser.serialize(Fixtures.compactProtoTestStruct);
@@ -362,24 +295,14 @@ public abstract class ProtocolTestBase {
CompactProtoTestStruct cpts = new CompactProtoTestStruct();
deser.deserialize(cpts, bytes);
- if (!Fixtures.compactProtoTestStruct.equals(cpts)) {
- throw new RuntimeException(Fixtures.compactProtoTestStruct + " and " +
cpts + " do not match!");
- }
+ assertEquals(Fixtures.compactProtoTestStruct, cpts);
}
//
// Helper methods
//
- private static String bytesToString(byte[] bytes) {
- String s = "";
- for (int i = 0; i < bytes.length; i++) {
- s += Integer.toHexString((int)bytes[i]) + " ";
- }
- return s;
- }
-
- private void testStructField(StructFieldTestCase testCase) throws Exception {
+ private void internalTestStructField(StructFieldTestCase testCase) throws
Exception {
TMemoryBuffer buf = new TMemoryBuffer(0);
TProtocol proto = getFactory().getProtocol(buf);
@@ -390,19 +313,15 @@ public abstract class ProtocolTestBase {
proto.writeFieldEnd();
proto.writeStructEnd();
- // System.out.println(buf.inspect());
-
proto.readStructBegin();
TField readField = proto.readFieldBegin();
- // TODO: verify the field is as expected
- if (!field.equals(readField)) {
- throw new RuntimeException("Expected " + field + " but got " +
readField);
- }
+ assertEquals(testCase.id_, readField.id);
+ assertEquals(testCase.type_, readField.type);
testCase.readMethod(proto);
proto.readStructEnd();
}
- public static abstract class StructFieldTestCase {
+ private static abstract class StructFieldTestCase {
byte type_;
short id_;
public StructFieldTestCase(byte type, short id) {
Propchange:
incubator/thrift/trunk/lib/java/test/org/apache/thrift/protocol/ProtocolTestBase.java
------------------------------------------------------------------------------
svn:mergeinfo =
Copied:
incubator/thrift/trunk/lib/java/test/org/apache/thrift/protocol/TestTBinaryProtocol.java
(from r927642,
incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/TBinaryProtocolTest.java)
URL:
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/java/test/org/apache/thrift/protocol/TestTBinaryProtocol.java?p2=incubator/thrift/trunk/lib/java/test/org/apache/thrift/protocol/TestTBinaryProtocol.java&p1=incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/TBinaryProtocolTest.java&r1=927642&r2=927699&rev=927699&view=diff
==============================================================================
---
incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/TBinaryProtocolTest.java
(original)
+++
incubator/thrift/trunk/lib/java/test/org/apache/thrift/protocol/TestTBinaryProtocol.java
Fri Mar 26 05:58:34 2010
@@ -1,17 +1,9 @@
-package org.apache.thrift.test;
+package org.apache.thrift.protocol;
-import org.apache.thrift.protocol.TBinaryProtocol;
-import org.apache.thrift.protocol.TProtocolFactory;
-public class TBinaryProtocolTest extends ProtocolTestBase {
-
- public static void main(String[] args) throws Exception {
- new TBinaryProtocolTest().main();
- }
-
+public class TestTBinaryProtocol extends ProtocolTestBase {
@Override
protected TProtocolFactory getFactory() {
return new TBinaryProtocol.Factory();
}
-
}
Propchange:
incubator/thrift/trunk/lib/java/test/org/apache/thrift/protocol/TestTBinaryProtocol.java
------------------------------------------------------------------------------
svn:mergeinfo =
Copied:
incubator/thrift/trunk/lib/java/test/org/apache/thrift/protocol/TestTCompactProtocol.java
(from r927642,
incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/TCompactProtocolTest.java)
URL:
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/java/test/org/apache/thrift/protocol/TestTCompactProtocol.java?p2=incubator/thrift/trunk/lib/java/test/org/apache/thrift/protocol/TestTCompactProtocol.java&p1=incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/TCompactProtocolTest.java&r1=927642&r2=927699&rev=927699&view=diff
==============================================================================
---
incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/TCompactProtocolTest.java
(original)
+++
incubator/thrift/trunk/lib/java/test/org/apache/thrift/protocol/TestTCompactProtocol.java
Fri Mar 26 05:58:34 2010
@@ -18,18 +18,12 @@
*/
-package org.apache.thrift.test;
+package org.apache.thrift.protocol;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
-public class TCompactProtocolTest extends ProtocolTestBase {
-
- public static void main(String[] args) throws Exception {
- new TCompactProtocolTest().main();
- }
-
-
+public class TestTCompactProtocol extends ProtocolTestBase {
@Override
protected TProtocolFactory getFactory() {
return new TCompactProtocol.Factory();
Propchange:
incubator/thrift/trunk/lib/java/test/org/apache/thrift/protocol/TestTCompactProtocol.java
------------------------------------------------------------------------------
svn:executable = *
Propchange:
incubator/thrift/trunk/lib/java/test/org/apache/thrift/protocol/TestTCompactProtocol.java
------------------------------------------------------------------------------
svn:mergeinfo =