Move the trash repos to a subdirectory for smoother cleanup
[egit.git] / org.spearce.jgit / tst / org / spearce / jgit / lib / RepositoryTestCase.java
blob347caddc3b250f5ddfaec74c920bcefea1858315
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 {
30 final File trashParent = new File("trash");
32 protected File trash;
34 protected File trash_git;
36 protected static final PersonIdent jauthor;
38 protected static final PersonIdent jcommitter;
40 static {
41 jauthor = new PersonIdent("J. Author", "jauthor@example.com");
42 jcommitter = new PersonIdent("J. Committer", "jcommitter@example.com");
45 protected static void recursiveDelete(final File dir) {
46 final File[] ls = dir.listFiles();
47 if (ls != null) {
48 for (int k = 0; k < ls.length; k++) {
49 final File e = ls[k];
50 if (e.isDirectory()) {
51 recursiveDelete(e);
52 } else {
53 e.delete();
57 dir.delete();
58 if (dir.exists()) {
59 System.out.println("Warning: Failed to delete " + dir);
63 protected static void copyFile(final File src, final File dst)
64 throws IOException {
65 final FileInputStream fis = new FileInputStream(src);
66 final FileOutputStream fos = new FileOutputStream(dst);
67 final byte[] buf = new byte[4096];
68 int r;
69 while ((r = fis.read(buf)) > 0) {
70 fos.write(buf, 0, r);
72 fis.close();
73 fos.close();
76 protected File writeTrashFile(final String name, final String data)
77 throws IOException {
78 File tf = new File(trash, name);
79 File tfp = tf.getParentFile();
80 if (!tfp.exists() && !tf.getParentFile().mkdirs())
81 throw new Error("Could not create directory " + tf.getParentFile());
82 final OutputStreamWriter fw = new OutputStreamWriter(
83 new FileOutputStream(tf), "UTF-8");
84 fw.write(data);
85 fw.close();
86 return tf;
89 protected static void checkFile(File f, final String checkData)
90 throws IOException {
91 byte[] data = new byte[(int) f.length()];
92 assertEquals(f.length(), data.length);
93 FileInputStream stream = new FileInputStream(f);
94 stream.read(data);
95 byte[] bytes = checkData.getBytes("ISO-8859-1");
96 assertTrue(Arrays.equals(bytes, data));
99 protected Repository db;
101 public void setUp() throws Exception {
102 super.setUp();
103 recursiveDelete(trashParent);
104 trash = new File(trashParent,"trash"+System.currentTimeMillis());
105 trash_git = new File(trash, ".git");
107 Runtime.getRuntime().addShutdownHook(new Thread() {
108 @Override
109 public void run() {
110 recursiveDelete(trashParent);
114 db = new Repository(trash_git);
115 db.create();
117 final String[] packs = {
118 "pack-34be9032ac282b11fa9babdc2b2a93ca996c9c2f",
119 "pack-df2982f284bbabb6bdb59ee3fcc6eb0983e20371"
121 final File tst = new File("tst");
122 final File packDir = new File(db.getObjectsDirectory(), "pack");
123 for (int k = 0; k < packs.length; k++) {
124 copyFile(new File(tst, packs[k] + ".pack"), new File(packDir,
125 packs[k] + ".pack"));
126 copyFile(new File(tst, packs[k] + ".idx"), new File(packDir,
127 packs[k] + ".idx"));
130 copyFile(new File(tst, "packed-refs"), new File(trash_git,"packed-refs"));
132 db.scanForPacks();
135 protected void tearDown() throws Exception {
136 db.close();
137 super.tearDown();