MessageSerializationTest.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.internal.message;
00018 
00019 import static org.junit.Assert.assertEquals;
00020 
00021 import com.google.common.collect.Lists;
00022 
00023 import org.jboss.netty.buffer.ChannelBuffer;
00024 import org.junit.Before;
00025 import org.junit.Test;
00026 import org.ros.internal.message.definition.MessageDefinitionReflectionProvider;
00027 import org.ros.message.Duration;
00028 import org.ros.message.Time;
00029 
00030 import java.util.List;
00031 
00035 public class MessageSerializationTest {
00036 
00037   private MessageDefinitionReflectionProvider messageDefinitionReflectionProvider;
00038   private DefaultMessageFactory defaultMessageFactory;
00039   private DefaultMessageSerializer serializer;
00040 
00041   private interface Nested extends Message {
00042     static final java.lang.String _TYPE = "test/Nested";
00043     static final java.lang.String _DEFINITION = "std_msgs/String data\n";
00044 
00045     std_msgs.String getData();
00046 
00047     void setData(std_msgs.String value);
00048   }
00049 
00050   private interface NestedList extends Message {
00051     static final java.lang.String _TYPE = "test/NestedList";
00052     static final java.lang.String _DEFINITION = "std_msgs/String[] data\n";
00053 
00054     List<std_msgs.String> getData();
00055 
00056     void setData(List<std_msgs.String> value);
00057   }
00058 
00059   @Before
00060   public void before() {
00061     messageDefinitionReflectionProvider = new MessageDefinitionReflectionProvider();
00062     messageDefinitionReflectionProvider.add(Nested._TYPE, Nested._DEFINITION);
00063     messageDefinitionReflectionProvider.add(NestedList._TYPE, NestedList._DEFINITION);
00064     defaultMessageFactory = new DefaultMessageFactory(messageDefinitionReflectionProvider);
00065     defaultMessageFactory.getMessageInterfaceClassProvider().add(Nested._TYPE, Nested.class);
00066     defaultMessageFactory.getMessageInterfaceClassProvider()
00067         .add(NestedList._TYPE, NestedList.class);
00068     serializer = new DefaultMessageSerializer();
00069   }
00070 
00071   private <T extends Message> void checkSerializeAndDeserialize(T message) {
00072     ChannelBuffer buffer = MessageBuffers.dynamicBuffer();
00073     serializer.serialize(message, buffer);
00074     dumpBuffer(buffer);
00075     DefaultMessageDeserializer<T> deserializer =
00076         new DefaultMessageDeserializer<T>(message.toRawMessage().getIdentifier(),
00077             defaultMessageFactory);
00078     assertEquals(message, deserializer.deserialize(buffer));
00079   }
00080 
00081   @Test
00082   public void testBool() {
00083     std_msgs.Bool message = defaultMessageFactory.newFromType(std_msgs.Bool._TYPE);
00084     message.setData(true);
00085     checkSerializeAndDeserialize(message);
00086     message.setData(false);
00087     checkSerializeAndDeserialize(message);
00088   }
00089 
00090   @Test
00091   public void testInt8() {
00092     std_msgs.Int8 message = defaultMessageFactory.newFromType(std_msgs.Int8._TYPE);
00093     message.setData((byte) 42);
00094     checkSerializeAndDeserialize(message);
00095   }
00096 
00097   @Test
00098   public void testUint8() {
00099     std_msgs.UInt8 message = defaultMessageFactory.newFromType(std_msgs.UInt8._TYPE);
00100     message.setData((byte) 42);
00101     checkSerializeAndDeserialize(message);
00102   }
00103 
00104   @Test
00105   public void testInt16() {
00106     std_msgs.Int16 message = defaultMessageFactory.newFromType(std_msgs.Int16._TYPE);
00107     message.setData((short) 42);
00108     checkSerializeAndDeserialize(message);
00109   }
00110 
00111   @Test
00112   public void testUInt16() {
00113     std_msgs.UInt16 message = defaultMessageFactory.newFromType(std_msgs.UInt16._TYPE);
00114     message.setData((short) 42);
00115     checkSerializeAndDeserialize(message);
00116   }
00117 
00118   @Test
00119   public void testInt32() {
00120     std_msgs.Int32 message = defaultMessageFactory.newFromType(std_msgs.Int32._TYPE);
00121     message.setData(42);
00122     checkSerializeAndDeserialize(message);
00123   }
00124 
00125   @Test
00126   public void testUInt32() {
00127     std_msgs.UInt32 message = defaultMessageFactory.newFromType(std_msgs.UInt32._TYPE);
00128     message.setData(42);
00129     checkSerializeAndDeserialize(message);
00130   }
00131 
00132   @Test
00133   public void testInt64() {
00134     std_msgs.Int64 message = defaultMessageFactory.newFromType(std_msgs.Int64._TYPE);
00135     message.setData(42);
00136     checkSerializeAndDeserialize(message);
00137   }
00138 
00139   @Test
00140   public void testUInt64() {
00141     std_msgs.UInt64 message = defaultMessageFactory.newFromType(std_msgs.UInt64._TYPE);
00142     message.setData(42);
00143     checkSerializeAndDeserialize(message);
00144   }
00145 
00146   @Test
00147   public void testFloat32() {
00148     std_msgs.Float32 message = defaultMessageFactory.newFromType(std_msgs.Float32._TYPE);
00149     message.setData(42);
00150     checkSerializeAndDeserialize(message);
00151   }
00152 
00153   @Test
00154   public void testFloat64() {
00155     std_msgs.Float64 message = defaultMessageFactory.newFromType(std_msgs.Float64._TYPE);
00156     message.setData(42);
00157     checkSerializeAndDeserialize(message);
00158   }
00159 
00160   @Test
00161   public void testString() {
00162     std_msgs.String message = defaultMessageFactory.newFromType(std_msgs.String._TYPE);
00163     message.setData("Hello, ROS!");
00164     checkSerializeAndDeserialize(message);
00165   }
00166 
00167   @Test
00168   public void testTime() {
00169     std_msgs.Time message = defaultMessageFactory.newFromType(std_msgs.Time._TYPE);
00170     message.setData(new Time());
00171     checkSerializeAndDeserialize(message);
00172   }
00173 
00174   @Test
00175   public void testDuration() {
00176     std_msgs.Duration message = defaultMessageFactory.newFromType(std_msgs.Duration._TYPE);
00177     message.setData(new Duration());
00178     checkSerializeAndDeserialize(message);
00179   }
00180 
00181   @Test
00182   public void testNestedMessage() {
00183     Nested nestedMessage = defaultMessageFactory.newFromType(Nested._TYPE);
00184     std_msgs.String stringMessage = defaultMessageFactory.newFromType(std_msgs.String._TYPE);
00185     stringMessage.setData("Hello, ROS!");
00186     nestedMessage.setData(stringMessage);
00187     checkSerializeAndDeserialize(nestedMessage);
00188   }
00189 
00190   @Test
00191   public void testNestedMessageList() {
00192     messageDefinitionReflectionProvider.add(NestedList._TYPE, NestedList._DEFINITION);
00193     NestedList nestedListMessage = defaultMessageFactory.newFromType(NestedList._TYPE);
00194     std_msgs.String stringMessageA = defaultMessageFactory.newFromType(std_msgs.String._TYPE);
00195     stringMessageA.setData("Hello, ROS!");
00196     std_msgs.String stringMessageB = defaultMessageFactory.newFromType(std_msgs.String._TYPE);
00197     stringMessageB.setData("Hello, ROS!");
00198     nestedListMessage.setData(Lists.<std_msgs.String>newArrayList(stringMessageA, stringMessageB));
00199     checkSerializeAndDeserialize(nestedListMessage);
00200   }
00201 
00205   @Test
00206   public void testOdometry() {
00207     nav_msgs.Odometry message = defaultMessageFactory.newFromType(nav_msgs.Odometry._TYPE);
00208     checkSerializeAndDeserialize(message);
00209     ChannelBuffer buffer = MessageBuffers.dynamicBuffer();
00210     serializer.serialize(message, buffer);
00211     dumpBuffer(buffer);
00212     // Throw away sequence number.
00213     buffer.readInt();
00214     while (buffer.readable()) {
00215       byte b = buffer.readByte();
00216       assertEquals("All serialized bytes should be 0. Check stdout.", 0, b);
00217     }
00218   }
00219 
00220   private void dumpBuffer(ChannelBuffer buffer) {
00221     buffer = buffer.duplicate();
00222     System.out.printf("Dumping %d readable bytes:\n", buffer.readableBytes());
00223     int i = 0;
00224     while (buffer.readable()) {
00225       byte b = buffer.readByte();
00226       System.out.printf("0x%02x ", b);
00227       if (++i % 8 == 0) {
00228         System.out.print("   ");
00229       }
00230       if (i % 16 == 0) {
00231         System.out.print("\n");
00232       }
00233     }
00234     System.out.print("\n\n");
00235   }
00236 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49