GraphNameTest.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 package org.ros.namespace;
00017 
00018 import static org.junit.Assert.assertEquals;
00019 import static org.junit.Assert.assertFalse;
00020 import static org.junit.Assert.assertTrue;
00021 import static org.junit.Assert.fail;
00022 
00023 import org.junit.Test;
00024 
00025 import java.util.Collections;
00026 import java.util.HashSet;
00027 import java.util.Set;
00028 import java.util.concurrent.CountDownLatch;
00029 import java.util.concurrent.Executor;
00030 import java.util.concurrent.Executors;
00031 import java.util.concurrent.TimeUnit;
00032 
00037 public class GraphNameTest {
00038 
00039   public void assertGraphNameEquals(String name, GraphName graphName) {
00040     assertEquals(name, graphName.toString());
00041   }
00042 
00043   @Test
00044   public void testToString() {
00045     try {
00046       String[] canonical = { "abc", "ab7", "/abc", "/abc/bar", "/", "~garage", "~foo/bar" };
00047       for (String c : canonical) {
00048         assertGraphNameEquals(c, GraphName.of(c));
00049       }
00050     } catch (IllegalArgumentException e) {
00051       fail("These names should be valid" + e.toString());
00052     }
00053   }
00054 
00055   @Test
00056   public void testValidNames() {
00057     String[] valid =
00058         { "", "abc", "ab7", "ab7_kdfJKSDJFGkd", "/abc", "/", "~private", "~private/something",
00059             "/global", "/global/", "/global/local" };
00060     for (String v : valid) {
00061       GraphName.of(v);
00062     }
00063   }
00064 
00065   @Test
00066   public void testInvalidNames() {
00067     final String[] illegalChars = { "=", "-", "(", ")", "*", "%", "^" };
00068     for (String i : illegalChars) {
00069       try {
00070         GraphName.of("good" + i);
00071         fail("Bad name not caught: " + i);
00072       } catch (RuntimeException e) {
00073       }
00074     }
00075     final String[] illegalNames = { "/~private", "5foo" };
00076     for (String i : illegalNames) {
00077       try {
00078         GraphName.of(i);
00079         fail("Bad name not caught" + i);
00080       } catch (RuntimeException e) {
00081       }
00082     }
00083   }
00084 
00085   @Test
00086   public void testIsGlobal() {
00087     final String[] tests = { "/", "/global", "/global2" };
00088     for (String t : tests) {
00089       assertTrue(GraphName.of(t).isGlobal());
00090     }
00091     final String[] fails = { "", "not_global", "not/global" };
00092     for (String t : fails) {
00093       assertFalse(GraphName.of(t).isGlobal());
00094     }
00095   }
00096 
00097   @Test
00098   public void testIsPrivate() {
00099     String[] tests = { "~name", "~name/sub" };
00100     for (String t : tests) {
00101       assertTrue(GraphName.of(t).isPrivate());
00102     }
00103     String[] fails = { "", "not_private", "not/private", "/" };
00104     for (String f : fails) {
00105       assertFalse(GraphName.of(f).isPrivate());
00106     }
00107   }
00108 
00109   @Test
00110   public void testIsRelative() {
00111     GraphName n = GraphName.of("name");
00112     assertTrue(n.isRelative());
00113     n = GraphName.of("/name");
00114     assertFalse(n.isRelative());
00115   }
00116 
00117   @Test
00118   public void testGetParent() {
00119     GraphName global = GraphName.of("/");
00120     GraphName empty = GraphName.of("");
00121     // parent of empty is empty, just like dirname
00122     assertEquals(empty, GraphName.of("").getParent());
00123     // parent of global is global, just like dirname
00124     assertEquals(global, GraphName.of("/").getParent());
00125 
00126     // test with global names
00127     assertEquals(GraphName.of("/wg"), GraphName.of("/wg/name").getParent());
00128     assertEquals(GraphName.of("/wg"), GraphName.of("/wg/name/").getParent());
00129     assertEquals(global, GraphName.of("/wg/").getParent());
00130     assertEquals(global, GraphName.of("/wg").getParent());
00131 
00132     // test with relative names
00133     assertEquals(GraphName.of("wg"), GraphName.of("wg/name").getParent());
00134     assertEquals(empty, GraphName.of("wg/").getParent());
00135   }
00136 
00137   @Test
00138   public void testCanonicalizeName() {
00139     assertGraphNameEquals("", GraphName.of(""));
00140     assertGraphNameEquals("/", GraphName.of("/"));
00141     assertGraphNameEquals("/", GraphName.of("//"));
00142     assertGraphNameEquals("/", GraphName.of("///"));
00143 
00144     assertGraphNameEquals("foo", GraphName.of("foo"));
00145     assertGraphNameEquals("foo", GraphName.of("foo/"));
00146     assertGraphNameEquals("foo", GraphName.of("foo//"));
00147 
00148     assertGraphNameEquals("/foo", GraphName.of("/foo"));
00149     assertGraphNameEquals("/foo", GraphName.of("/foo/"));
00150     assertGraphNameEquals("/foo", GraphName.of("/foo//"));
00151 
00152     assertGraphNameEquals("/foo/bar", GraphName.of("/foo/bar"));
00153     assertGraphNameEquals("/foo/bar", GraphName.of("/foo/bar/"));
00154     assertGraphNameEquals("/foo/bar", GraphName.of("/foo/bar//"));
00155 
00156     assertGraphNameEquals("~foo", GraphName.of("~foo"));
00157     assertGraphNameEquals("~foo", GraphName.of("~foo/"));
00158     assertGraphNameEquals("~foo", GraphName.of("~foo//"));
00159     assertGraphNameEquals("~foo", GraphName.of("~/foo"));
00160   }
00161 
00162   @Test
00163   public void testGetName() {
00164     assertGraphNameEquals("", GraphName.of("").getBasename());
00165     assertGraphNameEquals("", GraphName.of("").getBasename());
00166     assertGraphNameEquals("foo", GraphName.of("/foo").getBasename());
00167     assertGraphNameEquals("foo", GraphName.of("foo").getBasename());
00168     // The trailing slash is removed when creating a GraphName.
00169     assertGraphNameEquals("foo", GraphName.of("foo/").getBasename());
00170     assertGraphNameEquals("bar", GraphName.of("/foo/bar").getBasename());
00171     assertGraphNameEquals("bar", GraphName.of("foo/bar").getBasename());
00172   }
00173 
00174   @Test
00175   public void testJoin() {
00176     assertEquals(GraphName.of("/bar"), GraphName.of("/").join(GraphName.of("bar")));
00177     assertEquals(GraphName.of("bar"), GraphName.of("").join(GraphName.of("bar")));
00178     assertEquals(GraphName.of("bar"), GraphName.of("bar").join(GraphName.of("")));
00179     assertEquals(GraphName.of("foo/bar"), GraphName.of("foo").join(GraphName.of("bar")));
00180     assertEquals(GraphName.of("/foo/bar"), GraphName.of("/foo").join(GraphName.of("bar")));
00181     assertEquals(GraphName.of("/bar"), GraphName.of("/foo").join(GraphName.of("/bar")));
00182   }
00183 
00184   @Test
00185   public void testNewAnonymous() throws InterruptedException {
00186     Executor executor = Executors.newFixedThreadPool(10);
00187     int sampleSize = 10000;
00188     final CountDownLatch latch = new CountDownLatch(sampleSize);
00189     final Set<GraphName> anonymousGraphNames =
00190         Collections.synchronizedSet(new HashSet<GraphName>());
00191     for (int i = 0; i < sampleSize; i++) {
00192       executor.execute(new Runnable() {
00193         @Override
00194         public void run() {
00195           GraphName name = GraphName.newAnonymous();
00196           assertTrue(name.toString().startsWith(GraphName.ANONYMOUS_PREFIX));
00197           anonymousGraphNames.add(name);
00198           latch.countDown();
00199         }
00200       });
00201     }
00202     assertTrue(latch.await(1, TimeUnit.SECONDS));
00203     assertEquals(sampleSize, anonymousGraphNames.size());
00204   }
00205 }


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