Commit 6f11387d authored by vince-bourgmayer's avatar vince-bourgmayer
Browse files

add method to remove oldestCrashlogs. add CrashlogsFileFilter with unitTest class.

parent 05d8a901
package foundation.e.drive.fileFilters;
public class CrashlogsFileFilter {
}
import java.io.File;
import java.io.FileFilter;
import foundation.e.drive.utils.ServiceExceptionHandler;
public class CrashlogsFileFilter implements FileFilter {
private final static long max_timestamp_delta = 864000000; //10 days in ms (240*3600*1000)
@Override
public boolean accept(File pathname) {
String fileTimestamp = extractTimestamp(pathname.getName(),
ServiceExceptionHandler.LOG_FILE_NAME_PREFIX,
ServiceExceptionHandler.LOG_FILE_EXTENSION);
long timestamp;
try {
timestamp = Long.parseLong(fileTimestamp);
}catch (NumberFormatException e){
//Can't parse the extracted timestamp
//This file has not the expected name. It must be removed
return true;
}
//if current Date - file date >= max deta allowed
return ((System.currentTimeMillis() - timestamp ) >= max_timestamp_delta);
}
/**
* Extract the timestamp from the name of the file
* UnitTested!
* @param fileName Filename
* @param prefix prefix to ignore
* @param extension extension to ignore
* @return the timestamp extracted from the name
*/
private String extractTimestamp(String fileName, String prefix, String extension){
return fileName.substring(prefix.length(), (fileName.length() - extension.length()));
}
}
\ No newline at end of file
......@@ -24,8 +24,11 @@ import foundation.e.drive.services.OperationManagerService;
* @author Vincent Bourgmayer
*/
public class ServiceExceptionHandler implements UncaughtExceptionHandler{
private UncaughtExceptionHandler defaultUEH;
private final static String TAG = ServiceExceptionHandler.class.getSimpleName();
public final static String CRASH_LOG_FOLDER = "crash-logs";
public final static String LOG_FILE_NAME_PREFIX = "eDrive-crash-";
public final static String LOG_FILE_EXTENSION = ".log";
private UncaughtExceptionHandler defaultUEH;
Service service;
......@@ -58,9 +61,9 @@ public class ServiceExceptionHandler implements UncaughtExceptionHandler{
Long timestamp = System.currentTimeMillis();
//Create a new file that user can sent to us
String fileName = "eDrive-crash-"+timestamp+".log";
String fileName = LOG_FILE_NAME_PREFIX+timestamp+LOG_FILE_EXTENSION;
File downloadDir = service.getApplication().getExternalFilesDir("Logs");
File downloadDir = service.getApplication().getExternalFilesDir(CRASH_LOG_FOLDER);
File logFile = new File(downloadDir, fileName);
try {
......
package foundation.e.drive.Test.FileFilterTest;
import org.junit.Assert;
import org.junit.Test;
public class CrashlogFileFilterTest {
}
private String mockFileName(String target, String prefix, String extension){
return prefix+target+extension;
}
private String extractTimestamp(String fileName, String prefix, String extension){
return fileName.substring(prefix.length(), (fileName.length() - extension.length()));
}
@Test
public void extractTimeStampFromFileNameTest(){
String prefix = "edrive-";
String extension = ".log";
String target = "";
//Case 1 Empty Target
String base = mockFileName(target, prefix, extension);
Assert.assertEquals("Base length is incorrect", prefix.length()+extension.length(), base.length());
String fileTimestamp = extractTimestamp(base, prefix, extension);
Assert.assertEquals("result is not empty String", "", fileTimestamp);
//Case 2: Prefix is empty
prefix = "";
target = "1234";
base = mockFileName(target, prefix, extension);
Assert.assertEquals("Base length is incorrect", prefix.length()+target.length()+extension.length(), base.length());
fileTimestamp = extractTimestamp(base, prefix, extension);
Assert.assertEquals("result is not empty String", target, fileTimestamp);
//Case 3: extension is empty
prefix = "edrive-";
extension = "";
base = mockFileName(target, prefix, extension);
Assert.assertEquals("Base length is incorrect", prefix.length()+target.length(), base.length());
fileTimestamp = extractTimestamp(base, prefix, extension);
Assert.assertEquals("result is not empty String", target, fileTimestamp);
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment