Javadocs and unit tests. Woo!

This commit is contained in:
akwizgran
2011-06-22 11:42:33 +01:00
parent 9e76cc6a4f
commit eb1c855278
14 changed files with 287 additions and 28 deletions

View File

@@ -1,13 +1,16 @@
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();
}
@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());
}
@After
public void tearDown() throws IOException {
delete(testDir);
}
private static void delete(File f) throws IOException {
if(f.isDirectory()) for(File child : f.listFiles()) delete(child);
f.delete();
TestUtils.delete(testDir);
}
}

View File

@@ -0,0 +1,20 @@
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);
}
}

View File

@@ -0,0 +1,22 @@
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();
}
}

View File

@@ -0,0 +1,120 @@
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);
}
}