ParameterTreeIntegrationTest.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.node.parameter;
00018 
00019 import static org.junit.Assert.assertEquals;
00020 import static org.junit.Assert.assertFalse;
00021 import static org.junit.Assert.assertTrue;
00022 import static org.junit.Assert.fail;
00023 
00024 import com.google.common.collect.Lists;
00025 import com.google.common.collect.Maps;
00026 
00027 import org.junit.Before;
00028 import org.junit.Test;
00029 import org.ros.RosTest;
00030 import org.ros.exception.ParameterClassCastException;
00031 import org.ros.exception.ParameterNotFoundException;
00032 import org.ros.namespace.GraphName;
00033 import org.ros.node.AbstractNodeMain;
00034 import org.ros.node.ConnectedNode;
00035 
00036 import java.util.Collection;
00037 import java.util.List;
00038 import java.util.Map;
00039 import java.util.concurrent.CountDownLatch;
00040 import java.util.concurrent.TimeUnit;
00041 
00045 public class ParameterTreeIntegrationTest extends RosTest {
00046 
00047   private ParameterTree parameters;
00048 
00049   @Before
00050   public void setup() throws InterruptedException {
00051     final CountDownLatch latch = new CountDownLatch(1);
00052     nodeMainExecutor.execute(new AbstractNodeMain() {
00053       @Override
00054       public GraphName getDefaultNodeName() {
00055         return GraphName.of("node_name");
00056       }
00057 
00058       @Override
00059       public void onStart(ConnectedNode connectedNode) {
00060         parameters = connectedNode.getParameterTree();
00061         latch.countDown();
00062       }
00063     }, nodeConfiguration);
00064     assertTrue(latch.await(1, TimeUnit.SECONDS));
00065   }
00066 
00067   @Test
00068   public void testGetNonExistentParameter() {
00069     try {
00070       parameters.getBoolean("bloop");
00071       fail();
00072     } catch (ParameterNotFoundException e) {
00073       // Thrown when a parameter does not exist.
00074     }
00075   }
00076 
00077   @Test
00078   public void testGetParameterOfWrongType() {
00079     parameters.set("bloop", "foo");
00080     try {
00081       parameters.getBoolean("bloop");
00082       fail();
00083     } catch (ParameterClassCastException e) {
00084       // Thrown when a parameter is of the wrong type.
00085     }
00086   }
00087 
00088   @Test
00089   public void testGetParameterWithDefault() {
00090     assertTrue(parameters.getBoolean("bloop", true));
00091     List<String> expectedList = Lists.newArrayList("foo", "bar", "baz");
00092     assertEquals(expectedList, parameters.getList("bloop", expectedList));
00093     parameters.set("bloop", expectedList);
00094     assertEquals(expectedList, parameters.getList("bloop", Lists.newArrayList()));
00095   }
00096 
00097   @Test
00098   public void testGetParameterWithDefaultOfWrongType() {
00099     parameters.set("bloop", "foo");
00100     try {
00101       parameters.getBoolean("bloop", true);
00102       fail();
00103     } catch (ParameterClassCastException e) {
00104       // Thrown when a parameter is of the wrong type.
00105     }
00106   }
00107 
00108   @Test
00109   public void testSetAndGetStrings() {
00110     parameters.set("/foo/bar", "baz");
00111     assertEquals("baz", parameters.getString("/foo/bar"));
00112     parameters.set("/foo/bar", "baz");
00113     assertEquals("baz", parameters.getString("/foo/bar"));
00114     Map<String, Object> expected = Maps.newHashMap();
00115     expected.put("bar", "baz");
00116     assertEquals(expected, parameters.getMap("/foo"));
00117   }
00118 
00119   @Test
00120   public void testSetAndGetAllTypes() {
00121     String name = "/foo/bar";
00122     parameters.set(name, true);
00123     assertTrue(parameters.getBoolean(name));
00124     parameters.set(name, 42);
00125     assertEquals(42, parameters.getInteger(name));
00126     parameters.set(name, 0.42d);
00127     assertEquals(0.42d, parameters.getDouble(name), 0.01);
00128     parameters.set(name, "foo");
00129     assertEquals("foo", parameters.getString(name));
00130     List<String> expectedList = Lists.newArrayList("foo", "bar", "baz");
00131     parameters.set(name, expectedList);
00132     assertEquals(expectedList, parameters.getList(name));
00133     Map<String, String> expectedMap = Maps.newHashMap();
00134     expectedMap.put("foo", "bar");
00135     expectedMap.put("baz", "bloop");
00136     parameters.set(name, expectedMap);
00137     assertEquals(expectedMap, parameters.getMap(name));
00138   }
00139 
00140   @Test
00141   public void testDeleteAndHas() {
00142     parameters.set("/foo/bar", "baz");
00143     assertTrue(parameters.has("/foo/bar"));
00144     parameters.delete("/foo/bar");
00145     assertFalse(parameters.has("/foo/bar"));
00146   }
00147 
00148   @Test
00149   public void testGetNames() {
00150     parameters.set("/foo/bar", "baz");
00151     parameters.set("/bloop", "doh");
00152     Collection<GraphName> names = parameters.getNames();
00153     assertEquals(2, names.size());
00154     assertTrue(names.contains(GraphName.of("/foo/bar")));
00155     assertTrue(names.contains(GraphName.of("/bloop")));
00156   }
00157 
00158   @Test
00159   public void testParameterPubSub() throws InterruptedException {
00160     final CountDownLatch nodeLatch = new CountDownLatch(1);
00161     final CountDownLatch parameterLatch = new CountDownLatch(1);
00162     nodeMainExecutor.execute(new AbstractNodeMain() {
00163       @Override
00164       public GraphName getDefaultNodeName() {
00165         return GraphName.of("subscriber");
00166       }
00167 
00168       @Override
00169       public void onStart(ConnectedNode connectedNode) {
00170         ParameterTree subscriberParameters = connectedNode.getParameterTree();
00171         subscriberParameters.addParameterListener("/foo/bar", new ParameterListener() {
00172           @Override
00173           public void onNewValue(Object value) {
00174             assertEquals(42, value);
00175             parameterLatch.countDown();
00176           }
00177         });
00178         nodeLatch.countDown();
00179       }
00180     }, nodeConfiguration);
00181     nodeLatch.await(1, TimeUnit.SECONDS);
00182     parameters.set("/foo/bar", 42);
00183     assertTrue(parameterLatch.await(1, TimeUnit.SECONDS));
00184   }
00185 }


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