11d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/*
21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2009 The Guava Authors
31d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
41d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License"); you may not
51d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * use this file except in compliance with the License.  You may obtain a copy
61d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * of the License at
71d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
81d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * http://www.apache.org/licenses/LICENSE-2.0
91d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Unless required by applicable law or agreed to in writing, software
111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * License for the specific language governing permissions and limitations under
141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * the License.
151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */
161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpackage com.google.common.io;
181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.base.Charsets.UTF_8;
201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.io.Files.simplifyPath;
211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.base.CharMatcher;
231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.base.Splitter;
241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport junit.framework.TestCase;
261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.io.IOException;
281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.net.URL;
291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Iterator;
301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/**
321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Unit tests for {@link Files#simplifyPath}.
331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert *
341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @author Pablo Bellver
351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */
361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpublic class FilesSimplifyPathTest extends TestCase {
371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testSimplifyEmptyString() {
391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals(".", simplifyPath(""));
401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testSimplifyDot() {
431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals(".", simplifyPath("."));
441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testSimplifyWhiteSpace() {
471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals(" ", simplifyPath(" "));
481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testSimplify2() {
511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("x", simplifyPath("x"));
521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testSimplify3() {
551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/d", simplifyPath("/a/b/c/d"));
561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testSimplify4() {
591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/d", simplifyPath("/a/b/c/d/"));
601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testSimplify5() {
631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b", simplifyPath("/a//b"));
641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testSimplify6() {
671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b", simplifyPath("//a//b/"));
681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testSimplify7() {
711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/", simplifyPath("/.."));
721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testSimplify8() {
751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/", simplifyPath("/././././"));
761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testSimplify9() {
791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a", simplifyPath("/a/b/.."));
801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testSimplify10() {
831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/", simplifyPath("/a/b/../../.."));
841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testSimplify11() {
871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/", simplifyPath("//a//b/..////../..//"));
881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testSimplify12() {
911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/x", simplifyPath("//a//../x//"));
921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testSimplify13() {
951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("../c", simplifyPath("a/b/../../../c"));
961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testSimplifyDotDot() {
991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("..", simplifyPath(".."));
1001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testSimplifyDotDotSlash() {
1031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("..", simplifyPath("../"));
1041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("..", simplifyPath("a/../.."));
1051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("..", simplifyPath("a/../../"));
1061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testSimplifyDotDots() {
1091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("../..", simplifyPath("a/../../.."));
1101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("../../..", simplifyPath("a/../../../.."));
1111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testSimplifyRootedDotDots() {
1141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/", simplifyPath("/../../.."));
1151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/", simplifyPath("/../../../"));
1161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  // b/4558855
1191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testMadbotsBug() {
1201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("../this", simplifyPath("../this"));
1211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("../this/is/ok", simplifyPath("../this/is/ok"));
1221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("../ok", simplifyPath("../this/../ok"));
1231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  // https://code.google.com/p/guava-libraries/issues/detail?id=705
1261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void test705() {
1271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("../b", simplifyPath("x/../../b"));
1281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("b", simplifyPath("x/../b"));
1291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  // https://code.google.com/p/guava-libraries/issues/detail?id=716
1321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void test716() {
1331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("b", simplifyPath("./b"));
1341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("b", simplifyPath("./b/."));
1351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("b", simplifyPath("././b/./."));
1361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("b", simplifyPath("././b"));
1371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("a/b", simplifyPath("./a/b"));
1381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testHiddenFiles() {
1411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals(".b", simplifyPath(".b"));
1421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals(".b", simplifyPath("./.b"));
1431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals(".metadata/b", simplifyPath(".metadata/b"));
1441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals(".metadata/b", simplifyPath("./.metadata/b"));
1451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  // https://code.google.com/p/guava-libraries/issues/detail?id=716
1481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testMultipleDotFilenames() {
1491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("..a", simplifyPath("..a"));
1501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/..a", simplifyPath("/..a"));
1511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/..a/..b", simplifyPath("/..a/..b"));
1521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/.....a/..b", simplifyPath("/.....a/..b"));
1531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("..../....", simplifyPath("..../...."));
1541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("..a../..b..", simplifyPath("..a../..b.."));
1551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testSlashDot() {
1581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/", simplifyPath("/."));
1591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  // http://code.google.com/p/guava-libraries/issues/detail?id=722
1621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testInitialSlashDotDot() {
1631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/c", simplifyPath("/../c"));
1641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  // http://code.google.com/p/guava-libraries/issues/detail?id=722
1671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testInitialSlashDot() {
1681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a", simplifyPath("/./a"));
1691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/.a", simplifyPath("/.a/a/.."));
1701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  // http://code.google.com/p/guava-libraries/issues/detail?id=722
1731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testConsecutiveParentsAfterPresent() {
1741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("../..", simplifyPath("./../../"));
1751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("../..", simplifyPath("./.././../"));
1761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
1771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /*
1791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * We co-opt some URI resolution tests for our purposes.
1801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Some of the tests have queries and anchors that are a little silly here.
1811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
1821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /** http://gbiv.com/protocols/uri/rfc/rfc2396.html#rfc.section.C.1 */
1841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testRfc2396Normal() {
1851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g", simplifyPath("/a/b/c/g"));
1861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g", simplifyPath("/a/b/c/./g"));
1871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g", simplifyPath("/a/b/c/g/"));
1881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g?y", simplifyPath("/a/b/c/g?y"));
1901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g#s", simplifyPath("/a/b/c/g#s"));
1911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g?y#s", simplifyPath("/a/b/c/g?y#s"));
1921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/;x", simplifyPath("/a/b/c/;x"));
1931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g;x", simplifyPath("/a/b/c/g;x"));
1941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g;x?y#s", simplifyPath("/a/b/c/g;x?y#s"));
1951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c", simplifyPath("/a/b/c/."));
1961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c", simplifyPath("/a/b/c/./"));
1971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b", simplifyPath("/a/b/c/.."));
1981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b", simplifyPath("/a/b/c/../"));
1991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/g", simplifyPath("/a/b/c/../g"));
2001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a", simplifyPath("/a/b/c/../.."));
2011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a", simplifyPath("/a/b/c/../../"));
2021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/g", simplifyPath("/a/b/c/../../g"));
2031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
2041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /** http://gbiv.com/protocols/uri/rfc/rfc2396.html#rfc.section.C.2 */
2061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testRfc2396Abnormal() {
2071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g.", simplifyPath("/a/b/c/g."));
2081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/.g", simplifyPath("/a/b/c/.g"));
2091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g..", simplifyPath("/a/b/c/g.."));
2101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/..g", simplifyPath("/a/b/c/..g"));
2111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/g", simplifyPath("/a/b/c/./../g"));
2121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g", simplifyPath("/a/b/c/./g/."));
2131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g/h", simplifyPath("/a/b/c/g/./h"));
2141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/h", simplifyPath("/a/b/c/g/../h"));
2151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g;x=1/y", simplifyPath("/a/b/c/g;x=1/./y"));
2161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/y", simplifyPath("/a/b/c/g;x=1/../y"));
2171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
2181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /** http://gbiv.com/protocols/uri/rfc/rfc3986.html#relative-normal */
2201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testRfc3986Normal() {
2211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g", simplifyPath("/a/b/c/g"));
2221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g", simplifyPath("/a/b/c/./g"));
2231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g", simplifyPath("/a/b/c/g/"));
2241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g?y", simplifyPath("/a/b/c/g?y"));
2261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g#s", simplifyPath("/a/b/c/g#s"));
2271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g?y#s", simplifyPath("/a/b/c/g?y#s"));
2281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/;x", simplifyPath("/a/b/c/;x"));
2291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g;x", simplifyPath("/a/b/c/g;x"));
2301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g;x?y#s", simplifyPath("/a/b/c/g;x?y#s"));
2311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c", simplifyPath("/a/b/c/."));
2331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c", simplifyPath("/a/b/c/./"));
2341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b", simplifyPath("/a/b/c/.."));
2351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b", simplifyPath("/a/b/c/../"));
2361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/g", simplifyPath("/a/b/c/../g"));
2371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a", simplifyPath("/a/b/c/../.."));
2381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a", simplifyPath("/a/b/c/../../"));
2391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/g", simplifyPath("/a/b/c/../../g"));
2401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
2411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /** http://gbiv.com/protocols/uri/rfc/rfc3986.html#relative-abnormal */
2431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testRfc3986Abnormal() {
2441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/g", simplifyPath("/a/b/c/../../../g"));
2451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/g", simplifyPath("/a/b/c/../../../../g"));
2461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g.", simplifyPath("/a/b/c/g."));
2481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/.g", simplifyPath("/a/b/c/.g"));
2491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g..", simplifyPath("/a/b/c/g.."));
2501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/..g", simplifyPath("/a/b/c/..g"));
2511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/g", simplifyPath("/a/b/c/./../g"));
2521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g", simplifyPath("/a/b/c/./g/."));
2531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g/h", simplifyPath("/a/b/c/g/./h"));
2541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/h", simplifyPath("/a/b/c/g/../h"));
2551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/g;x=1/y", simplifyPath("/a/b/c/g;x=1/./y"));
2561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    assertEquals("/a/b/c/y", simplifyPath("/a/b/c/g;x=1/../y"));
2571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
2581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testExtensiveWithAbsolutePrefix() throws IOException {
2601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    // Inputs are /b/c/<every possible 10-character string of characters "a./">
2611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    // Expected outputs are from realpath -s.
2621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    doExtensiveTest("testdata/simplifypathwithabsoluteprefixtests.txt");
2631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
2641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  public void testExtensiveNoPrefix() throws IOException {
2661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    /*
2671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * Inputs are <every possible 10-character string of characters "a./">
2681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     *
2691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * Expected outputs are generated by the code itself, but they've been
2701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * checked against the inputs under Bash in order to confirm that the two
2711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * forms are equivalent (though not necessarily minimal, though we hope this
2721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * to be the case). Thus, this test is more of a regression test.
2731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     *
2741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * Rough instructions to regenerate the test outputs and verify correctness:
2751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * - Temporarily change this test:
2761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * --- Comment out assertEquals.
2771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * --- System.out.println(input + " " + simplifyPath(input));
2781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * --- fail(). (If the test were to pass, its output would be hidden.)
2791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * - Run the test.
2801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * - Pull the relevant lines of output from the test into a testcases file.
2811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * - Test the output:
2821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * --- cat testcases | while read L; do
2831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     *       X=($L)
2841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     *       A=$( cd /b/c && sudo mkdir -p ${X[0]} && cd ${X[0]} && pwd |
2851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     *           sed -e 's#^//*#/#' )
2861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     *       B=$( cd /b/c && cd ${X[1]} && pwd )
2871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     *       cmp -s <(echo $A) <(echo $B) || echo "$X[0] -> $A vs. $B"
2881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     *     done | tee testoutput
2891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * - Move that testcases file to the appropriate name under testdata.
2901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     *
2911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     * The last test will take hours, and if it passes, the output will be empty.
2921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert     */
2931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    doExtensiveTest("testdata/simplifypathnoprefixtests.txt");
2941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
2951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
2961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  private void doExtensiveTest(String resourceName) throws IOException {
2971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    Splitter splitter = Splitter.on(CharMatcher.WHITESPACE);
2981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    URL url = getClass().getResource(resourceName);
2991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    for (String line : Resources.readLines(url, UTF_8)) {
3001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      Iterator<String> iterator = splitter.split(line).iterator();
3011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      String input = iterator.next();
3021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      String expectedOutput = iterator.next();
3031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      assertFalse(iterator.hasNext());
3041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      assertEquals(expectedOutput, simplifyPath(input));
3051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    }
3061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
3071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert}
308