Switch jgit library to the EDL (3-clause BSD)
[jgit.git] / org.spearce.jgit.test / tst / org / spearce / jgit / lib / RepositoryTestCase.java
blob14e7179e4d09f069886a335fca28c113557e11a1
1 /*
2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2007, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * - Neither the name of the Git Development Community nor the
20 * names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
22 * written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 package org.spearce.jgit.lib;
41 import java.io.File;
42 import java.io.FileInputStream;
43 import java.io.FileOutputStream;
44 import java.io.IOException;
45 import java.io.InputStreamReader;
46 import java.io.OutputStreamWriter;
47 import java.io.Reader;
49 import junit.framework.TestCase;
51 public abstract class RepositoryTestCase extends TestCase {
53 protected final File trashParent = new File("trash");
55 protected File trash;
57 protected File trash_git;
59 protected static final PersonIdent jauthor;
61 protected static final PersonIdent jcommitter;
63 static {
64 jauthor = new PersonIdent("J. Author", "jauthor@example.com");
65 jcommitter = new PersonIdent("J. Committer", "jcommitter@example.com");
68 protected static void recursiveDelete(final File dir) {
69 final File[] ls = dir.listFiles();
70 if (ls != null) {
71 for (int k = 0; k < ls.length; k++) {
72 final File e = ls[k];
73 if (e.isDirectory()) {
74 recursiveDelete(e);
75 } else {
76 e.delete();
80 dir.delete();
81 if (dir.exists()) {
82 System.out.println("Warning: Failed to delete " + dir);
86 protected static void copyFile(final File src, final File dst)
87 throws IOException {
88 final FileInputStream fis = new FileInputStream(src);
89 final FileOutputStream fos = new FileOutputStream(dst);
90 final byte[] buf = new byte[4096];
91 int r;
92 while ((r = fis.read(buf)) > 0) {
93 fos.write(buf, 0, r);
95 fis.close();
96 fos.close();
99 protected File writeTrashFile(final String name, final String data)
100 throws IOException {
101 File tf = new File(trash, name);
102 File tfp = tf.getParentFile();
103 if (!tfp.exists() && !tf.getParentFile().mkdirs())
104 throw new Error("Could not create directory " + tf.getParentFile());
105 final OutputStreamWriter fw = new OutputStreamWriter(
106 new FileOutputStream(tf), "UTF-8");
107 fw.write(data);
108 fw.close();
109 return tf;
112 protected static void checkFile(File f, final String checkData)
113 throws IOException {
114 Reader r = new InputStreamReader(new FileInputStream(f), "ISO-8859-1");
115 char[] data = new char[(int) f.length()];
116 if (f.length() != r.read(data))
117 throw new IOException("Internal error reading file data from "+f);
118 assertEquals(checkData, new String(data));
121 protected Repository db;
123 public void setUp() throws Exception {
124 super.setUp();
125 recursiveDelete(trashParent);
126 trash = new File(trashParent,"trash"+System.currentTimeMillis());
127 trash_git = new File(trash, ".git");
129 Runtime.getRuntime().addShutdownHook(new Thread() {
130 @Override
131 public void run() {
132 recursiveDelete(trashParent);
136 db = new Repository(trash_git);
137 db.create();
139 final String[] packs = {
140 "pack-34be9032ac282b11fa9babdc2b2a93ca996c9c2f",
141 "pack-df2982f284bbabb6bdb59ee3fcc6eb0983e20371",
142 "pack-9fb5b411fe6dfa89cc2e6b89d2bd8e5de02b5745",
143 "pack-e6d07037cbcf13376308a0a995d1fa48f8f76aaa"
145 final File tst = new File("tst");
146 final File packDir = new File(db.getObjectsDirectory(), "pack");
147 for (int k = 0; k < packs.length; k++) {
148 copyFile(new File(tst, packs[k] + ".pack"), new File(packDir,
149 packs[k] + ".pack"));
150 copyFile(new File(tst, packs[k] + ".idx"), new File(packDir,
151 packs[k] + ".idx"));
154 copyFile(new File(tst, "packed-refs"), new File(trash_git,"packed-refs"));
156 db.scanForPacks();
159 protected void tearDown() throws Exception {
160 db.close();
161 super.tearDown();
165 * Helper for creating extra empty repos
167 * @return a new empty git repository for testing purposes
169 * @throws IOException
171 protected Repository createNewEmptyRepo() throws IOException {
172 File newTestRepo = new File(trashParent, "new"+System.currentTimeMillis()+"/.git");
173 assertFalse(newTestRepo.exists());
174 File unusedDir = new File(trashParent, "tmp"+System.currentTimeMillis());
175 assertTrue(unusedDir.mkdirs());
176 final Repository newRepo = new Repository(newTestRepo);
177 newRepo.create();
178 return newRepo;