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]"
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
;
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
;
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
) {
69 protected void setUp() throws Exception
{
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
);
78 loggerMessages
= new ArrayList
<String
>();
79 logger
= MockOutputLogger
.create(loggerMessages
);
81 copyRepositoryFiles("default", repository
);
85 protected void tearDown() throws Exception
{
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
]));
97 * Build an abstract File from a base file and a relative path, e.g.:
99 * toFile(repository.getWorkDir(), "a/b")
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()) {
135 for (File file
: src
.listFiles()) {
136 File fileDst
= new File(dst
, file
.getName());
137 copyFile(file
, fileDst
);
140 copyStream(new FileInputStream(src
), dst
);
144 private void copyStream(InputStream in
, File dst
) throws IOException
{
145 FileOutputStream out
= new FileOutputStream(dst
);
147 byte[] buf
= new byte[4096];
149 while ((r
= in
.read(buf
)) > 0) {
150 out
.write(buf
, 0, r
);
157 private static class MockSystemReader
extends SystemReader
{
159 private final File userConfigFile
;
161 public MockSystemReader(File userConfigFile
) {
162 this.userConfigFile
= userConfigFile
;
166 public String
getHostname() {
171 public String
getenv(String name
) {
176 public String
getProperty(String arg0
) {
181 public FileBasedConfig
openUserConfig() {
182 return new FileBasedConfig(userConfigFile
);