Add more repository test data
[egit.git] / org.spearce.jgit / tst / org / spearce / jgit / lib / RepositoryTestCase.java
blobc3e855dedb583001af5ba69b9a5b54f814f48c6e
1 /*
2 * Copyright (C) 2006 Shawn Pearce <spearce@spearce.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License, version 2, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org.spearce.jgit.lib;
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.OutputStreamWriter;
24 import java.util.Arrays;
26 import junit.framework.TestCase;
28 public abstract class RepositoryTestCase extends TestCase {
29 protected static final File trash = new File("trash");
31 protected static final File trash_git = new File(trash, ".git");
33 protected static final PersonIdent jauthor;
35 protected static final PersonIdent jcommitter;
37 static {
38 jauthor = new PersonIdent("J. Author", "jauthor@example.com");
39 jcommitter = new PersonIdent("J. Committer", "jcommitter@example.com");
42 protected static void recursiveDelete(final File dir) {
43 final File[] ls = dir.listFiles();
44 if (ls != null) {
45 for (int k = 0; k < ls.length; k++) {
46 final File e = ls[k];
47 if (e.isDirectory()) {
48 recursiveDelete(e);
49 } else {
50 e.delete();
54 dir.delete();
55 if (dir.exists()) {
56 throw new IllegalStateException("Failed to delete " + dir);
60 protected static void copyFile(final File src, final File dst)
61 throws IOException {
62 final FileInputStream fis = new FileInputStream(src);
63 final FileOutputStream fos = new FileOutputStream(dst);
64 final byte[] buf = new byte[4096];
65 int r;
66 while ((r = fis.read(buf)) > 0) {
67 fos.write(buf, 0, r);
69 fis.close();
70 fos.close();
73 protected static File writeTrashFile(final String name, final String data)
74 throws IOException {
75 File tf = new File(trash, name);
76 File tfp = tf.getParentFile();
77 if (!tfp.exists() && !tf.getParentFile().mkdirs())
78 throw new Error("Could not create directory " + tf.getParentFile());
79 final OutputStreamWriter fw = new OutputStreamWriter(
80 new FileOutputStream(tf), "UTF-8");
81 fw.write(data);
82 fw.close();
83 return tf;
86 protected static void checkFile(File f, final String checkData)
87 throws IOException {
88 byte[] data = new byte[(int) f.length()];
89 assertEquals(f.length(), data.length);
90 FileInputStream stream = new FileInputStream(f);
91 stream.read(data);
92 byte[] bytes = checkData.getBytes("ISO-8859-1");
93 assertTrue(Arrays.equals(bytes, data));
96 protected Repository db;
98 public void setUp() throws Exception {
99 super.setUp();
100 recursiveDelete(trash);
101 db = new Repository(trash_git);
102 db.create();
104 final String[] packs = {
105 "pack-34be9032ac282b11fa9babdc2b2a93ca996c9c2f",
106 "pack-df2982f284bbabb6bdb59ee3fcc6eb0983e20371"
108 final File tst = new File("tst");
109 final File packDir = new File(db.getObjectsDirectory(), "pack");
110 for (int k = 0; k < packs.length; k++) {
111 copyFile(new File(tst, packs[k] + ".pack"), new File(packDir,
112 packs[k] + ".pack"));
113 copyFile(new File(tst, packs[k] + ".idx"), new File(packDir,
114 packs[k] + ".idx"));
117 copyFile(new File(tst, "packed-refs"), new File(trash_git,"packed-refs"));
119 db.scanForPacks();
122 protected void tearDown() throws Exception {
123 db.close();
124 super.tearDown();