From bf0595fcb871dc496c35aa453713ae04bd1f9017 Mon Sep 17 00:00:00 2001 From: Bryan Duxbury Date: Fri, 26 Mar 2010 16:59:46 +0000 Subject: [PATCH] java: Convert TJSONProtocol test to JUnit, making a few minor changes to ProtocolTestBase in the process. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@927967 13f79535-47bb-0310-9956-ffa450edef68 --- lib/java/build.xml | 2 - .../org/apache/thrift/protocol/TMessage.java | 2 + .../thrift/protocol/ProtocolTestBase.java | 56 ++++++--- .../thrift/protocol/TestTBinaryProtocol.java | 5 + .../thrift/protocol/TestTCompactProtocol.java | 5 + .../thrift/protocol/TestTJSONProtocol.java | 31 +++++ .../org/apache/thrift/test/JSONProtoTest.java | 110 ------------------ 7 files changed, 86 insertions(+), 125 deletions(-) create mode 100644 lib/java/test/org/apache/thrift/protocol/TestTJSONProtocol.java delete mode 100644 lib/java/test/org/apache/thrift/test/JSONProtoTest.java diff --git a/lib/java/build.xml b/lib/java/build.xml index 02172fb5..22a924fd 100644 --- a/lib/java/build.xml +++ b/lib/java/build.xml @@ -191,8 +191,6 @@ - "; } + @Override public boolean equals(Object other) { if (other instanceof TMessage) { return equals((TMessage) other); diff --git a/lib/java/test/org/apache/thrift/protocol/ProtocolTestBase.java b/lib/java/test/org/apache/thrift/protocol/ProtocolTestBase.java index e3e592ae..b69c47d1 100644 --- a/lib/java/test/org/apache/thrift/protocol/ProtocolTestBase.java +++ b/lib/java/test/org/apache/thrift/protocol/ProtocolTestBase.java @@ -38,30 +38,42 @@ import thrift.test.Srv; public abstract class ProtocolTestBase extends TestCase { + protected abstract boolean canBeUsedNaked(); + protected abstract TProtocolFactory getFactory(); public void testProtocol() throws Exception { - internalTestNakedByte(); + if (canBeUsedNaked()) { + internalTestNakedByte(); + } for (int i = 0; i < 128; i++) { internalTestByteField((byte)i); internalTestByteField((byte)-i); } for (int s : Arrays.asList(0, 1, 7, 150, 15000, 0x7fff, -1, -7, -150, -15000, -0x7fff)) { - internalTestNakedI16((short)s); + if (canBeUsedNaked()) { + 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)) { - internalTestNakedI32(i); + if (canBeUsedNaked()) { + internalTestNakedI32(i); + } internalTestI32Field(i); } - internalTestNakedI64(0); + if (canBeUsedNaked()) { + internalTestNakedI64(0); + } internalTestI64Field(0); for (int i = 0; i < 62; i++) { - internalTestNakedI64(1L << i); - internalTestNakedI64(-(1L << i)); + if (canBeUsedNaked()) { + internalTestNakedI64(1L << i); + internalTestNakedI64(-(1L << i)); + } internalTestI64Field(1L << i); internalTestI64Field(-(1L << i)); } @@ -69,12 +81,16 @@ public abstract class ProtocolTestBase extends TestCase { internalTestDouble(); for (String s : Arrays.asList("", "short", "borderlinetiny", "a bit longer than the smallest possible")) { - internalTestNakedString(s); + if (canBeUsedNaked()) { + 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])) { - internalTestNakedBinary(b); + if (canBeUsedNaked()) { + internalTestNakedBinary(b); + } internalTestBinaryField(b); } @@ -91,7 +107,7 @@ public abstract class ProtocolTestBase extends TestCase { } private void internalTestNakedByte() throws Exception { - TMemoryBuffer buf = new TMemoryBuffer(0); + TMemoryBuffer buf = new TMemoryBuffer(1000); TProtocol proto = getFactory().getProtocol(buf); proto.writeByte((byte)123); assertEquals((byte) 123, proto.readByte()); @@ -167,10 +183,24 @@ public abstract class ProtocolTestBase extends TestCase { } private void internalTestDouble() throws Exception { - TMemoryBuffer buf = new TMemoryBuffer(1000); - TProtocol proto = getFactory().getProtocol(buf); - proto.writeDouble(123.456); - assertEquals(123.456, proto.readDouble()); + if (canBeUsedNaked()) { + TMemoryBuffer buf = new TMemoryBuffer(1000); + TProtocol proto = getFactory().getProtocol(buf); + proto.writeDouble(123.456); + assertEquals(123.456, proto.readDouble()); + } + + internalTestStructField(new StructFieldTestCase(TType.DOUBLE, (short)15) { + @Override + public void readMethod(TProtocol proto) throws TException { + assertEquals(123.456, proto.readDouble()); + } + + @Override + public void writeMethod(TProtocol proto) throws TException { + proto.writeDouble(123.456); + } + }); } private void internalTestNakedString(String str) throws Exception { diff --git a/lib/java/test/org/apache/thrift/protocol/TestTBinaryProtocol.java b/lib/java/test/org/apache/thrift/protocol/TestTBinaryProtocol.java index 9f91a5e9..aff1492e 100644 --- a/lib/java/test/org/apache/thrift/protocol/TestTBinaryProtocol.java +++ b/lib/java/test/org/apache/thrift/protocol/TestTBinaryProtocol.java @@ -6,4 +6,9 @@ public class TestTBinaryProtocol extends ProtocolTestBase { protected TProtocolFactory getFactory() { return new TBinaryProtocol.Factory(); } + + @Override + protected boolean canBeUsedNaked() { + return true; + } } diff --git a/lib/java/test/org/apache/thrift/protocol/TestTCompactProtocol.java b/lib/java/test/org/apache/thrift/protocol/TestTCompactProtocol.java index 67b1655a..7d0a37c3 100755 --- a/lib/java/test/org/apache/thrift/protocol/TestTCompactProtocol.java +++ b/lib/java/test/org/apache/thrift/protocol/TestTCompactProtocol.java @@ -28,4 +28,9 @@ public class TestTCompactProtocol extends ProtocolTestBase { protected TProtocolFactory getFactory() { return new TCompactProtocol.Factory(); } + + @Override + protected boolean canBeUsedNaked() { + return true; + } } \ No newline at end of file diff --git a/lib/java/test/org/apache/thrift/protocol/TestTJSONProtocol.java b/lib/java/test/org/apache/thrift/protocol/TestTJSONProtocol.java new file mode 100644 index 00000000..d7376ac6 --- /dev/null +++ b/lib/java/test/org/apache/thrift/protocol/TestTJSONProtocol.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.thrift.protocol; + +public class TestTJSONProtocol extends ProtocolTestBase { + @Override + protected TProtocolFactory getFactory() { + return new TJSONProtocol.Factory(); + } + + @Override + protected boolean canBeUsedNaked() { + return false; + } +} diff --git a/lib/java/test/org/apache/thrift/test/JSONProtoTest.java b/lib/java/test/org/apache/thrift/test/JSONProtoTest.java deleted file mode 100644 index 23d89a2f..00000000 --- a/lib/java/test/org/apache/thrift/test/JSONProtoTest.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.thrift.test; - -// Generated code -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; - -import org.apache.thrift.protocol.TJSONProtocol; -import org.apache.thrift.transport.TMemoryBuffer; - -import thrift.test.Base64; -import thrift.test.Bonk; -import thrift.test.HolyMoley; -import thrift.test.Nesting; -import thrift.test.OneOfEach; - -/** - * Tests for the Java implementation of TJSONProtocol. Mirrors the C++ version - * - */ -public class JSONProtoTest { - - public static void main(String [] args) throws Exception { - try { - System.out.println("In JSON Proto test"); - - OneOfEach ooe = Fixtures.oneOfEach; - Nesting n = Fixtures.nesting; - - HolyMoley hm = Fixtures.holyMoley; - - TMemoryBuffer buffer = new TMemoryBuffer(1024); - TJSONProtocol proto = new TJSONProtocol(buffer); - - System.out.println("Writing ooe"); - ooe.write(proto); - System.out.println("Reading ooe"); - OneOfEach ooe2 = new OneOfEach(); - ooe2.read(proto); - - System.out.println("Comparing ooe"); - if (!ooe.equals(ooe2)) { - throw new RuntimeException("ooe != ooe2"); - } - - System.out.println("Writing hm"); - hm.write(proto); - - System.out.println("Reading hm"); - HolyMoley hm2 = new HolyMoley(); - hm2.read(proto); - - System.out.println("Comparing hm"); - if (!hm.equals(hm2)) { - throw new RuntimeException("hm != hm2"); - } - - hm2.big.get(0).a_bite = (byte)0xFF; - if (hm.equals(hm2)) { - throw new RuntimeException("hm should not equal hm2"); - } - - Base64 base = new Base64(); - base.a = 123; - base.b1 = "1".getBytes("UTF-8"); - base.b2 = "12".getBytes("UTF-8"); - base.b3 = "123".getBytes("UTF-8"); - base.b4 = "1234".getBytes("UTF-8"); - base.b5 = "12345".getBytes("UTF-8"); - base.b6 = "123456".getBytes("UTF-8"); - - System.out.println("Writing base"); - base.write(proto); - - System.out.println("Reading base"); - Base64 base2 = new Base64(); - base2.read(proto); - - System.out.println("Comparing base"); - if (!base.equals(base2)) { - throw new RuntimeException("base != base2"); - } - - } catch (Exception ex) { - ex.printStackTrace(); - throw ex; - } - } - -} -- 2.17.1