4343import java .nio .file .Path ;
4444import java .nio .file .Paths ;
4545import java .nio .file .attribute .FileTime ;
46- import java .util .ArrayList ;
4746import java .util .Arrays ;
4847import java .util .Date ;
4948import java .util .Iterator ;
5049import java .util .LinkedList ;
5150import java .util .List ;
5251import java .util .Map ;
5352import java .util .Set ;
53+ import java .util .stream .Collectors ;
5454
5555import org .apache .commons .lang3 .time .DateUtils ;
5656import org .eclipse .jgit .api .Git ;
@@ -1013,9 +1013,12 @@ void testGetLastHistoryEntry() throws Exception {
10131013 * getting history cache entries for directories.
10141014 */
10151015 @ Test
1016- void testGetLastHistoryEntries () throws Exception {
1016+ void testFillLastHistoryEntries () throws Exception {
10171017 File repositoryRoot = new File (repositories .getSourceRoot (), "git" );
10181018 Repository repository = RepositoryFactory .getRepository (repositoryRoot );
1019+
1020+ // Create non-empty directory without repository involvement. This will be used to check
1021+ // that fillLastHistoryEntries() does not attempt to get history entry for it.
10191022 File subDir = new File (repositoryRoot , "subdir" );
10201023 assertTrue (subDir .mkdir ());
10211024 File subFile = new File (subDir , "subfile.txt" );
@@ -1031,14 +1034,51 @@ void testGetLastHistoryEntries() throws Exception {
10311034 assertNotNull (files );
10321035 assertTrue (files .length > 0 );
10331036 assertTrue (Arrays .stream (files ).anyMatch (File ::isDirectory ));
1034- List <DirectoryEntry > directoryEntries = new ArrayList <>();
1035- for (File file : files ) {
1036- directoryEntries .add (new DirectoryEntry (file ));
1037- }
1037+ List <DirectoryEntry > directoryEntries = Arrays .stream (files ).map (DirectoryEntry ::new ).
1038+ collect (Collectors .toList ());
10381039
1039- spyCache .fillLastHistoryEntries (directoryEntries );
1040+ assertTrue ( spyCache .fillLastHistoryEntries (directoryEntries ) );
10401041 Mockito .verify (spyCache , never ()).getLastHistoryEntry (ArgumentMatchers .eq (subDir ));
10411042
1043+ assertEquals (directoryEntries .size () - 3 ,
1044+ (int ) directoryEntries .stream ().filter (e -> e .getDate () != null ).count ());
1045+ assertEquals (directoryEntries .size (),
1046+ (int ) directoryEntries .stream ().filter (e -> e .getDescription () != null ).count ());
1047+
1048+ // Cleanup.
1049+ cache .clear (repository );
1050+ }
1051+
1052+ /**
1053+ * Test {@link FileHistoryCache#fillLastHistoryEntries(List)}, in particular that it
1054+ * returns {@code false} and resets date/descriptions if some entries cannot be filled.
1055+ */
1056+ @ Test
1057+ void testFillLastHistoryEntriesAllOrNothing () throws Exception {
1058+ File repositoryRoot = new File (repositories .getSourceRoot (), "git" );
1059+ Repository repository = RepositoryFactory .getRepository (repositoryRoot );
1060+
1061+ // This file will be created without any repository involvement, therefore it will not be possible
1062+ // to get history entry for it. This should make fillLastHistoryEntries() to return false.
1063+ File subFile = new File (repositoryRoot , "file.txt" );
1064+ assertFalse (subFile .exists ());
1065+ assertTrue (subFile .createNewFile ());
1066+
1067+ FileHistoryCache spyCache = Mockito .spy (cache );
1068+ spyCache .clear (repository );
1069+ History historyToStore = repository .getHistory (repositoryRoot );
1070+ spyCache .store (historyToStore , repository );
1071+
1072+ File [] files = repositoryRoot .listFiles ();
1073+ assertNotNull (files );
1074+ assertTrue (files .length > 0 );
1075+ List <DirectoryEntry > directoryEntries = Arrays .stream (files ).map (DirectoryEntry ::new ).
1076+ collect (Collectors .toList ());
1077+
1078+ assertFalse (spyCache .fillLastHistoryEntries (directoryEntries ));
1079+ assertEquals (0 , (int ) directoryEntries .stream ().filter (e -> e .getDate () != null ).count ());
1080+ assertEquals (0 , (int ) directoryEntries .stream ().filter (e -> e .getDescription () != null ).count ());
1081+
10421082 // Cleanup.
10431083 cache .clear (repository );
10441084 }
0 commit comments