NameResolverTest.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.namespace;
00018 
00019 import static org.junit.Assert.assertEquals;
00020 import static org.junit.Assert.fail;
00021 import static org.ros.Assert.assertGraphNameEquals;
00022 
00023 import java.util.HashMap;
00024 
00025 import org.junit.Test;
00026 
00030 public class NameResolverTest {
00031 
00032   @Test
00033   public void testResolveNameOneArg() {
00034     NameResolver r = NameResolver.newRoot();
00035 
00036     assertGraphNameEquals("/foo", r.resolve("foo"));
00037     assertGraphNameEquals("/foo", r.resolve("/foo"));
00038     assertGraphNameEquals("/foo/bar", r.resolve("foo/bar"));
00039 
00040     try {
00041       r.resolve("~foo");
00042       fail();
00043     } catch (RuntimeException e) {
00044       // Should throw if trying to resolve private name.
00045     }
00046 
00047     r = NameResolver.newFromNamespace("/ns1");
00048 
00049     assertGraphNameEquals("/ns1/foo", r.resolve("foo"));
00050     assertGraphNameEquals("/foo", r.resolve("/foo"));
00051     assertGraphNameEquals("/ns1/foo/bar", r.resolve("foo/bar"));
00052   }
00053 
00054   @Test
00055   public void testResolveNameTwoArg() {
00056     // These tests are based on test_roslib_names.py.
00057     NameResolver r = NameResolver.newRoot();
00058     try {
00059       r.resolve("foo", "bar");
00060       fail("should have raised");
00061     } catch (IllegalArgumentException e) {
00062     }
00063 
00064     GraphName root = GraphName.root();
00065     assertEquals(root, r.resolve(root, ""));
00066     assertEquals(root, r.resolve(root, root));
00067     assertEquals(root, r.resolve("/anything/bar", root));
00068     assertGraphNameEquals("/ns1/node", r.resolve("/ns1/node", ""));
00069 
00070     // relative namespaces get resolved to default namespace
00071     assertGraphNameEquals("/foo", r.resolve(root, "foo"));
00072     assertGraphNameEquals("/foo", r.resolve(root, "foo/"));
00073     assertGraphNameEquals("/foo", r.resolve(root, "/foo"));
00074     assertGraphNameEquals("/foo", r.resolve(root, "/foo/"));
00075 
00076     assertGraphNameEquals("/ns1/ns2/foo", r.resolve("/ns1/ns2", "foo"));
00077     assertGraphNameEquals("/ns1/ns2/foo", r.resolve("/ns1/ns2", "foo/"));
00078     assertGraphNameEquals("/ns1/ns2/foo", r.resolve("/ns1/ns2/", "foo"));
00079     assertGraphNameEquals("/foo", r.resolve("/ns1/ns2", "/foo/"));
00080 
00081     assertGraphNameEquals("/ns1/ns2/ns3/foo", r.resolve("/ns1/ns2/ns3", "foo"));
00082     assertGraphNameEquals("/ns1/ns2/ns3/foo", r.resolve("/ns1/ns2/ns3/", "foo"));
00083     assertGraphNameEquals("/foo", r.resolve(root, "/foo/"));
00084 
00085     assertGraphNameEquals("/ns1/ns2/foo/bar", r.resolve("/ns1/ns2", "foo/bar"));
00086     assertGraphNameEquals("/ns1/ns2/ns3/foo/bar", r.resolve("/ns1/ns2/ns3", "foo/bar"));
00087 
00088     try {
00089       r.resolve(root, "~foo");
00090       fail();
00091     } catch (RuntimeException e) {
00092       // resolveName() with two args should never allow private names
00093     }
00094   }
00095 
00099   @Test
00100   public void testResolveNameRemapping() {
00101     HashMap<GraphName, GraphName> remappings = new HashMap<GraphName, GraphName>();
00102     remappings.put(GraphName.of("name"), GraphName.of("/my/name"));
00103     remappings.put(GraphName.of("foo"), GraphName.of("/my/foo"));
00104 
00105     NameResolver r = NameResolver.newRootFromRemappings(remappings);
00106 
00107     GraphName n = r.resolve("name");
00108     assertGraphNameEquals("/my/name", n);
00109     assertGraphNameEquals("/name", r.resolve("/name"));
00110     assertGraphNameEquals("/my/foo", r.resolve("foo"));
00111     assertGraphNameEquals("/my/name", r.resolve("/my/name"));
00112   }
00113 }


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