CommandLineLoaderTest.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.loader;
00018 
00019 import static org.junit.Assert.assertEquals;
00020 import static org.junit.Assert.assertTrue;
00021 import static org.ros.Assert.assertGraphNameEquals;
00022 
00023 import com.google.common.collect.Lists;
00024 import com.google.common.collect.Maps;
00025 
00026 import org.junit.Assert;
00027 import org.junit.Before;
00028 import org.junit.Test;
00029 import org.ros.CommandLineVariables;
00030 import org.ros.EnvironmentVariables;
00031 import org.ros.namespace.GraphName;
00032 import org.ros.namespace.NameResolver;
00033 import org.ros.node.NodeConfiguration;
00034 
00035 import java.io.File;
00036 import java.net.URI;
00037 import java.net.URISyntaxException;
00038 import java.util.HashMap;
00039 import java.util.List;
00040 import java.util.Map;
00041 
00047 public class CommandLineLoaderTest {
00048 
00049   private URI defaultMasterUri;
00050   private File defaultRosRoot;
00051   private List<String> emptyArgv;
00052 
00053   private HashMap<String, String> getDefaultEnv() {
00054     HashMap<String, String> env = new HashMap<String, String>();
00055     env.put(EnvironmentVariables.ROS_MASTER_URI, defaultMasterUri.toString());
00056     env.put(EnvironmentVariables.ROS_ROOT, defaultRosRoot.getAbsolutePath());
00057     return env;
00058   }
00059 
00060   @Before
00061   public void setup() throws URISyntaxException {
00062     defaultMasterUri = new URI("http://localhost:33133");
00063     defaultRosRoot = new File(System.getProperty("user.dir"));
00064     emptyArgv = Lists.newArrayList("Foo");
00065   }
00066 
00067   @Test
00068   public void testCommandLineLoader() {
00069     // Test with no args.
00070     CommandLineLoader loader = new CommandLineLoader(emptyArgv);
00071     assertEquals(0, loader.getNodeArguments().size());
00072 
00073     // Test with no remappings.
00074     emptyArgv.add("one two --three");
00075     loader = new CommandLineLoader(emptyArgv);
00076     Assert.assertEquals(emptyArgv.subList(1, emptyArgv.size()), loader.getNodeArguments());
00077 
00078     // Test with actual remappings. All of these are equivalent.
00079     List<String> tests =
00080         Lists.newArrayList("--help one name:=/my/name -i foo:=/my/foo",
00081             "name:=/my/name --help one -i foo:=/my/foo",
00082             "name:=/my/name --help foo:=/my/foo one -i");
00083     List<String> expected = Lists.newArrayList("--help", "one", "-i");
00084     for (String test : tests) {
00085       loader = new CommandLineLoader(Lists.newArrayList(("Foo " + test).split("\\s+")));
00086       // Test command-line parsing
00087       Assert.assertEquals(expected, loader.getNodeArguments());
00088     }
00089   }
00090 
00095   @Test
00096   public void testcreateConfigurationEnvironment() {
00097     // Construct artificial environment. Test failure without required settings.
00098     // Failure: ROS_ROOT not set.
00099     String tmpDir = System.getProperty("java.io.tmpdir");
00100     String rosPackagePath = tmpDir + File.pathSeparator + defaultRosRoot;
00101     List<File> rosPackagePathList = Lists.newArrayList(new File(tmpDir), defaultRosRoot);
00102     Map<String, String> env = new HashMap<String, String>();
00103     CommandLineLoader loader = null;
00104     env = Maps.newHashMap();
00105     env.put(EnvironmentVariables.ROS_ROOT, defaultRosRoot.getAbsolutePath());
00106     loader = new CommandLineLoader(emptyArgv, env);
00107     NodeConfiguration nodeConfiguration = loader.build();
00108     assertEquals(NodeConfiguration.DEFAULT_MASTER_URI, nodeConfiguration.getMasterUri());
00109 
00110     // Construct artificial environment. Set required environment variables.
00111     env = getDefaultEnv();
00112     loader = new CommandLineLoader(emptyArgv, env);
00113     nodeConfiguration = loader.build();
00114     assertEquals(defaultMasterUri, nodeConfiguration.getMasterUri());
00115     assertEquals(defaultRosRoot, nodeConfiguration.getRosRoot());
00116     assertTrue(nodeConfiguration.getParentResolver().getNamespace().isRoot());
00117     // Default is loopback.
00118     assertTrue(nodeConfiguration.getTcpRosAdvertiseAddress().isLoopbackAddress());
00119     assertTrue(nodeConfiguration.getXmlRpcAdvertiseAddress().isLoopbackAddress());
00120 
00121     // Construct artificial environment. Set optional environment variables.
00122     env = getDefaultEnv();
00123     env.put(EnvironmentVariables.ROS_IP, "192.168.0.1");
00124     env.put(EnvironmentVariables.ROS_NAMESPACE, "/foo/bar");
00125     env.put(EnvironmentVariables.ROS_PACKAGE_PATH, rosPackagePath);
00126     loader = new CommandLineLoader(emptyArgv, env);
00127     nodeConfiguration = loader.build();
00128 
00129     assertEquals(defaultMasterUri, nodeConfiguration.getMasterUri());
00130     assertEquals(defaultRosRoot, nodeConfiguration.getRosRoot());
00131     assertEquals("192.168.0.1", nodeConfiguration.getTcpRosAdvertiseAddress().getHost());
00132     assertEquals("192.168.0.1", nodeConfiguration.getXmlRpcAdvertiseAddress().getHost());
00133     assertEquals(GraphName.of("/foo/bar"), nodeConfiguration.getParentResolver().getNamespace());
00134     Assert.assertEquals(rosPackagePathList, nodeConfiguration.getRosPackagePath());
00135 
00136     // Test ROS namespace resolution and canonicalization
00137     GraphName canonical = GraphName.of("/baz/bar");
00138     env = getDefaultEnv();
00139     env.put(EnvironmentVariables.ROS_NAMESPACE, "baz/bar");
00140     loader = new CommandLineLoader(emptyArgv, env);
00141     nodeConfiguration = loader.build();
00142     assertEquals(canonical, nodeConfiguration.getParentResolver().getNamespace());
00143     env = getDefaultEnv();
00144     env.put(EnvironmentVariables.ROS_NAMESPACE, "baz/bar/");
00145     loader = new CommandLineLoader(emptyArgv, env);
00146     nodeConfiguration = loader.build();
00147     assertEquals(canonical, nodeConfiguration.getParentResolver().getNamespace());
00148   }
00149 
00150   @Test
00151   public void testCreateConfigurationCommandLine() throws URISyntaxException {
00152     Map<String, String> env = getDefaultEnv();
00153 
00154     // Test __name override
00155     NodeConfiguration nodeConfiguration = new CommandLineLoader(emptyArgv, env).build();
00156     assertEquals(null, nodeConfiguration.getNodeName());
00157     List<String> args = Lists.newArrayList("Foo", "__name:=newname");
00158     nodeConfiguration = new CommandLineLoader(args, env).build();
00159     assertEquals("newname", nodeConfiguration.getNodeName().toString());
00160 
00161     // Test ROS_MASTER_URI from command-line
00162     args =
00163         Lists.newArrayList("Foo", CommandLineVariables.ROS_MASTER_URI + ":=http://override:22622");
00164     nodeConfiguration = new CommandLineLoader(args, env).build();
00165     assertEquals(new URI("http://override:22622"), nodeConfiguration.getMasterUri());
00166 
00167     // Test again with env var removed, make sure that it still behaves the
00168     // same.
00169     env.remove(EnvironmentVariables.ROS_MASTER_URI);
00170     nodeConfiguration = new CommandLineLoader(args, env).build();
00171     assertEquals(new URI("http://override:22622"), nodeConfiguration.getMasterUri());
00172 
00173     // Test ROS namespace resolution and canonicalization
00174     GraphName canonical = GraphName.of("/baz/bar");
00175     env = getDefaultEnv();
00176     args = Lists.newArrayList("Foo", CommandLineVariables.ROS_NAMESPACE + ":=baz/bar");
00177     nodeConfiguration = new CommandLineLoader(args, env).build();
00178     assertEquals(canonical, nodeConfiguration.getParentResolver().getNamespace());
00179 
00180     args = Lists.newArrayList("Foo", CommandLineVariables.ROS_NAMESPACE + ":=baz/bar/");
00181     nodeConfiguration = new CommandLineLoader(args, env).build();
00182     assertEquals(canonical, nodeConfiguration.getParentResolver().getNamespace());
00183 
00184     // Verify precedence of command-line __ns over environment.
00185     env.put(EnvironmentVariables.ROS_NAMESPACE, "wrong/answer/");
00186     nodeConfiguration = new CommandLineLoader(args, env).build();
00187     assertEquals(canonical, nodeConfiguration.getParentResolver().getNamespace());
00188 
00189     // Verify address override.
00190     env = getDefaultEnv();
00191     args = Lists.newArrayList("Foo", CommandLineVariables.ROS_IP + ":=192.168.0.2");
00192     nodeConfiguration = new CommandLineLoader(args, env).build();
00193     assertEquals("192.168.0.2", nodeConfiguration.getTcpRosAdvertiseAddress().getHost());
00194     assertEquals("192.168.0.2", nodeConfiguration.getXmlRpcAdvertiseAddress().getHost());
00195 
00196     // Verify multiple options work together.
00197     env = getDefaultEnv();
00198     args =
00199         Lists.newArrayList("Foo", CommandLineVariables.ROS_NAMESPACE + ":=baz/bar/", "ignore",
00200             CommandLineVariables.ROS_MASTER_URI + ":=http://override:22622", "--bad",
00201             CommandLineVariables.ROS_IP + ":=192.168.0.2");
00202     nodeConfiguration = new CommandLineLoader(args, env).build();
00203     assertEquals(new URI("http://override:22622"), nodeConfiguration.getMasterUri());
00204     assertEquals("192.168.0.2", nodeConfiguration.getTcpRosAdvertiseAddress().getHost());
00205     assertEquals("192.168.0.2", nodeConfiguration.getXmlRpcAdvertiseAddress().getHost());
00206     assertEquals(canonical, nodeConfiguration.getParentResolver().getNamespace());
00207   }
00208 
00212   @Test
00213   public void testcreateConfigurationResolver() {
00214     // Construct artificial environment. Set required environment variables.
00215     HashMap<String, String> env = getDefaultEnv();
00216 
00217     // Test with no args.
00218     CommandLineLoader loader = new CommandLineLoader(emptyArgv, env);
00219     NodeConfiguration nodeConfiguration = loader.build();
00220     nodeConfiguration.getParentResolver().getRemappings();
00221     nodeConfiguration = loader.build();
00222 
00223     // Test with no remappings.
00224     List<String> args = Lists.newArrayList("Foo", "foo", "--bar");
00225     loader = new CommandLineLoader(args, env);
00226     nodeConfiguration = loader.build();
00227     NameResolver resolver = nodeConfiguration.getParentResolver();
00228     assertTrue(resolver.getRemappings().isEmpty());
00229 
00230     // test with actual remappings. All of these tests are equivalent.
00231     List<String> tests =
00232         Lists.newArrayList("--help name:=/my/name -i foo:=/my/foo",
00233             "name:=/my/name --help -i foo:=/my/foo", "name:=/my/name foo:=/my/foo --help -i");
00234     for (String test : tests) {
00235       args = Lists.newArrayList(("Foo " + test).split("\\s+"));
00236       loader = new CommandLineLoader(args, env);
00237       nodeConfiguration = loader.build();
00238 
00239       // Test that our remappings loaded.
00240       NameResolver r = nodeConfiguration.getParentResolver();
00241       GraphName n = r.resolve("name");
00242       assertGraphNameEquals("/my/name", n);
00243       assertGraphNameEquals("/name", r.resolve("/name"));
00244       assertGraphNameEquals("/my/foo", r.resolve("foo"));
00245       assertGraphNameEquals("/my/name", r.resolve("/my/name"));
00246     }
00247   }
00248 }


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