@Test public void testCopyFiles() throws IOException { final URI newRepo = URI.create( "git://copyasset-test-repo" ); PROVIDER.newFileSystem( newRepo, EMPTY_ENV ); final Path path = PROVIDER.getPath( URI.create( "git://master@copyasset-test-repo/myfile1.txt" ) ); { final OutputStream outStream = PROVIDER.newOutputStream( path ); outStream.write( "my cool content".getBytes() ); outStream.close(); } final Path path2 = PROVIDER.getPath( URI.create( "git://user_branch@copyasset-test-repo/other/path/myfile2.txt" ) ); { final OutputStream outStream2 = PROVIDER.newOutputStream( path2 ); outStream2.write( "my cool content".getBytes() ); outStream2.close(); } final Path path3 = PROVIDER.getPath( URI.create( "git://user_branch@copyasset-test-repo/myfile3.txt" ) ); { final OutputStream outStream3 = PROVIDER.newOutputStream( path3 ); outStream3.write( "my cool content".getBytes() ); outStream3.close(); } final Path target = PROVIDER.getPath( URI.create( "git://user_branch@copyasset-test-repo/myfile1.txt" ) ); PROVIDER.copy( path, target ); final DirectoryStream<Path> stream = PROVIDER.newDirectoryStream( PROVIDER.getPath( URI.create( "git://user_branch@copyasset-test-repo/" ) ), null ); for ( Path path1 : stream ) { System.out.println("content: " + path1.toUri()); } assertThat( stream ).isNotNull().hasSize( 3 ); }
@Test public void testDeleteNonEmptyDirectory() throws IOException { final URI newRepo = URI.create( "git://delete-non-empty-test-repo" ); PROVIDER.newFileSystem( newRepo, EMPTY_ENV ); final Path dir = PROVIDER.getPath( URI.create( "git://master@delete-non-empty-test-repo/other/path" ) ); final Path _root = PROVIDER.getPath( URI.create( "git://master@delete-non-empty-test-repo/myfile1.txt" ) ); final OutputStream outRootStream = PROVIDER.newOutputStream( _root ); outRootStream.write( "my cool content".getBytes() ); outRootStream.close(); final Path path = PROVIDER.getPath( URI.create( "git://master@delete-non-empty-test-repo/other/path/myfile1.txt" ) ); final OutputStream outStream = PROVIDER.newOutputStream( path ); outStream.write( "my cool content".getBytes() ); outStream.close(); final Path path2 = PROVIDER.getPath( URI.create( "git://master@delete-non-empty-test-repo/other/path/myfile2.txt" ) ); final OutputStream outStream2 = PROVIDER.newOutputStream( path2 ); outStream2.write( "my cool content".getBytes() ); outStream2.close(); final Path path3 = PROVIDER.getPath( URI.create( "git://master@delete-non-empty-test-repo/other/path/myfile3.txt" ) ); final OutputStream outStream3 = PROVIDER.newOutputStream( path3 ); outStream3.write( "my cool content".getBytes() ); outStream3.close();
@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(); }
private JGitPathImpl composePath( final JGitPathImpl directory, final JGitPathImpl fileName, final CopyOption... options ) { if ( directory.getPath().endsWith( "/" ) ) { return toPathImpl( getPath( URI.create( directory.toUri().toString() + fileName.toString( false ) ) ) ); } return toPathImpl( getPath( URI.create( directory.toUri().toString() + "/" + fileName.toString( false ) ) ) ); }
@Override public Path getPath( final URI uri ) throws IllegalArgumentException, FileSystemNotFoundException, SecurityException { checkNotNull( "uri", uri ); checkCondition( "uri scheme not supported", uri.getScheme().equals( getScheme() ) || uri.getScheme().equals( "default" ) ); checkURI( "uri", uri ); final JGitFileSystem fileSystem = fileSystems.get( extractRepoName( uri ) ); if ( fileSystem == null ) { throw new FileSystemNotFoundException(); } try { return JGitPathImpl.create( fileSystem, URIUtil.decode( extractPath( uri ) ), extractHost( uri ), false ); } catch ( final URIException e ) { return null; } }
@Override public Map<String, Object> readAttributes( final Path path, final String attributes, final LinkOption... options ) throws UnsupportedOperationException, IllegalArgumentException, IOException, SecurityException { checkNotNull( "path", path ); checkNotEmpty( "attributes", attributes ); final String[] s = split( attributes ); if ( s[ 0 ].length() == 0 ) { throw new IllegalArgumentException( attributes ); } final ExtendedAttributeView view = getFileAttributeView( toPathImpl( path ), s[ 0 ], options ); if ( view == null ) { throw new UnsupportedOperationException( "View '" + s[ 0 ] + "' not available" ); } return view.readAttributes( s[ 1 ].split( "," ) ); }
throws IllegalArgumentException, IOException, SecurityException, FileSystemAlreadyExistsException { checkNotNull( "uri", uri ); checkCondition( "uri scheme not supported", uri.getScheme().equals( getScheme() ) || uri.getScheme().equals( "default" ) ); checkURI( "uri", uri ); checkNotNull( "env", env ); final String name = extractRepoName( uri ); credential = buildCredential( env ); git = cloneRepository( repoDest, originURI, bare, credential ); } else { credential = buildCredential( null ); git = newRepository( repoDest, bare ); final URI initURI = URI.create( getScheme() + "://master@" + name + "/readme.md" ); final CommentedOption op = setupOp( env ); final OutputStream stream = newOutputStream( getPath( initURI ), op ); final String _init = "Repository Init Content\n" + "=======================\n" + buildAndStartDaemon();
@Test public void testMoveFiles() throws IOException { final URI newRepo = URI.create( "git://moveasset-test-repo" ); PROVIDER.newFileSystem( newRepo, EMPTY_ENV ); final Path path = PROVIDER.getPath( URI.create( "git://master@moveasset-test-repo/myfile1.txt" ) ); { final OutputStream outStream = PROVIDER.newOutputStream( path ); outStream.write( "my cool content".getBytes() ); outStream.close(); } final Path path2 = PROVIDER.getPath( URI.create( "git://user_branch@moveasset-test-repo/other/path/myfile2.txt" ) ); { final OutputStream outStream2 = PROVIDER.newOutputStream( path2 ); outStream2.write( "my cool content".getBytes() ); outStream2.close(); } final Path path3 = PROVIDER.getPath( URI.create( "git://user_branch@moveasset-test-repo/myfile3.txt" ) ); { final OutputStream outStream3 = PROVIDER.newOutputStream( path3 ); outStream3.write( "my cool content".getBytes() ); outStream3.close(); } final Path target = PROVIDER.getPath( URI.create( "git://user_branch@moveasset-test-repo/myfile1.txt" ) ); try { PROVIDER.move( path, target ); } catch ( final Exception e ) { fail( "should move file", e ); } }
@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 public void testReadAttributes() throws IOException { final URI newRepo = URI.create( "git://readattrs-test-repo" ); PROVIDER.newFileSystem( newRepo, EMPTY_ENV ); final Path path = PROVIDER.getPath( URI.create( "git://master@readattrs-test-repo/myfile1.txt" ) ); final OutputStream outStream = PROVIDER.newOutputStream( path ); outStream.write( "my cool content".getBytes() ); outStream.close(); final Path path2 = PROVIDER.getPath( URI.create( "git://user_branch@readattrs-test-repo/other/path/myfile2.txt" ) ); final OutputStream outStream2 = PROVIDER.newOutputStream( path2 ); outStream2.write( "my cool content".getBytes() ); outStream2.close(); final Path path3 = PROVIDER.getPath( URI.create( "git://user_branch@readattrs-test-repo/myfile3.txt" ) ); final OutputStream outStream3 = PROVIDER.newOutputStream( path3 ); outStream3.write( "my cool content".getBytes() ); outStream3.close(); final BasicFileAttributes attrs = PROVIDER.readAttributes( path3, BasicFileAttributes.class ); PROVIDER.readAttributes( PROVIDER.getPath( URI.create( "git://user_branch@readattrs-test-repo/not_exists.txt" ) ), BasicFileAttributes.class ); failBecauseExceptionWasNotThrown( NoSuchFileException.class ); } catch ( NoSuchFileException e ) { assertThat( PROVIDER.readAttributes( path3, MyAttrs.class ) ).isNull();
private void copyDirectory( final JGitPathImpl source, final JGitPathImpl target, final CopyOption... options ) { final List<JGitPathImpl> directories = new ArrayList<JGitPathImpl>(); for ( final Path path : newDirectoryStream( source, null ) ) { final JGitPathImpl gPath = toPathImpl( path ); final Pair<PathType, ObjectId> pathResult = checkPath( gPath.getFileSystem().gitRepo(), gPath.getRefTree(), gPath.getPath() ); if ( pathResult.getK1() == DIRECTORY ) { directories.add( gPath ); continue; } final JGitPathImpl gTarget = composePath( target, (JGitPathImpl) gPath.getFileName() ); copyFile( gPath, gTarget ); } for ( final JGitPathImpl directory : directories ) { createDirectory( composePath( target, (JGitPathImpl) directory.getFileName() ) ); } }
@Override public FileSystem getFileSystem( final URI uri ) throws IllegalArgumentException, FileSystemNotFoundException, SecurityException { checkNotNull( "uri", uri ); checkCondition( "uri scheme not supported", uri.getScheme().equals( getScheme() ) || uri.getScheme().equals( "default" ) ); checkURI( "uri", uri ); final JGitFileSystem fileSystem = fileSystems.get( extractRepoName( uri ) ); if ( fileSystem == null ) { throw new FileSystemNotFoundException( "No filesystem for uri (" + uri + ") found." ); } if ( hasSyncFlag( uri ) ) { try { final String treeRef = "master"; final ObjectId oldHead = JGitUtil.getTreeRefObjectId( fileSystem.gitRepo().getRepository(), treeRef ); final Map<String, String> params = getQueryParams( uri ); syncRepository( fileSystem.gitRepo(), fileSystem.getCredential(), params.get( "sync" ), hasForceFlag( uri ) ); final ObjectId newHead = JGitUtil.getTreeRefObjectId( fileSystem.gitRepo().getRepository(), treeRef ); notifyDiffs( fileSystem, treeRef, "<system>", "<system>", oldHead, newHead ); } catch ( final Exception ex ) { throw new IOException( ex ); } } return fileSystem; }
@Test public void testCheckAccess() throws Exception { final URI newRepo = URI.create( "git://checkaccess-test-repo" ); PROVIDER.newFileSystem( newRepo, EMPTY_ENV ); final Path path = PROVIDER.getPath( URI.create( "git://master@checkaccess-test-repo/path/to/myfile1.txt" ) ); final OutputStream outStream = PROVIDER.newOutputStream( path ); outStream.write( "my cool content".getBytes() ); outStream.close(); PROVIDER.checkAccess( path ); final Path path_to_dir = PROVIDER.getPath( URI.create( "git://master@checkaccess-test-repo/path/to" ) ); PROVIDER.checkAccess( path_to_dir ); final Path path_not_exists = PROVIDER.getPath( URI.create( "git://master@checkaccess-test-repo/path/to/some.txt" ) ); try { PROVIDER.checkAccess( path_not_exists ); failBecauseExceptionWasNotThrown( NoSuchFileException.class ); } catch ( NoSuchFileException e ) { } }
@Test public void testGetFileAttributeView() throws IOException { final URI newRepo = URI.create( "git://getfileattriview-test-repo" ); PROVIDER.newFileSystem( newRepo, EMPTY_ENV ); final Path path = PROVIDER.getPath( URI.create( "git://master@getfileattriview-test-repo/myfile1.txt" ) ); final OutputStream outStream = PROVIDER.newOutputStream( path ); outStream.write( "my cool content".getBytes() ); outStream.close(); final Path path2 = PROVIDER.getPath( URI.create( "git://user_branch@getfileattriview-test-repo/other/path/myfile2.txt" ) ); final OutputStream outStream2 = PROVIDER.newOutputStream( path2 ); outStream2.write( "my cool content".getBytes() ); outStream2.close(); final Path path3 = PROVIDER.getPath( URI.create( "git://user_branch@getfileattriview-test-repo/myfile3.txt" ) ); final OutputStream outStream3 = PROVIDER.newOutputStream( path3 ); outStream3.write( "my cool content".getBytes() ); outStream3.close(); final JGitVersionAttributeView attrs = PROVIDER.getFileAttributeView( path3, JGitVersionAttributeView.class ); PROVIDER.getFileAttributeView( PROVIDER.getPath( URI.create( "git://user_branch@getfileattriview-test-repo/not_exists.txt" ) ), BasicFileAttributeView.class ); failBecauseExceptionWasNotThrown( NoSuchFileException.class ); } catch ( Exception e ) { assertThat( PROVIDER.getFileAttributeView( path3, MyInvalidFileAttributeView.class ) ).isNull();
@Test public void testPathBranchRooted2() throws IOException, GitAPIException { final JGitFileSystemProvider fsProvider = mock( JGitFileSystemProvider.class ); when( fsProvider.isDefault() ).thenReturn( false ); when( fsProvider.getScheme() ).thenReturn( "git" ); final Git git = setupGit(); final JGitFileSystem fileSystem = new JGitFileSystem( fsProvider, null, git, "my-repo", CredentialsProvider.getDefault() ); final Path path = fileSystem.getPath( "test-branch", "/path/to", "some/place.txt" ); assertThat( path ).isNotNull(); assertThat( path.isAbsolute() ).isTrue(); assertThat( path.toString() ).isEqualTo( "/path/to/some/place.txt" ); assertThat( path.toUri().toString() ).isEqualTo( "git://test-branch@my-repo/path/to/some/place.txt" ); assertThat( path.getNameCount() ).isEqualTo( 4 ); assertThat( path.getName( 0 ).toString() ).isNotNull().isEqualTo( "path" ); assertThat( path.getRoot().toString() ).isNotNull().isEqualTo( "/" ); }
@Test public void testIsSameFile() throws IOException { final URI newRepo = URI.create( "git://issamefile-test-repo" ); PROVIDER.newFileSystem( newRepo, EMPTY_ENV ); final Path path = PROVIDER.getPath( URI.create( "git://master@issamefile-test-repo/path/to/myfile1.txt" ) ); final OutputStream outStream = PROVIDER.newOutputStream( path ); outStream.write( "my cool content".getBytes() ); outStream.close(); final Path path2 = PROVIDER.getPath( URI.create( "git://user_branch@issamefile-test-repo/path/to/myfile2.txt" ) ); final OutputStream outStream2 = PROVIDER.newOutputStream( path2 ); outStream2.write( "my cool content".getBytes() ); outStream2.close(); final Path path3 = PROVIDER.getPath( URI.create( "git://user_branch@issamefile-test-repo/path/to/myfile3.txt" ) ); final OutputStream outStream3 = PROVIDER.newOutputStream( path3 ); outStream3.write( "my cool content".getBytes() ); outStream3.close(); assertThat( PROVIDER.isSameFile( path, path2 ) ).isTrue(); assertThat( PROVIDER.isSameFile( path, path3 ) ).isTrue(); }
@Test public void testGetFileStore() throws Exception { final URI newRepo = URI.create( "git://filestore-test-repo" ); PROVIDER.newFileSystem( newRepo, EMPTY_ENV ); final Path path = PROVIDER.getPath( URI.create( "git://master@filestore-test-repo/path/to/myfile1.txt" ) ); final OutputStream outStream = PROVIDER.newOutputStream( path ); outStream.write( "my cool content".getBytes() ); outStream.close(); final FileStore fileStore = PROVIDER.getFileStore( path ); assertThat( fileStore ).isNotNull(); assertThat( fileStore.getAttribute( "readOnly" ) ).isEqualTo( Boolean.FALSE ); }
@Test public void testIsHidden() throws IOException { final URI newRepo = URI.create( "git://ishidden-test-repo" ); PROVIDER.newFileSystem( newRepo, EMPTY_ENV ); final Path path = PROVIDER.getPath( URI.create( "git://user_branch@ishidden-test-repo/path/to/.myfile.txt" ) ); final OutputStream outStream = PROVIDER.newOutputStream( path ); assertThat( outStream ).isNotNull(); outStream.write( "my cool content".getBytes() ); outStream.close(); final Path path2 = PROVIDER.getPath( URI.create( "git://user_branch@ishidden-test-repo/path/to/myfile.txt" ) ); final OutputStream outStream2 = PROVIDER.newOutputStream( path2 ); assertThat( outStream2 ).isNotNull(); outStream2.write( "my cool content".getBytes() ); outStream2.close(); assertThat( PROVIDER.isHidden( PROVIDER.getPath( URI.create( "git://user_branch@ishidden-test-repo/path/to/.myfile.txt" ) ) ) ).isTrue(); assertThat( PROVIDER.isHidden( PROVIDER.getPath( URI.create( "git://user_branch@ishidden-test-repo/path/to/myfile.txt" ) ) ) ).isFalse(); assertThat( PROVIDER.isHidden( PROVIDER.getPath( URI.create( "git://user_branch@ishidden-test-repo/path/to/non_existent/.myfile.txt" ) ) ) ).isTrue(); assertThat( PROVIDER.isHidden( PROVIDER.getPath( URI.create( "git://user_branch@ishidden-test-repo/path/to/non_existent/myfile.txt" ) ) ) ).isFalse(); assertThat( PROVIDER.isHidden( PROVIDER.getPath( URI.create( "git://user_branch@ishidden-test-repo/" ) ) ) ).isFalse(); assertThat( PROVIDER.isHidden( PROVIDER.getPath( URI.create( "git://user_branch@ishidden-test-repo/some" ) ) ) ).isFalse(); }
@Test public void testSetAttribute() throws IOException { final URI newRepo = URI.create( "git://setattr-test-repo" ); PROVIDER.newFileSystem( newRepo, EMPTY_ENV ); final Path path = PROVIDER.getPath( URI.create( "git://master@setattr-test-repo/myfile1.txt" ) ); final OutputStream outStream = PROVIDER.newOutputStream( path ); outStream.write( "my cool content".getBytes() ); outStream.close(); final Path path2 = PROVIDER.getPath( URI.create( "git://user_branch@setattr-test-repo/other/path/myfile2.txt" ) ); final OutputStream outStream2 = PROVIDER.newOutputStream( path2 ); outStream2.write( "my cool content".getBytes() ); outStream2.close(); final Path path3 = PROVIDER.getPath( URI.create( "git://user_branch@setattr-test-repo/myfile3.txt" ) ); final OutputStream outStream3 = PROVIDER.newOutputStream( path3 ); outStream3.write( "my cool content".getBytes() ); outStream3.close(); PROVIDER.setAttribute( path3, "basic:isRegularFile", true ); failBecauseExceptionWasNotThrown( NotImplementedException.class ); } catch ( NotImplementedException ex ) { PROVIDER.setAttribute( path3, "isRegularFile", true ); failBecauseExceptionWasNotThrown( NotImplementedException.class ); } catch ( NotImplementedException ex ) {
@Test public void testNewFileSystem() { final URI newRepo = URI.create( "git://repo-name" ); final FileSystem fs = PROVIDER.newFileSystem( newRepo, EMPTY_ENV ); assertThat( fs ).isNotNull(); final DirectoryStream<Path> stream = PROVIDER.newDirectoryStream( PROVIDER.getPath( newRepo ), null ); assertThat( stream ).isNotNull().hasSize( 0 ); try { PROVIDER.newFileSystem( newRepo, EMPTY_ENV ); failBecauseExceptionWasNotThrown( FileSystemAlreadyExistsException.class ); } catch ( final Exception ex ) { } PROVIDER.newFileSystem( URI.create( "git://repo-name2" ), EMPTY_ENV ); }