Separate FileUtils implementations for Android and desktop builds.

The method used by Commons IO to get the available disk space fails on
Android devices that lack a df binary - use the Android API instead.
This commit is contained in:
akwizgran
2013-07-27 20:50:05 +01:00
parent 0941697922
commit c868764244
23 changed files with 116 additions and 295 deletions

View File

@@ -3,6 +3,7 @@ package net.sf.briar;
import java.io.File;
import net.sf.briar.api.db.DatabaseConfig;
import net.sf.briar.api.os.FileUtils;
import com.google.inject.AbstractModule;
@@ -24,5 +25,6 @@ public class TestDatabaseModule extends AbstractModule {
protected void configure() {
bind(DatabaseConfig.class).toInstance(config);
bind(FileUtils.class).to(TestFileUtils.class);
}
}

View File

@@ -0,0 +1,15 @@
package net.sf.briar;
import java.io.File;
import java.io.IOException;
import net.sf.briar.api.os.FileUtils;
import org.apache.commons.io.FileSystemUtils;
public class TestFileUtils implements FileUtils {
public long getFreeSpace(File f) throws IOException {
return FileSystemUtils.freeSpaceKb(f.getAbsolutePath()) * 1024;
}
}

View File

@@ -21,6 +21,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestDatabaseConfig;
import net.sf.briar.TestFileUtils;
import net.sf.briar.TestMessage;
import net.sf.briar.TestUtils;
import net.sf.briar.api.Author;
@@ -1931,7 +1932,7 @@ public class H2DatabaseTest extends BriarTestCase {
private Database<Connection> open(boolean resume) throws Exception {
Database<Connection> db = new H2Database(new TestDatabaseConfig(testDir,
MAX_SIZE), new SystemClock());
MAX_SIZE), new TestFileUtils(), new SystemClock());
if(!resume) TestUtils.deleteTestDirectory(testDir);
db.open();
return db;

View File

@@ -12,8 +12,10 @@ import java.util.List;
import java.util.concurrent.Executor;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestFileUtils;
import net.sf.briar.TestUtils;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.os.FileUtils;
import net.sf.briar.api.plugins.simplex.SimplexPluginCallback;
import net.sf.briar.api.plugins.simplex.SimplexTransportWriter;
import net.sf.briar.plugins.ImmediateExecutor;
@@ -29,6 +31,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
private final File testDir = TestUtils.getTestDirectory();
private final ContactId contactId = new ContactId(234);
private final FileUtils fileUtils = new TestFileUtils();
@Before
public void setUp() {
@@ -55,7 +58,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
callback, finder, monitor, MAX_FRAME_LENGTH, 0);
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0);
plugin.start();
assertNull(plugin.createWriter(contactId));
@@ -90,7 +93,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
callback, finder, monitor, MAX_FRAME_LENGTH, 0);
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0);
plugin.start();
assertNull(plugin.createWriter(contactId));
@@ -127,7 +130,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
callback, finder, monitor, MAX_FRAME_LENGTH, 0);
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0);
plugin.start();
assertNull(plugin.createWriter(contactId));
@@ -166,7 +169,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
callback, finder, monitor, MAX_FRAME_LENGTH, 0);
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0);
plugin.start();
assertNull(plugin.createWriter(contactId));
@@ -205,7 +208,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
callback, finder, monitor, MAX_FRAME_LENGTH, 0);
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0);
plugin.start();
assertNotNull(plugin.createWriter(contactId));
@@ -248,7 +251,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
callback, finder, monitor, MAX_FRAME_LENGTH, 0);
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0);
plugin.start();
SimplexTransportWriter writer = plugin.createWriter(contactId);
@@ -287,7 +290,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
callback, finder, monitor, MAX_FRAME_LENGTH, 0);
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0);
plugin.start();
plugin.driveInserted(testDir);
@@ -307,7 +310,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
context.mock(RemovableDriveMonitor.class);
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
callback, finder, monitor, MAX_FRAME_LENGTH, 0);
fileUtils, callback, finder, monitor, MAX_FRAME_LENGTH, 0);
assertFalse(plugin.isPossibleConnectionFilename("abcdefg.dat"));
assertFalse(plugin.isPossibleConnectionFilename("abcdefghi.dat"));
@@ -335,7 +338,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(
new ImmediateExecutor(), callback, finder, monitor,
new ImmediateExecutor(), fileUtils, callback, finder, monitor,
MAX_FRAME_LENGTH, 0);
plugin.start();

View File

@@ -1,165 +0,0 @@
package net.sf.briar.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.util.FileUtils.Callback;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class FileUtilsTest extends BriarTestCase {
private final File testDir = TestUtils.getTestDirectory();
@Before
public void setUp() {
testDir.mkdirs();
}
@Test
public void testCreateTempFile() throws IOException {
File temp = FileUtils.createTempFile();
assertTrue(temp.exists());
assertTrue(temp.isFile());
assertEquals(0, temp.length());
temp.delete();
}
@Test
public void testCopy() throws IOException {
File src = new File(testDir, "src");
File dest = new File(testDir, "dest");
TestUtils.createFile(src, "Foo bar\r\nBar foo\r\n");
long length = src.length();
FileUtils.copy(src, dest);
assertEquals(length, dest.length());
Scanner in = new Scanner(dest);
assertTrue(in.hasNextLine());
assertEquals("Foo bar", in.nextLine());
assertTrue(in.hasNextLine());
assertEquals("Bar foo", in.nextLine());
assertFalse(in.hasNext());
in.close();
}
@Test
public void testCopyFromStream() throws IOException {
File src = new File(testDir, "src");
File dest = new File(testDir, "dest");
TestUtils.createFile(src, "Foo bar\r\nBar foo\r\n");
long length = src.length();
InputStream is = new FileInputStream(src);
is.skip(4);
FileUtils.copy(is, dest);
assertEquals(length - 4, dest.length());
Scanner in = new Scanner(dest);
assertTrue(in.hasNextLine());
assertEquals("bar", in.nextLine());
assertTrue(in.hasNextLine());
assertEquals("Bar foo", in.nextLine());
assertFalse(in.hasNext());
in.close();
}
@Test
public void testCopyRecursively() throws IOException {
final File dest1 = new File(testDir, "dest/abc/def/1");
final File dest2 = new File(testDir, "dest/abc/def/2");
final File dest3 = new File(testDir, "dest/abc/3");
Mockery context = new Mockery();
final Callback callback = context.mock(Callback.class);
context.checking(new Expectations() {{
oneOf(callback).processingFile(dest1);
oneOf(callback).processingFile(dest2);
oneOf(callback).processingFile(dest3);
}});
copyRecursively(callback);
context.assertIsSatisfied();
}
@Test
public void testCopyRecursivelyNoCallback() throws IOException {
copyRecursively(null);
}
private void copyRecursively(Callback callback) throws IOException {
TestUtils.createFile(new File(testDir, "abc/def/1"), "one one one");
TestUtils.createFile(new File(testDir, "abc/def/2"), "two two two");
TestUtils.createFile(new File(testDir, "abc/3"), "three three three");
File dest = new File(testDir, "dest");
dest.mkdir();
FileUtils.copyRecursively(new File(testDir, "abc"), dest, callback);
File dest1 = new File(testDir, "dest/abc/def/1");
assertTrue(dest1.exists());
assertTrue(dest1.isFile());
assertEquals("one one one".length(), dest1.length());
File dest2 = new File(testDir, "dest/abc/def/2");
assertTrue(dest2.exists());
assertTrue(dest2.isFile());
assertEquals("two two two".length(), dest2.length());
File dest3 = new File(testDir, "dest/abc/3");
assertTrue(dest3.exists());
assertTrue(dest3.isFile());
assertEquals("three three three".length(), dest3.length());
}
@Test
public void testDeleteFile() throws IOException {
File foo = new File(testDir, "foo");
foo.createNewFile();
assertTrue(foo.exists());
FileUtils.delete(foo);
assertFalse(foo.exists());
}
@Test
public void testDeleteDirectory() throws IOException {
File f1 = new File(testDir, "abc/def/1");
File f2 = new File(testDir, "abc/def/2");
File f3 = new File(testDir, "abc/3");
File abc = new File(testDir, "abc");
File def = new File(testDir, "abc/def");
TestUtils.createFile(f1, "one one one");
TestUtils.createFile(f2, "two two two");
TestUtils.createFile(f3, "three three three");
assertTrue(f1.exists());
assertTrue(f2.exists());
assertTrue(f3.exists());
assertTrue(abc.exists());
assertTrue(def.exists());
FileUtils.delete(def);
assertFalse(f1.exists());
assertFalse(f2.exists());
assertTrue(f3.exists());
assertTrue(abc.exists());
assertFalse(def.exists());
}
@After
public void tearDown() {
TestUtils.deleteTestDirectory(testDir);
}
}