Commit eb1c8552 authored by akwizgran's avatar akwizgran

Javadocs and unit tests. Woo!

parent 9e76cc6a
<project name='api' default='compile'>
<project name='api' default='depend'>
<import file='../build-common.xml'/>
</project>
<project name='build-common' default='compile'>
<project name='build-common'>
<import file='dependencies.xml'/>
<dirname property='build-common.root' file='${ant.file.build-common}'/>
<fileset id='bundled-jars' dir='${build-common.root}/lib'>
......
<project name='components' default='compile'>
<project name='components' default='depend'>
<import file='../build-common.xml'/>
</project>
......@@ -2,19 +2,24 @@
<dirname property='depend.root' file='${ant.file.dependencies}'/>
<target name='depend.all' depends='depend.components, depend.ui'/>
<target name='depend.api'>
<ant dir='${depend.root}/api' inheritAll='false'/>
<ant dir='${depend.root}/api' target='compile'
inheritAll='false'/>
</target>
<target name='depend.components' depends='depend.api, depend.util'>
<ant dir='${depend.root}/components' inheritAll='false'/>
<ant dir='${depend.root}/components' target='compile'
inheritAll='false'/>
</target>
<target name='depend.test' depends='depend.components'>
<ant dir='${depend.root}/test' inheritAll='false'/>
<ant dir='${depend.root}/test' target='compile'
inheritAll='false'/>
</target>
<target name='depend.ui' depends='depend.api, depend.util'>
<ant dir='${depend.root}/ui' inheritAll='false'/>
<ant dir='${depend.root}/ui' target='compile'
inheritAll='false'/>
</target>
<target name='depend.util'>
<ant dir='${depend.root}/util' inheritAll='false'/>
<ant dir='${depend.root}/util' target='compile'
inheritAll='false'/>
</target>
<target name='depend-clean.all'
depends='depend-clean.components, depend-clean.ui'/>
......
<project name='test' default='compile'>
<project name='test' default='test'>
<import file='../build-common.xml'/>
<target name='test' depends='depend'>
<junit haltonfailure='true' printsummary='on' showoutput='true'>
......
package net.sf.briar.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.InputStream;
import java.util.Scanner;
import junit.framework.TestCase;
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;
......@@ -21,15 +24,20 @@ public class FileUtilsTest extends TestCase {
testDir.mkdirs();
}
@Test
public void testCreateTempFile() throws IOException {
File temp = FileUtils.createTempFile();
assertTrue(temp.exists());
assertTrue(temp.isFile());
assertEquals(0L, temp.length());
temp.delete();
}
@Test
public void testCopy() throws IOException {
File src = new File(testDir, "src");
File dest = new File(testDir, "dest");
PrintStream out = new PrintStream(new FileOutputStream(src));
out.print("Foo bar\r\nBar foo\r\n");
out.flush();
out.close();
TestUtils.createFile(src, "Foo bar\r\nBar foo\r\n");
long length = src.length();
FileUtils.copy(src, dest);
......@@ -42,18 +50,78 @@ public class FileUtilsTest extends TestCase {
assertEquals("Bar foo", in.nextLine());
assertFalse(in.hasNext());
in.close();
}
src.delete();
dest.delete();
@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();
}
@After
public void tearDown() throws IOException {
delete(testDir);
@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 static void delete(File f) throws IOException {
if(f.isDirectory()) for(File child : f.listFiles()) delete(child);
f.delete();
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());
}
@After
public void tearDown() throws IOException {
TestUtils.delete(testDir);
}
}
package net.sf.briar.util;
import junit.framework.TestCase;
import org.junit.Test;
public class StringUtilsTest extends TestCase {
@Test
public void testHead() {
String head = StringUtils.head("123456789", 5);
assertEquals("12345...", head);
}
@Test
public void testTail() {
String tail = StringUtils.tail("987654321", 5);
assertEquals("...54321", tail);
}
}
package net.sf.briar.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
class TestUtils {
static void delete(File f) throws IOException {
if(f.isDirectory()) for(File child : f.listFiles()) delete(child);
f.delete();
}
static void createFile(File f, String s) throws IOException {
f.getParentFile().mkdirs();
PrintStream out = new PrintStream(new FileOutputStream(f));
out.print(s);
out.flush();
out.close();
}
}
package net.sf.briar.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import junit.framework.TestCase;
import net.sf.briar.util.ZipUtils.Callback;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class ZipUtilsTest extends TestCase {
private final File testDir = new File("test.tmp");
@Before
public void setUp() {
testDir.mkdirs();
}
@Test
public void testCopyToZip() throws IOException {
File src = new File(testDir, "src");
File dest = new File(testDir, "dest");
TestUtils.createFile(src, "foo bar baz");
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(dest));
ZipUtils.copyToZip("abc/def", src, zip);
zip.flush();
zip.close();
Map<String, String> expected = new TreeMap<String, String>();
expected.put("abc/def", "foo bar baz");
checkZipEntries(dest, expected);
}
private void checkZipEntries(File f, Map<String, String> expected)
throws IOException {
Map<String, String> found = new TreeMap<String, String>();
assertTrue(f.exists());
assertTrue(f.isFile());
ZipInputStream unzip = new ZipInputStream(new FileInputStream(f));
ZipEntry entry;
while((entry = unzip.getNextEntry()) != null) {
String name = entry.getName();
Scanner s = new Scanner(unzip);
assertTrue(s.hasNextLine());
String contents = s.nextLine();
assertFalse(s.hasNextLine());
unzip.closeEntry();
found.put(name, contents);
}
unzip.close();
assertEquals(expected.size(), found.size());
for(String name : expected.keySet()) {
String contents = found.get(name);
assertNotNull(contents);
assertEquals(expected.get(name), contents);
}
}
@Test
public void testCopyToZipRecursively() throws IOException {
final File src1 = new File(testDir, "abc/def/1");
final File src2 = new File(testDir, "abc/def/2");
final File src3 = new File(testDir, "abc/3");
Mockery context = new Mockery();
final Callback callback = context.mock(Callback.class);
context.checking(new Expectations() {{
oneOf(callback).processingFile(src1);
oneOf(callback).processingFile(src2);
oneOf(callback).processingFile(src3);
}});
copyRecursively(callback);
context.assertIsSatisfied();
}
@Test
public void testCopyToZipRecursivelyNoCallback() 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 src = new File(testDir, "abc");
File dest = new File(testDir, "dest");
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(dest));
ZipUtils.copyToZipRecursively("ghi", src, zip, callback);
zip.flush();
zip.close();
Map<String, String> expected = new TreeMap<String, String>();
expected.put("ghi/def/1", "one one one");
expected.put("ghi/def/2", "two two two");
expected.put("ghi/3", "three three three");
checkZipEntries(dest, expected);
}
@After
public void tearDown() throws IOException {
TestUtils.delete(testDir);
}
}
<project name='ui' default='compile'>
<project name='ui' default='depend'>
<import file='../build-common.xml'/>
</project>
<project name='util' default='compile'>
<project name='util' default='depend'>
<import file='../build-common.xml'/>
</project>
......@@ -67,7 +67,8 @@ public class FileUtils {
}
/**
* Copies the source file or directory to the destination directory.
* Copies the source file or directory to the destination directory. If the
* callback is not null it's called once for each file created.
*/
public static void copyRecursively(File src, File dest, Callback callback)
throws IOException {
......
......@@ -2,11 +2,19 @@ package net.sf.briar.util;
public class StringUtils {
/**
* Trims the given string to the given length, returning the head and
* appending "..." if the string was trimmed.
*/
public static String head(String s, int length) {
if(s.length() > length) return s.substring(0, length) + "...";
else return s;
}
/**
* Trims the given string to the given length, returning the tail and
* prepending "..." if the string was trimmed.
*/
public static String tail(String s, int length) {
if(s.length() > length) return "..." + s.substring(s.length() - length);
else return s;
......
......@@ -11,9 +11,13 @@ import java.util.zip.ZipOutputStream;
public class ZipUtils {
/**
* Copies the given file to the given zip, using the given path for the
* zip entry.
*/
public static void copyToZip(String path, File file, ZipOutputStream zip)
throws IOException {
assert file.isFile() : file.getAbsolutePath();
assert file.isFile();
zip.putNextEntry(new ZipEntry(path));
FileInputStream in = new FileInputStream(file);
byte[] buf = new byte[1024];
......@@ -23,6 +27,12 @@ public class ZipUtils {
zip.closeEntry();
}
/**
* Copies the given directory to the given zip recursively, using the
* given path in place of the directory's name as the parent of all the zip
* entries. If the callback is not null it's called once for each file
* added.
*/
public static void copyToZipRecursively(String path, File dir,
ZipOutputStream zip, Callback callback) throws IOException {
assert dir.isDirectory();
......@@ -42,6 +52,11 @@ public class ZipUtils {
else return path + "/" + name;
}
/**
* Unzips the given stream to the given directory, skipping any zip entries
* that don't match the given regex. If the callback is not null it's
* called once for each file extracted.
*/
public static void unzipStream(InputStream in, File dir, String regex,
Callback callback) throws IOException {
String path = dir.getCanonicalPath();
......
Markdown is supported
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