Tests and fixes for dereferencing tags in Repository.resolve()
[egit/fonseca.git] / org.spearce.jgit.test / tst / org / spearce / jgit / lib / RepositoryTestCase.java
blob9d7d133aa043498506becf2f95a36495014e731c
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 public abstract class RepositoryTestCase extends TestCase {
54 protected final File trashParent = new File("trash");
56 protected File trash;
58 protected File trash_git;
60 protected static final PersonIdent jauthor;
62 protected static final PersonIdent jcommitter;
64 static {
65 jauthor = new PersonIdent("J. Author", "jauthor@example.com");
66 jcommitter = new PersonIdent("J. Committer", "jcommitter@example.com");
69 protected static void recursiveDelete(final File dir) {
70 final File[] ls = dir.listFiles();
71 if (ls != null) {
72 for (int k = 0; k < ls.length; k++) {
73 final File e = ls[k];
74 if (e.isDirectory()) {
75 recursiveDelete(e);
76 } else {
77 e.delete();
81 dir.delete();
82 if (dir.exists()) {
83 System.out.println("Warning: Failed to delete " + dir);
87 protected static void copyFile(final File src, final File dst)
88 throws IOException {
89 final FileInputStream fis = new FileInputStream(src);
90 final FileOutputStream fos = new FileOutputStream(dst);
91 final byte[] buf = new byte[4096];
92 int r;
93 while ((r = fis.read(buf)) > 0) {
94 fos.write(buf, 0, r);
96 fis.close();
97 fos.close();
100 protected File writeTrashFile(final String name, final String data)
101 throws IOException {
102 File tf = new File(trash, name);
103 File tfp = tf.getParentFile();
104 if (!tfp.exists() && !tf.getParentFile().mkdirs())
105 throw new Error("Could not create directory " + tf.getParentFile());
106 final OutputStreamWriter fw = new OutputStreamWriter(
107 new FileOutputStream(tf), "UTF-8");
108 fw.write(data);
109 fw.close();
110 return tf;
113 protected static void checkFile(File f, final String checkData)
114 throws IOException {
115 Reader r = new InputStreamReader(new FileInputStream(f), "ISO-8859-1");
116 char[] data = new char[(int) f.length()];
117 if (f.length() != r.read(data))
118 throw new IOException("Internal error reading file data from "+f);
119 assertEquals(checkData, new String(data));
122 protected Repository db;
124 public void setUp() throws Exception {
125 super.setUp();
126 recursiveDelete(trashParent);
127 trash = new File(trashParent,"trash"+System.currentTimeMillis());
128 trash_git = new File(trash, ".git");
130 Runtime.getRuntime().addShutdownHook(new Thread() {
131 @Override
132 public void run() {
133 recursiveDelete(trashParent);
137 db = new Repository(trash_git);
138 db.create();
140 final String[] packs = {
141 "pack-34be9032ac282b11fa9babdc2b2a93ca996c9c2f",
142 "pack-df2982f284bbabb6bdb59ee3fcc6eb0983e20371",
143 "pack-9fb5b411fe6dfa89cc2e6b89d2bd8e5de02b5745",
144 "pack-546ff360fe3488adb20860ce3436a2d6373d2796",
145 "pack-e6d07037cbcf13376308a0a995d1fa48f8f76aaa"
147 final File packDir = new File(db.getObjectsDirectory(), "pack");
148 for (int k = 0; k < packs.length; k++) {
149 copyFile(JGitTestUtil.getTestResourceFile(packs[k] + ".pack"), new File(packDir,
150 packs[k] + ".pack"));
151 copyFile(JGitTestUtil.getTestResourceFile(packs[k] + ".idx"), new File(packDir,
152 packs[k] + ".idx"));
155 copyFile(JGitTestUtil.getTestResourceFile("packed-refs"), new File(trash_git,"packed-refs"));
157 db.scanForPacks();
160 protected void tearDown() throws Exception {
161 db.close();
162 super.tearDown();
166 * Helper for creating extra empty repos
168 * @return a new empty git repository for testing purposes
170 * @throws IOException
172 protected Repository createNewEmptyRepo() throws IOException {
173 File newTestRepo = new File(trashParent, "new"+System.currentTimeMillis()+"/.git");
174 assertFalse(newTestRepo.exists());
175 File unusedDir = new File(trashParent, "tmp"+System.currentTimeMillis());
176 assertTrue(unusedDir.mkdirs());
177 final Repository newRepo = new Repository(newTestRepo);
178 newRepo.create();
179 return newRepo;