Add a counter to make sure the test repo name is unique
[egit/qmx.git] / org.spearce.jgit.test / tst / org / spearce / jgit / lib / RepositoryTestCase.java
blob6ea9b453c8b3e0d180fbc12ec76a669ee541d0d1
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;
50 import org.spearce.jgit.util.JGitTestUtil;
52 /**
53 * Base class for most JGit unit tests.
55 * Sets up a predefined test repository and has support for creating additional
56 * repositories and destroying them when the tests are finished.
58 * A system property <em>jgit.junit.usemmmap</em> defines whether memory mapping
59 * is used. Memory mapping has an effect on the file system, in that memory
60 * mapped files in java cannot be deleted as long as they mapped arrays have not
61 * been reclaimed by the garbage collector. The programmer cannot control this
62 * with precision, though hinting using <em>{@link java.lang.System#gc}</em>
63 * often helps.
65 public abstract class RepositoryTestCase extends TestCase {
67 protected final File trashParent = new File("trash");
69 protected File trash;
71 protected File trash_git;
73 protected static final PersonIdent jauthor;
75 protected static final PersonIdent jcommitter;
77 static {
78 jauthor = new PersonIdent("J. Author", "jauthor@example.com");
79 jcommitter = new PersonIdent("J. Committer", "jcommitter@example.com");
82 protected boolean packedGitMMAP;
84 /**
85 * Configure JGit before setting up test repositories.
87 protected void configure() {
88 packedGitMMAP = "true".equals(System.getProperty("jgit.junit.usemmmap"));
89 WindowCache.reconfigure(128*1024, 8192, packedGitMMAP, 8192);
92 /**
93 * Utility method to delete a directory recursively. It is
94 * also used internally.
96 * @param dir
98 protected static void recursiveDelete(final File dir) {
99 final File[] ls = dir.listFiles();
100 if (ls != null) {
101 for (int k = 0; k < ls.length; k++) {
102 final File e = ls[k];
103 if (e.isDirectory()) {
104 recursiveDelete(e);
105 } else {
106 e.delete();
110 dir.delete();
111 if (dir.exists()) {
112 System.out.println("Warning: Failed to delete " + dir);
116 protected static void copyFile(final File src, final File dst)
117 throws IOException {
118 final FileInputStream fis = new FileInputStream(src);
119 final FileOutputStream fos = new FileOutputStream(dst);
120 final byte[] buf = new byte[4096];
121 int r;
122 while ((r = fis.read(buf)) > 0) {
123 fos.write(buf, 0, r);
125 fis.close();
126 fos.close();
129 protected File writeTrashFile(final String name, final String data)
130 throws IOException {
131 File tf = new File(trash, name);
132 File tfp = tf.getParentFile();
133 if (!tfp.exists() && !tf.getParentFile().mkdirs())
134 throw new Error("Could not create directory " + tf.getParentFile());
135 final OutputStreamWriter fw = new OutputStreamWriter(
136 new FileOutputStream(tf), "UTF-8");
137 fw.write(data);
138 fw.close();
139 return tf;
142 protected static void checkFile(File f, final String checkData)
143 throws IOException {
144 Reader r = new InputStreamReader(new FileInputStream(f), "ISO-8859-1");
145 char[] data = new char[(int) f.length()];
146 if (f.length() != r.read(data))
147 throw new IOException("Internal error reading file data from "+f);
148 assertEquals(checkData, new String(data));
151 protected Repository db;
153 private static int testcount;
155 public void setUp() throws Exception {
156 super.setUp();
157 configure();
158 recursiveDelete(trashParent);
159 trash = new File(trashParent,"trash"+System.currentTimeMillis()+"."+(testcount++));
160 trash_git = new File(trash, ".git");
162 Runtime.getRuntime().addShutdownHook(new Thread() {
163 @Override
164 public void run() {
165 recursiveDelete(trashParent);
169 db = new Repository(trash_git);
170 db.create();
172 final String[] packs = {
173 "pack-34be9032ac282b11fa9babdc2b2a93ca996c9c2f",
174 "pack-df2982f284bbabb6bdb59ee3fcc6eb0983e20371",
175 "pack-9fb5b411fe6dfa89cc2e6b89d2bd8e5de02b5745",
176 "pack-546ff360fe3488adb20860ce3436a2d6373d2796",
177 "pack-e6d07037cbcf13376308a0a995d1fa48f8f76aaa"
179 final File packDir = new File(db.getObjectsDirectory(), "pack");
180 for (int k = 0; k < packs.length; k++) {
181 copyFile(JGitTestUtil.getTestResourceFile(packs[k] + ".pack"), new File(packDir,
182 packs[k] + ".pack"));
183 copyFile(JGitTestUtil.getTestResourceFile(packs[k] + ".idx"), new File(packDir,
184 packs[k] + ".idx"));
187 copyFile(JGitTestUtil.getTestResourceFile("packed-refs"), new File(trash_git,"packed-refs"));
189 db.scanForPacks();
192 protected void tearDown() throws Exception {
193 db.close();
194 super.tearDown();
198 * Helper for creating extra empty repos
200 * @return a new empty git repository for testing purposes
202 * @throws IOException
204 protected Repository createNewEmptyRepo() throws IOException {
205 File newTestRepo = new File(trashParent, "new"+System.currentTimeMillis()+"."+(testcount++)+"/.git");
206 assertFalse(newTestRepo.exists());
207 final Repository newRepo = new Repository(newTestRepo);
208 newRepo.create();
209 return newRepo;