Top Description Fields Constructors Methods
org.eclipse.jgit.junit

public abstract Class LocalDiskRepositoryTestCase

extends Object
Class Inheritance
Known Direct Subclasses
org.eclipse.jgit.junit.RepositoryTestCase
Static Imports
java.nio.charset.StandardCharsets.UTF_8, org.junit.Assert.assertFalse, .Assert.fail

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 org.eclipse.jgit.lib.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.

Field Summary

Modifier and TypeField and Description
public static final 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.

public static final int
CONTENT

Constant CONTENT=16

public static final int
CONTENT_ID

Constant CONTENT_ID=8

public TestName
currentTest

The current test name.

private File
public static final int
LENGTH

Constant LENGTH=4

protected MockSystemReader
mockSystemReader

A SystemReader used to coordinate time, envars, etc.

public static final int
MOD_TIME

Constant MOD_TIME=1

pack-priv Random
public static final int
SMUDGE

Constant SMUDGE=2

public TemporaryFolder
testRoot

Temporary test root directory for files created by tests.

private File
private final Set<Repository>
private static final boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
addRepoToClose(Repository
the repository to be closed
r
)

Adds a repository to the list of repositories which is closed at the end of the tests

private void
private static HashMap<String, String>
protected FileRepository

Returns:

the newly created bare repository, opened for access. The repository will not be closed in tearDown(); the caller is responsible for closing it.
createBareRepository
()

Creates a new empty bare repository.

protected FileRepository

Returns:

the newly created repository, opened for access. The repository will not be closed in tearDown(); the caller is responsible for closing it.
createRepository
(boolean
true to create a bare repository; false to make a repository within its working directory
bare
)

Creates a new empty repository.

public FileRepository

Returns:

the newly created repository, opened for access
createRepository
(boolean
true to create a bare repository; false to make a repository within its working directory
bare
,
boolean
auto close the repository in tearDown()
autoClose
)
Deprecated use createRepository(boolean) instead

Creates a new empty repository.

protected File

Returns:

a unique directory for a test
createTempDirectory
(String
a subdirectory
name
)

Creates a unique directory for a test

protected File

Returns:

a unique path that does not exist.
createTempFile
()

Allocates a new unique file path that does not exist.

protected File

Returns:

a unique directory for a test repository
createUniqueTestGitDir
(boolean
true for a bare repository; false for a repository with a working directory
bare
)

Creates a new unique directory for a test repository

protected FileRepository

Returns:

the newly created repository, opened for access. The repository will not be closed in tearDown(); the caller is responsible for closing it.
createWorkRepository
()

Creates a new empty repository within a new empty working directory.

protected List<File>

Returns:

list of ceiling directories
getCeilings
()

Get list of ceiling directories

protected File

Returns:

the temporary directory
getTemporaryDirectory
()

Get temporary directory.

private String
public static String

Returns:

a string encoding the index state
indexState
(Repository
the repository the index state should be determined for
repo
,
int
a bitmask constructed out of the constants MOD_TIME, SMUDGE, LENGTH, CONTENT_ID and CONTENT controlling which info is present in the resulting string.
includedOptions
)

Represent the state of the index in one String.

private static String
makePath(List<?> objects)

private static void
putPersonIdent(final Map<String, String> env, final String type, final PersonIdent who)

protected String

Returns:

the content of the file
read
(File
the file
f
)

Read a file's content

protected void
recursiveDelete(File
the recursively directory to delete, if present.
dir
)

Recursively delete a directory, failing the test if the delete fails.

private static boolean
recursiveDelete(final File dir, boolean silent, boolean failOnError)

private static void
reportDeleteFailure(boolean failOnError, File f, Exception cause)

protected int

Returns:

exit status code of the invoked hook
runHook
(final Repository
repository the script should see in GIT_DIR environment
db
,
final File
path of the hook script to execute, must be executable file type on this platform
hook
,
final String...
arguments to pass to the hook script
args
)

Run a hook script in the repository, returning the exit status.

public void
setUp()

Setup test

public void
tearDown()

Tear down the test

protected void
tick()

Increment the author and committer times.

private static String[]
protected File

Returns:

path of the temporary file created within the trash area.
write
(String
complete content to write to the file. If the file should end with a trailing LF, the string should end with an LF.
body
)

Create a string to a UTF-8 temporary file and return the path.

protected void
write(File
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.
f
,
String
content to write to the file.
body
)

Write a string as a UTF-8 file.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ASSUME_UNCHANGEDback to summary
public static final int ASSUME_UNCHANGED

Constant ASSUME_UNCHANGED=32

authorback to summary
protected PersonIdent author

A fake (but stable) identity for author fields in the test.

committerback to summary
protected PersonIdent committer

A fake (but stable) identity for committer fields in the test.

CONTENTback to summary
public static final int CONTENT

Constant CONTENT=16

CONTENT_IDback to summary
public static final int CONTENT_ID

Constant CONTENT_ID=8

currentTestback to summary
public TestName currentTest

The current test name.

Annotations
@Rule
Since
6.0.1
homeDirback to summary
private File homeDir
LENGTHback to summary
public static final int LENGTH

Constant LENGTH=4

mockSystemReaderback to summary
protected MockSystemReader mockSystemReader

A SystemReader used to coordinate time, envars, etc.

Since
4.2
MOD_TIMEback to summary
public static final int MOD_TIME

Constant MOD_TIME=1

randback to summary
pack-priv Random rand
SMUDGEback to summary
public static final int SMUDGE

Constant SMUDGE=2

testRootback to summary
public TemporaryFolder testRoot

Temporary test root directory for files created by tests.

Annotations
@Rule
Since
7.2
tmpback to summary
private File tmp
toCloseback to summary
private final Set<Repository> toClose
useMMAPback to summary
private static final boolean useMMAP

Constructor Detail

LocalDiskRepositoryTestCaseback to summary
public LocalDiskRepositoryTestCase()

Method Detail

addRepoToCloseback to summary
public void addRepoToClose(Repository r)

Adds a repository to the list of repositories which is closed at the end of the tests

Parameters
r:Repository

the repository to be closed

ceilTestDirectoriesback to summary
private void ceilTestDirectories(List<File> ceilings)
cloneEnvback to summary
private static HashMap<String, String> cloneEnv()
createBareRepositoryback to summary
protected FileRepository createBareRepository() throws IOException

Creates a new empty bare repository.

Returns:FileRepository

the newly created bare repository, opened for access. The repository will not be closed in tearDown(); the caller is responsible for closing it.

Exceptions
IOException:
the repository could not be created in the temporary area
createRepositoryback to summary
protected FileRepository createRepository(boolean bare) throws IOException

Creates a new empty repository.

Parameters
bare:boolean

true to create a bare repository; false to make a repository within its working directory

Returns:FileRepository

the newly created repository, opened for access. The repository will not be closed in tearDown(); the caller is responsible for closing it.

Exceptions
IOException:
the repository could not be created in the temporary area
Since
5.3
createRepositoryback to summary
public FileRepository createRepository(boolean bare, boolean autoClose) throws IOException

Deprecated

use createRepository(boolean) instead

Creates a new empty repository.

Parameters
bare:boolean

true to create a bare repository; false to make a repository within its working directory

autoClose:boolean

auto close the repository in tearDown()

Returns:FileRepository

the newly created repository, opened for access

Annotations
@Deprecated
Exceptions
IOException:
the repository could not be created in the temporary area
createTempDirectoryback to summary
protected File createTempDirectory(String name) throws IOException

Creates a unique directory for a test

Parameters
name:String

a subdirectory

Returns:File

a unique directory for a test

Exceptions
IOException:
if an IO error occurred
createTempFileback to summary
protected File createTempFile() throws 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:File

a unique path that does not exist.

Exceptions
IOException:
if an IO error occurred
createUniqueTestGitDirback to summary
protected File createUniqueTestGitDir(boolean bare) throws IOException

Creates a new unique directory for a test repository

Parameters
bare:boolean

true for a bare repository; false for a repository with a working directory

Returns:File

a unique directory for a test repository

Exceptions
IOException:
if an IO error occurred
createWorkRepositoryback to summary
protected FileRepository createWorkRepository() throws IOException

Creates a new empty repository within a new empty working directory.

Returns:FileRepository

the newly created repository, opened for access. The repository will not be closed in tearDown(); the caller is responsible for closing it.

Exceptions
IOException:
the repository could not be created in the temporary area
getCeilingsback to summary
protected List<File> getCeilings()

Get list of ceiling directories

Returns:List<File>

list of ceiling directories

getTemporaryDirectoryback to summary
protected File getTemporaryDirectory()

Get temporary directory.

Returns:File

the temporary directory

getTestNameback to summary
private String getTestName()
indexStateback to summary
public static String indexState(Repository repo, int includedOptions) throws 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:Repository

the repository the index state should be determined for

includedOptions:int

a bitmask constructed out of the constants MOD_TIME, SMUDGE, LENGTH, CONTENT_ID and CONTENT controlling which info is present in the resulting string.

Returns:String

a string encoding the index state

Exceptions
IOException:
if an IO error occurred
makePathback to summary
private static String makePath(List<?> objects)
putPersonIdentback to summary
private static void putPersonIdent(final Map<String, String> env, final String type, final PersonIdent who)
readback to summary
protected String read(File f) throws IOException

Read a file's content

Parameters
f:File

the file

Returns:String

the content of the file

Exceptions
IOException:
if an IO error occurred
recursiveDeleteback to summary
protected void recursiveDelete(File dir)

Recursively delete a directory, failing the test if the delete fails.

Parameters
dir:File

the recursively directory to delete, if present.

recursiveDeleteback to summary
private static boolean recursiveDelete(final File dir, boolean silent, boolean failOnError)
reportDeleteFailureback to summary
private static void reportDeleteFailure(boolean failOnError, File f, Exception cause)
runHookback to summary
protected int runHook(final Repository db, final File hook, final String... args) throws IOException, InterruptedException

Run a hook script in the repository, returning the exit status.

Parameters
db:Repository

repository the script should see in GIT_DIR environment

hook:File

path of the hook script to execute, must be executable file type on this platform

args:String[]

arguments to pass to the hook script

Returns:int

exit status code of the invoked hook

Exceptions
IOException:
the hook could not be executed
InterruptedException:
the caller was interrupted before the hook completed
setUpback to summary
public void setUp() throws Exception

Setup test

Annotations
@Before
Exceptions
Exception:
if an error occurred
tearDownback to summary
public void tearDown() throws Exception

Tear down the test

Annotations
@After
Exceptions
Exception:
if an error occurred
tickback to summary
protected void tick()

Increment the author and committer times.

toEnvArrayback to summary
private static String[] toEnvArray(Map<String, String> env)
writeback to summary
protected File write(String body) throws IOException

Create a string to a UTF-8 temporary file and return the path.

Parameters
body:String

complete content to write to the file. If the file should end with a trailing LF, the string should end with an LF.

Returns:File

path of the temporary file created within the trash area.

Exceptions
IOException:
the file could not be written.
writeback to summary
protected void write(File f, String body) throws IOException

Write a string as a UTF-8 file.

Parameters
f:File

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:String

content to write to the file.

Exceptions
IOException:
the file could not be written.