Skip to content

Commit a649e1b

Browse files
committed
fixed spotbugs issue and provided test for FileBoolean
1 parent 23f2b9e commit a649e1b

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

core/src/main/java/jenkins/util/io/FileBoolean.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import java.nio.file.InvalidPathException;
88
import java.util.logging.Level;
99
import java.util.logging.Logger;
10+
1011
import jenkins.model.Jenkins;
12+
import org.apache.commons.io.FilenameUtils;
1113

1214
/**
1315
* Uses a presence/absence of a file as a persisted boolean storage.
@@ -29,7 +31,7 @@ public FileBoolean(File file) {
2931
}
3032

3133
public FileBoolean(Class owner, String name) {
32-
this(new File(Jenkins.get().getRootDir(), owner.getName().replace('$', '.') + '/' + name));
34+
this(new File(Jenkins.get().getRootDir(), owner.getName().replace('$', '.') + '/' + FilenameUtils.getName(name)));
3335
}
3436

3537
/**
@@ -39,6 +41,14 @@ public boolean get() {
3941
return state = file.exists();
4042
}
4143

44+
/**
45+
* @return the getFilePath or empty string
46+
*/
47+
public String getFilePath() {
48+
if (file == null) return "";
49+
return file.getAbsolutePath();
50+
}
51+
4252
/**
4353
* Like {@link #get()} except instead of checking the actual file, use the result from the last {@link #get()} call.
4454
*/

core/src/spotbugs/excludesFilter.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,6 @@
399399
<Class name="jenkins.slaves.restarter.UnixSlaveRestarter"/>
400400
<Class name="jenkins.SoloFilePathFilter"/>
401401
<Class name="jenkins.util.groovy.GroovyHookScript"/>
402-
<Class name="jenkins.util.io.FileBoolean"/>
403402
<Class name="jenkins.util.JavaVMArguments"/>
404403
<Class name="jenkins.util.SystemProperties"/>
405404
<Class name="jenkins.util.VirtualFile$FilePathVF"/>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package jenkins.util.io;
2+
3+
import java.io.File;
4+
import jenkins.model.Jenkins;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.Test;
7+
import org.jvnet.hudson.test.JenkinsRule;
8+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
9+
10+
@WithJenkins
11+
class FileBooleanTest {
12+
13+
@Test
14+
void getFileName(JenkinsRule j) {
15+
String path = j.jenkins.getRootDir().getAbsolutePath();
16+
final String foo = new FileBoolean(Jenkins.class, "foo").getFilePath();
17+
final String fooPath = String.join(File.separator, path, Jenkins.class.getName(), "foo");
18+
Assertions.assertEquals(fooPath, foo);
19+
}
20+
}

0 commit comments

Comments
 (0)