RawMessageSerializationTest.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 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.assertTrue;
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.topic.TopicDefinitionResourceProvider;
00027 import org.ros.message.Duration;
00028 import org.ros.message.MessageFactory;
00029 import org.ros.message.Time;
00030 
00034 public class RawMessageSerializationTest {
00035 
00036   private TopicDefinitionResourceProvider topicDefinitionResourceProvider;
00037   private MessageFactory messageFactory;
00038 
00039   @Before
00040   public void before() {
00041     topicDefinitionResourceProvider = new TopicDefinitionResourceProvider();
00042     messageFactory = new DefaultMessageFactory(topicDefinitionResourceProvider);
00043   }
00044 
00045   private void checkSerializeAndDeserialize(Message message) {
00046     ChannelBuffer buffer = MessageBuffers.dynamicBuffer();
00047     DefaultMessageSerializer serializer = new DefaultMessageSerializer();
00048     serializer.serialize(message, buffer);
00049     DefaultMessageDeserializer<RawMessage> deserializer =
00050         new DefaultMessageDeserializer<RawMessage>(message.toRawMessage().getIdentifier(),
00051             messageFactory);
00052     RawMessage deserializedMessage = deserializer.deserialize(buffer);
00053     assertTrue(message.equals(deserializedMessage));
00054   }
00055 
00056   @Test
00057   public void testBool() {
00058     RawMessage rawMessage = messageFactory.newFromType("std_msgs/Bool");
00059     rawMessage.setBool("data", true);
00060     checkSerializeAndDeserialize(rawMessage);
00061     rawMessage.setBool("data", false);
00062     checkSerializeAndDeserialize(rawMessage);
00063   }
00064 
00065   @Test
00066   public void testInt8() {
00067     RawMessage rawMessage = messageFactory.newFromType("std_msgs/Int8");
00068     rawMessage.setInt8("data", (byte) 42);
00069     checkSerializeAndDeserialize(rawMessage);
00070   }
00071 
00072   @Test
00073   public void testUint8() {
00074     RawMessage rawMessage = messageFactory.newFromType("std_msgs/UInt8");
00075     rawMessage.setUInt8("data", (byte) 42);
00076     checkSerializeAndDeserialize(rawMessage);
00077   }
00078 
00079   @Test
00080   public void testInt16() {
00081     RawMessage rawMessage = messageFactory.newFromType("std_msgs/Int16");
00082     rawMessage.setInt16("data", (short) 42);
00083     checkSerializeAndDeserialize(rawMessage);
00084   }
00085 
00086   @Test
00087   public void testUInt16() {
00088     RawMessage rawMessage = messageFactory.newFromType("std_msgs/UInt16");
00089     rawMessage.setUInt16("data", (short) 42);
00090     checkSerializeAndDeserialize(rawMessage);
00091   }
00092 
00093   @Test
00094   public void testInt32() {
00095     RawMessage rawMessage = messageFactory.newFromType("std_msgs/Int32");
00096     rawMessage.setInt32("data", 42);
00097     checkSerializeAndDeserialize(rawMessage);
00098   }
00099 
00100   @Test
00101   public void testUInt32() {
00102     RawMessage rawMessage = messageFactory.newFromType("std_msgs/UInt32");
00103     rawMessage.setUInt32("data", 42);
00104     checkSerializeAndDeserialize(rawMessage);
00105   }
00106 
00107   @Test
00108   public void testInt64() {
00109     RawMessage rawMessage = messageFactory.newFromType("std_msgs/Int64");
00110     rawMessage.setInt64("data", 42);
00111     checkSerializeAndDeserialize(rawMessage);
00112   }
00113 
00114   @Test
00115   public void testUInt64() {
00116     RawMessage rawMessage = messageFactory.newFromType("std_msgs/UInt64");
00117     rawMessage.setUInt64("data", 42);
00118     checkSerializeAndDeserialize(rawMessage);
00119   }
00120 
00121   @Test
00122   public void testFloat32() {
00123     RawMessage rawMessage = messageFactory.newFromType("std_msgs/Float32");
00124     rawMessage.setFloat32("data", 42);
00125     checkSerializeAndDeserialize(rawMessage);
00126   }
00127 
00128   @Test
00129   public void testFloat64() {
00130     RawMessage rawMessage = messageFactory.newFromType("std_msgs/Float64");
00131     rawMessage.setFloat64("data", 42);
00132     checkSerializeAndDeserialize(rawMessage);
00133   }
00134 
00135   @Test
00136   public void testString() {
00137     RawMessage rawMessage = messageFactory.newFromType("std_msgs/String");
00138     rawMessage.setString("data", "Hello, ROS!");
00139     checkSerializeAndDeserialize(rawMessage);
00140   }
00141 
00142   @Test
00143   public void testTime() {
00144     RawMessage rawMessage = messageFactory.newFromType("std_msgs/Time");
00145     rawMessage.setTime("data", new Time());
00146     checkSerializeAndDeserialize(rawMessage);
00147   }
00148 
00149   @Test
00150   public void testDuration() {
00151     RawMessage rawMessage = messageFactory.newFromType("std_msgs/Duration");
00152     rawMessage.setDuration("data", new Duration());
00153     checkSerializeAndDeserialize(rawMessage);
00154   }
00155 
00156   @Test
00157   public void testNestedMessage() {
00158     topicDefinitionResourceProvider.add("foo/foo", "std_msgs/String data");
00159     RawMessage fooMessage = messageFactory.newFromType("foo/foo");
00160     RawMessage stringMessage = messageFactory.newFromType("std_msgs/String");
00161     stringMessage.setString("data", "Hello, ROS!");
00162     fooMessage.setMessage("data", stringMessage);
00163     checkSerializeAndDeserialize(fooMessage);
00164   }
00165 
00166   @Test
00167   public void testNestedMessageArray() {
00168     topicDefinitionResourceProvider.add("foo/foo", "std_msgs/String[] data");
00169     RawMessage fooMessage = messageFactory.newFromType("foo/foo");
00170     RawMessage stringMessageA = messageFactory.newFromType("std_msgs/String");
00171     stringMessageA.setString("data", "Hello, ROS!");
00172     RawMessage stringMessageB = messageFactory.newFromType("std_msgs/String");
00173     stringMessageB.setString("data", "Goodbye, ROS!");
00174     fooMessage.setMessageList("data", Lists.<Message>newArrayList(stringMessageA, stringMessageB));
00175     checkSerializeAndDeserialize(fooMessage);
00176   }
00177 
00178   @Test
00179   public void testChannelBuffer() {
00180     topicDefinitionResourceProvider.add("foo/foo", "uint8[] data");
00181     ChannelBuffer buffer = MessageBuffers.dynamicBuffer();
00182     buffer.writeBytes(new byte[] { 1, 2, 3, 4, 5 });
00183     RawMessage rawMessage = messageFactory.newFromType("foo/foo");
00184     rawMessage.setChannelBuffer("data", buffer);
00185     checkSerializeAndDeserialize(rawMessage);
00186   }
00187 
00188   @Test
00189   public void testInt32Array() {
00190     topicDefinitionResourceProvider.add("foo/foo", "int32[] data");
00191     RawMessage rawMessage = messageFactory.newFromType("foo/foo");
00192     rawMessage.setInt32Array("data", new int[] { 1, 2, 3, 4, 5 });
00193     checkSerializeAndDeserialize(rawMessage);
00194   }
00195 
00196   @Test
00197   public void testFloat64Array() {
00198     topicDefinitionResourceProvider.add("foo/foo", "float64[] data");
00199     RawMessage rawMessage = messageFactory.newFromType("foo/foo");
00200     rawMessage.setFloat64Array("data", new double[] { 1, 2, 3, 4, 5 });
00201     checkSerializeAndDeserialize(rawMessage);
00202   }
00203 }


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