1 /* 2 * Copyright 2013 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.jimfs; 18 19 import static com.google.common.jimfs.PathNormalization.CASE_FOLD_ASCII; 20 import static com.google.common.jimfs.PathSubject.paths; 21 import static com.google.common.truth.Truth.assertAbout; 22 import static com.google.common.truth.Truth.assertThat; 23 24 import com.google.common.collect.ImmutableList; 25 import com.google.common.collect.ImmutableSet; 26 import java.io.IOException; 27 import java.net.URI; 28 import java.nio.file.FileSystem; 29 import java.nio.file.PathMatcher; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.junit.runners.JUnit4; 33 34 /** 35 * Tests for {@link PathService}. 36 * 37 * @author Colin Decker 38 */ 39 @RunWith(JUnit4.class) 40 public class PathServiceTest { 41 42 private static final ImmutableSet<PathNormalization> NO_NORMALIZATIONS = ImmutableSet.of(); 43 44 private final PathService service = fakeUnixPathService(); 45 46 @Test testBasicProperties()47 public void testBasicProperties() { 48 assertThat(service.getSeparator()).isEqualTo("/"); 49 assertThat(fakeWindowsPathService().getSeparator()).isEqualTo("\\"); 50 } 51 52 @Test testPathCreation()53 public void testPathCreation() { 54 assertAbout(paths()) 55 .that(service.emptyPath()) 56 .hasRootComponent(null) 57 .and() 58 .hasNameComponents(""); 59 60 assertAbout(paths()) 61 .that(service.createRoot(service.name("/"))) 62 .isAbsolute() 63 .and() 64 .hasRootComponent("/") 65 .and() 66 .hasNoNameComponents(); 67 68 assertAbout(paths()) 69 .that(service.createFileName(service.name("foo"))) 70 .hasRootComponent(null) 71 .and() 72 .hasNameComponents("foo"); 73 74 JimfsPath relative = service.createRelativePath(service.names(ImmutableList.of("foo", "bar"))); 75 assertAbout(paths()) 76 .that(relative) 77 .hasRootComponent(null) 78 .and() 79 .hasNameComponents("foo", "bar"); 80 81 JimfsPath absolute = 82 service.createPath(service.name("/"), service.names(ImmutableList.of("foo", "bar"))); 83 assertAbout(paths()) 84 .that(absolute) 85 .isAbsolute() 86 .and() 87 .hasRootComponent("/") 88 .and() 89 .hasNameComponents("foo", "bar"); 90 } 91 92 @Test testPathCreation_emptyPath()93 public void testPathCreation_emptyPath() { 94 // normalized to empty path with single empty string name 95 assertAbout(paths()) 96 .that(service.createPath(null, ImmutableList.<Name>of())) 97 .hasRootComponent(null) 98 .and() 99 .hasNameComponents(""); 100 } 101 102 @Test testPathCreation_parseIgnoresEmptyString()103 public void testPathCreation_parseIgnoresEmptyString() { 104 // if the empty string wasn't ignored, the resulting path would be "/foo" since the empty 105 // string would be joined with foo 106 assertAbout(paths()) 107 .that(service.parsePath("", "foo")) 108 .hasRootComponent(null) 109 .and() 110 .hasNameComponents("foo"); 111 } 112 113 @Test testToString()114 public void testToString() { 115 // not much to test for this since it just delegates to PathType anyway 116 JimfsPath path = 117 new JimfsPath(service, null, ImmutableList.of(Name.simple("foo"), Name.simple("bar"))); 118 assertThat(service.toString(path)).isEqualTo("foo/bar"); 119 120 path = new JimfsPath(service, Name.simple("/"), ImmutableList.of(Name.simple("foo"))); 121 assertThat(service.toString(path)).isEqualTo("/foo"); 122 } 123 124 @Test testHash_usingDisplayForm()125 public void testHash_usingDisplayForm() { 126 PathService pathService = fakePathService(PathType.unix(), false); 127 128 JimfsPath path1 = new JimfsPath(pathService, null, ImmutableList.of(Name.create("FOO", "foo"))); 129 JimfsPath path2 = new JimfsPath(pathService, null, ImmutableList.of(Name.create("FOO", "FOO"))); 130 JimfsPath path3 = 131 new JimfsPath( 132 pathService, null, ImmutableList.of(Name.create("FOO", "9874238974897189741"))); 133 134 assertThat(pathService.hash(path1)).isEqualTo(pathService.hash(path2)); 135 assertThat(pathService.hash(path2)).isEqualTo(pathService.hash(path3)); 136 } 137 138 @Test testHash_usingCanonicalForm()139 public void testHash_usingCanonicalForm() { 140 PathService pathService = fakePathService(PathType.unix(), true); 141 142 JimfsPath path1 = new JimfsPath(pathService, null, ImmutableList.of(Name.create("foo", "foo"))); 143 JimfsPath path2 = new JimfsPath(pathService, null, ImmutableList.of(Name.create("FOO", "foo"))); 144 JimfsPath path3 = 145 new JimfsPath( 146 pathService, null, ImmutableList.of(Name.create("28937497189478912374897", "foo"))); 147 148 assertThat(pathService.hash(path1)).isEqualTo(pathService.hash(path2)); 149 assertThat(pathService.hash(path2)).isEqualTo(pathService.hash(path3)); 150 } 151 152 @Test testCompareTo_usingDisplayForm()153 public void testCompareTo_usingDisplayForm() { 154 PathService pathService = fakePathService(PathType.unix(), false); 155 156 JimfsPath path1 = new JimfsPath(pathService, null, ImmutableList.of(Name.create("a", "z"))); 157 JimfsPath path2 = new JimfsPath(pathService, null, ImmutableList.of(Name.create("b", "y"))); 158 JimfsPath path3 = new JimfsPath(pathService, null, ImmutableList.of(Name.create("c", "x"))); 159 160 assertThat(pathService.compare(path1, path2)).isEqualTo(-1); 161 assertThat(pathService.compare(path2, path3)).isEqualTo(-1); 162 } 163 164 @Test testCompareTo_usingCanonicalForm()165 public void testCompareTo_usingCanonicalForm() { 166 PathService pathService = fakePathService(PathType.unix(), true); 167 168 JimfsPath path1 = new JimfsPath(pathService, null, ImmutableList.of(Name.create("a", "z"))); 169 JimfsPath path2 = new JimfsPath(pathService, null, ImmutableList.of(Name.create("b", "y"))); 170 JimfsPath path3 = new JimfsPath(pathService, null, ImmutableList.of(Name.create("c", "x"))); 171 172 assertThat(pathService.compare(path1, path2)).isEqualTo(1); 173 assertThat(pathService.compare(path2, path3)).isEqualTo(1); 174 } 175 176 @Test testPathMatcher()177 public void testPathMatcher() { 178 assertThat(service.createPathMatcher("regex:foo")) 179 .isInstanceOf(PathMatchers.RegexPathMatcher.class); 180 assertThat(service.createPathMatcher("glob:foo")) 181 .isInstanceOf(PathMatchers.RegexPathMatcher.class); 182 } 183 184 @Test testPathMatcher_usingCanonicalForm_usesCanonicalNormalizations()185 public void testPathMatcher_usingCanonicalForm_usesCanonicalNormalizations() { 186 // https://github.com/google/jimfs/issues/91 187 // This matches the behavior of Windows (the only built-in configuration that uses canonical 188 // form for equality). There, PathMatchers should do case-insensitive matching despite Windows 189 // not normalizing case for display. 190 assertCaseInsensitiveMatches( 191 new PathService( 192 PathType.unix(), NO_NORMALIZATIONS, ImmutableSet.of(CASE_FOLD_ASCII), true)); 193 assertCaseSensitiveMatches( 194 new PathService( 195 PathType.unix(), ImmutableSet.of(CASE_FOLD_ASCII), NO_NORMALIZATIONS, true)); 196 } 197 198 @Test testPathMatcher_usingDisplayForm_usesDisplayNormalizations()199 public void testPathMatcher_usingDisplayForm_usesDisplayNormalizations() { 200 assertCaseInsensitiveMatches( 201 new PathService( 202 PathType.unix(), ImmutableSet.of(CASE_FOLD_ASCII), NO_NORMALIZATIONS, false)); 203 assertCaseSensitiveMatches( 204 new PathService( 205 PathType.unix(), NO_NORMALIZATIONS, ImmutableSet.of(CASE_FOLD_ASCII), false)); 206 } 207 assertCaseInsensitiveMatches(PathService service)208 private static void assertCaseInsensitiveMatches(PathService service) { 209 ImmutableList<PathMatcher> matchers = 210 ImmutableList.of( 211 service.createPathMatcher("glob:foo"), service.createPathMatcher("glob:FOO")); 212 213 JimfsPath lowerCasePath = singleNamePath(service, "foo"); 214 JimfsPath upperCasePath = singleNamePath(service, "FOO"); 215 JimfsPath nonMatchingPath = singleNamePath(service, "bar"); 216 217 for (PathMatcher matcher : matchers) { 218 assertThat(matcher.matches(lowerCasePath)).isTrue(); 219 assertThat(matcher.matches(upperCasePath)).isTrue(); 220 assertThat(matcher.matches(nonMatchingPath)).isFalse(); 221 } 222 } 223 assertCaseSensitiveMatches(PathService service)224 private static void assertCaseSensitiveMatches(PathService service) { 225 PathMatcher matcher = service.createPathMatcher("glob:foo"); 226 227 JimfsPath lowerCasePath = singleNamePath(service, "foo"); 228 JimfsPath upperCasePath = singleNamePath(service, "FOO"); 229 230 assertThat(matcher.matches(lowerCasePath)).isTrue(); 231 assertThat(matcher.matches(upperCasePath)).isFalse(); 232 } 233 fakeUnixPathService()234 public static PathService fakeUnixPathService() { 235 return fakePathService(PathType.unix(), false); 236 } 237 fakeWindowsPathService()238 public static PathService fakeWindowsPathService() { 239 return fakePathService(PathType.windows(), false); 240 } 241 fakePathService(PathType type, boolean equalityUsesCanonicalForm)242 public static PathService fakePathService(PathType type, boolean equalityUsesCanonicalForm) { 243 PathService service = 244 new PathService(type, NO_NORMALIZATIONS, NO_NORMALIZATIONS, equalityUsesCanonicalForm); 245 service.setFileSystem(FILE_SYSTEM); 246 return service; 247 } 248 singleNamePath(PathService service, String name)249 private static JimfsPath singleNamePath(PathService service, String name) { 250 return new JimfsPath(service, null, ImmutableList.of(Name.create(name, name))); 251 } 252 253 private static final FileSystem FILE_SYSTEM; 254 255 static { 256 try { 257 FILE_SYSTEM = 258 JimfsFileSystems.newFileSystem( 259 new JimfsFileSystemProvider(), URI.create("jimfs://foo"), Configuration.unix()); 260 } catch (IOException e) { 261 throw new AssertionError(e); 262 } 263 } 264 } 265