1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
/*
* Copyright (C) 2009 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.common.io;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.io.Files.simplifyPath;
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import junit.framework.TestCase;
/**
* Unit tests for {@link Files#simplifyPath}.
*
* @author Pablo Bellver
*/
public class FilesSimplifyPathTest extends TestCase {
public void testSimplifyEmptyString() {
assertEquals(".", simplifyPath(""));
}
public void testSimplifyDot() {
assertEquals(".", simplifyPath("."));
}
public void testSimplifyWhiteSpace() {
assertEquals(" ", simplifyPath(" "));
}
public void testSimplify2() {
assertEquals("x", simplifyPath("x"));
}
public void testSimplify3() {
assertEquals("/a/b/c/d", simplifyPath("/a/b/c/d"));
}
public void testSimplify4() {
assertEquals("/a/b/c/d", simplifyPath("/a/b/c/d/"));
}
public void testSimplify5() {
assertEquals("/a/b", simplifyPath("/a//b"));
}
public void testSimplify6() {
assertEquals("/a/b", simplifyPath("//a//b/"));
}
public void testSimplify7() {
assertEquals("/", simplifyPath("/.."));
}
public void testSimplify8() {
assertEquals("/", simplifyPath("/././././"));
}
public void testSimplify9() {
assertEquals("/a", simplifyPath("/a/b/.."));
}
public void testSimplify10() {
assertEquals("/", simplifyPath("/a/b/../../.."));
}
public void testSimplify11() {
assertEquals("/", simplifyPath("//a//b/..////../..//"));
}
public void testSimplify12() {
assertEquals("/x", simplifyPath("//a//../x//"));
}
public void testSimplify13() {
assertEquals("../c", simplifyPath("a/b/../../../c"));
}
public void testSimplifyDotDot() {
assertEquals("..", simplifyPath(".."));
}
public void testSimplifyDotDotSlash() {
assertEquals("..", simplifyPath("../"));
assertEquals("..", simplifyPath("a/../.."));
assertEquals("..", simplifyPath("a/../../"));
}
public void testSimplifyDotDots() {
assertEquals("../..", simplifyPath("a/../../.."));
assertEquals("../../..", simplifyPath("a/../../../.."));
}
public void testSimplifyRootedDotDots() {
assertEquals("/", simplifyPath("/../../.."));
assertEquals("/", simplifyPath("/../../../"));
}
// b/4558855
public void testMadbotsBug() {
assertEquals("../this", simplifyPath("../this"));
assertEquals("../this/is/ok", simplifyPath("../this/is/ok"));
assertEquals("../ok", simplifyPath("../this/../ok"));
}
// https://code.google.com/p/guava-libraries/issues/detail?id=705
public void test705() {
assertEquals("../b", simplifyPath("x/../../b"));
assertEquals("b", simplifyPath("x/../b"));
}
// https://code.google.com/p/guava-libraries/issues/detail?id=716
public void test716() {
assertEquals("b", simplifyPath("./b"));
assertEquals("b", simplifyPath("./b/."));
assertEquals("b", simplifyPath("././b/./."));
assertEquals("b", simplifyPath("././b"));
assertEquals("a/b", simplifyPath("./a/b"));
}
public void testHiddenFiles() {
assertEquals(".b", simplifyPath(".b"));
assertEquals(".b", simplifyPath("./.b"));
assertEquals(".metadata/b", simplifyPath(".metadata/b"));
assertEquals(".metadata/b", simplifyPath("./.metadata/b"));
}
// https://code.google.com/p/guava-libraries/issues/detail?id=716
public void testMultipleDotFilenames() {
assertEquals("..a", simplifyPath("..a"));
assertEquals("/..a", simplifyPath("/..a"));
assertEquals("/..a/..b", simplifyPath("/..a/..b"));
assertEquals("/.....a/..b", simplifyPath("/.....a/..b"));
assertEquals("..../....", simplifyPath("..../...."));
assertEquals("..a../..b..", simplifyPath("..a../..b.."));
}
public void testSlashDot() {
assertEquals("/", simplifyPath("/."));
}
// http://code.google.com/p/guava-libraries/issues/detail?id=722
public void testInitialSlashDotDot() {
assertEquals("/c", simplifyPath("/../c"));
}
// http://code.google.com/p/guava-libraries/issues/detail?id=722
public void testInitialSlashDot() {
assertEquals("/a", simplifyPath("/./a"));
assertEquals("/.a", simplifyPath("/.a/a/.."));
}
// http://code.google.com/p/guava-libraries/issues/detail?id=722
public void testConsecutiveParentsAfterPresent() {
assertEquals("../..", simplifyPath("./../../"));
assertEquals("../..", simplifyPath("./.././../"));
}
/*
* We co-opt some URI resolution tests for our purposes.
* Some of the tests have queries and anchors that are a little silly here.
*/
/** http://gbiv.com/protocols/uri/rfc/rfc2396.html#rfc.section.C.1 */
public void testRfc2396Normal() {
assertEquals("/a/b/c/g", simplifyPath("/a/b/c/g"));
assertEquals("/a/b/c/g", simplifyPath("/a/b/c/./g"));
assertEquals("/a/b/c/g", simplifyPath("/a/b/c/g/"));
assertEquals("/a/b/c/g?y", simplifyPath("/a/b/c/g?y"));
assertEquals("/a/b/c/g#s", simplifyPath("/a/b/c/g#s"));
assertEquals("/a/b/c/g?y#s", simplifyPath("/a/b/c/g?y#s"));
assertEquals("/a/b/c/;x", simplifyPath("/a/b/c/;x"));
assertEquals("/a/b/c/g;x", simplifyPath("/a/b/c/g;x"));
assertEquals("/a/b/c/g;x?y#s", simplifyPath("/a/b/c/g;x?y#s"));
assertEquals("/a/b/c", simplifyPath("/a/b/c/."));
assertEquals("/a/b/c", simplifyPath("/a/b/c/./"));
assertEquals("/a/b", simplifyPath("/a/b/c/.."));
assertEquals("/a/b", simplifyPath("/a/b/c/../"));
assertEquals("/a/b/g", simplifyPath("/a/b/c/../g"));
assertEquals("/a", simplifyPath("/a/b/c/../.."));
assertEquals("/a", simplifyPath("/a/b/c/../../"));
assertEquals("/a/g", simplifyPath("/a/b/c/../../g"));
}
/** http://gbiv.com/protocols/uri/rfc/rfc2396.html#rfc.section.C.2 */
public void testRfc2396Abnormal() {
assertEquals("/a/b/c/g.", simplifyPath("/a/b/c/g."));
assertEquals("/a/b/c/.g", simplifyPath("/a/b/c/.g"));
assertEquals("/a/b/c/g..", simplifyPath("/a/b/c/g.."));
assertEquals("/a/b/c/..g", simplifyPath("/a/b/c/..g"));
assertEquals("/a/b/g", simplifyPath("/a/b/c/./../g"));
assertEquals("/a/b/c/g", simplifyPath("/a/b/c/./g/."));
assertEquals("/a/b/c/g/h", simplifyPath("/a/b/c/g/./h"));
assertEquals("/a/b/c/h", simplifyPath("/a/b/c/g/../h"));
assertEquals("/a/b/c/g;x=1/y", simplifyPath("/a/b/c/g;x=1/./y"));
assertEquals("/a/b/c/y", simplifyPath("/a/b/c/g;x=1/../y"));
}
/** http://gbiv.com/protocols/uri/rfc/rfc3986.html#relative-normal */
public void testRfc3986Normal() {
assertEquals("/a/b/c/g", simplifyPath("/a/b/c/g"));
assertEquals("/a/b/c/g", simplifyPath("/a/b/c/./g"));
assertEquals("/a/b/c/g", simplifyPath("/a/b/c/g/"));
assertEquals("/a/b/c/g?y", simplifyPath("/a/b/c/g?y"));
assertEquals("/a/b/c/g#s", simplifyPath("/a/b/c/g#s"));
assertEquals("/a/b/c/g?y#s", simplifyPath("/a/b/c/g?y#s"));
assertEquals("/a/b/c/;x", simplifyPath("/a/b/c/;x"));
assertEquals("/a/b/c/g;x", simplifyPath("/a/b/c/g;x"));
assertEquals("/a/b/c/g;x?y#s", simplifyPath("/a/b/c/g;x?y#s"));
assertEquals("/a/b/c", simplifyPath("/a/b/c/."));
assertEquals("/a/b/c", simplifyPath("/a/b/c/./"));
assertEquals("/a/b", simplifyPath("/a/b/c/.."));
assertEquals("/a/b", simplifyPath("/a/b/c/../"));
assertEquals("/a/b/g", simplifyPath("/a/b/c/../g"));
assertEquals("/a", simplifyPath("/a/b/c/../.."));
assertEquals("/a", simplifyPath("/a/b/c/../../"));
assertEquals("/a/g", simplifyPath("/a/b/c/../../g"));
}
/** http://gbiv.com/protocols/uri/rfc/rfc3986.html#relative-abnormal */
public void testRfc3986Abnormal() {
assertEquals("/g", simplifyPath("/a/b/c/../../../g"));
assertEquals("/g", simplifyPath("/a/b/c/../../../../g"));
assertEquals("/a/b/c/g.", simplifyPath("/a/b/c/g."));
assertEquals("/a/b/c/.g", simplifyPath("/a/b/c/.g"));
assertEquals("/a/b/c/g..", simplifyPath("/a/b/c/g.."));
assertEquals("/a/b/c/..g", simplifyPath("/a/b/c/..g"));
assertEquals("/a/b/g", simplifyPath("/a/b/c/./../g"));
assertEquals("/a/b/c/g", simplifyPath("/a/b/c/./g/."));
assertEquals("/a/b/c/g/h", simplifyPath("/a/b/c/g/./h"));
assertEquals("/a/b/c/h", simplifyPath("/a/b/c/g/../h"));
assertEquals("/a/b/c/g;x=1/y", simplifyPath("/a/b/c/g;x=1/./y"));
assertEquals("/a/b/c/y", simplifyPath("/a/b/c/g;x=1/../y"));
}
public void testExtensiveWithAbsolutePrefix() throws IOException {
// Inputs are /b/c/<every possible 10-character string of characters "a./">
// Expected outputs are from realpath -s.
doExtensiveTest("testdata/simplifypathwithabsoluteprefixtests.txt");
}
public void testExtensiveNoPrefix() throws IOException {
/*
* Inputs are <every possible 10-character string of characters "a./">
*
* Expected outputs are generated by the code itself, but they've been
* checked against the inputs under Bash in order to confirm that the two
* forms are equivalent (though not necessarily minimal, though we hope this
* to be the case). Thus, this test is more of a regression test.
*
* Rough instructions to regenerate the test outputs and verify correctness:
* - Temporarily change this test:
* --- Comment out assertEquals.
* --- System.out.println(input + " " + simplifyPath(input));
* --- fail(). (If the test were to pass, its output would be hidden.)
* - Run the test.
* - Pull the relevant lines of output from the test into a testcases file.
* - Test the output:
* --- cat testcases | while read L; do
* X=($L)
* A=$( cd /b/c && sudo mkdir -p ${X[0]} && cd ${X[0]} && pwd |
* sed -e 's#^//*#/#' )
* B=$( cd /b/c && cd ${X[1]} && pwd )
* cmp -s <(echo $A) <(echo $B) || echo "$X[0] -> $A vs. $B"
* done | tee testoutput
* - Move that testcases file to the appropriate name under testdata.
*
* The last test will take hours, and if it passes, the output will be empty.
*/
doExtensiveTest("testdata/simplifypathnoprefixtests.txt");
}
private void doExtensiveTest(String resourceName) throws IOException {
Splitter splitter = Splitter.on(CharMatcher.whitespace());
URL url = getClass().getResource(resourceName);
for (String line : Resources.readLines(url, UTF_8)) {
Iterator<String> iterator = splitter.split(line).iterator();
String input = iterator.next();
String expectedOutput = iterator.next();
assertFalse(iterator.hasNext());
assertEquals(expectedOutput, simplifyPath(input));
}
}
}
|