|
| 1 | +import org.eclipse.jgit.api.Git |
| 2 | +import org.eclipse.jgit.api.errors.GitAPIException |
| 3 | +import org.eclipse.jgit.api.errors.JGitInternalException |
| 4 | +import org.eclipse.jgit.internal.storage.file.FileRepository |
| 5 | +import org.eclipse.jgit.storage.file.FileRepositoryBuilder |
| 6 | +import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider |
| 7 | +import org.junit.Test |
| 8 | +import java.io.File |
| 9 | +import java.io.IOException |
| 10 | +// Modified UsernamePasswordCredentialsProvider Constructors string to apply git function is work. |
| 11 | +class GitTest { |
| 12 | + var remotePath: String? = null |
| 13 | + var localPath: String? = null |
| 14 | + var initPath: String? = null |
| 15 | + @Test |
| 16 | + @Throws(IOException::class, GitAPIException::class) |
| 17 | + fun TestClone() { |
| 18 | + val usernamePasswordCredentialsProvider = UsernamePasswordCredentialsProvider("username", "password") |
| 19 | + val cloneCommand = Git.cloneRepository() |
| 20 | + val git = cloneCommand.setURI(remotePath).setBranch("master").setDirectory(File(localPath!!)).setCredentialsProvider(usernamePasswordCredentialsProvider).call() |
| 21 | + print(git.tag()) |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + @Throws(IOException::class) |
| 26 | + fun TestCreate() { |
| 27 | + val newRepo = FileRepositoryBuilder.create(File(initPath!! + "/.git")) |
| 28 | + newRepo.create() |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + @Throws(IOException::class, GitAPIException::class) |
| 33 | + fun TestAdd() { |
| 34 | + val myFile = File(localPath!! + "/myfile.txt") |
| 35 | + myFile.createNewFile() |
| 36 | + val git = Git(FileRepository(localPath!! + "/.git")) |
| 37 | + git.add().addFilepattern("myFile").call() |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + @Throws(IOException::class, GitAPIException::class, JGitInternalException::class) |
| 42 | + fun TestCommit() { |
| 43 | + val git = Git(FileRepository(localPath!! + "/.git")) |
| 44 | + git.commit().setMessage("Test Kotlin version").call() |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + @Throws(IOException::class, GitAPIException::class) |
| 49 | + fun TestPull() { |
| 50 | + val usernamePasswordCredentialsProvider = UsernamePasswordCredentialsProvider("username", "password") |
| 51 | + val git = Git(FileRepository(localPath!! + "/.git")) |
| 52 | + git.pull().setRemoteBranchName("master").setCredentialsProvider(usernamePasswordCredentialsProvider).call() |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + @Throws(IOException::class, GitAPIException::class, JGitInternalException::class) |
| 57 | + fun TestPush() { |
| 58 | + val usernamePasswordCredentialsProvider = UsernamePasswordCredentialsProvider("username", "password") |
| 59 | + val git = Git(FileRepository(localPath!! + "/.git")) |
| 60 | + git.push().setRemote("origin").setCredentialsProvider(usernamePasswordCredentialsProvider).call() |
| 61 | + } |
| 62 | +} |
0 commit comments