MessageTest.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.assertEquals;
00020 
00021 import com.google.common.collect.Lists;
00022 
00023 import org.junit.Before;
00024 import org.junit.Test;
00025 import org.ros.internal.message.topic.TopicDefinitionResourceProvider;
00026 import org.ros.message.MessageFactory;
00027 
00031 public class MessageTest {
00032 
00033   private TopicDefinitionResourceProvider topicDefinitionResourceProvider;
00034   private MessageFactory messageFactory;
00035 
00036   @Before
00037   public void before() {
00038     topicDefinitionResourceProvider = new TopicDefinitionResourceProvider();
00039     messageFactory = new DefaultMessageFactory(topicDefinitionResourceProvider);
00040   }
00041 
00042   @Test
00043   public void testCreateEmptyMessage() {
00044     topicDefinitionResourceProvider.add("foo/foo", "");
00045     messageFactory.newFromType("foo/foo");
00046   }
00047 
00048   @Test
00049   public void testCreateEmptyMessageWithBlankLines() {
00050     topicDefinitionResourceProvider.add("foo/foo", "\n\n\n\n\n");
00051     messageFactory.newFromType("foo/foo");
00052   }
00053 
00054   @Test
00055   public void testString() {
00056     String data = "Hello, ROS!";
00057     RawMessage rawMessage = messageFactory.newFromType("std_msgs/String");
00058     rawMessage.setString("data", data);
00059     assertEquals(data, rawMessage.getString("data"));
00060   }
00061 
00062   @Test
00063   public void testStringWithComments() {
00064     topicDefinitionResourceProvider.add("foo/foo", "# foo\nstring data\n    # string other data");
00065     String data = "Hello, ROS!";
00066     RawMessage rawMessage = messageFactory.newFromType("foo/foo");
00067     rawMessage.setString("data", data);
00068     assertEquals(data, rawMessage.getString("data"));
00069   }
00070 
00071   @Test
00072   public void testInt8() {
00073     byte data = 42;
00074     RawMessage rawMessage = messageFactory.newFromType("std_msgs/Int8");
00075     rawMessage.setInt8("data", data);
00076     assertEquals(data, rawMessage.getInt8("data"));
00077   }
00078 
00079   @Test
00080   public void testNestedMessage() {
00081     topicDefinitionResourceProvider.add("foo/foo", "bar data");
00082     topicDefinitionResourceProvider.add("foo/bar", "int8 data");
00083     RawMessage fooMessage = messageFactory.newFromType("foo/foo");
00084     RawMessage barMessage = messageFactory.newFromType("foo/bar");
00085     fooMessage.setMessage("data", barMessage);
00086     byte data = 42;
00087     barMessage.setInt8("data", data);
00088     assertEquals(data, fooMessage.getMessage("data").toRawMessage().getInt8("data"));
00089   }
00090 
00091   @Test
00092   public void testNestedMessageList() {
00093     topicDefinitionResourceProvider.add("foo/foo", "bar[] data");
00094     topicDefinitionResourceProvider.add("foo/bar", "int8 data");
00095     RawMessage fooMessage = messageFactory.newFromType("foo/foo");
00096     RawMessage barMessage = messageFactory.newFromType("foo/bar");
00097     fooMessage.setMessageList("data", Lists.<Message>newArrayList(barMessage));
00098     byte data = 42;
00099     barMessage.toRawMessage().setInt8("data", data);
00100     assertEquals(data, fooMessage.getMessageList("data").get(0).toRawMessage().getInt8("data"));
00101   }
00102 
00103   @Test
00104   public void testConstantInt8() {
00105     topicDefinitionResourceProvider.add("foo/foo", "int8 data=42");
00106     RawMessage rawMessage = messageFactory.newFromType("foo/foo");
00107     assertEquals(42, rawMessage.getInt8("data"));
00108   }
00109 
00110   @Test
00111   public void testConstantString() {
00112     topicDefinitionResourceProvider.add("foo/foo", "string data=Hello, ROS! # comment ");
00113     RawMessage rawMessage = messageFactory.newFromType("foo/foo");
00114     assertEquals("Hello, ROS! # comment", rawMessage.getString("data"));
00115   }
00116 
00117   public void testInt8List() {
00118     topicDefinitionResourceProvider.add("foo/foo", "int8[] data");
00119     RawMessage rawMessage = messageFactory.newFromType("foo/foo");
00120     byte[] data = new byte[] { (byte) 1, (byte) 2, (byte) 3 };
00121     rawMessage.setInt8Array("data", data);
00122     assertEquals(data, rawMessage.getInt8Array("data"));
00123   }
00124 }


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