mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 20:59:54 +01:00
Unit tests for the removable drive plugin.
This commit is contained in:
@@ -91,7 +91,6 @@ abstract class FilePlugin implements BatchTransportPlugin {
|
|||||||
OutputStream out = new FileOutputStream(f);
|
OutputStream out = new FileOutputStream(f);
|
||||||
return new FileTransportWriter(f, out, capacity, this);
|
return new FileTransportWriter(f, out, capacity, this);
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
e.printStackTrace();
|
|
||||||
f.delete();
|
f.delete();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,14 +4,24 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import net.sf.briar.api.TransportId;
|
||||||
|
|
||||||
class RemovableDrivePlugin extends FilePlugin {
|
class RemovableDrivePlugin extends FilePlugin {
|
||||||
|
|
||||||
|
public static final int TRANSPORT_ID = 0;
|
||||||
|
|
||||||
|
private static final TransportId id = new TransportId(TRANSPORT_ID);
|
||||||
|
|
||||||
private final RemovableDriveFinder finder;
|
private final RemovableDriveFinder finder;
|
||||||
|
|
||||||
RemovableDrivePlugin(RemovableDriveFinder finder) {
|
RemovableDrivePlugin(RemovableDriveFinder finder) {
|
||||||
this.finder = finder;
|
this.finder = finder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public TransportId getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected File chooseOutputDirectory() {
|
protected File chooseOutputDirectory() {
|
||||||
try {
|
try {
|
||||||
@@ -19,7 +29,7 @@ class RemovableDrivePlugin extends FilePlugin {
|
|||||||
if(drives.isEmpty()) return null;
|
if(drives.isEmpty()) return null;
|
||||||
String[] paths = new String[drives.size()];
|
String[] paths = new String[drives.size()];
|
||||||
for(int i = 0; i < paths.length; i++) {
|
for(int i = 0; i < paths.length; i++) {
|
||||||
paths[i] = drives.get(i).getAbsolutePath();
|
paths[i] = drives.get(i).getPath();
|
||||||
}
|
}
|
||||||
int i = callback.showChoice("REMOVABLE_DRIVE_CHOOSE_DRIVE", paths);
|
int i = callback.showChoice("REMOVABLE_DRIVE_CHOOSE_DRIVE", paths);
|
||||||
if(i == -1) return null;
|
if(i == -1) return null;
|
||||||
|
|||||||
@@ -9,9 +9,7 @@ import java.util.Scanner;
|
|||||||
abstract class UnixRemovableDriveFinder implements RemovableDriveFinder {
|
abstract class UnixRemovableDriveFinder implements RemovableDriveFinder {
|
||||||
|
|
||||||
protected abstract String getMountCommand();
|
protected abstract String getMountCommand();
|
||||||
|
|
||||||
protected abstract String parseMountPoint(String line);
|
protected abstract String parseMountPoint(String line);
|
||||||
|
|
||||||
protected abstract boolean isRemovableDriveMountPoint(String path);
|
protected abstract boolean isRemovableDriveMountPoint(String path);
|
||||||
|
|
||||||
public List<File> findRemovableDrives() throws IOException {
|
public List<File> findRemovableDrives() throws IOException {
|
||||||
|
|||||||
@@ -25,6 +25,7 @@
|
|||||||
<test name='net.sf.briar.i18n.FontManagerTest'/>
|
<test name='net.sf.briar.i18n.FontManagerTest'/>
|
||||||
<test name='net.sf.briar.i18n.I18nTest'/>
|
<test name='net.sf.briar.i18n.I18nTest'/>
|
||||||
<test name='net.sf.briar.invitation.InvitationWorkerTest'/>
|
<test name='net.sf.briar.invitation.InvitationWorkerTest'/>
|
||||||
|
<test name='net.sf.briar.plugins.file.RemovableDrivePluginTest'/>
|
||||||
<test name='net.sf.briar.protocol.AckReaderTest'/>
|
<test name='net.sf.briar.protocol.AckReaderTest'/>
|
||||||
<test name='net.sf.briar.protocol.BatchReaderTest'/>
|
<test name='net.sf.briar.protocol.BatchReaderTest'/>
|
||||||
<test name='net.sf.briar.protocol.ConsumersTest'/>
|
<test name='net.sf.briar.protocol.ConsumersTest'/>
|
||||||
|
|||||||
244
test/net/sf/briar/plugins/file/RemovableDrivePluginTest.java
Normal file
244
test/net/sf/briar/plugins/file/RemovableDrivePluginTest.java
Normal file
@@ -0,0 +1,244 @@
|
|||||||
|
package net.sf.briar.plugins.file;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import net.sf.briar.TestUtils;
|
||||||
|
import net.sf.briar.api.ContactId;
|
||||||
|
import net.sf.briar.api.transport.batch.BatchTransportCallback;
|
||||||
|
import net.sf.briar.api.transport.batch.BatchTransportWriter;
|
||||||
|
|
||||||
|
import org.jmock.Expectations;
|
||||||
|
import org.jmock.Mockery;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class RemovableDrivePluginTest extends TestCase {
|
||||||
|
|
||||||
|
private final File testDir = TestUtils.getTestDirectory();
|
||||||
|
private final ContactId contactId = new ContactId(0);
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
testDir.mkdirs();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetId() {
|
||||||
|
Mockery context = new Mockery();
|
||||||
|
final RemovableDriveFinder finder =
|
||||||
|
context.mock(RemovableDriveFinder.class);
|
||||||
|
RemovableDrivePlugin plugin = new RemovableDrivePlugin(finder);
|
||||||
|
|
||||||
|
assertEquals(RemovableDrivePlugin.TRANSPORT_ID,
|
||||||
|
plugin.getId().getInt());
|
||||||
|
|
||||||
|
context.assertIsSatisfied();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWriterIsNullIfNoDrivesAreFound() throws Exception {
|
||||||
|
final List<File> drives = Collections.emptyList();
|
||||||
|
|
||||||
|
Mockery context = new Mockery();
|
||||||
|
final RemovableDriveFinder finder =
|
||||||
|
context.mock(RemovableDriveFinder.class);
|
||||||
|
final BatchTransportCallback callback =
|
||||||
|
context.mock(BatchTransportCallback.class);
|
||||||
|
context.checking(new Expectations() {{
|
||||||
|
oneOf(finder).findRemovableDrives();
|
||||||
|
will(returnValue(drives));
|
||||||
|
}});
|
||||||
|
RemovableDrivePlugin plugin = new RemovableDrivePlugin(finder);
|
||||||
|
plugin.start(null, null, null, callback);
|
||||||
|
|
||||||
|
assertNull(plugin.createWriter(contactId));
|
||||||
|
|
||||||
|
context.assertIsSatisfied();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWriterIsNullIfNoDriveIsChosen() throws Exception {
|
||||||
|
final File drive1 = new File(testDir, "1");
|
||||||
|
final File drive2 = new File(testDir, "2");
|
||||||
|
final List<File> drives = new ArrayList<File>();
|
||||||
|
drives.add(drive1);
|
||||||
|
drives.add(drive2);
|
||||||
|
|
||||||
|
Mockery context = new Mockery();
|
||||||
|
final RemovableDriveFinder finder =
|
||||||
|
context.mock(RemovableDriveFinder.class);
|
||||||
|
final BatchTransportCallback callback =
|
||||||
|
context.mock(BatchTransportCallback.class);
|
||||||
|
context.checking(new Expectations() {{
|
||||||
|
oneOf(finder).findRemovableDrives();
|
||||||
|
will(returnValue(drives));
|
||||||
|
oneOf(callback).showChoice(with(any(String.class)),
|
||||||
|
with(any(String[].class)));
|
||||||
|
will(returnValue(-1)); // The user cancelled the choice
|
||||||
|
}});
|
||||||
|
RemovableDrivePlugin plugin = new RemovableDrivePlugin(finder);
|
||||||
|
plugin.start(null, null, null, callback);
|
||||||
|
|
||||||
|
assertNull(plugin.createWriter(contactId));
|
||||||
|
File[] files = drive1.listFiles();
|
||||||
|
assertTrue(files == null || files.length == 0);
|
||||||
|
|
||||||
|
context.assertIsSatisfied();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWriterIsNullIfOutputDirDoesNotExist() throws Exception {
|
||||||
|
final File drive1 = new File(testDir, "1");
|
||||||
|
final File drive2 = new File(testDir, "2");
|
||||||
|
final List<File> drives = new ArrayList<File>();
|
||||||
|
drives.add(drive1);
|
||||||
|
drives.add(drive2);
|
||||||
|
|
||||||
|
Mockery context = new Mockery();
|
||||||
|
final RemovableDriveFinder finder =
|
||||||
|
context.mock(RemovableDriveFinder.class);
|
||||||
|
final BatchTransportCallback callback =
|
||||||
|
context.mock(BatchTransportCallback.class);
|
||||||
|
context.checking(new Expectations() {{
|
||||||
|
oneOf(finder).findRemovableDrives();
|
||||||
|
will(returnValue(drives));
|
||||||
|
oneOf(callback).showChoice(with(any(String.class)),
|
||||||
|
with(any(String[].class)));
|
||||||
|
will(returnValue(0)); // The user chose drive1 but it doesn't exist
|
||||||
|
}});
|
||||||
|
RemovableDrivePlugin plugin = new RemovableDrivePlugin(finder);
|
||||||
|
plugin.start(null, null, null, callback);
|
||||||
|
|
||||||
|
assertNull(plugin.createWriter(contactId));
|
||||||
|
File[] files = drive1.listFiles();
|
||||||
|
assertTrue(files == null || files.length == 0);
|
||||||
|
|
||||||
|
context.assertIsSatisfied();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWriterIsNullIfOutputDirIsAFile() throws Exception {
|
||||||
|
final File drive1 = new File(testDir, "1");
|
||||||
|
final File drive2 = new File(testDir, "2");
|
||||||
|
final List<File> drives = new ArrayList<File>();
|
||||||
|
drives.add(drive1);
|
||||||
|
drives.add(drive2);
|
||||||
|
// Create drive1 as a file rather than a directory
|
||||||
|
assertTrue(drive1.createNewFile());
|
||||||
|
|
||||||
|
Mockery context = new Mockery();
|
||||||
|
final RemovableDriveFinder finder =
|
||||||
|
context.mock(RemovableDriveFinder.class);
|
||||||
|
final BatchTransportCallback callback =
|
||||||
|
context.mock(BatchTransportCallback.class);
|
||||||
|
context.checking(new Expectations() {{
|
||||||
|
oneOf(finder).findRemovableDrives();
|
||||||
|
will(returnValue(drives));
|
||||||
|
oneOf(callback).showChoice(with(any(String.class)),
|
||||||
|
with(any(String[].class)));
|
||||||
|
will(returnValue(0)); // The user chose drive1 but it's not a dir
|
||||||
|
}});
|
||||||
|
RemovableDrivePlugin plugin = new RemovableDrivePlugin(finder);
|
||||||
|
plugin.start(null, null, null, callback);
|
||||||
|
|
||||||
|
assertNull(plugin.createWriter(contactId));
|
||||||
|
File[] files = drive1.listFiles();
|
||||||
|
assertTrue(files == null || files.length == 0);
|
||||||
|
|
||||||
|
context.assertIsSatisfied();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWriterIsNotNullIfOutputDirIsADir() throws Exception {
|
||||||
|
final File drive1 = new File(testDir, "1");
|
||||||
|
final File drive2 = new File(testDir, "2");
|
||||||
|
final List<File> drives = new ArrayList<File>();
|
||||||
|
drives.add(drive1);
|
||||||
|
drives.add(drive2);
|
||||||
|
// Create drive1 as a directory
|
||||||
|
assertTrue(drive1.mkdir());
|
||||||
|
|
||||||
|
Mockery context = new Mockery();
|
||||||
|
final RemovableDriveFinder finder =
|
||||||
|
context.mock(RemovableDriveFinder.class);
|
||||||
|
final BatchTransportCallback callback =
|
||||||
|
context.mock(BatchTransportCallback.class);
|
||||||
|
context.checking(new Expectations() {{
|
||||||
|
oneOf(finder).findRemovableDrives();
|
||||||
|
will(returnValue(drives));
|
||||||
|
oneOf(callback).showChoice(with(any(String.class)),
|
||||||
|
with(any(String[].class)));
|
||||||
|
will(returnValue(0)); // The user chose drive1
|
||||||
|
}});
|
||||||
|
RemovableDrivePlugin plugin = new RemovableDrivePlugin(finder);
|
||||||
|
plugin.start(null, null, null, callback);
|
||||||
|
|
||||||
|
assertNotNull(plugin.createWriter(contactId));
|
||||||
|
// The output file should exist and should be empty
|
||||||
|
File[] files = drive1.listFiles();
|
||||||
|
assertNotNull(files);
|
||||||
|
assertEquals(1, files.length);
|
||||||
|
assertEquals(0L, files[0].length());
|
||||||
|
|
||||||
|
context.assertIsSatisfied();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWritingToWriter() throws Exception {
|
||||||
|
final File drive1 = new File(testDir, "1");
|
||||||
|
final File drive2 = new File(testDir, "2");
|
||||||
|
final List<File> drives = new ArrayList<File>();
|
||||||
|
drives.add(drive1);
|
||||||
|
drives.add(drive2);
|
||||||
|
// Create drive1 as a directory
|
||||||
|
assertTrue(drive1.mkdir());
|
||||||
|
|
||||||
|
Mockery context = new Mockery();
|
||||||
|
final RemovableDriveFinder finder =
|
||||||
|
context.mock(RemovableDriveFinder.class);
|
||||||
|
final BatchTransportCallback callback =
|
||||||
|
context.mock(BatchTransportCallback.class);
|
||||||
|
context.checking(new Expectations() {{
|
||||||
|
oneOf(finder).findRemovableDrives();
|
||||||
|
will(returnValue(drives));
|
||||||
|
oneOf(callback).showChoice(with(any(String.class)),
|
||||||
|
with(any(String[].class)));
|
||||||
|
will(returnValue(0)); // The user chose drive1
|
||||||
|
oneOf(callback).showMessage(with(any(String.class)));
|
||||||
|
}});
|
||||||
|
RemovableDrivePlugin plugin = new RemovableDrivePlugin(finder);
|
||||||
|
plugin.start(null, null, null, callback);
|
||||||
|
|
||||||
|
BatchTransportWriter writer = plugin.createWriter(contactId);
|
||||||
|
assertNotNull(writer);
|
||||||
|
// The output file should exist and should be empty
|
||||||
|
File[] files = drive1.listFiles();
|
||||||
|
assertNotNull(files);
|
||||||
|
assertEquals(1, files.length);
|
||||||
|
assertEquals(0L, files[0].length());
|
||||||
|
// Writing to the output stream should increase the size of the file
|
||||||
|
OutputStream out = writer.getOutputStream();
|
||||||
|
out.write(new byte[123]);
|
||||||
|
out.flush();
|
||||||
|
out.close();
|
||||||
|
writer.finish();
|
||||||
|
assertEquals(123L, files[0].length());
|
||||||
|
// Disposing of the writer should delete the file
|
||||||
|
writer.dispose();
|
||||||
|
files = drive1.listFiles();
|
||||||
|
assertTrue(files == null || files.length == 0);
|
||||||
|
|
||||||
|
context.assertIsSatisfied();
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
TestUtils.deleteTestDirectory(testDir);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
package net.sf.briar.plugins.file;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
|
|
||||||
public class TestFilePlugin extends FilePlugin {
|
|
||||||
|
|
||||||
private final File outputDir;
|
|
||||||
private final long capacity;
|
|
||||||
|
|
||||||
public TestFilePlugin(File outputDir, long capacity) {
|
|
||||||
this.outputDir = outputDir;
|
|
||||||
this.capacity = capacity;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected File chooseOutputDirectory() {
|
|
||||||
return outputDir;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void writerFinished(File f) {
|
|
||||||
// Nothing to do
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected long getCapacity(String path) {
|
|
||||||
return capacity;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user