summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Kossakowski <grek@google.com>2009-08-12 18:23:54 -0700
committerGrzegorz Kossakowski <grek@google.com>2009-08-12 18:23:54 -0700
commit72c14879e6720cc6960c7182f4020a4f397fa653 (patch)
tree3f4fe9f07fa089957f6d6f68703cc355bb197abd
parent505014c2b6133c4eb1cd25b5e7333d26390522cc (diff)
downloadgimd-72c14879e6720cc6960c7182f4020a4f397fa653.tar.gz
Provide JGit-based implementation for File trait.
JGitFile uses lazy vals as mechanism to avoid computing values that might not be needed at all. For every JGitFile created there's check performed to make sure that given object exists. Signed-off-by: Grzegorz Kossakowski <grek@google.com>
-rw-r--r--src/main/scala/com/google/gimd/jgit/JGitDatabaseException.scala22
-rw-r--r--src/main/scala/com/google/gimd/jgit/JGitFile.scala36
-rw-r--r--src/test/scala/com/google/gimd/jgit/JGitFileTestCase.scala70
3 files changed, 128 insertions, 0 deletions
diff --git a/src/main/scala/com/google/gimd/jgit/JGitDatabaseException.scala b/src/main/scala/com/google/gimd/jgit/JGitDatabaseException.scala
new file mode 100644
index 0000000..c7fd49a
--- /dev/null
+++ b/src/main/scala/com/google/gimd/jgit/JGitDatabaseException.scala
@@ -0,0 +1,22 @@
+// 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
+
+class JGitDatabaseException(message: String, cause: Throwable)
+ extends DatabaseException(message, cause) {
+
+ def this(message: String) = this(message, null)
+
+}
diff --git a/src/main/scala/com/google/gimd/jgit/JGitFile.scala b/src/main/scala/com/google/gimd/jgit/JGitFile.scala
new file mode 100644
index 0000000..6c6c13c
--- /dev/null
+++ b/src/main/scala/com/google/gimd/jgit/JGitFile.scala
@@ -0,0 +1,36 @@
+// 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 file.{FileType, File}
+import java.io.{InputStreamReader, ByteArrayInputStream}
+import org.spearce.jgit.lib.{ObjectId, Repository}
+import text.Parser
+
+final class JGitFile[T](val path: String, val blobId: ObjectId, val fileType: FileType[T],
+ val jgitRepository: Repository) extends File[T] {
+
+ lazy val message = Parser.parse(new InputStreamReader(blobInputStream, "UTF-8"))
+ lazy val userObject = fileType.userType.toUserType(message)
+
+ private lazy val blobInputStream = {
+ val objectLoader = jgitRepository.openBlob(blobId)
+ if (objectLoader == null)
+ throw new JGitDatabaseException("Blob '" + blobId.name + "' does not exist.")
+
+ new ByteArrayInputStream(objectLoader.getCachedBytes)
+ }
+
+}
diff --git a/src/test/scala/com/google/gimd/jgit/JGitFileTestCase.scala b/src/test/scala/com/google/gimd/jgit/JGitFileTestCase.scala
new file mode 100644
index 0000000..2f0da79
--- /dev/null
+++ b/src/test/scala/com/google/gimd/jgit/JGitFileTestCase.scala
@@ -0,0 +1,70 @@
+// 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 file.FileType
+import junit.framework.Assert._
+import org.junit.Test
+import org.spearce.jgit.lib.ObjectId
+
+class JGitFileTestCase extends AbstractJGitTestCase {
+
+ case class SimpleMessage(name: String, value: Int)
+
+ object SimpleMessageType extends UserType[SimpleMessage] {
+ def toMessageBuffer(sm: SimpleMessage) = (new MessageBuffer()) ++
+ List(Field("name", sm.name), Field("value", sm.value))
+ def toUserType(m: Message): SimpleMessage = {
+ val name = m.one("name").stringField.value
+ val value = m.one("value").intField.value
+ SimpleMessage(name, value)
+ }
+ }
+
+ object SimpleMessageFileType extends FileType[SimpleMessage] {
+ val pathPrefix = None
+ val pathSuffix = None
+ val userType = SimpleMessageType
+ }
+
+ @Test
+ def testMessage {
+ val simpleMessage = SimpleMessage("Test", 10)
+ val expected: Message = SimpleMessageType.toMessageBuffer(simpleMessage).readOnly
+
+ val blobId = writeTextContent(expected.toString)
+
+ val jGitFile = new JGitFile("test", blobId, SimpleMessageFileType, repository)
+ assertEquals(expected, jGitFile.message)
+ }
+
+ @Test
+ def testUserObject {
+ val expected = SimpleMessage("Test", 10)
+ val message: Message = SimpleMessageType.toMessageBuffer(expected).readOnly
+
+ val blobId = writeTextContent(message.toString)
+
+ val jGitFile = new JGitFile("test", blobId, SimpleMessageFileType, repository)
+ assertEquals(expected, jGitFile.userObject)
+ }
+
+ @Test{val expected = classOf[JGitDatabaseException]}
+ def testMessageOfNonExistingObject {
+ val jGitFile = new JGitFile("test", ObjectId.zeroId, SimpleMessageFileType, repository)
+ jGitFile.message
+ }
+}