Skip to content

Commit

Permalink
DSS-1431 : TSLRepository#clearRepository() fails if the cache directo…
Browse files Browse the repository at this point in the history
…ry doesn't exist
  • Loading branch information
pvandenbroucke committed Sep 13, 2018
1 parent 076cd47 commit 6646425
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public void setCacheDirectoryPath(String cacheDirectoryPath) {
this.cacheDirectoryPath = cacheDirectoryPath;
}

public String getCacheDirectoryPath() {
return cacheDirectoryPath;
}

public void setTrustedListsCertificateSource(TrustedListsCertificateSource trustedListsCertificateSource) {
this.trustedListsCertificateSource = trustedListsCertificateSource;
}
Expand All @@ -96,13 +100,9 @@ public Map<String, TSLValidationModel> getAllMapTSLValidationModels() {
return Collections.unmodifiableMap(new TreeMap<String, TSLValidationModel>(tsls));
}

public void clearRepository() {
try {
Utils.cleanDirectory(new File(cacheDirectoryPath));
tsls.clear();
} catch (IOException e) {
LOG.error("Unable to clean cache directory : " + e.getMessage(), e);
}
public void clearRepository() throws IOException {
Utils.cleanDirectory(new File(cacheDirectoryPath));
tsls.clear();
}

boolean isLastCountryVersion(TSLLoaderResult resultLoader) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package eu.europa.esig.dss.tsl.service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class TSLRepositoryTest {

@Rule
public TemporaryFolder folder = new TemporaryFolder();

@Test
public void clear() throws IOException {
File testFolder = folder.newFolder("test");
createFile(testFolder, "test1.xml");

assertEquals(1, testFolder.listFiles().length);

TSLRepository repo = new TSLRepository();
repo.setCacheDirectoryPath(testFolder.getAbsolutePath());
repo.clearRepository();
assertEquals(0, testFolder.listFiles().length);
assertTrue(testFolder.exists());
}

@Test(expected = FileNotFoundException.class)
public void clearFolderNotExist() throws IOException {
TSLRepository repo = new TSLRepository();
repo.setCacheDirectoryPath("wrong");
repo.clearRepository();
}

private void createFile(File folder, String name) throws IOException {
File file = new File(folder, name);
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(0);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -181,7 +182,12 @@ public void write(byte[] content, OutputStream os) throws IOException {

@Override
public void cleanDirectory(File directory) throws IOException {
FileUtils.cleanDirectory(directory);
try {
FileUtils.cleanDirectory(directory);
} catch (IllegalArgumentException e) {
// Apache throws IllegalArgumentException
throw new FileNotFoundException(e.getMessage());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;

import com.google.common.base.Ascii;
import com.google.common.base.CharMatcher;
Expand Down Expand Up @@ -228,8 +230,9 @@ public void write(byte[] content, OutputStream os) throws IOException {

@Override
public void cleanDirectory(File directory) throws IOException {
if (directory == null || !directory.exists()) {
throw new IllegalArgumentException("Not exists");
Objects.requireNonNull(directory, "Directory cannot be null");
if (!directory.exists() || !directory.isDirectory()) {
throw new FileNotFoundException("Directory '" + directory.getAbsolutePath() + "' not found");
} else if (directory.isDirectory()) {
File[] listFiles = directory.listFiles();
if (listFiles != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,25 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import eu.europa.esig.dss.utils.Utils;

public abstract class AbstractUtilsTest {

@Rule
public TemporaryFolder folder = new TemporaryFolder();

@Test
public void isStringEmpty() {
assertTrue(Utils.isStringEmpty(""));
Expand Down Expand Up @@ -295,4 +301,16 @@ public void listFiles() {
listFiles = Utils.listFiles(folder, extensions, true);
assertTrue(Utils.isCollectionEmpty(listFiles));
}

@Test
public void clearDirectory() throws IOException {
File tempFolder = folder.newFolder("test");
Utils.cleanDirectory(tempFolder);
}

@Test(expected = FileNotFoundException.class)
public void clearDirectoryNotFound() throws IOException {
Utils.cleanDirectory(new File("wrong"));
}

}

0 comments on commit 6646425

Please sign in to comment.