diff options
author | Grzegorz Kossakowski <grek@google.com> | 2009-08-12 16:32:47 -0700 |
---|---|---|
committer | Grzegorz Kossakowski <grek@google.com> | 2009-08-12 17:34:57 -0700 |
commit | 505014c2b6133c4eb1cd25b5e7333d26390522cc (patch) | |
tree | 93a4839c423624fb32948602d592e1f7847ff750 /src | |
parent | 4572469aaa322efbc907997c3969b262e90fb696 (diff) | |
download | gimd-505014c2b6133c4eb1cd25b5e7333d26390522cc.tar.gz |
Add AbstractJGitTestCase class that makes easier to write JGit-based tests.
Introduced Apache Commons IO library as dependency because Java/Scala does
not have any native API to delete directories recursively.
Signed-off-by: Grzegorz Kossakowski <grek@google.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/test/scala/com/google/gimd/jgit/AbstractJGitTestCase.scala | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/test/scala/com/google/gimd/jgit/AbstractJGitTestCase.scala b/src/test/scala/com/google/gimd/jgit/AbstractJGitTestCase.scala new file mode 100644 index 0000000..e587f40 --- /dev/null +++ b/src/test/scala/com/google/gimd/jgit/AbstractJGitTestCase.scala @@ -0,0 +1,75 @@ +// Copyright (C) 2009 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gimd.jgit + + +import java.io.{ByteArrayInputStream, IOException, File} +import org.junit.{After, Before} +import org.spearce.jgit.lib._ +class AbstractJGitTestCase { + + private val repositoryPath = "test-repository/.git" + + var repository: Repository = null + + @Before protected def createRepository { + val file = new File(repositoryPath) + repository = new Repository(file) + if (file.exists) + throw new IOException("Repository already exists: " + file) + file.mkdirs() + repository.create() + } + + @After protected def removeRepository { + org.apache.commons.io.FileUtils.deleteDirectory(new File(repositoryPath)) + } + + protected def writeTextContent(text: String): ObjectId = { + val ow = new ObjectWriter(repository) + ow.writeBlob(text.getBytes("UTF-8")) + } + + protected def addFiles(files: List[(String, ObjectId)]): ObjectId = { + val dc = org.spearce.jgit.dircache.DirCache.newInCore + val builder = dc.builder + for ((path, blobId) <- files) { + val entry = new org.spearce.jgit.dircache.DirCacheEntry(path) + entry.setFileMode(org.spearce.jgit.lib.FileMode.REGULAR_FILE) + entry.setObjectId(blobId) + builder.add(entry) + } + builder.finish() + dc.writeTree(new ObjectWriter(repository)) + } + + protected def createCommit(message: String, treeId: ObjectId): ObjectId = { + val commit = new Commit(repository, Array()) + val person = new PersonIdent("A U Thor", "author@example.com") + commit.setAuthor(person) + commit.setCommitter(person) + commit.setMessage(message) + commit.setTreeId(treeId) + commit.commit() + commit.getCommitId + } + + protected def moveHEAD(commitId: ObjectId) { + val refUpdate = repository.updateRef(Constants.HEAD) + refUpdate.setNewObjectId(commitId) + refUpdate.forceUpdate() + } + +} |