Class LocalDiskRepositoryTestCase
- java.lang.Object
-
- org.eclipse.jgit.junit.LocalDiskRepositoryTestCase
-
- Direct Known Subclasses:
HttpTestCase
,RepositoryTestCase
public abstract class LocalDiskRepositoryTestCase extends java.lang.Object
JUnit TestCase with specialized support for temporary local repository.A temporary directory is created for each test, allowing each test to use a fresh environment. The temporary directory is cleaned up after the test ends.
Callers should not use
RepositoryCache
from within these tests as it may wedge file descriptors open past the end of the test.A system property
jgit.junit.usemmap
defines whether memory mapping is used. Memory mapping has an effect on the file system, in that memory mapped files in Java cannot be deleted as long as the mapped arrays have not been reclaimed by the garbage collector. The programmer cannot control this with precision, so temporary files may hang around longer than desired during a test, or tests may fail altogether if there is insufficient file descriptors or address space for the test process.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
LocalDiskRepositoryTestCase.CleanupThread
-
Field Summary
Fields Modifier and Type Field Description static int
ASSUME_UNCHANGED
ConstantASSUME_UNCHANGED=32
protected PersonIdent
author
A fake (but stable) identity for author fields in the test.protected PersonIdent
committer
A fake (but stable) identity for committer fields in the test.static int
CONTENT
ConstantCONTENT=16
static int
CONTENT_ID
ConstantCONTENT_ID=8
static int
LENGTH
ConstantLENGTH=4
protected MockSystemReader
mockSystemReader
ASystemReader
used to coordinate time, envars, etc.static int
MOD_TIME
ConstantMOD_TIME=1
static int
SMUDGE
ConstantSMUDGE=2
private java.io.File
tmp
private java.util.Set<Repository>
toClose
private static boolean
useMMAP
-
Constructor Summary
Constructors Constructor Description LocalDiskRepositoryTestCase()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addRepoToClose(Repository r)
Adds a repository to the list of repositories which is closed at the end of the testsprivate void
ceilTestDirectories(java.util.List<java.io.File> ceilings)
private static java.util.HashMap<java.lang.String,java.lang.String>
cloneEnv()
protected FileRepository
createBareRepository()
Creates a new empty bare repository.protected FileRepository
createRepository(boolean bare)
Creates a new empty repository.FileRepository
createRepository(boolean bare, boolean autoClose)
Deprecated.usecreateRepository(boolean)
insteadprotected java.io.File
createTempDirectory(java.lang.String name)
Creates a unique directory for a testprotected java.io.File
createTempFile()
Allocates a new unique file path that does not exist.protected java.io.File
createUniqueTestGitDir(boolean bare)
Creates a new unique directory for a test repositoryprotected FileRepository
createWorkRepository()
Creates a new empty repository within a new empty working directory.protected java.util.List<java.io.File>
getCeilings()
Get list of ceiling directoriesprotected java.io.File
getTemporaryDirectory()
Get temporary directory.static java.lang.String
indexState(Repository repo, int includedOptions)
Represent the state of the index in one String.private static java.lang.String
makePath(java.util.List<?> objects)
private static void
putPersonIdent(java.util.Map<java.lang.String,java.lang.String> env, java.lang.String type, PersonIdent who)
protected java.lang.String
read(java.io.File f)
Read a file's contentprotected void
recursiveDelete(java.io.File dir)
Recursively delete a directory, failing the test if the delete fails.private static boolean
recursiveDelete(java.io.File dir, boolean silent, boolean failOnError)
private static void
reportDeleteFailure(boolean failOnError, java.io.File f, java.lang.Exception cause)
protected int
runHook(Repository db, java.io.File hook, java.lang.String... args)
Run a hook script in the repository, returning the exit status.void
setUp()
Setup testvoid
tearDown()
Tear down the testprotected void
tick()
private static java.lang.String[]
toEnvArray(java.util.Map<java.lang.String,java.lang.String> env)
protected void
write(java.io.File f, java.lang.String body)
Write a string as a UTF-8 file.protected java.io.File
write(java.lang.String body)
Create a string to a UTF-8 temporary file and return the path.
-
-
-
Field Detail
-
useMMAP
private static final boolean useMMAP
-
author
protected PersonIdent author
A fake (but stable) identity for author fields in the test.
-
committer
protected PersonIdent committer
A fake (but stable) identity for committer fields in the test.
-
mockSystemReader
protected MockSystemReader mockSystemReader
ASystemReader
used to coordinate time, envars, etc.- Since:
- 4.2
-
toClose
private final java.util.Set<Repository> toClose
-
tmp
private java.io.File tmp
-
MOD_TIME
public static final int MOD_TIME
ConstantMOD_TIME=1
- See Also:
- Constant Field Values
-
SMUDGE
public static final int SMUDGE
ConstantSMUDGE=2
- See Also:
- Constant Field Values
-
LENGTH
public static final int LENGTH
ConstantLENGTH=4
- See Also:
- Constant Field Values
-
CONTENT_ID
public static final int CONTENT_ID
ConstantCONTENT_ID=8
- See Also:
- Constant Field Values
-
CONTENT
public static final int CONTENT
ConstantCONTENT=16
- See Also:
- Constant Field Values
-
ASSUME_UNCHANGED
public static final int ASSUME_UNCHANGED
ConstantASSUME_UNCHANGED=32
- See Also:
- Constant Field Values
-
-
Method Detail
-
setUp
public void setUp() throws java.lang.Exception
Setup test- Throws:
java.lang.Exception
-
getTemporaryDirectory
protected java.io.File getTemporaryDirectory()
Get temporary directory.- Returns:
- the temporary directory
-
getCeilings
protected java.util.List<java.io.File> getCeilings()
Get list of ceiling directories- Returns:
- list of ceiling directories
-
ceilTestDirectories
private void ceilTestDirectories(java.util.List<java.io.File> ceilings)
-
makePath
private static java.lang.String makePath(java.util.List<?> objects)
-
tearDown
public void tearDown() throws java.lang.Exception
Tear down the test- Throws:
java.lang.Exception
-
tick
protected void tick()
-
recursiveDelete
protected void recursiveDelete(java.io.File dir)
Recursively delete a directory, failing the test if the delete fails.- Parameters:
dir
- the recursively directory to delete, if present.
-
recursiveDelete
private static boolean recursiveDelete(java.io.File dir, boolean silent, boolean failOnError)
-
reportDeleteFailure
private static void reportDeleteFailure(boolean failOnError, java.io.File f, java.lang.Exception cause)
-
indexState
public static java.lang.String indexState(Repository repo, int includedOptions) throws java.lang.IllegalStateException, java.io.IOException
Represent the state of the index in one String. This representation is useful when writing tests which do assertions on the state of the index. By default information about path, mode, stage (if different from 0) is included. A bitmask controls which additional info about modificationTimes, smudge state and length is included.The format of the returned string is described with this BNF:
result = ( "[" path mode stage? time? smudge? length? sha1? content? "]" )* . mode = ", mode:" number . stage = ", stage:" number . time = ", time:t" timestamp-index . smudge = "" | ", smudged" . length = ", length:" number . sha1 = ", sha1:" hex-sha1 . content = ", content:" blob-data .
'stage' is only presented when the stage is different from 0. All reported time stamps are mapped to strings like "t0", "t1", ... "tn". The smallest reported time-stamp will be called "t0". This allows to write assertions against the string although the concrete value of the time stamps is unknown.- Parameters:
repo
- the repository the index state should be determined forincludedOptions
- a bitmask constructed out of the constantsMOD_TIME
,SMUDGE
,LENGTH
,CONTENT_ID
andCONTENT
controlling which info is present in the resulting string.- Returns:
- a string encoding the index state
- Throws:
java.lang.IllegalStateException
java.io.IOException
-
createBareRepository
protected FileRepository createBareRepository() throws java.io.IOException
Creates a new empty bare repository.- Returns:
- the newly created bare repository, opened for access. The
repository will not be closed in
tearDown()
; the caller is responsible for closing it. - Throws:
java.io.IOException
- the repository could not be created in the temporary area
-
createWorkRepository
protected FileRepository createWorkRepository() throws java.io.IOException
Creates a new empty repository within a new empty working directory.- Returns:
- the newly created repository, opened for access. The repository
will not be closed in
tearDown()
; the caller is responsible for closing it. - Throws:
java.io.IOException
- the repository could not be created in the temporary area
-
createRepository
protected FileRepository createRepository(boolean bare) throws java.io.IOException
Creates a new empty repository.- Parameters:
bare
- true to create a bare repository; false to make a repository within its working directory- Returns:
- the newly created repository, opened for access. The repository
will not be closed in
tearDown()
; the caller is responsible for closing it. - Throws:
java.io.IOException
- the repository could not be created in the temporary area- Since:
- 5.3
-
createRepository
@Deprecated public FileRepository createRepository(boolean bare, boolean autoClose) throws java.io.IOException
Deprecated.usecreateRepository(boolean)
insteadCreates a new empty repository.- Parameters:
bare
- true to create a bare repository; false to make a repository within its working directoryautoClose
- auto close the repository intearDown()
- Returns:
- the newly created repository, opened for access
- Throws:
java.io.IOException
- the repository could not be created in the temporary area
-
addRepoToClose
public void addRepoToClose(Repository r)
Adds a repository to the list of repositories which is closed at the end of the tests- Parameters:
r
- the repository to be closed
-
createTempDirectory
protected java.io.File createTempDirectory(java.lang.String name) throws java.io.IOException
Creates a unique directory for a test- Parameters:
name
- a subdirectory- Returns:
- a unique directory for a test
- Throws:
java.io.IOException
-
createUniqueTestGitDir
protected java.io.File createUniqueTestGitDir(boolean bare) throws java.io.IOException
Creates a new unique directory for a test repository- Parameters:
bare
- true for a bare repository; false for a repository with a working directory- Returns:
- a unique directory for a test repository
- Throws:
java.io.IOException
-
createTempFile
protected java.io.File createTempFile() throws java.io.IOException
Allocates a new unique file path that does not exist.Unlike the standard
File.createTempFile
the returned path does not exist, but may be created by another thread in a race with the caller. Good luck.This method is inherently unsafe due to a race condition between creating the name and the first use that reserves it.
- Returns:
- a unique path that does not exist.
- Throws:
java.io.IOException
-
runHook
protected int runHook(Repository db, java.io.File hook, java.lang.String... args) throws java.io.IOException, java.lang.InterruptedException
Run a hook script in the repository, returning the exit status.- Parameters:
db
- repository the script should see in GIT_DIR environmenthook
- path of the hook script to execute, must be executable file type on this platformargs
- arguments to pass to the hook script- Returns:
- exit status code of the invoked hook
- Throws:
java.io.IOException
- the hook could not be executedjava.lang.InterruptedException
- the caller was interrupted before the hook completed
-
putPersonIdent
private static void putPersonIdent(java.util.Map<java.lang.String,java.lang.String> env, java.lang.String type, PersonIdent who)
-
write
protected java.io.File write(java.lang.String body) throws java.io.IOException
Create a string to a UTF-8 temporary file and return the path.- Parameters:
body
- complete content to write to the file. If the file should end with a trailing LF, the string should end with an LF.- Returns:
- path of the temporary file created within the trash area.
- Throws:
java.io.IOException
- the file could not be written.
-
write
protected void write(java.io.File f, java.lang.String body) throws java.io.IOException
Write a string as a UTF-8 file.- Parameters:
f
- file to write the string to. Caller is responsible for making sure it is in the trash directory or will otherwise be cleaned up at the end of the test. If the parent directory does not exist, the missing parent directories are automatically created.body
- content to write to the file.- Throws:
java.io.IOException
- the file could not be written.
-
read
protected java.lang.String read(java.io.File f) throws java.io.IOException
Read a file's content- Parameters:
f
- the file- Returns:
- the content of the file
- Throws:
java.io.IOException
-
toEnvArray
private static java.lang.String[] toEnvArray(java.util.Map<java.lang.String,java.lang.String> env)
-
cloneEnv
private static java.util.HashMap<java.lang.String,java.lang.String> cloneEnv()
-
-