Class RepositoryTestCase

java.lang.Object
org.eclipse.jgit.junit.LocalDiskRepositoryTestCase
org.eclipse.jgit.junit.RepositoryTestCase

public abstract class RepositoryTestCase extends LocalDiskRepositoryTestCase
Base class for most JGit unit tests. Sets up a predefined test repository and has support for creating additional repositories and destroying them when the tests are finished.
  • Field Details

    • db

      protected org.eclipse.jgit.internal.storage.file.FileRepository db
      Test repository, initialized for this test case.
    • trash

      protected File trash
      Working directory of db.
  • Constructor Details

    • RepositoryTestCase

      public RepositoryTestCase()
  • Method Details

    • copyFile

      protected static void copyFile(File src, File dst) throws IOException
      Copy a file
      Parameters:
      src - file to copy
      dst - destination of the copy
      Throws:
      IOException - if an IO error occurred
    • writeTrashFile

      protected File writeTrashFile(String name, String data) throws IOException
      Write a trash file
      Parameters:
      name - file name
      data - file content
      Returns:
      the trash file
      Throws:
      IOException - if an IO error occurred
    • writeLink

      protected Path writeLink(String link, String target) throws Exception
      Create a symbolic link
      Parameters:
      link - the path of the symbolic link to create
      target - the target of the symbolic link
      Returns:
      the path to the symbolic link
      Throws:
      Exception - if an error occurred
      Since:
      4.2
    • writeTrashFile

      protected File writeTrashFile(String subdir, String name, String data) throws IOException
      Write a trash file
      Parameters:
      subdir - in working tree
      name - file name
      data - file content
      Returns:
      the trash file
      Throws:
      IOException - if an IO error occurred
    • read

      protected String read(String name) throws IOException
      Read content of a file
      Parameters:
      name - file name
      Returns:
      the file's content
      Throws:
      IOException - if an IO error occurred
    • check

      protected boolean check(String name)
      Check if file exists
      Parameters:
      name - file name
      Returns:
      if the file exists
    • deleteTrashFile

      protected void deleteTrashFile(String name) throws IOException
      Delete a trash file
      Parameters:
      name - file name
      Throws:
      IOException - if an IO error occurred
    • checkFile

      protected static void checkFile(File f, String checkData) throws IOException
      Check content of a file.
      Parameters:
      f - file
      checkData - expected content
      Throws:
      IOException - if an IO error occurred
    • setUp

      public void setUp() throws Exception
      Description copied from class: LocalDiskRepositoryTestCase
      Setup test
      Overrides:
      setUp in class LocalDiskRepositoryTestCase
      Throws:
      Exception - if an error occurred
    • tearDown

      public void tearDown() throws Exception
      Description copied from class: LocalDiskRepositoryTestCase
      Tear down the test
      Overrides:
      tearDown in class LocalDiskRepositoryTestCase
      Throws:
      Exception - if an error occurred
    • indexState

      public String indexState(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:
      includedOptions - a bitmask constructed out of the constants LocalDiskRepositoryTestCase.MOD_TIME, LocalDiskRepositoryTestCase.SMUDGE, LocalDiskRepositoryTestCase.LENGTH, LocalDiskRepositoryTestCase.CONTENT_ID and LocalDiskRepositoryTestCase.CONTENT controlling which info is present in the resulting string.
      Returns:
      a string encoding the index state
      Throws:
      IOException - if an IO error occurred
    • resetIndex

      protected void resetIndex(org.eclipse.jgit.treewalk.FileTreeIterator treeItr) throws FileNotFoundException, IOException
      Resets the index to represent exactly some filesystem content. E.g. the following call will replace the index with the working tree content:

      resetIndex(new FileSystemIterator(db))

      This method can be used by testcases which first prepare a new commit somewhere in the filesystem (e.g. in the working-tree) and then want to have an index which matches their prepared content.

      Parameters:
      treeItr - a FileTreeIterator which determines which files should go into the new index
      Throws:
      FileNotFoundException - file was not found
      IOException - if an IO error occurred
    • lookup

      public static String lookup(Object l, String nameTemplate, Map<Object,String> lookupTable)
      Helper method to map arbitrary objects to user-defined names. This can be used create short names for objects to produce small and stable debug output. It is guaranteed that when you lookup the same object multiple times even with different nameTemplates this method will always return the same name which was derived from the first nameTemplate. nameTemplates can contain "%n" which will be replaced by a running number before used as a name.
      Parameters:
      l - the object to lookup
      lookupTable - a table storing object-name mappings.
      nameTemplate - the name for that object. Can contain "%n" which will be replaced by a running number before used as a name. If the lookup table already contains the object this parameter will be ignored
      Returns:
      a name of that object. Is not guaranteed to be unique. Use nameTemplates containing "%n" to always have unique names
    • slashify

      public static String slashify(String str)
      Replaces '\' by '/'
      Parameters:
      str - the string in which backslashes should be replaced
      Returns:
      the resulting string with slashes
      Since:
      4.2
    • fsTick

      public static Instant fsTick(File lastFile) throws InterruptedException, IOException
      Waits until it is guaranteed that a subsequent file modification has a younger modification timestamp than the modification timestamp of the given file. This is done by touching a temporary file, reading the lastmodified attribute and, if needed, sleeping. After sleeping this loop starts again until the filesystem timer has advanced enough. The temporary file will be created as a sibling of lastFile.
      Parameters:
      lastFile - the file on which we want to wait until the filesystem timer has advanced more than the lastmodification timestamp of this file
      Returns:
      return the last measured value of the filesystem timer which is greater than then the lastmodification time of lastfile.
      Throws:
      InterruptedException - if thread was interrupted
      IOException - if an IO error occurred
      Since:
      5.1.9
    • createBranch

      protected void createBranch(org.eclipse.jgit.lib.ObjectId objectId, String branchName) throws IOException
      Create a branch
      Parameters:
      objectId - new value to create the branch on
      branchName - branch name
      Throws:
      IOException - if an IO error occurred
    • getRefs

      public List<org.eclipse.jgit.lib.Ref> getRefs() throws IOException
      Get all Refs
      Returns:
      list of refs
      Throws:
      IOException - if an IO error occurred
    • checkoutBranch

      protected void checkoutBranch(String branchName) throws IOException
      Checkout a branch
      Parameters:
      branchName - branch name
      Throws:
      IOException - if an IO error occurred
    • writeTrashFiles

      protected File writeTrashFiles(boolean ensureDistinctTimestamps, String... contents) throws IOException, InterruptedException
      Writes a number of files in the working tree. The first content specified will be written into a file named '0', the second into a file named "1" and so on. If null is specified as content then this file is skipped.
      Parameters:
      ensureDistinctTimestamps - if set to true then between two write operations this method will wait to ensure that the second file will get a different lastmodification timestamp than the first file.
      contents - the contents which should be written into the files
      Returns:
      the File object associated to the last written file.
      Throws:
      IOException - if an IO error occurred
      InterruptedException - if thread was interrupted
    • commitFile

      protected org.eclipse.jgit.revwalk.RevCommit commitFile(String filename, String contents, String branch)
      Commit a file with the specified contents on the specified branch, creating the branch if it didn't exist before.

      It switches back to the original branch after the commit if there was one.

      Parameters:
      filename - file name
      contents - file content
      branch - branch name
      Returns:
      the created commit
    • createEntry

      protected org.eclipse.jgit.dircache.DirCacheEntry createEntry(String path, org.eclipse.jgit.lib.FileMode mode)
      Create DirCacheEntry
      Parameters:
      path - file path
      mode - file mode
      Returns:
      the DirCacheEntry
    • createEntry

      protected org.eclipse.jgit.dircache.DirCacheEntry createEntry(String path, org.eclipse.jgit.lib.FileMode mode, String content)
      Create DirCacheEntry
      Parameters:
      path - file path
      mode - file mode
      content - file content
      Returns:
      the DirCacheEntry
    • createEntry

      protected org.eclipse.jgit.dircache.DirCacheEntry createEntry(String path, org.eclipse.jgit.lib.FileMode mode, int stage, String content)
      Create DirCacheEntry
      Parameters:
      path - file path
      mode - file mode
      stage - stage index of the new entry
      content - file content
      Returns:
      the DirCacheEntry
    • createGitLink

      protected org.eclipse.jgit.dircache.DirCacheEntry createGitLink(String path, org.eclipse.jgit.lib.AnyObjectId objectId)
      Create DirCacheEntry
      Parameters:
      path - file path
      objectId - of the entry
      Returns:
      the DirCacheEntry
    • assertEqualsFile

      public static void assertEqualsFile(File expected, File actual) throws IOException
      Assert files are equal
      Parameters:
      expected - expected file
      actual - actual file
      Throws:
      IOException - if an IO error occurred