Issue 20: Factor out ClientBuilder from IndexBuilder
[nbgit.git] / test / unit / src / org / nbgit / junit / RepositoryTestCase.java
blob254110e74908c05f8603722932afd3670f9c801e
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2009 Jonas Fonseca <fonseca@diku.dk>
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common
8 * Development and Distribution License("CDDL") (collectively, the
9 * "License"). You may not use this file except in compliance with the
10 * License. You can obtain a copy of the License at
11 * http://www.netbeans.org/cddl-gplv2.html. See the License for the
12 * specific language governing permissions and limitations under the
13 * License. When distributing the software, include this License Header
14 * Notice in each file.
16 * This particular file is subject to the "Classpath" exception as provided
17 * by Sun in the GPL Version 2 section of the License file that
18 * accompanied this code. If applicable, add the following below the
19 * License Header, with the fields enclosed by brackets [] replaced by
20 * your own identifying information:
21 * "Portions Copyrighted [year] [name of copyright owner]"
23 * Contributor(s):
25 * If you wish your version of this file to be governed by only the CDDL
26 * or only the GPL Version 2, indicate your decision by adding
27 * "[Contributor] elects to include this software in this distribution
28 * under the [CDDL or GPL Version 2] license." If you do not indicate a
29 * single choice of license, a recipient has the option to distribute
30 * your version of this file under either the CDDL, the GPL Version 2 or
31 * to extend the choice of license to its licensees as provided above.
32 * However, if you add GPL Version 2 code and therefore, elected the GPL
33 * Version 2 license, then the option applies only if the new code is
34 * made subject to such option by the copyright holder.
36 package org.nbgit.junit;
38 import java.io.File;
39 import java.io.FileInputStream;
40 import java.io.FileOutputStream;
41 import java.io.IOException;
42 import java.io.InputStream;
43 import java.util.ArrayList;
44 import java.util.Collection;
45 import org.nbgit.OutputLogger;
46 import org.netbeans.junit.NbTestCase;
47 import org.spearce.jgit.lib.FileBasedConfig;
48 import org.spearce.jgit.lib.Repository;
49 import org.spearce.jgit.util.SystemReader;
51 /**
52 * Base test case for testing with repositories.
54 public class RepositoryTestCase extends NbTestCase {
56 private final File dataRoot = new File(getDataDir(), getClass().getCanonicalName());
57 protected Repository repository;
58 protected OutputLogger logger;
59 protected ArrayList<String> loggerMessages;
60 protected File dataDir;
61 protected File gitDir;
62 protected File workDir;
64 public RepositoryTestCase(String name) {
65 super(name);
68 @Override
69 protected void setUp() throws Exception {
70 super.setUp();
71 clearWorkDir();
72 SystemReader.setInstance(new MockSystemReader(new File(gitDir, "userconfig")));
73 dataDir = new File(dataRoot, getName());
74 workDir = new File(getWorkDir(), "repo");
75 gitDir = new File(workDir, ".git");
76 repository = new Repository(gitDir);
77 repository.create();
78 loggerMessages = new ArrayList<String>();
79 logger = MockOutputLogger.create(loggerMessages);
81 copyRepositoryFiles("default", repository);
84 @Override
85 protected void tearDown() throws Exception {
86 super.tearDown();
89 protected Collection<File> toFiles(File base, String... paths) {
90 Collection<File> files = new ArrayList<File>(paths.length);
91 for (int i = 0; i < paths.length; i++)
92 files.add(toFile(base, paths[i]));
93 return files;
96 /**
97 * Build an abstract File from a base file and a relative path, e.g.:
98 * <pre>
99 * toFile(repository.getWorkDir(), "a/b")
100 * </pre>.
102 protected File toFile(File base, String path) {
103 return new File(base, path.replace('/', File.separatorChar));
107 * Build an abstract File using workDir as the base.
108 * @see RepositoryTestCase#toFile(java.io.File, java.lang.String)
110 protected File toWorkDirFile(String path) {
111 return toFile(workDir, path);
115 * Build an abstract File using gitDir as the base.
116 * @see RepositoryTestCase#toFile(java.io.File, java.lang.String)
118 protected File toGitDirFile(String path) {
119 return toFile(gitDir, path);
122 protected boolean copyRepositoryFiles(String name, Repository repo) throws IOException {
123 File repoGitDir = new File(dataDir, name + ".git");
124 File repoWorkDir = new File(dataDir, name + ".workdir");
125 if (repoGitDir.exists())
126 copyFile(repoGitDir, repo.getDirectory());
127 if (repoWorkDir.exists())
128 copyFile(repoWorkDir, repo.getWorkDir());
129 return repoGitDir.exists() || repoWorkDir.exists();
132 protected void copyFile(File src, File dst) throws IOException {
133 if (src.isDirectory()) {
134 dst.mkdir();
135 for (File file : src.listFiles()) {
136 File fileDst = new File(dst, file.getName());
137 copyFile(file, fileDst);
139 } else {
140 copyStream(new FileInputStream(src), dst);
144 private void copyStream(InputStream in, File dst) throws IOException {
145 FileOutputStream out = new FileOutputStream(dst);
146 try {
147 byte[] buf = new byte[4096];
148 int r;
149 while ((r = in.read(buf)) > 0) {
150 out.write(buf, 0, r);
152 } finally {
153 out.close();
157 private static class MockSystemReader extends SystemReader {
159 private final File userConfigFile;
161 public MockSystemReader(File userConfigFile) {
162 this.userConfigFile = userConfigFile;
165 @Override
166 public String getHostname() {
167 return "localhost";
170 @Override
171 public String getenv(String name) {
172 return null;
175 @Override
176 public String getProperty(String arg0) {
177 return null;
180 @Override
181 public FileBasedConfig openUserConfig() {
182 return new FileBasedConfig(userConfigFile);