Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.lib.vfs; |
| 15 | |
Ulf Adams | 795895a | 2015-03-06 15:58:35 +0000 | [diff] [blame] | 16 | import static com.google.common.truth.Truth.assertThat; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 17 | import static org.junit.Assert.assertEquals; |
| 18 | import static org.junit.Assert.assertFalse; |
| 19 | import static org.junit.Assert.assertNull; |
| 20 | import static org.junit.Assert.assertTrue; |
| 21 | import static org.junit.Assert.fail; |
| 22 | |
| 23 | import com.google.common.collect.ImmutableList; |
Mark Schaller | b815432 | 2015-06-22 16:02:24 +0000 | [diff] [blame] | 24 | import com.google.common.collect.ImmutableSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | import com.google.common.collect.Lists; |
| 26 | import com.google.common.testing.EqualsTester; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | import com.google.devtools.build.lib.testutil.TestUtils; |
| 28 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
| 29 | |
| 30 | import org.junit.Test; |
| 31 | import org.junit.runner.RunWith; |
| 32 | import org.junit.runners.JUnit4; |
| 33 | |
| 34 | import java.io.File; |
| 35 | import java.util.Collections; |
| 36 | import java.util.List; |
| 37 | |
| 38 | /** |
| 39 | * This class tests the functionality of the PathFragment. |
| 40 | */ |
| 41 | @RunWith(JUnit4.class) |
| 42 | public class PathFragmentTest { |
| 43 | @Test |
| 44 | public void testMergeFourPathsWithAbsolute() { |
| 45 | assertEquals(new PathFragment("x/y/z/a/b/c/d/e"), |
| 46 | new PathFragment(new PathFragment("x/y"), new PathFragment("z/a"), |
| 47 | new PathFragment("/b/c"), // absolute! |
| 48 | new PathFragment("d/e"))); |
| 49 | } |
| 50 | |
| 51 | @Test |
Michajlo Matijkiw | baf44c9 | 2016-06-23 21:54:05 +0000 | [diff] [blame^] | 52 | public void testCreateInternsPathFragments() { |
| 53 | String[] firstSegments = new String[] {"hello", "world"}; |
| 54 | PathFragment first = PathFragment.create( |
| 55 | /*driveLetter=*/ '\0', /*isAbsolute=*/ false, firstSegments); |
| 56 | |
| 57 | String[] secondSegments = new String[] {new String("hello"), new String("world")}; |
| 58 | PathFragment second = PathFragment.create( |
| 59 | /*driveLetter=*/ '\0', /*isAbsolute=*/ false, secondSegments); |
| 60 | |
| 61 | assertThat(first.segmentCount()).isEqualTo(second.segmentCount()); |
| 62 | for (int i = 0; i < first.segmentCount(); i++) { |
| 63 | assertThat(first.getSegment(i)).isSameAs(second.getSegment(i)); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | @Test |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 68 | public void testEqualsAndHashCode() { |
| 69 | InMemoryFileSystem filesystem = new InMemoryFileSystem(); |
| 70 | |
| 71 | new EqualsTester() |
| 72 | .addEqualityGroup(new PathFragment("../relative/path"), |
| 73 | new PathFragment("../relative/path"), |
| 74 | new PathFragment(new File("../relative/path"))) |
| 75 | .addEqualityGroup(new PathFragment("something/else")) |
| 76 | .addEqualityGroup(new PathFragment("/something/else")) |
| 77 | .addEqualityGroup(new PathFragment("/"), |
| 78 | new PathFragment("//////")) |
| 79 | .addEqualityGroup(new PathFragment("")) |
| 80 | .addEqualityGroup(filesystem.getRootDirectory()) // A Path object. |
| 81 | .testEquals(); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void testHashCodeCache() { |
| 86 | PathFragment relativePath = new PathFragment("../relative/path"); |
| 87 | PathFragment rootPath = new PathFragment("/"); |
| 88 | |
| 89 | int oldResult = relativePath.hashCode(); |
| 90 | int rootResult = rootPath.hashCode(); |
| 91 | assertEquals(oldResult, relativePath.hashCode()); |
| 92 | assertEquals(rootResult, rootPath.hashCode()); |
| 93 | } |
| 94 | |
| 95 | private void checkRelativeTo(String path, String base) { |
| 96 | PathFragment relative = new PathFragment(path).relativeTo(base); |
| 97 | assertEquals(new PathFragment(path), new PathFragment(base).getRelative(relative).normalize()); |
| 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void testRelativeTo() { |
| 102 | assertPath("bar/baz", new PathFragment("foo/bar/baz").relativeTo("foo")); |
| 103 | assertPath("bar/baz", new PathFragment("/foo/bar/baz").relativeTo("/foo")); |
| 104 | assertPath("baz", new PathFragment("foo/bar/baz").relativeTo("foo/bar")); |
| 105 | assertPath("baz", new PathFragment("/foo/bar/baz").relativeTo("/foo/bar")); |
| 106 | assertPath("foo", new PathFragment("/foo").relativeTo("/")); |
| 107 | assertPath("foo", new PathFragment("foo").relativeTo("")); |
| 108 | assertPath("foo/bar", new PathFragment("foo/bar").relativeTo("")); |
| 109 | |
| 110 | checkRelativeTo("foo/bar/baz", "foo"); |
| 111 | checkRelativeTo("/foo/bar/baz", "/foo"); |
| 112 | checkRelativeTo("foo/bar/baz", "foo/bar"); |
| 113 | checkRelativeTo("/foo/bar/baz", "/foo/bar"); |
| 114 | checkRelativeTo("/foo", "/"); |
| 115 | checkRelativeTo("foo", ""); |
| 116 | checkRelativeTo("foo/bar", ""); |
| 117 | } |
| 118 | |
| 119 | @Test |
| 120 | public void testIsAbsolute() { |
| 121 | assertTrue(new PathFragment("/absolute/test").isAbsolute()); |
| 122 | assertFalse(new PathFragment("relative/test").isAbsolute()); |
| 123 | assertTrue(new PathFragment(new File("/absolute/test")).isAbsolute()); |
| 124 | assertFalse(new PathFragment(new File("relative/test")).isAbsolute()); |
| 125 | } |
| 126 | |
| 127 | @Test |
| 128 | public void testIsNormalized() { |
| 129 | assertTrue(new PathFragment("/absolute/path").isNormalized()); |
| 130 | assertTrue(new PathFragment("some//path").isNormalized()); |
| 131 | assertFalse(new PathFragment("some/./path").isNormalized()); |
| 132 | assertFalse(new PathFragment("../some/path").isNormalized()); |
| 133 | assertFalse(new PathFragment("some/other/../path").isNormalized()); |
| 134 | assertTrue(new PathFragment("some/other//tricky..path..").isNormalized()); |
| 135 | assertTrue(new PathFragment("/some/other//tricky..path..").isNormalized()); |
| 136 | } |
| 137 | |
| 138 | @Test |
| 139 | public void testRootNodeReturnsRootString() { |
| 140 | PathFragment rootFragment = new PathFragment("/"); |
| 141 | assertEquals("/", rootFragment.getPathString()); |
| 142 | } |
| 143 | |
| 144 | @Test |
| 145 | public void testGetPathFragmentDoesNotNormalize() { |
| 146 | String nonCanonicalPath = "/a/weird/noncanonical/../path/."; |
| 147 | assertEquals(nonCanonicalPath, |
| 148 | new PathFragment(nonCanonicalPath).getPathString()); |
| 149 | } |
| 150 | |
| 151 | @Test |
| 152 | public void testGetRelative() { |
| 153 | assertEquals("a/b", new PathFragment("a").getRelative("b").getPathString()); |
| 154 | assertEquals("a/b/c/d", new PathFragment("a/b").getRelative("c/d").getPathString()); |
| 155 | assertEquals("/a/b", new PathFragment("c/d").getRelative("/a/b").getPathString()); |
| 156 | assertEquals("a", new PathFragment("a").getRelative("").getPathString()); |
| 157 | assertEquals("/", new PathFragment("/").getRelative("").getPathString()); |
| 158 | } |
| 159 | |
| 160 | @Test |
| 161 | public void testGetChildWorks() { |
| 162 | PathFragment pf = new PathFragment("../some/path"); |
| 163 | assertEquals(new PathFragment("../some/path/hi"), pf.getChild("hi")); |
| 164 | } |
| 165 | |
| 166 | @Test |
| 167 | public void testGetChildRejectsInvalidBaseNames() { |
| 168 | PathFragment pf = new PathFragment("../some/path"); |
| 169 | assertGetChildFails(pf, "."); |
| 170 | assertGetChildFails(pf, ".."); |
| 171 | assertGetChildFails(pf, "x/y"); |
| 172 | assertGetChildFails(pf, "/y"); |
| 173 | assertGetChildFails(pf, "y/"); |
| 174 | assertGetChildFails(pf, ""); |
| 175 | } |
| 176 | |
| 177 | private void assertGetChildFails(PathFragment pf, String baseName) { |
| 178 | try { |
| 179 | pf.getChild(baseName); |
| 180 | fail(); |
| 181 | } catch (Exception e) { /* Expected. */ } |
| 182 | } |
| 183 | |
| 184 | // Tests after here test the canonicalization |
| 185 | private void assertRegular(String expected, String actual) { |
| 186 | assertEquals(expected, new PathFragment(actual).getPathString()); // compare string forms |
| 187 | assertEquals(new PathFragment(expected), new PathFragment(actual)); // compare fragment forms |
| 188 | } |
| 189 | |
| 190 | @Test |
| 191 | public void testEmptyPathToEmptyPath() { |
| 192 | assertRegular("/", "/"); |
| 193 | assertRegular("", ""); |
| 194 | } |
| 195 | |
| 196 | @Test |
| 197 | public void testRedundantSlashes() { |
| 198 | assertRegular("/", "///"); |
| 199 | assertRegular("/foo/bar", "/foo///bar"); |
| 200 | assertRegular("/foo/bar", "////foo//bar"); |
| 201 | } |
| 202 | |
| 203 | @Test |
| 204 | public void testSimpleNameToSimpleName() { |
| 205 | assertRegular("/foo", "/foo"); |
| 206 | assertRegular("foo", "foo"); |
| 207 | } |
| 208 | |
| 209 | @Test |
| 210 | public void testSimplePathToSimplePath() { |
| 211 | assertRegular("/foo/bar", "/foo/bar"); |
| 212 | assertRegular("foo/bar", "foo/bar"); |
| 213 | } |
| 214 | |
| 215 | @Test |
| 216 | public void testStripsTrailingSlash() { |
| 217 | assertRegular("/foo/bar", "/foo/bar/"); |
| 218 | } |
| 219 | |
| 220 | @Test |
| 221 | public void testGetParentDirectory() { |
| 222 | PathFragment fooBarWiz = new PathFragment("foo/bar/wiz"); |
| 223 | PathFragment fooBar = new PathFragment("foo/bar"); |
| 224 | PathFragment foo = new PathFragment("foo"); |
| 225 | PathFragment empty = new PathFragment(""); |
| 226 | assertEquals(fooBar, fooBarWiz.getParentDirectory()); |
| 227 | assertEquals(foo, fooBar.getParentDirectory()); |
| 228 | assertEquals(empty, foo.getParentDirectory()); |
| 229 | assertNull(empty.getParentDirectory()); |
| 230 | |
| 231 | PathFragment fooBarWizAbs = new PathFragment("/foo/bar/wiz"); |
| 232 | PathFragment fooBarAbs = new PathFragment("/foo/bar"); |
| 233 | PathFragment fooAbs = new PathFragment("/foo"); |
| 234 | PathFragment rootAbs = new PathFragment("/"); |
| 235 | assertEquals(fooBarAbs, fooBarWizAbs.getParentDirectory()); |
| 236 | assertEquals(fooAbs, fooBarAbs.getParentDirectory()); |
| 237 | assertEquals(rootAbs, fooAbs.getParentDirectory()); |
| 238 | assertNull(rootAbs.getParentDirectory()); |
| 239 | |
| 240 | // Note, this is surprising but correct behavior: |
| 241 | assertEquals(fooBarAbs, |
| 242 | new PathFragment("/foo/bar/..").getParentDirectory()); |
| 243 | } |
Michajlo Matijkiw | baf44c9 | 2016-06-23 21:54:05 +0000 | [diff] [blame^] | 244 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 245 | @Test |
| 246 | public void testSegmentsCount() { |
| 247 | assertEquals(2, new PathFragment("foo/bar").segmentCount()); |
| 248 | assertEquals(2, new PathFragment("/foo/bar").segmentCount()); |
| 249 | assertEquals(2, new PathFragment("foo//bar").segmentCount()); |
| 250 | assertEquals(2, new PathFragment("/foo//bar").segmentCount()); |
| 251 | assertEquals(1, new PathFragment("foo/").segmentCount()); |
| 252 | assertEquals(1, new PathFragment("/foo/").segmentCount()); |
| 253 | assertEquals(1, new PathFragment("foo").segmentCount()); |
| 254 | assertEquals(1, new PathFragment("/foo").segmentCount()); |
| 255 | assertEquals(0, new PathFragment("/").segmentCount()); |
| 256 | assertEquals(0, new PathFragment("").segmentCount()); |
| 257 | } |
| 258 | |
| 259 | |
| 260 | @Test |
| 261 | public void testGetSegment() { |
| 262 | assertEquals("foo", new PathFragment("foo/bar").getSegment(0)); |
| 263 | assertEquals("bar", new PathFragment("foo/bar").getSegment(1)); |
| 264 | assertEquals("foo", new PathFragment("/foo/bar").getSegment(0)); |
| 265 | assertEquals("bar", new PathFragment("/foo/bar").getSegment(1)); |
| 266 | assertEquals("foo", new PathFragment("foo/").getSegment(0)); |
| 267 | assertEquals("foo", new PathFragment("/foo/").getSegment(0)); |
| 268 | assertEquals("foo", new PathFragment("foo").getSegment(0)); |
| 269 | assertEquals("foo", new PathFragment("/foo").getSegment(0)); |
| 270 | } |
| 271 | |
| 272 | @Test |
| 273 | public void testBasename() throws Exception { |
| 274 | assertEquals("bar", new PathFragment("foo/bar").getBaseName()); |
| 275 | assertEquals("bar", new PathFragment("/foo/bar").getBaseName()); |
| 276 | assertEquals("foo", new PathFragment("foo/").getBaseName()); |
| 277 | assertEquals("foo", new PathFragment("/foo/").getBaseName()); |
| 278 | assertEquals("foo", new PathFragment("foo").getBaseName()); |
| 279 | assertEquals("foo", new PathFragment("/foo").getBaseName()); |
Ulf Adams | 795895a | 2015-03-06 15:58:35 +0000 | [diff] [blame] | 280 | assertThat(new PathFragment("/").getBaseName()).isEmpty(); |
| 281 | assertThat(new PathFragment("").getBaseName()).isEmpty(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | private static void assertPath(String expected, PathFragment actual) { |
| 285 | assertEquals(expected, actual.getPathString()); |
| 286 | } |
| 287 | |
| 288 | @Test |
| 289 | public void testReplaceName() throws Exception { |
| 290 | assertPath("foo/baz", new PathFragment("foo/bar").replaceName("baz")); |
| 291 | assertPath("/foo/baz", new PathFragment("/foo/bar").replaceName("baz")); |
| 292 | assertPath("foo", new PathFragment("foo/bar").replaceName("")); |
| 293 | assertPath("baz", new PathFragment("foo/").replaceName("baz")); |
| 294 | assertPath("/baz", new PathFragment("/foo/").replaceName("baz")); |
| 295 | assertPath("baz", new PathFragment("foo").replaceName("baz")); |
| 296 | assertPath("/baz", new PathFragment("/foo").replaceName("baz")); |
Ulf Adams | 795895a | 2015-03-06 15:58:35 +0000 | [diff] [blame] | 297 | assertNull(new PathFragment("/").replaceName("baz")); |
| 298 | assertNull(new PathFragment("/").replaceName("")); |
| 299 | assertNull(new PathFragment("").replaceName("baz")); |
| 300 | assertNull(new PathFragment("").replaceName("")); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 301 | |
| 302 | assertPath("foo/bar/baz", new PathFragment("foo/bar").replaceName("bar/baz")); |
| 303 | assertPath("foo/bar/baz", new PathFragment("foo/bar").replaceName("bar/baz/")); |
| 304 | |
| 305 | // Absolute path arguments will clobber the original path. |
| 306 | assertPath("/absolute", new PathFragment("foo/bar").replaceName("/absolute")); |
| 307 | assertPath("/", new PathFragment("foo/bar").replaceName("/")); |
| 308 | } |
| 309 | @Test |
| 310 | public void testSubFragment() throws Exception { |
| 311 | assertPath("/foo/bar/baz", |
Mark Schaller | b815432 | 2015-06-22 16:02:24 +0000 | [diff] [blame] | 312 | new PathFragment("/foo/bar/baz").subFragment(0, 3)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 313 | assertPath("foo/bar/baz", |
Mark Schaller | b815432 | 2015-06-22 16:02:24 +0000 | [diff] [blame] | 314 | new PathFragment("foo/bar/baz").subFragment(0, 3)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 315 | assertPath("/foo/bar", |
| 316 | new PathFragment("/foo/bar/baz").subFragment(0, 2)); |
| 317 | assertPath("bar/baz", |
| 318 | new PathFragment("/foo/bar/baz").subFragment(1, 3)); |
| 319 | assertPath("/foo", |
| 320 | new PathFragment("/foo/bar/baz").subFragment(0, 1)); |
| 321 | assertPath("bar", |
| 322 | new PathFragment("/foo/bar/baz").subFragment(1, 2)); |
| 323 | assertPath("baz", new PathFragment("/foo/bar/baz").subFragment(2, 3)); |
| 324 | assertPath("/", new PathFragment("/foo/bar/baz").subFragment(0, 0)); |
| 325 | assertPath("", new PathFragment("foo/bar/baz").subFragment(0, 0)); |
| 326 | assertPath("", new PathFragment("foo/bar/baz").subFragment(1, 1)); |
| 327 | try { |
| 328 | fail("unexpectedly succeeded: " + new PathFragment("foo/bar/baz").subFragment(3, 2)); |
| 329 | } catch (IndexOutOfBoundsException e) { /* Expected. */ } |
| 330 | try { |
| 331 | fail("unexpectedly succeeded: " + new PathFragment("foo/bar/baz").subFragment(4, 4)); |
| 332 | } catch (IndexOutOfBoundsException e) { /* Expected. */ } |
| 333 | } |
| 334 | |
| 335 | @Test |
| 336 | public void testStartsWith() { |
| 337 | PathFragment foobar = new PathFragment("/foo/bar"); |
| 338 | PathFragment foobarRelative = new PathFragment("foo/bar"); |
| 339 | |
| 340 | // (path, prefix) => true |
| 341 | assertTrue(foobar.startsWith(foobar)); |
| 342 | assertTrue(foobar.startsWith(new PathFragment("/"))); |
| 343 | assertTrue(foobar.startsWith(new PathFragment("/foo"))); |
| 344 | assertTrue(foobar.startsWith(new PathFragment("/foo/"))); |
| 345 | assertTrue(foobar.startsWith(new PathFragment("/foo/bar/"))); // Includes trailing slash. |
| 346 | |
| 347 | // (prefix, path) => false |
| 348 | assertFalse(new PathFragment("/foo").startsWith(foobar)); |
| 349 | assertFalse(new PathFragment("/").startsWith(foobar)); |
| 350 | |
| 351 | // (absolute, relative) => false |
| 352 | assertFalse(foobar.startsWith(foobarRelative)); |
| 353 | assertFalse(foobarRelative.startsWith(foobar)); |
| 354 | |
| 355 | // (relative path, relative prefix) => true |
| 356 | assertTrue(foobarRelative.startsWith(foobarRelative)); |
| 357 | assertTrue(foobarRelative.startsWith(new PathFragment("foo"))); |
| 358 | assertTrue(foobarRelative.startsWith(new PathFragment(""))); |
| 359 | |
| 360 | // (path, sibling) => false |
| 361 | assertFalse(new PathFragment("/foo/wiz").startsWith(foobar)); |
| 362 | assertFalse(foobar.startsWith(new PathFragment("/foo/wiz"))); |
| 363 | |
| 364 | // Does not normalize. |
| 365 | PathFragment foodotbar = new PathFragment("foo/./bar"); |
| 366 | assertTrue(foodotbar.startsWith(foodotbar)); |
| 367 | assertTrue(foodotbar.startsWith(new PathFragment("foo/."))); |
| 368 | assertTrue(foodotbar.startsWith(new PathFragment("foo/./"))); |
| 369 | assertTrue(foodotbar.startsWith(new PathFragment("foo/./bar"))); |
| 370 | assertFalse(foodotbar.startsWith(new PathFragment("foo/bar"))); |
| 371 | } |
| 372 | |
| 373 | @Test |
Mark Schaller | b815432 | 2015-06-22 16:02:24 +0000 | [diff] [blame] | 374 | public void testFilterPathsStartingWith() { |
| 375 | // Retains everything: |
| 376 | ImmutableSet<PathFragment> allUnderA = toPathsSet("a/b", "a/c", "a/d"); |
| 377 | assertThat(PathFragment.filterPathsStartingWith(allUnderA, new PathFragment("a"))) |
| 378 | .containsExactlyElementsIn(allUnderA); |
| 379 | |
| 380 | // Retains some but not others: |
| 381 | ImmutableSet<PathFragment> mixed = toPathsSet("a/b", "a/c", "b/c"); |
| 382 | assertThat(PathFragment.filterPathsStartingWith(mixed, |
| 383 | new PathFragment("a"))).containsExactlyElementsIn(toPathsSet("a/b", "a/c")); |
| 384 | |
| 385 | // Retains none: |
| 386 | assertThat(PathFragment.filterPathsStartingWith(allUnderA, new PathFragment("b"))).isEmpty(); |
| 387 | |
| 388 | // Retains paths equal to the startingWithPath: |
| 389 | assertThat(PathFragment.filterPathsStartingWith(toPathsSet("a"), |
| 390 | new PathFragment("a"))).containsExactlyElementsIn(toPathsSet("a")); |
| 391 | |
| 392 | // Retains everything when startingWithPath is the empty fragment: |
| 393 | assertThat(PathFragment.filterPathsStartingWith(mixed, PathFragment.EMPTY_FRAGMENT)) |
| 394 | .containsExactlyElementsIn(mixed); |
| 395 | |
| 396 | // Supports multi-segment startingWithPaths: |
| 397 | assertThat(PathFragment.filterPathsStartingWith(toPathsSet("a/b/c", "a/b/d", "a/c/d"), |
| 398 | new PathFragment("a/b"))).containsExactlyElementsIn(toPathsSet("a/b/c", "a/b/d")); |
| 399 | } |
| 400 | |
| 401 | @Test |
| 402 | public void testCheckAllPathsStartWithButAreNotEqualTo() { |
| 403 | // Check passes: |
| 404 | PathFragment.checkAllPathsAreUnder(toPathsSet("a/b", "a/c"), |
| 405 | new PathFragment("a")); |
| 406 | |
| 407 | // Check trivially passes: |
| 408 | PathFragment.checkAllPathsAreUnder(ImmutableList.<PathFragment>of(), |
| 409 | new PathFragment("a")); |
| 410 | |
| 411 | // Check fails when some path does not start with startingWithPath: |
| 412 | try { |
| 413 | PathFragment.checkAllPathsAreUnder(toPathsSet("a/b", "b/c"), |
| 414 | new PathFragment("a")); |
| 415 | fail(); |
| 416 | } catch (IllegalArgumentException expected) { |
| 417 | } |
| 418 | |
| 419 | // Check fails when some path is equal to startingWithPath: |
| 420 | try { |
| 421 | PathFragment.checkAllPathsAreUnder(toPathsSet("a/b", "a"), |
| 422 | new PathFragment("a")); |
| 423 | fail(); |
| 424 | } catch (IllegalArgumentException expected) { |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | @Test |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 429 | public void testEndsWith() { |
| 430 | PathFragment foobar = new PathFragment("/foo/bar"); |
| 431 | PathFragment foobarRelative = new PathFragment("foo/bar"); |
| 432 | |
| 433 | // (path, suffix) => true |
| 434 | assertTrue(foobar.endsWith(foobar)); |
| 435 | assertTrue(foobar.endsWith(new PathFragment("bar"))); |
| 436 | assertTrue(foobar.endsWith(new PathFragment("foo/bar"))); |
| 437 | assertTrue(foobar.endsWith(new PathFragment("/foo/bar"))); |
| 438 | assertFalse(foobar.endsWith(new PathFragment("/bar"))); |
| 439 | |
| 440 | // (prefix, path) => false |
| 441 | assertFalse(new PathFragment("/foo").endsWith(foobar)); |
| 442 | assertFalse(new PathFragment("/").endsWith(foobar)); |
| 443 | |
| 444 | // (suffix, path) => false |
| 445 | assertFalse(new PathFragment("/bar").endsWith(foobar)); |
| 446 | assertFalse(new PathFragment("bar").endsWith(foobar)); |
| 447 | assertFalse(new PathFragment("").endsWith(foobar)); |
| 448 | |
| 449 | // (absolute, relative) => true |
| 450 | assertTrue(foobar.endsWith(foobarRelative)); |
| 451 | |
| 452 | // (relative, absolute) => false |
| 453 | assertFalse(foobarRelative.endsWith(foobar)); |
| 454 | |
| 455 | // (relative path, relative prefix) => true |
| 456 | assertTrue(foobarRelative.endsWith(foobarRelative)); |
| 457 | assertTrue(foobarRelative.endsWith(new PathFragment("bar"))); |
| 458 | assertTrue(foobarRelative.endsWith(new PathFragment(""))); |
| 459 | |
| 460 | // (path, sibling) => false |
| 461 | assertFalse(new PathFragment("/foo/wiz").endsWith(foobar)); |
| 462 | assertFalse(foobar.endsWith(new PathFragment("/foo/wiz"))); |
| 463 | } |
| 464 | |
| 465 | static List<PathFragment> toPaths(List<String> strs) { |
| 466 | List<PathFragment> paths = Lists.newArrayList(); |
| 467 | for (String s : strs) { |
| 468 | paths.add(new PathFragment(s)); |
| 469 | } |
| 470 | return paths; |
| 471 | } |
| 472 | |
Mark Schaller | b815432 | 2015-06-22 16:02:24 +0000 | [diff] [blame] | 473 | static ImmutableSet<PathFragment> toPathsSet(String... strs) { |
| 474 | ImmutableSet.Builder<PathFragment> builder = ImmutableSet.builder(); |
| 475 | for (String str : strs) { |
| 476 | builder.add(new PathFragment(str)); |
| 477 | } |
| 478 | return builder.build(); |
| 479 | } |
| 480 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 481 | @Test |
| 482 | public void testCompareTo() throws Exception { |
| 483 | List<String> pathStrs = ImmutableList.of( |
| 484 | "", "/", "//", ".", "/./", "foo/.//bar", "foo", "/foo", "foo/bar", "foo/Bar", "Foo/bar"); |
| 485 | List<PathFragment> paths = toPaths(pathStrs); |
| 486 | // First test that compareTo is self-consistent. |
| 487 | for (PathFragment x : paths) { |
| 488 | for (PathFragment y : paths) { |
| 489 | for (PathFragment z : paths) { |
| 490 | // Anti-symmetry |
| 491 | assertEquals(Integer.signum(x.compareTo(y)), |
| 492 | -1 * Integer.signum(y.compareTo(x))); |
| 493 | // Transitivity |
| 494 | if (x.compareTo(y) > 0 && y.compareTo(z) > 0) { |
Googler | 2fa3ccf | 2015-11-10 14:30:39 +0000 | [diff] [blame] | 495 | assertThat(x.compareTo(z)).isGreaterThan(0); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 496 | } |
| 497 | // "Substitutability" |
| 498 | if (x.compareTo(y) == 0) { |
| 499 | assertEquals(Integer.signum(x.compareTo(z)), Integer.signum(y.compareTo(z))); |
| 500 | } |
| 501 | // Consistency with equals |
| 502 | assertEquals((x.compareTo(y) == 0), x.equals(y)); |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | // Now test that compareTo does what we expect. The exact ordering here doesn't matter much, |
| 507 | // but there are three things to notice: 1. absolute < relative, 2. comparison is lexicographic |
| 508 | // 3. repeated slashes are ignored. (PathFragment("//") prints as "/"). |
| 509 | Collections.shuffle(paths); |
| 510 | Collections.sort(paths); |
| 511 | List<PathFragment> expectedOrder = toPaths(ImmutableList.of( |
| 512 | "/", "//", "/./", "/foo", "", ".", "Foo/bar", "foo", "foo/.//bar", "foo/Bar", "foo/bar")); |
| 513 | assertEquals(expectedOrder, paths); |
| 514 | } |
| 515 | |
| 516 | @Test |
| 517 | public void testGetSafePathString() { |
| 518 | assertEquals("/", new PathFragment("/").getSafePathString()); |
| 519 | assertEquals("/abc", new PathFragment("/abc").getSafePathString()); |
| 520 | assertEquals(".", new PathFragment("").getSafePathString()); |
| 521 | assertEquals(".", PathFragment.EMPTY_FRAGMENT.getSafePathString()); |
| 522 | assertEquals("abc/def", new PathFragment("abc/def").getSafePathString()); |
| 523 | } |
Michajlo Matijkiw | baf44c9 | 2016-06-23 21:54:05 +0000 | [diff] [blame^] | 524 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 525 | @Test |
| 526 | public void testNormalize() { |
| 527 | assertEquals(new PathFragment("/a/b"), new PathFragment("/a/b").normalize()); |
| 528 | assertEquals(new PathFragment("/a/b"), new PathFragment("/a/./b").normalize()); |
| 529 | assertEquals(new PathFragment("/b"), new PathFragment("/a/../b").normalize()); |
| 530 | assertEquals(new PathFragment("a/b"), new PathFragment("a/b").normalize()); |
| 531 | assertEquals(new PathFragment("../b"), new PathFragment("a/../../b").normalize()); |
| 532 | assertEquals(new PathFragment(".."), new PathFragment("a/../..").normalize()); |
| 533 | assertEquals(new PathFragment("b"), new PathFragment("a/../b").normalize()); |
| 534 | assertEquals(new PathFragment("a/b"), new PathFragment("a/b/../b").normalize()); |
| 535 | assertEquals(new PathFragment("/.."), new PathFragment("/..").normalize()); |
| 536 | } |
| 537 | |
| 538 | @Test |
| 539 | public void testSerializationSimple() throws Exception { |
| 540 | checkSerialization("a", 91); |
| 541 | } |
| 542 | |
| 543 | @Test |
| 544 | public void testSerializationAbsolute() throws Exception { |
| 545 | checkSerialization("/foo", 94); |
| 546 | } |
| 547 | |
| 548 | @Test |
| 549 | public void testSerializationNested() throws Exception { |
| 550 | checkSerialization("foo/bar/baz", 101); |
| 551 | } |
| 552 | |
| 553 | private void checkSerialization(String pathFragmentString, int expectedSize) throws Exception { |
| 554 | PathFragment a = new PathFragment(pathFragmentString); |
| 555 | byte[] sa = TestUtils.serializeObject(a); |
| 556 | assertEquals(expectedSize, sa.length); |
| 557 | |
| 558 | PathFragment a2 = (PathFragment) TestUtils.deserializeObject(sa); |
| 559 | assertEquals(a, a2); |
| 560 | } |
| 561 | } |