Skip to content

Commit 431d47a

Browse files
committed
fix: sonar issue and coverage
1 parent a1995e8 commit 431d47a

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

fj-doc-maven-plugin/src/main/java/org/fugerit/java/doc/project/facade/FeatureFacade.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ private FeatureFacade() {}
2424
* Checks that the given file is inside the baseFolder after normalization.
2525
* Throws IOException if not.
2626
*/
27-
private static void checkIfInBaseFolder(File baseFolder, File file) throws IOException {
27+
public static void checkIfInBaseFolder(File baseFolder, File file) throws IOException {
2828
Path base = baseFolder.getCanonicalFile().toPath().normalize();
2929
Path target = file.getCanonicalFile().toPath().normalize();
3030
if (!target.startsWith(base)) {
31-
throw new IOException("File path " + file.getCanonicalPath() + " is not within permitted base folder " + baseFolder.getCanonicalPath());
31+
throw new IOException( String.format( "File path %s is not within permitted base folder %s", file.getCanonicalPath(), baseFolder.getCanonicalPath() ) );
3232
}
3333
}
3434

@@ -50,17 +50,17 @@ private static void copyResourcesList( File baseFolder, String mode, String id )
5050
}
5151
}
5252

53-
protected static void insureParent( File file ) throws IOException {
53+
public static void insureParent( File file ) throws IOException {
5454
File parentFile = file.getParentFile();
5555
// Defensive: check parent is within project's root as well
5656
if (parentFile != null) {
5757
File baseFolder = file.getParentFile().getParentFile();
5858
if (baseFolder != null) {
5959
checkIfInBaseFolder(baseFolder, parentFile);
6060
}
61-
}
62-
if ( !parentFile.exists() ) {
63-
log.info( "creates parent directory {}, mkdirs:? {}", parentFile.getCanonicalPath(), parentFile.mkdirs() );
61+
if ( !parentFile.exists() ) {
62+
log.info( "creates parent directory {}, mkdirs:? {}", parentFile.getCanonicalPath(), parentFile.mkdirs() );
63+
}
6464
}
6565
}
6666

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package test.org.fugerit.java.doc.project.facade;
2+
3+
import org.fugerit.java.doc.project.facade.FeatureFacade;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
10+
class TestFeatureFacade {
11+
12+
@Test
13+
void checkIfInBaseFolderTestOk() throws IOException {
14+
File baseFolder = new File( "." );
15+
File file = new File( "pom.xml" );
16+
FeatureFacade.checkIfInBaseFolder( baseFolder, file );
17+
Assertions.assertTrue( file.exists() );
18+
}
19+
20+
@Test
21+
void checkIfInBaseFolderTestKo() {
22+
File baseFolder = new File( "fj-doc-base" );
23+
File file = new File( "pom.xml" );
24+
Assertions.assertThrows( IOException.class, () -> FeatureFacade.checkIfInBaseFolder( baseFolder, file ) );
25+
}
26+
27+
@Test
28+
void insureParentTestNull1() throws IOException {
29+
File root = File.listRoots()[0];
30+
FeatureFacade.insureParent( root );
31+
Assertions.assertTrue( root.exists() );
32+
}
33+
34+
@Test
35+
void insureParentTestNull2() throws IOException {
36+
File root = File.listRoots()[0];
37+
if ( root != null ) {
38+
File[] list = root.listFiles();
39+
if ( list != null && list.length > 0 ) {
40+
FeatureFacade.insureParent( list[0] );
41+
Assertions.assertTrue( root.exists() );
42+
}
43+
}
44+
}
45+
46+
}

0 commit comments

Comments
 (0)