1
+ package io .github .svaningelgem ;
2
+
3
+ import org .junit .Test ;
4
+
5
+ import java .util .Collections ;
6
+ import java .util .List ;
7
+
8
+ import static org .junit .Assert .*;
9
+
10
+ public class DirectoryNodeTest extends BaseTestClass {
11
+
12
+ @ Test
13
+ public void testShouldInclude () {
14
+ DirectoryNode emptyNode = new DirectoryNode ("empty" );
15
+ assertFalse ("Empty directory should not be included" , emptyNode .shouldInclude ());
16
+
17
+ DirectoryNode withFiles = new DirectoryNode ("withFiles" );
18
+ withFiles .getSourceFiles ().add (new SourceFileNode ("Test.java" , new CoverageMetrics ()));
19
+ assertTrue ("Directory with files should be included" , withFiles .shouldInclude ());
20
+
21
+ DirectoryNode withSubDir = new DirectoryNode ("withSubDir" );
22
+ DirectoryNode subDir = new DirectoryNode ("subDir" );
23
+ subDir .getSourceFiles ().add (new SourceFileNode ("Test.java" , new CoverageMetrics ()));
24
+ withSubDir .getSubdirectories ().put ("subDir" , subDir );
25
+ assertTrue ("Directory with non-empty subdirectory should be included" , withSubDir .shouldInclude ());
26
+
27
+ DirectoryNode withEmptySubDir = new DirectoryNode ("withEmptySubDir" );
28
+ withEmptySubDir .getSubdirectories ().put ("emptySubDir" , new DirectoryNode ("emptySubDir" ));
29
+ assertFalse ("Directory with only empty subdirectory should not be included" , withEmptySubDir .shouldInclude ());
30
+ }
31
+
32
+ @ Test
33
+ public void testCollapseSingleNodeDirectory () {
34
+ DirectoryNode root = new DirectoryNode ("" );
35
+ DirectoryNode singleChild = new DirectoryNode ("single" );
36
+ root .getSubdirectories ().put ("single" , singleChild );
37
+
38
+ // This will test the shouldCollapse condition: dirNodes.size() == 1 && fileNodes.isEmpty()
39
+ root .printTree (log , "" , Defaults .LINE_FORMAT , "" , false );
40
+
41
+ // The log should only contain the collapsed path, not separate entries for root and single
42
+ assertFalse ("Log should not contain the root node separately" ,
43
+ log .writtenData .stream ().anyMatch (s -> s .contains ("<root>" )));
44
+ }
45
+
46
+ @ Test
47
+ public void testNoCollapseWithMultipleDirectories () {
48
+ DirectoryNode root = new DirectoryNode ("" );
49
+ root .getSubdirectories ().put ("dir1" , new DirectoryNode ("dir1" ));
50
+ root .getSubdirectories ().put ("dir2" , new DirectoryNode ("dir2" ));
51
+
52
+ // Add a file to each directory so shouldInclude() returns true
53
+ root .getSubdirectories ().get ("dir1" ).getSourceFiles ().add (
54
+ new SourceFileNode ("Test1.java" , new CoverageMetrics ()));
55
+ root .getSubdirectories ().get ("dir2" ).getSourceFiles ().add (
56
+ new SourceFileNode ("Test2.java" , new CoverageMetrics ()));
57
+
58
+ // This will test the !shouldCollapse condition
59
+ root .printTree (log , "" , Defaults .LINE_FORMAT , "" , false );
60
+
61
+ // The log should contain the root and both directories as separate entries
62
+ assertTrue ("Log should contain the root node" ,
63
+ log .writtenData .stream ().anyMatch (s -> s .contains ("<root>" )));
64
+ }
65
+
66
+ @ Test
67
+ public void testNoCollapseWithFiles () {
68
+ DirectoryNode root = new DirectoryNode ("" );
69
+ root .getSubdirectories ().put ("dir" , new DirectoryNode ("dir" ));
70
+ root .getSourceFiles ().add (new SourceFileNode ("RootTest.java" , new CoverageMetrics ()));
71
+
72
+ // Add a file to the directory so shouldInclude() returns true
73
+ root .getSubdirectories ().get ("dir" ).getSourceFiles ().add (
74
+ new SourceFileNode ("Test.java" , new CoverageMetrics ()));
75
+
76
+ // This will test the !shouldCollapse condition due to files in root
77
+ root .printTree (log , "" , Defaults .LINE_FORMAT , "" , true );
78
+
79
+ // The log should contain the root and directory as separate entries
80
+ assertTrue ("Log should contain the root node" ,
81
+ log .writtenData .stream ().anyMatch (s -> s .contains ("<root>" )));
82
+ }
83
+
84
+ @ Test
85
+ public void testPrintNodesWithEmptyList () {
86
+ DirectoryNode root = new DirectoryNode ("" );
87
+
88
+ // Access the private method using reflection
89
+ try {
90
+ java .lang .reflect .Method printNodesMethod = DirectoryNode .class .getDeclaredMethod (
91
+ "printNodes" ,
92
+ org .apache .maven .plugin .logging .Log .class ,
93
+ String .class ,
94
+ String .class ,
95
+ String .class ,
96
+ boolean .class ,
97
+ List .class ,
98
+ boolean .class
99
+ );
100
+ printNodesMethod .setAccessible (true );
101
+
102
+ // Call the method with an empty list - should not throw exception
103
+ printNodesMethod .invoke (root , log , "" , "" , "" , true , Collections .emptyList (), true );
104
+
105
+ // Test passed if no exception was thrown
106
+ assertTrue (true );
107
+ } catch (Exception e ) {
108
+ fail ("Exception should not be thrown: " + e .getMessage ());
109
+ }
110
+ }
111
+
112
+ @ Test
113
+ public void testDetermineNewPrefixCases () {
114
+ DirectoryNode node = new DirectoryNode ("test" );
115
+
116
+ try {
117
+ java .lang .reflect .Method determineNewPrefixMethod = DirectoryNode .class .getDeclaredMethod (
118
+ "determineNewPrefix" ,
119
+ String .class ,
120
+ boolean .class
121
+ );
122
+ determineNewPrefixMethod .setAccessible (true );
123
+
124
+ // Test with corner prefix
125
+ String result1 = (String ) determineNewPrefixMethod .invoke (node , Defaults .CORNER , true );
126
+ assertEquals ("Should replace corner with space and add corner" ,
127
+ Defaults .LAST_DIR_SPACE + Defaults .CORNER , result1 );
128
+
129
+ // Test with tee prefix
130
+ String result2 = (String ) determineNewPrefixMethod .invoke (node , Defaults .TEE , false );
131
+ assertEquals ("Should replace tee with vertical line and add tee" ,
132
+ Defaults .VERTICAL_LINE + Defaults .TEE , result2 );
133
+
134
+ // Test with other prefix not ending in corner or tee
135
+ String otherPrefix = " " ;
136
+ String result3 = (String ) determineNewPrefixMethod .invoke (node , otherPrefix , true );
137
+ assertEquals ("Should add corner to prefix without modification" ,
138
+ otherPrefix + Defaults .CORNER , result3 );
139
+
140
+ } catch (Exception e ) {
141
+ fail ("Exception should not be thrown: " + e .getMessage ());
142
+ }
143
+ }
144
+ }
0 commit comments