public abstract class LocalDiskRepositoryTestCase extends Object
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.
Modifier and Type | Field and Description |
---|---|
static int |
ASSUME_UNCHANGED
Constant
ASSUME_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
Constant
CONTENT=16 |
static int |
CONTENT_ID
Constant
CONTENT_ID=8 |
static int |
LENGTH
Constant
LENGTH=4 |
protected MockSystemReader |
mockSystemReader
A
SystemReader used to coordinate time, envars, etc. |
static int |
MOD_TIME
Constant
MOD_TIME=1 |
static int |
SMUDGE
Constant
SMUDGE=2 |
Constructor and Description |
---|
LocalDiskRepositoryTestCase() |
Modifier and Type | Method and Description |
---|---|
void |
addRepoToClose(Repository r)
Adds a repository to the list of repositories which is closed at the end
of the tests
|
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.
use
createRepository(boolean) instead |
protected File |
createTempDirectory(String name)
Creates a unique directory for a test
|
protected File |
createTempFile()
Allocates a new unique file path that does not exist.
|
protected File |
createUniqueTestGitDir(boolean bare)
Creates a new unique directory for a test repository
|
protected FileRepository |
createWorkRepository()
Creates a new empty repository within a new empty working directory.
|
protected List<File> |
getCeilings()
Get list of ceiling directories
|
protected File |
getTemporaryDirectory()
Get temporary directory.
|
static String |
indexState(Repository repo,
int includedOptions)
Represent the state of the index in one String.
|
protected String |
read(File f)
Read a file's content
|
protected void |
recursiveDelete(File dir)
Recursively delete a directory, failing the test if the delete fails.
|
protected int |
runHook(Repository db,
File hook,
String... args)
Run a hook script in the repository, returning the exit status.
|
void |
setUp()
Setup test
|
void |
tearDown()
Tear down the test
|
protected void |
tick()
|
protected void |
write(File f,
String body)
Write a string as a UTF-8 file.
|
protected File |
write(String body)
Create a string to a UTF-8 temporary file and return the path.
|
protected PersonIdent author
protected PersonIdent committer
protected MockSystemReader mockSystemReader
SystemReader
used to coordinate time, envars, etc.public static final int MOD_TIME
MOD_TIME=1
public static final int SMUDGE
SMUDGE=2
public static final int LENGTH
LENGTH=4
public static final int CONTENT_ID
CONTENT_ID=8
public static final int CONTENT
CONTENT=16
public static final int ASSUME_UNCHANGED
ASSUME_UNCHANGED=32
protected File getTemporaryDirectory()
protected List<File> getCeilings()
protected void tick()
protected void recursiveDelete(File dir)
dir
- the recursively directory to delete, if present.public static String indexState(Repository repo, int includedOptions) throws IllegalStateException, IOException
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.
repo
- the repository the index state should be determined forincludedOptions
- a bitmask constructed out of the constants MOD_TIME
,
SMUDGE
, LENGTH
, CONTENT_ID
and
CONTENT
controlling which info is present in the
resulting string.IllegalStateException
IOException
protected FileRepository createBareRepository() throws IOException
tearDown()
; the caller
is responsible for closing it.IOException
- the repository could not be created in the temporary areaprotected FileRepository createWorkRepository() throws IOException
tearDown()
; the caller is
responsible for closing it.IOException
- the repository could not be created in the temporary areaprotected FileRepository createRepository(boolean bare) throws IOException
bare
- true to create a bare repository; false to make a repository
within its working directorytearDown()
; the caller is
responsible for closing it.IOException
- the repository could not be created in the temporary area@Deprecated public FileRepository createRepository(boolean bare, boolean autoClose) throws IOException
createRepository(boolean)
insteadbare
- true to create a bare repository; false to make a repository
within its working directoryautoClose
- auto close the repository in tearDown()
IOException
- the repository could not be created in the temporary areapublic void addRepoToClose(Repository r)
r
- the repository to be closedprotected File createTempDirectory(String name) throws IOException
name
- a subdirectoryIOException
protected File createUniqueTestGitDir(boolean bare) throws IOException
bare
- true for a bare repository; false for a repository with a
working directoryIOException
protected File createTempFile() throws IOException
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.
IOException
protected int runHook(Repository db, File hook, String... args) throws IOException, InterruptedException
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 scriptIOException
- the hook could not be executedInterruptedException
- the caller was interrupted before the hook completedprotected File write(String body) throws IOException
body
- complete content to write to the file. If the file should end
with a trailing LF, the string should end with an LF.IOException
- the file could not be written.protected void write(File f, String body) throws IOException
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.IOException
- the file could not be written.protected String read(File f) throws IOException
f
- the fileIOException
Copyright © 2019 Eclipse JGit Project. All rights reserved.