final CopyOption... options ) { final InputStream in = newInputStream( source, convert( options ) ); final SeekableByteChannel out = newByteChannel( target, new HashSet<OpenOption>() {{ add( StandardOpenOption.TRUNCATE_EXISTING );
@Test public void testDeleteBranchIfExists() throws IOException { final URI newRepo = URI.create( "git://deletebranchifexists1-test-repo" ); PROVIDER.newFileSystem( newRepo, EMPTY_ENV ); final Path path = PROVIDER.getPath( URI.create( "git://user_branch@deletebranchifexists1-test-repo/path/to/myfile.txt" ) ); final OutputStream outStream = PROVIDER.newOutputStream( path ); assertThat( outStream ).isNotNull(); outStream.write( "my cool content".getBytes() ); outStream.close(); PROVIDER.newInputStream( path ).close(); assertThat( PROVIDER.deleteIfExists( PROVIDER.getPath( URI.create( "git://user_branch@deletebranchifexists1-test-repo" ) ) ) ).isTrue(); assertThat( PROVIDER.deleteIfExists( PROVIDER.getPath( URI.create( "git://not_user_branch@deletebranchifexists1-test-repo" ) ) ) ).isFalse(); assertThat( PROVIDER.deleteIfExists( PROVIDER.getPath( URI.create( "git://user_branch@deletebranchifexists1-test-repo" ) ) ) ).isFalse(); }
@Test public void testDeleteBranch() throws IOException { final URI newRepo = URI.create( "git://delete-branch-test-repo" ); PROVIDER.newFileSystem( newRepo, EMPTY_ENV ); final Path path = PROVIDER.getPath( URI.create( "git://user_branch@delete-branch-test-repo/path/to/myfile.txt" ) ); final OutputStream outStream = PROVIDER.newOutputStream( path ); assertThat( outStream ).isNotNull(); outStream.write( "my cool content".getBytes() ); outStream.close(); PROVIDER.newInputStream( path ).close(); PROVIDER.delete( PROVIDER.getPath( URI.create( "git://user_branch@delete-branch-test-repo" ) ) ); try { PROVIDER.delete( PROVIDER.getPath( URI.create( "git://user_branch@delete-branch-test-repo" ) ) ); failBecauseExceptionWasNotThrown( NoSuchFileException.class ); } catch ( NoSuchFileException ex ) { } try { PROVIDER.delete( PROVIDER.getPath( URI.create( "git://some_user_branch@delete-branch-test-repo" ) ) ); failBecauseExceptionWasNotThrown( NoSuchFileException.class ); } catch ( NoSuchFileException ex ) { } }
@Test public void testDeleteIfExists() throws IOException { final URI newRepo = URI.create( "git://deleteifexists1-test-repo" ); PROVIDER.newFileSystem( newRepo, EMPTY_ENV ); final Path path = PROVIDER.getPath( URI.create( "git://user_branch@deleteifexists1-test-repo/path/to/myfile.txt" ) ); final OutputStream outStream = PROVIDER.newOutputStream( path ); assertThat( outStream ).isNotNull(); outStream.write( "my cool content".getBytes() ); outStream.close(); PROVIDER.newInputStream( path ).close(); assertThat( PROVIDER.deleteIfExists( PROVIDER.getPath( URI.create( "git://user_branch@deleteifexists1-test-repo/non_existent_path" ) ) ) ).isFalse(); try { PROVIDER.deleteIfExists( PROVIDER.getPath( URI.create( "git://user_branch@deleteifexists1-test-repo/path/to/" ) ) ); failBecauseExceptionWasNotThrown( DirectoryNotEmptyException.class ); } catch ( DirectoryNotEmptyException ex ) { } assertThat( PROVIDER.deleteIfExists( path ) ).isTrue(); }
@Test public void testInputStream() throws IOException { final File parentFolder = createTempDirectory(); final File gitFolder = new File( parentFolder, "mytest.git" ); final Git origin = JGitUtil.newRepository( gitFolder, true ); commit( origin, "master", "user", "user@example.com", "commit message", null, null, false, new HashMap<String, File>() {{ put( "myfile.txt", tempFile( "temp\n.origin\n.content" ) ); }} ); final URI newRepo = URI.create( "git://inputstream-test-repo" ); final Map<String, Object> env = new HashMap<String, Object>() {{ put( JGitFileSystemProvider.GIT_DEFAULT_REMOTE_NAME, origin.getRepository().getDirectory().toString() ); }}; final FileSystem fs = PROVIDER.newFileSystem( newRepo, env ); assertThat( fs ).isNotNull(); final Path path = PROVIDER.getPath( URI.create( "git://origin/master@inputstream-test-repo/myfile.txt" ) ); final InputStream inputStream = PROVIDER.newInputStream( path ); assertThat( inputStream ).isNotNull(); final String content = new Scanner( inputStream ).useDelimiter( "\\A" ).next(); inputStream.close(); assertThat( content ).isNotNull().isEqualTo( "temp\n.origin\n.content" ); }
@Test public void testInputStream2() throws IOException { final File parentFolder = createTempDirectory(); final File gitFolder = new File( parentFolder, "mytest.git" ); final Git origin = JGitUtil.newRepository( gitFolder, true ); commit( origin, "master", "user", "user@example.com", "commit message", null, null, false, new HashMap<String, File>() {{ put( "path/to/file/myfile.txt", tempFile( "temp\n.origin\n.content" ) ); }} ); final URI newRepo = URI.create( "git://xinputstream-test-repo" ); final Map<String, Object> env = new HashMap<String, Object>() {{ put( JGitFileSystemProvider.GIT_DEFAULT_REMOTE_NAME, origin.getRepository().getDirectory().toString() ); }}; final FileSystem fs = PROVIDER.newFileSystem( newRepo, env ); assertThat( fs ).isNotNull(); final Path path = PROVIDER.getPath( URI.create( "git://origin/master@xinputstream-test-repo/path/to/file/myfile.txt" ) ); final InputStream inputStream = PROVIDER.newInputStream( path ); assertThat( inputStream ).isNotNull(); final String content = new Scanner( inputStream ).useDelimiter( "\\A" ).next(); inputStream.close(); assertThat( content ).isNotNull().isEqualTo( "temp\n.origin\n.content" ); }
@Test public void testNewOutputStreamWithJGitOp() throws Exception { final File parentFolder = createTempDirectory(); final File gitFolder = new File( parentFolder, "mytest.git" ); final Git origin = JGitUtil.newRepository( gitFolder, true ); commit( origin, "master", "user", "user@example.com", "commit message", null, null, false, new HashMap<String, File>() {{ put( "myfile.txt", tempFile( "temp\n.origin\n.content" ) ); }} ); commit( origin, "user_branch", "user", "user@example.com", "commit message", null, null, false, new HashMap<String, File>() {{ put( "path/to/some/file/myfile.txt", tempFile( "some\n.content\nhere" ) ); }} ); final URI newRepo = URI.create( "git://outstreamwithop-test-repo" ); final Map<String, Object> env = new HashMap<String, Object>() {{ put( JGitFileSystemProvider.GIT_DEFAULT_REMOTE_NAME, origin.getRepository().getDirectory().toString() ); }}; final FileSystem fs = PROVIDER.newFileSystem( newRepo, env ); assertThat( fs ).isNotNull(); final SimpleDateFormat formatter = new SimpleDateFormat( "dd/MM/yyyy" ); final CommentedOption op = new CommentedOption( "User Tester", "user.tester@example.com", "omg, is it the end?", formatter.parse( "31/12/2012" ) ); final Path path = PROVIDER.getPath( URI.create( "git://user_branch@outstreamwithop-test-repo/some/path/myfile.txt" ) ); final OutputStream outStream = PROVIDER.newOutputStream( path, op ); assertThat( outStream ).isNotNull(); outStream.write( "my cool content".getBytes() ); outStream.close(); final InputStream inStream = PROVIDER.newInputStream( path ); final String content = new Scanner( inStream ).useDelimiter( "\\A" ).next(); inStream.close(); assertThat( content ).isNotNull().isEqualTo( "my cool content" ); }
@Test(expected = NoSuchFileException.class) public void testInputStream3() throws IOException { final File parentFolder = createTempDirectory(); final File gitFolder = new File( parentFolder, "mytest.git" ); final Git origin = JGitUtil.newRepository( gitFolder, true ); commit( origin, "master", "user", "user@example.com", "commit message", null, null, false, new HashMap<String, File>() {{ put( "path/to/file/myfile.txt", tempFile( "temp\n.origin\n.content" ) ); }} ); final URI newRepo = URI.create( "git://xxinputstream-test-repo" ); final Map<String, Object> env = new HashMap<String, Object>() {{ put( JGitFileSystemProvider.GIT_DEFAULT_REMOTE_NAME, origin.getRepository().getDirectory().toString() ); }}; final FileSystem fs = PROVIDER.newFileSystem( newRepo, env ); assertThat( fs ).isNotNull(); final Path path = PROVIDER.getPath( URI.create( "git://origin/master@xxinputstream-test-repo/path/to" ) ); PROVIDER.newInputStream( path ); }
@Test(expected = NoSuchFileException.class) public void testInputStreamNoSuchFile() throws IOException { final File parentFolder = createTempDirectory(); final File gitFolder = new File( parentFolder, "mytest.git" ); final Git origin = JGitUtil.newRepository( gitFolder, true ); commit( origin, "master", "user1", "user1@example.com", "commitx", null, null, false, new HashMap<String, File>() {{ put( "file.txt", tempFile( "temp.origin.content.2" ) ); }} ); final URI newRepo = URI.create( "git://inputstream-not-exists-test-repo" ); final Map<String, Object> env = new HashMap<String, Object>() {{ put( JGitFileSystemProvider.GIT_DEFAULT_REMOTE_NAME, origin.getRepository().getDirectory().toString() ); }}; final FileSystem fs = PROVIDER.newFileSystem( newRepo, env ); assertThat( fs ).isNotNull(); final Path path = PROVIDER.getPath( URI.create( "git://origin/master@inputstream-not-exists-test-repo/temp.txt" ) ); PROVIDER.newInputStream( path ); }
outStream.close(); final InputStream inStream = PROVIDER.newInputStream( path );
outStream.close(); PROVIDER.newInputStream( path ).close();