00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
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
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
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
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 }