Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Rosenberg2010-05-01 17:53:36 +0000
committerChris Aniszczyk2010-05-04 13:53:08 +0000
commit30235a88e38ab0483798b2e21d62dc76620af152 (patch)
tree7b232f0c07c98a8bc9d74b54b03813341cb33a56 /org.eclipse.egit.core.test
parenta29fc6296deb5c618d4d77b8eda347e8bb2aa4cf (diff)
downloadegit-30235a88e38ab0483798b2e21d62dc76620af152.tar.gz
egit-30235a88e38ab0483798b2e21d62dc76620af152.tar.xz
egit-30235a88e38ab0483798b2e21d62dc76620af152.zip
Remove usage of unpublished API's in org.eclipse.egit.core
This also flags further such use in this bundle as errors and upgrades other compiler settings to Eclipse 3.5.2 defaults. A testcase is introduced to cover error handling pattern, but it is incomplete and only implemented for BlobStorage. Bug: 310933 (partial fix) Change-Id: I40f6b9d457010a0e1a68d1d33edb11f4da7a1321 Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com> Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
Diffstat (limited to 'org.eclipse.egit.core.test')
-rw-r--r--org.eclipse.egit.core.test/src/org/eclipse/egit/core/internal/storage/BlobStorageTest.java100
-rw-r--r--org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/GitTestCase.java50
2 files changed, 150 insertions, 0 deletions
diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/internal/storage/BlobStorageTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/internal/storage/BlobStorageTest.java
new file mode 100644
index 000000000..e347116b1
--- /dev/null
+++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/internal/storage/BlobStorageTest.java
@@ -0,0 +1,100 @@
+/*******************************************************************************
+ * Copyright (C) 2010, Robin Rosenberg
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+package org.eclipse.egit.core.internal.storage;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.egit.core.test.GitTestCase;
+import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.Repository;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class BlobStorageTest extends GitTestCase {
+
+ Repository repository;
+
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+ repository = new Repository(gitDir);
+ repository.create();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ @Test
+ public void testOk() throws Exception {
+ ObjectId id = createFile(repository, project.getProject(), "file", "data");
+ BlobStorage blobStorage = new BlobStorage(repository, "p/file", id);
+ assertEquals("file", blobStorage.getName());
+ assertEquals("data", slurpAndClose(blobStorage.getContents()));
+ assertEquals(Path.fromPortableString("p/file").toOSString(), blobStorage.getFullPath().toOSString());
+
+ }
+
+ @Test
+ public void testFailNotFound() throws Exception {
+ BlobStorage blobStorage = new BlobStorage(repository, "file", ObjectId.fromString("0123456789012345678901234567890123456789"));
+ assertEquals("file", blobStorage.getName());
+ try {
+ blobStorage.getContents();
+ fail("We should not be able to read this 'blob'");
+ } catch (CoreException e) {
+ assertEquals("Git blob 0123456789012345678901234567890123456789 with path file not found", e.getMessage());
+ }
+ }
+
+ @Test
+ public void testFailWrongType() throws Exception {
+ createEmptyTree(repository);
+ BlobStorage blobStorage = new BlobStorage(repository, "file", ObjectId.fromString("4b825dc642cb6eb9a060e54bf8d69288fbee4904"));
+ assertEquals("file", blobStorage.getName());
+ try {
+ blobStorage.getContents();
+ fail("We should not be able to read this blob");
+ } catch (CoreException e) {
+ assertEquals("IO error reading Git blob 4b825dc642cb6eb9a060e54bf8d69288fbee4904 with path file", e.getMessage());
+ }
+ }
+
+ @Test
+ public void testFailCorrupt() throws Exception {
+ try {
+ createFileCorruptShort(repository, project.getProject(), "file", "data");
+ createEmptyTree(repository);
+ BlobStorage blobStorage = new BlobStorage(repository, "file", ObjectId.fromString("6320cd248dd8aeaab759d5871f8781b5c0505172"));
+ assertEquals("file", blobStorage.getName());
+ blobStorage.getContents();
+ fail("We should not be able to read this blob");
+ } catch (CoreException e) {
+ assertEquals("IO error reading Git blob 6320cd248dd8aeaab759d5871f8781b5c0505172 with path file", e.getMessage());
+ }
+ }
+
+ @Test
+ public void testFailCorrupt2() throws Exception {
+ try {
+ createFileCorruptShort(repository, project.getProject(), "file", "datjhjhjhjhjhjhjjkujioedfughjuop986rdfghjhiu7867586redtfguy675r6tfguhyo76r7tfa");
+ BlobStorage blobStorage = new BlobStorage(repository, "file", ObjectId.fromString("526ef34fc76ab0c35ccee343bda1a626efbd4134"));
+ assertEquals("file", blobStorage.getName());
+ blobStorage.getContents();
+ fail("We should not be able to read this blob");
+ } catch (CoreException e) {
+ assertEquals("IO error reading Git blob 526ef34fc76ab0c35ccee343bda1a626efbd4134 with path file", e.getMessage());
+ }
+ }
+}
diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/GitTestCase.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/GitTestCase.java
index 58faa4756..e788a52a8 100644
--- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/GitTestCase.java
+++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/GitTestCase.java
@@ -9,11 +9,20 @@
package org.eclipse.egit.core.test;
import java.io.File;
+import java.io.FileOutputStream;
+import java.io.FileWriter;
import java.io.IOException;
+import java.io.InputStream;
+import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jgit.junit.MockSystemReader;
import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.ObjectWriter;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.lib.Tree;
+import org.eclipse.jgit.util.IO;
import org.eclipse.jgit.util.SystemReader;
import org.junit.After;
import org.junit.Before;
@@ -61,4 +70,45 @@ public abstract class GitTestCase {
assert !d.exists();
}
+ protected ObjectId createFile(Repository repository, IProject project, String name, String content) throws IOException {
+ File file = new File(project.getProject().getLocation().toFile(), name);
+ FileWriter fileWriter = new FileWriter(file);
+ fileWriter.write(content);
+ fileWriter.close();
+ ObjectWriter objectWriter = new ObjectWriter(repository);
+ return objectWriter.writeBlob(file);
+ }
+
+ protected ObjectId createFileCorruptShort(Repository repository, IProject project, String name, String content) throws IOException {
+ ObjectId id = createFile(repository, project, name, content);
+ File file = new File(repository.getDirectory(), "objects/" + id.name().substring(0,2) + "/" + id.name().substring(2));
+ byte[] readFully = IO.readFully(file);
+ file.delete();
+ FileOutputStream fileOutputStream = new FileOutputStream(file);
+ byte[] truncatedData = new byte[readFully.length - 1];
+ System.arraycopy(readFully, 0, truncatedData, 0, truncatedData.length);
+ fileOutputStream.write(truncatedData);
+ fileOutputStream.close();
+ return id;
+ }
+
+ protected ObjectId createEmptyTree(Repository repository) throws IOException {
+ ObjectWriter objectWriter = new ObjectWriter(repository);
+ Tree tree = new Tree(repository);
+ return objectWriter.writeTree(tree);
+ }
+
+ protected String slurpAndClose(InputStream inputStream) throws IOException {
+ StringBuilder stringBuilder = new StringBuilder();
+ try {
+ int ch;
+ while ((ch = inputStream.read()) != -1) {
+ stringBuilder.append((char)ch);
+ }
+ } finally {
+ inputStream.close();
+ }
+ return stringBuilder.toString();
+ }
+
}

Back to the top