2020import static com .google .cloud .storage .contrib .nio .CloudStorageFileSystem .forBucket ;
2121import static com .google .common .truth .Truth .assertThat ;
2222import static java .nio .charset .StandardCharsets .UTF_8 ;
23+ import static java .nio .file .Files .readAllBytes ;
2324import static java .nio .file .StandardCopyOption .ATOMIC_MOVE ;
2425import static java .nio .file .StandardCopyOption .COPY_ATTRIBUTES ;
2526import static java .nio .file .StandardCopyOption .REPLACE_EXISTING ;
3637import com .google .cloud .storage .Acl ;
3738import com .google .cloud .storage .Acl .User ;
3839import com .google .cloud .storage .contrib .nio .testing .LocalStorageHelper ;
39- import com .google .cloud .testing .junit4 .MultipleAttemptsRule ;
4040import com .google .common .base .Optional ;
4141import com .google .common .collect .ImmutableList ;
4242import com .google .common .testing .NullPointerTester ;
7171import org .junit .Before ;
7272import org .junit .Rule ;
7373import org .junit .Test ;
74+ import org .junit .rules .TemporaryFolder ;
7475import org .junit .runner .RunWith ;
7576import org .junit .runners .JUnit4 ;
7677
7778/** Unit tests for {@link CloudStorageFileSystemProvider}. */
7879@ RunWith (JUnit4 .class )
7980public class CloudStorageFileSystemProviderTest {
80- @ Rule public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule (3 );
81+ // @Rule(order = 1) public final MultipleAttemptsRule multipleAttemptsRule = new
82+ // MultipleAttemptsRule(3);
83+ @ Rule (order = 1 )
84+ public final TemporaryFolder temporaryFolder = new TemporaryFolder ();
8185
8286 private static final List <String > FILE_CONTENTS =
8387 ImmutableList .of (
@@ -153,14 +157,14 @@ public void testSize_trailingSlash_disablePseudoDirectories() throws Exception {
153157 public void testReadAllBytes () throws Exception {
154158 Path path = Paths .get (URI .create ("gs://bucket/wat" ));
155159 Files .write (path , SINGULARITY .getBytes (UTF_8 ));
156- assertThat (new String (Files . readAllBytes (path ), UTF_8 )).isEqualTo (SINGULARITY );
160+ assertThat (new String (readAllBytes (path ), UTF_8 )).isEqualTo (SINGULARITY );
157161 Files .delete (path );
158162 }
159163
160164 @ Test
161165 public void testReadAllBytes_trailingSlash () throws Exception {
162166 try {
163- Files . readAllBytes (Paths .get (URI .create ("gs://bucket/wat/" )));
167+ readAllBytes (Paths .get (URI .create ("gs://bucket/wat/" )));
164168 Assert .fail ();
165169 } catch (CloudStoragePseudoDirectoryException ex ) {
166170 assertThat (ex .getMessage ()).isNotNull ();
@@ -255,7 +259,7 @@ public void testNewByteChannelWrite() throws Exception {
255259 assertThat (output .position ()).isEqualTo (10 );
256260 assertThat (output .size ()).isEqualTo (10 );
257261 }
258- assertThat (new String (Files . readAllBytes (path ), UTF_8 )).isEqualTo ("fileconten" );
262+ assertThat (new String (readAllBytes (path ), UTF_8 )).isEqualTo ("fileconten" );
259263 }
260264
261265 @ Test
@@ -298,7 +302,7 @@ public void testNewOutputStream() throws Exception {
298302 try (OutputStream output = Files .newOutputStream (path )) {
299303 output .write (SINGULARITY .getBytes (UTF_8 ));
300304 }
301- assertThat (new String (Files . readAllBytes (path ), UTF_8 )).isEqualTo (SINGULARITY );
305+ assertThat (new String (readAllBytes (path ), UTF_8 )).isEqualTo (SINGULARITY );
302306 }
303307
304308 @ Test
@@ -309,7 +313,7 @@ public void testNewOutputStream_truncateByDefault() throws Exception {
309313 try (OutputStream output = Files .newOutputStream (path )) {
310314 output .write (SINGULARITY .getBytes (UTF_8 ));
311315 }
312- assertThat (new String (Files . readAllBytes (path ), UTF_8 )).isEqualTo (SINGULARITY );
316+ assertThat (new String (readAllBytes (path ), UTF_8 )).isEqualTo (SINGULARITY );
313317 }
314318
315319 @ Test
@@ -320,7 +324,7 @@ public void testNewOutputStream_truncateExplicitly() throws Exception {
320324 try (OutputStream output = Files .newOutputStream (path , TRUNCATE_EXISTING )) {
321325 output .write (SINGULARITY .getBytes (UTF_8 ));
322326 }
323- assertThat (new String (Files . readAllBytes (path ), UTF_8 )).isEqualTo (SINGULARITY );
327+ assertThat (new String (readAllBytes (path ), UTF_8 )).isEqualTo (SINGULARITY );
324328 }
325329
326330 @ Test
@@ -577,7 +581,7 @@ public void testCopy() throws Exception {
577581 Path target = Paths .get (URI .create ("gs://greenbean/adipose" ));
578582 Files .write (source , "(✿◕ ‿◕ )ノ" .getBytes (UTF_8 ));
579583 Files .copy (source , target );
580- assertThat (new String (Files . readAllBytes (target ), UTF_8 )).isEqualTo ("(✿◕ ‿◕ )ノ" );
584+ assertThat (new String (readAllBytes (target ), UTF_8 )).isEqualTo ("(✿◕ ‿◕ )ノ" );
581585 assertThat (Files .exists (source )).isTrue ();
582586 assertThat (Files .exists (target )).isTrue ();
583587 }
@@ -641,7 +645,7 @@ public void testMove() throws Exception {
641645 Path target = Paths .get (URI .create ("gs://greenbean/adipose" ));
642646 Files .write (source , "(✿◕ ‿◕ )ノ" .getBytes (UTF_8 ));
643647 Files .move (source , target );
644- assertThat (new String (Files . readAllBytes (target ), UTF_8 )).isEqualTo ("(✿◕ ‿◕ )ノ" );
648+ assertThat (new String (readAllBytes (target ), UTF_8 )).isEqualTo ("(✿◕ ‿◕ )ノ" );
645649 assertThat (Files .exists (source )).isFalse ();
646650 assertThat (Files .exists (target )).isTrue ();
647651 }
@@ -763,6 +767,19 @@ public void testCopy_overwriteAttributes() throws Exception {
763767 Files .delete (target2 );
764768 }
765769
770+ @ Test
771+ public void testCopy_path_toLocalFileSystem () throws IOException {
772+ Path source = Paths .get (URI .create ("gs://mybucket/myobject" ));
773+ byte [] helloWorldBytes = "Hello, World!" .getBytes (UTF_8 );
774+ Files .write (source , helloWorldBytes );
775+
776+ Path path = temporaryFolder .newFile ().toPath ();
777+ // The new file created by temporaryFolder is an empty file on disk, specify REPLACE_EXISTING
778+ // so we can overwrite its contents.
779+ Files .copy (source , path , REPLACE_EXISTING );
780+ assertThat (Files .readAllBytes (path )).isEqualTo (helloWorldBytes );
781+ }
782+
766783 @ Test
767784 public void testNullness () throws Exception {
768785 try (FileSystem fs = FileSystems .getFileSystem (URI .create ("gs://blood" ))) {
0 commit comments