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 ) ) ) ); }
@Test(expected = FileSystemNotFoundException.class) public void testGetPathFileSystemNotExisting() { PROVIDER.getPath( URI.create( "git://master@not-exists-get-repo-name/home" ) ); }
@Test public void testCreateLinkUnsupportedOp() { final URI newRepo = URI.create("git://link-repo-name"); PROVIDER.newFileSystem(newRepo, EMPTY_ENV); final Path link = PROVIDER.getPath(URI.create("git://link-repo-name/link.lnk")); final Path path = PROVIDER.getPath(URI.create("git://link-repo-name/file.txt")); try { PROVIDER.createLink(link, path); failBecauseExceptionWasNotThrown(UnsupportedOperationException.class); } catch (Exception e) { } }
@Test public void testCreateSymbolicLinkUnsupportedOp() { final URI newRepo = URI.create("git://symbolic-repo-name"); PROVIDER.newFileSystem(newRepo, EMPTY_ENV); final Path link = PROVIDER.getPath(URI.create("git://symbolic-repo-name/link.lnk")); final Path path = PROVIDER.getPath(URI.create("git://symbolic-repo-name/file.txt")); try { PROVIDER.createSymbolicLink(link, path); failBecauseExceptionWasNotThrown(UnsupportedOperationException.class); } catch (Exception e) { } }
@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 testNewAsynchronousFileChannelUnsupportedOp() { final URI newRepo = URI.create("git://newasyncrepo-name"); PROVIDER.newFileSystem(newRepo, EMPTY_ENV); final Path path = PROVIDER.getPath(URI.create("git://newasyncrepo-name/file.txt")); final Set<? extends OpenOption> options = emptySet(); try { PROVIDER.newAsynchronousFileChannel(path, options, null); failBecauseExceptionWasNotThrown(UnsupportedOperationException.class); } catch (Exception e) { } }
@Test public void testNewFileChannelUnsupportedOp() { final URI newRepo = URI.create("git://newfcrepo-name"); PROVIDER.newFileSystem(newRepo, EMPTY_ENV); final Path path = PROVIDER.getPath(URI.create("git://newfcrepo-name/file.txt")); final Set<? extends OpenOption> options = emptySet(); try { PROVIDER.newFileChannel(path, options); failBecauseExceptionWasNotThrown(UnsupportedOperationException.class); } catch (Exception e) { } }
@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 testReadSymbolicLinkUnsupportedOp() { final URI newRepo = URI.create("git://read-link-repo-name"); PROVIDER.newFileSystem(newRepo, EMPTY_ENV); final Path link = PROVIDER.getPath(URI.create("git://read-link-repo-name/link.lnk")); try { PROVIDER.readSymbolicLink(link); failBecauseExceptionWasNotThrown(UnsupportedOperationException.class); } catch (Exception e) { } } }
@Test public void testInvalidURIGetPath() { final URI uri = URI.create( "git:///master@new-get-repo-name/home" ); try { PROVIDER.getPath( uri ); failBecauseExceptionWasNotThrown( IllegalArgumentException.class ); } catch ( final IllegalArgumentException ex ) { assertThat( ex.getMessage() ).isEqualTo( "Parameter named 'uri' is invalid, missing host repository!" ); } }
@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 testGetComplexPath() { final URI newRepo = URI.create( "git://new-complex-get-repo-name" ); PROVIDER.newFileSystem( newRepo, EMPTY_ENV ); final Path path = PROVIDER.getPath( URI.create( "git://origin/master@new-complex-get-repo-name/home" ) ); assertThat( path ).isNotNull(); assertThat( path.getRoot().toString() ).isEqualTo( "/" ); assertThat( path.toString() ).isEqualTo( "/home" ); final Path pathRelative = PROVIDER.getPath( URI.create( "git://origin/master@new-complex-get-repo-name/:home" ) ); assertThat( pathRelative ).isNotNull(); assertThat( pathRelative.getRoot().toString() ).isEqualTo( "" ); assertThat( pathRelative.toString() ).isEqualTo( "home" ); }
@Test public void testGetPath() { final URI newRepo = URI.create("default://default-new-get-repo-name"); PROVIDER.newFileSystem(newRepo, EMPTY_ENV); final Path path = PROVIDER.getPath(URI.create("default://master@default-new-get-repo-name/home")); assertThat(path).isNotNull(); assertThat(path.getRoot().toString()).isEqualTo("/"); assertThat(path.toString()).isEqualTo("/home"); assertThat(path.toUri().getScheme()).isEqualTo("default"); final Path pathRelative = PROVIDER.getPath(URI.create("default://master@default-new-get-repo-name/:home")); assertThat(pathRelative).isNotNull(); assertThat(pathRelative.toUri().toString()).isEqualTo("default://master@default-new-get-repo-name/:home"); assertThat(pathRelative.getRoot().toString()).isEqualTo(""); assertThat(pathRelative.toString()).isEqualTo("home"); }
@Test public void testGetComplexPath() { final URI newRepo = URI.create("default://default-new-complex-get-repo-name"); PROVIDER.newFileSystem(newRepo, EMPTY_ENV); final Path path = PROVIDER.getPath(URI.create("default://origin/master@default-new-complex-get-repo-name/home")); assertThat(path).isNotNull(); assertThat(path.getRoot().toString()).isEqualTo("/"); assertThat(path.toString()).isEqualTo("/home"); assertThat(path.toUri().getScheme()).isEqualTo("default"); final Path pathRelative = PROVIDER.getPath(URI.create("default://origin/master@default-new-complex-get-repo-name/:home")); assertThat(pathRelative).isNotNull(); assertThat(pathRelative.getRoot().toString()).isEqualTo(""); assertThat(pathRelative.getRoot().toUri().toString()).isEqualTo("default://origin/master@default-new-complex-get-repo-name"); assertThat(pathRelative.toString()).isEqualTo("home"); } }
@Test public void testGetPath() { final URI newRepo = URI.create( "git://new-get-repo-name" ); PROVIDER.newFileSystem( newRepo, EMPTY_ENV ); final Path path = PROVIDER.getPath( URI.create( "git://master@new-get-repo-name/home" ) ); assertThat( path ).isNotNull(); assertThat( path.getRoot().toString() ).isEqualTo( "/" ); assertThat( path.getRoot().toRealPath().toUri().toString() ).isEqualTo( "git://master@new-get-repo-name/" ); assertThat( path.toString() ).isEqualTo( "/home" ); final Path pathRelative = PROVIDER.getPath( URI.create( "git://master@new-get-repo-name/:home" ) ); assertThat( pathRelative ).isNotNull(); assertThat( pathRelative.toRealPath().toUri().toString() ).isEqualTo( "git://master@new-get-repo-name/:home" ); assertThat( pathRelative.getRoot().toString() ).isEqualTo( "" ); assertThat( pathRelative.toString() ).isEqualTo( "home" ); }
@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 ); }
@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 ); }
@Test public void testNewFileSystemInited() { final URI newRepo = URI.create( "git://init-repo-name" ); final Map<String, ?> env = new HashMap<String, Object>() {{ put( "init", Boolean.TRUE ); }}; final FileSystem fs = PROVIDER.newFileSystem( newRepo, env ); assertThat( fs ).isNotNull(); final DirectoryStream<Path> stream = PROVIDER.newDirectoryStream( PROVIDER.getPath( newRepo ), null ); assertThat( stream ).isNotNull().hasSize( 1 ); }
@Test public void testCreateDirectory() throws Exception { final URI newRepo = URI.create( "git://xcreatedir-test-repo" ); PROVIDER.newFileSystem( newRepo, EMPTY_ENV ); final JGitPathImpl path = (JGitPathImpl) PROVIDER.getPath( URI.create( "git://master@xcreatedir-test-repo/some/path/to/" ) ); final Pair<PathType, ObjectId> result = JGitUtil.checkPath( path.getFileSystem().gitRepo(), path.getRefTree(), path.getPath() ); assertThat( result.getK1() ).isEqualTo( PathType.NOT_FOUND ); PROVIDER.createDirectory( path ); final Pair<PathType, ObjectId> resultAfter = JGitUtil.checkPath( path.getFileSystem().gitRepo(), path.getRefTree(), path.getPath() ); assertThat( resultAfter.getK1() ).isEqualTo( PathType.DIRECTORY ); try { PROVIDER.createDirectory( path ); failBecauseExceptionWasNotThrown( FileAlreadyExistsException.class ); } catch ( FileAlreadyExistsException e ) { } }