Skip to content

Commit 3297595

Browse files
committed
Add unit tests for FileUtilsTest
1 parent 1613977 commit 3297595

File tree

8 files changed

+547
-183
lines changed

8 files changed

+547
-183
lines changed

pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,11 @@
174174
<version>1.17.3</version>
175175
<scope>test</scope>
176176
</dependency>
177+
<!-- We use mockito-inline as it supports mocking static methods -->
177178
<dependency>
178179
<groupId>org.mockito</groupId>
179-
<artifactId>mockito-all</artifactId>
180-
<version>1.10.19</version>
180+
<artifactId>mockito-inline</artifactId>
181+
<version>4.7.0</version>
181182
<scope>test</scope>
182183
</dependency>
183184
</dependencies>

src/main/java/org/commonwl/view/Scheduler.java

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -115,34 +115,23 @@ public void clearTmpDir() {
115115
* @param temporaryDirectory temporary directory
116116
*/
117117
private void clearDirectory(String temporaryDirectory) {
118-
final Path dir = Paths.get(temporaryDirectory);
119118
final Instant cutoff = Instant.now().minus(Duration.ofDays(TMP_DIR_AGE_LIMIT_DAYS));
120-
// TODO: Commons IO 2.12 has a constructor that takes an Instant; drop the Date#from call here when we upgrade.
121-
final AgeFileFilter fileAndDirFilter = new AgeFileFilter(Date.from(cutoff));
122-
final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(fileAndDirFilter, fileAndDirFilter);
123-
124-
// Walk the files.
125-
try {
126-
Files.walkFileTree(dir, Collections.emptySet(), /* maxDepth */ 1, visitor);
127-
} catch (IOException e) {
128-
// Really unexpected. walkFileTree should throw an IllegalArgumentException for negative maxDepth (clearly
129-
// not happening here), a SecurityException if the security manager denies access, or this IOException in
130-
// the cases where an I/O error happened (disk error, OS error, file not found, etc.). So just a warning.
131-
logger.warn(String.format("Unexpected I/O error was thrown walking directory [%s]: %s", dir, e.getMessage()), e);
132-
}
133119

134-
// Delete the directories accumulated by the visitor.
135-
final List<Path> dirList = visitor.getDirList();
136-
dirList.forEach(tooOldDeleteMe -> {
137-
File fileToDelete = tooOldDeleteMe.toFile();
138-
try {
139-
FileUtils.forceDelete(fileToDelete);
140-
} catch (IOException e) {
141-
// Here we probably have a more serious case. Since the Git repository, RO directory, or graphs are
142-
// not expected to be in use, and the application must have access, I/O errors are not expected and
143-
// must be treated as errors.
144-
logger.error(String.format("Failed to delete old temporary file or directory [%s]: %s", fileToDelete.getAbsolutePath(), e.getMessage()), e);
120+
File temporaryDirectoryFile = new File(temporaryDirectory);
121+
String[] files = temporaryDirectoryFile.list(new AgeFileFilter(Date.from(cutoff)));
122+
123+
if (files != null && files.length > 0) {
124+
for (String fileName : files) {
125+
File fileToDelete = new File(temporaryDirectoryFile, fileName);
126+
try {
127+
FileUtils.forceDelete(fileToDelete);
128+
} catch (IOException e) {
129+
// Here we probably have a more serious case. Since the Git repository, RO directory, or graphs are
130+
// not expected to be in use, and the application must have access, I/O errors are not expected and
131+
// must be treated as errors.
132+
logger.error(String.format("Failed to delete old temporary file or directory [%s]: %s", fileToDelete.getAbsolutePath(), e.getMessage()), e);
133+
}
145134
}
146-
});
135+
}
147136
}
148137
}

src/main/java/org/commonwl/view/researchobject/ROBundleFactory.java

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919

2020
package org.commonwl.view.researchobject;
2121

22-
import org.apache.commons.io.FileUtils;
2322
import org.apache.commons.io.FilenameUtils;
2423
import org.apache.taverna.robundle.Bundle;
2524
import org.apache.taverna.robundle.fs.BundleFileSystem;
2625
import org.commonwl.view.git.GitDetails;
26+
import org.commonwl.view.util.FileUtils;
2727
import org.commonwl.view.workflow.Workflow;
2828
import org.commonwl.view.workflow.WorkflowRepository;
2929
import org.slf4j.Logger;
@@ -81,25 +81,10 @@ public void createWorkflowRO(Workflow workflow)
8181

8282
// Save the bundle to the storage location in properties
8383
Path bundleLocation = roBundleService.saveToFile(bundle);
84-
// The RoBundleService#saveToFile call above will delegate to Taverna's
85-
// Bundles.closeAndSaveBundle, which empties the bundle temporary
86-
// directory without deleting the directory itself. The following call is
87-
// just for cleaning it up.
88-
if (bundle != null) {
89-
try {
90-
BundleFileSystem fs = (BundleFileSystem) bundle.getFileSystem();
91-
File tmpDir = new File(System.getProperty("java.io.tmpdir"));
92-
// The file system source will be something like /tmp/bundles_dir/hash/bundle.zip,
93-
// and we want /tmp/hash to be deleted. N.B. Taverna uses the temporary directory
94-
// for the temporary bundles' directory, not the bundles file specified by Spring.
95-
String bundleTmpDirName = fs.getSource().toAbsolutePath().getParent().getFileName().toString();
96-
File bundleTmpDir = new File(tmpDir, bundleTmpDirName);
97-
if (bundleTmpDir.exists() && bundleTmpDir.isDirectory()) {
98-
FileUtils.forceDelete(bundleTmpDir);
99-
}
100-
} catch (IOException e) {
101-
logger.warn(String.format("Failed to delete temporary directory for bundle [%s]: %s", bundle.getSource(), e.getMessage()), e);
102-
}
84+
try {
85+
FileUtils.deleteBundleTemporaryDirectory(bundle);
86+
} catch (IOException e) {
87+
logger.warn(String.format("Failed to delete temporary directory for bundle [%s]: %s", bundle.getSource(), e.getMessage()), e);
10388
}
10489

10590
// Add RO Bundle to associated workflow model

0 commit comments

Comments
 (0)