private void copyFile(final JGitPathImpl source, final JGitPathImpl target) { final InputStream in = newInputStream(source); final OutputStream out = newOutputStream(target); try { int count; byte[] buffer = new byte[8192]; while ((count = in.read(buffer)) > 0) { out.write(buffer, 0, count); } out.close(); } catch (Exception e) { throw new IOException(e); } finally { try { out.close(); } catch (java.io.IOException e) { throw new IOException(e); } finally { try { in.close(); } catch (java.io.IOException e) { throw new IOException(e); } } } }
private String extractContent(Path path) throws IOException { final InputStream inputStream = provider.newInputStream(path); assertThat(inputStream).isNotNull(); final String content = new Scanner(inputStream).useDelimiter("\\A").next(); inputStream.close(); return content; }
private String extractContent(Path path) throws IOException { final InputStream inputStream = provider.newInputStream(path); assertThat(inputStream).isNotNull(); final String content = new Scanner(inputStream).useDelimiter("\\A").next(); inputStream.close(); return content; }
@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 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(); }
outStream.close(); final InputStream inStream = provider.newInputStream(path);
outStream.close(); final InputStream inStream = provider.newInputStream(path);
@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 ignored) { } assertThat(provider.deleteIfExists(path)).isTrue(); }
@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 ignored) { } assertThat(provider.deleteIfExists(path)).isTrue(); }
@Test public void accessOldVersions() throws Exception { final URI newRepo = URI.create("git://old-versions-test-repo"); final FileSystem fs = provider.newFileSystem(newRepo, new HashMap<String, Object>() {{ put(JGitFileSystemProviderConfiguration.GIT_ENV_KEY_INIT, "true"); }}); assertThat(fs).isNotNull(); for (int i = 0; i < 5; i++) { final Path path = provider.getPath(URI.create("git://old-versions-test-repo/some/path/myfile.txt")); final OutputStream outStream = provider.newOutputStream(path); assertThat(outStream).isNotNull(); outStream.write(("my cool content" + i).getBytes()); outStream.close(); } final Path path = provider.getPath(URI.create("git://old-versions-test-repo/some/path/myfile.txt")); final JGitVersionAttributeView attrs = provider.getFileAttributeView(path, JGitVersionAttributeView.class); assertThat(attrs.readAttributes().history().records().size()).isEqualTo(5); for (int i = 0; i < 5; i++) { final Path oldPath = provider.getPath(URI.create("git://" + attrs.readAttributes().history().records().get(i).id() + "@old-versions-test-repo/some/path/myfile.txt")); final InputStream stream = provider.newInputStream(oldPath); assertNotNull(stream); final String content = new Scanner(stream).useDelimiter("\\A").next(); assertEquals("my cool content" + i, content); } }
@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 ignored) { } try { provider.delete(provider.getPath(URI.create("git://some_user_branch@delete-branch-test-repo"))); failBecauseExceptionWasNotThrown(NoSuchFileException.class); } catch (NoSuchFileException ignored) { } }
@Test public void accessOldVersions() throws Exception { final URI newRepo = URI.create("git://old-versions-test-repo"); final FileSystem fs = provider.newFileSystem(newRepo, new HashMap<String, Object>() {{ put(JGitFileSystemProviderConfiguration.GIT_ENV_KEY_INIT, "true"); }}); assertThat(fs).isNotNull(); for (int i = 0; i < 5; i++) { final Path path = provider.getPath(URI.create("git://old-versions-test-repo/some/path/myfile.txt")); final OutputStream outStream = provider.newOutputStream(path); assertThat(outStream).isNotNull(); outStream.write(("my cool content" + i).getBytes()); outStream.close(); } final Path path = provider.getPath(URI.create("git://old-versions-test-repo/some/path/myfile.txt")); final JGitVersionAttributeView attrs = provider.getFileAttributeView(path, JGitVersionAttributeView.class); assertThat(attrs.readAttributes().history().records().size()).isEqualTo(5); for (int i = 0; i < 5; i++) { final Path oldPath = provider.getPath(URI.create("git://" + attrs.readAttributes().history().records().get(i).id() + "@old-versions-test-repo/some/path/myfile.txt")); final InputStream stream = provider.newInputStream(oldPath); assertNotNull(stream); final String content = new Scanner(stream).useDelimiter("\\A").next(); assertEquals("my cool content" + i, content); } }
@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 ignored) { } try { provider.delete(provider.getPath(URI.create("git://some_user_branch@delete-branch-test-repo"))); failBecauseExceptionWasNotThrown(NoSuchFileException.class); } catch (NoSuchFileException ignored) { } }
@Test(expected = NoSuchFileException.class) public void testInputStream3() throws IOException { final File parentFolder = createTempDirectory(); final File gitFolder = new File(parentFolder, "mytest.git"); final Git origin = new CreateRepository(gitFolder).execute().get(); new 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")); }}).execute(); final URI newRepo = URI.create("git://xxinputstream-test-repo"); final Map<String, Object> env = new HashMap<String, Object>() {{ put(JGitFileSystemProviderConfiguration.GIT_ENV_KEY_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 = new CreateRepository(gitFolder).execute().get(); new Commit(origin, "master", "user1", "user1@example.com", "commitx", null, null, false, new HashMap<String, File>() {{ put("file.txt", tempFile("temp.origin.content.2")); }}).execute(); final URI newRepo = URI.create("git://inputstream-not-exists-test-repo"); final Map<String, Object> env = new HashMap<String, Object>() {{ put(JGitFileSystemProviderConfiguration.GIT_ENV_KEY_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(expected = NoSuchFileException.class) public void testInputStream3() throws IOException { final File parentFolder = createTempDirectory(); final File gitFolder = new File(parentFolder, "mytest.git"); final Git origin = new CreateRepository(gitFolder).execute().get(); new 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")); }}).execute(); final URI newRepo = URI.create("git://xxinputstream-test-repo"); final Map<String, Object> env = new HashMap<String, Object>() {{ put(JGitFileSystemProviderConfiguration.GIT_ENV_KEY_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 = new CreateRepository(gitFolder).execute().get(); new Commit(origin, "master", "user1", "user1@example.com", "commitx", null, null, false, new HashMap<String, File>() {{ put("file.txt", tempFile("temp.origin.content.2")); }}).execute(); final URI newRepo = URI.create("git://inputstream-not-exists-test-repo"); final Map<String, Object> env = new HashMap<String, Object>() {{ put(JGitFileSystemProviderConfiguration.GIT_ENV_KEY_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();
outStream.close(); provider.newInputStream(path).close();