• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.PathSubject.paths;
20 import static com.google.common.truth.Truth.assertThat;
21 import static com.google.common.truth.Truth.assert_;
22 
23 import java.io.IOException;
24 import java.nio.file.FileSystem;
25 import java.nio.file.Files;
26 import java.nio.file.LinkOption;
27 import java.nio.file.Path;
28 import java.nio.file.attribute.BasicFileAttributes;
29 import java.nio.file.attribute.FileTime;
30 import org.junit.After;
31 import org.junit.Before;
32 
33 /** @author Colin Decker */
34 public abstract class AbstractJimfsIntegrationTest {
35 
36   protected FileSystem fs;
37 
38   @Before
setUp()39   public void setUp() throws IOException {
40     fs = createFileSystem();
41   }
42 
43   @After
tearDown()44   public void tearDown() throws IOException {
45     fs.close();
46   }
47 
48   /** Creates the file system to use in the tests. */
createFileSystem()49   protected abstract FileSystem createFileSystem();
50 
51   // helpers
52 
path(String first, String... more)53   protected Path path(String first, String... more) {
54     return fs.getPath(first, more);
55   }
56 
getFileKey(String path, LinkOption... options)57   protected Object getFileKey(String path, LinkOption... options) throws IOException {
58     return Files.getAttribute(path(path), "fileKey", options);
59   }
60 
assertThatPath(String path, LinkOption... options)61   protected PathSubject assertThatPath(String path, LinkOption... options) {
62     return assertThatPath(path(path), options);
63   }
64 
assertThatPath(Path path, LinkOption... options)65   protected static PathSubject assertThatPath(Path path, LinkOption... options) {
66     PathSubject subject = assert_().about(paths()).that(path);
67     if (options.length != 0) {
68       subject = subject.noFollowLinks();
69     }
70     return subject;
71   }
72 
73   /** Tester for testing changes in file times. */
74   protected static final class FileTimeTester {
75 
76     private final Path path;
77 
78     private FileTime accessTime;
79     private FileTime modifiedTime;
80 
FileTimeTester(Path path)81     FileTimeTester(Path path) throws IOException {
82       this.path = path;
83 
84       BasicFileAttributes attrs = attrs();
85       accessTime = attrs.lastAccessTime();
86       modifiedTime = attrs.lastModifiedTime();
87     }
88 
attrs()89     private BasicFileAttributes attrs() throws IOException {
90       return Files.readAttributes(path, BasicFileAttributes.class);
91     }
92 
assertAccessTimeChanged()93     public void assertAccessTimeChanged() throws IOException {
94       FileTime t = attrs().lastAccessTime();
95       assertThat(t).isNotEqualTo(accessTime);
96       accessTime = t;
97     }
98 
assertAccessTimeDidNotChange()99     public void assertAccessTimeDidNotChange() throws IOException {
100       FileTime t = attrs().lastAccessTime();
101       assertThat(t).isEqualTo(accessTime);
102     }
103 
assertModifiedTimeChanged()104     public void assertModifiedTimeChanged() throws IOException {
105       FileTime t = attrs().lastModifiedTime();
106       assertThat(t).isNotEqualTo(modifiedTime);
107       modifiedTime = t;
108     }
109 
assertModifiedTimeDidNotChange()110     public void assertModifiedTimeDidNotChange() throws IOException {
111       FileTime t = attrs().lastModifiedTime();
112       assertThat(t).isEqualTo(modifiedTime);
113     }
114   }
115 }
116