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
;
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
;
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();
45 for (int k
= 0; k
< ls
.length
; k
++) {
47 if (e
.isDirectory()) {
56 throw new IllegalStateException("Failed to delete " + dir
);
60 protected static void copyFile(final File src
, final File dst
)
62 final FileInputStream fis
= new FileInputStream(src
);
63 final FileOutputStream fos
= new FileOutputStream(dst
);
64 final byte[] buf
= new byte[4096];
66 while ((r
= fis
.read(buf
)) > 0) {
73 protected static File
writeTrashFile(final String name
, final String data
)
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");
86 protected static void checkFile(File f
, final String checkData
)
88 byte[] data
= new byte[(int) f
.length()];
89 assertEquals(f
.length(), data
.length
);
90 FileInputStream stream
= new FileInputStream(f
);
92 byte[] bytes
= checkData
.getBytes("ISO-8859-1");
93 assertTrue(Arrays
.equals(bytes
, data
));
96 protected Repository db
;
98 public void setUp() throws Exception
{
100 recursiveDelete(trash
);
101 db
= new Repository(trash_git
);
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
,
117 copyFile(new File(tst
, "packed-refs"), new File(trash_git
,"packed-refs"));
122 protected void tearDown() throws Exception
{