More unit tests.

This commit is contained in:
akwizgran
2011-06-22 16:58:03 +01:00
parent eb1c855278
commit dad1b4fcb9
11 changed files with 296 additions and 50 deletions

View File

@@ -1,7 +1,7 @@
<project name='test' default='test'>
<import file='../build-common.xml'/>
<target name='test' depends='depend'>
<junit haltonfailure='true' printsummary='on' showoutput='true'>
<junit printsummary='on'>
<classpath>
<fileset refid='bundled-jars'/>
<fileset refid='test-jars'/>
@@ -10,7 +10,10 @@
<path refid='test-classes'/>
<path refid='util-classes'/>
</classpath>
<test name='net.sf.briar.setup.SetupWorkerTest'/>
<test name='net.sf.briar.util.FileUtilsTest'/>
<test name='net.sf.briar.util.StringUtilsTest'/>
<test name='net.sf.briar.util.ZipUtilsTest'/>
</junit>
</target>
</project>

View File

@@ -1,18 +1,18 @@
package net.sf.briar.util;
package net.sf.briar;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
class TestUtils {
public class TestUtils {
static void delete(File f) throws IOException {
public 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 {
public static void createFile(File f, String s) throws IOException {
f.getParentFile().mkdirs();
PrintStream out = new PrintStream(new FileOutputStream(f));
out.print(s);

View File

@@ -0,0 +1,168 @@
package net.sf.briar.setup;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipOutputStream;
import junit.framework.TestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.i18n.I18n;
import net.sf.briar.api.setup.SetupCallback;
import net.sf.briar.api.setup.SetupParameters;
import net.sf.briar.util.ZipUtils;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class SetupWorkerTest extends TestCase {
private static final int HEADER_SIZE = 1234;
private final File testDir = new File("test.tmp");
private final File jar = new File(testDir, "test.jar");
@Before
public void setUp() throws IOException {
testDir.mkdirs();
jar.createNewFile();
}
@Test
public void testHaltsIfNotRunningFromJar() {
Mockery context = new Mockery();
final SetupCallback callback = context.mock(SetupCallback.class);
SetupParameters params = context.mock(SetupParameters.class);
I18n i18n = context.mock(I18n.class);
context.checking(new Expectations() {{
oneOf(callback).error("Not running from jar");
}});
new SetupWorker(callback, params, i18n, testDir).run();
context.assertIsSatisfied();
File[] children = testDir.listFiles();
assertNotNull(children);
assertEquals(1, children.length);
assertEquals(jar, children[0]);
}
@Test
public void testHaltsIfDestinationDoesNotExist() {
final File nonExistent = new File(testDir, "does.not.exist");
Mockery context = new Mockery();
final SetupCallback callback = context.mock(SetupCallback.class);
final SetupParameters params = context.mock(SetupParameters.class);
I18n i18n = context.mock(I18n.class);
context.checking(new Expectations() {{
oneOf(params).getChosenLocation();
will(returnValue(nonExistent));
oneOf(callback).notFound(nonExistent);
}});
new SetupWorker(callback, params, i18n, jar).run();
context.assertIsSatisfied();
File[] children = testDir.listFiles();
assertNotNull(children);
assertEquals(1, children.length);
assertEquals(jar, children[0]);
}
@Test
public void testHaltsIfDestinationIsNotADirectory() {
Mockery context = new Mockery();
final SetupCallback callback = context.mock(SetupCallback.class);
final SetupParameters params = context.mock(SetupParameters.class);
I18n i18n = context.mock(I18n.class);
context.checking(new Expectations() {{
oneOf(params).getChosenLocation();
will(returnValue(jar));
oneOf(callback).notDirectory(jar);
}});
new SetupWorker(callback, params, i18n, jar).run();
context.assertIsSatisfied();
File[] children = testDir.listFiles();
assertNotNull(children);
assertEquals(1, children.length);
assertEquals(jar, children[0]);
}
@Test
public void testCreatesExpectedFiles() throws IOException {
final File setupDat = new File(testDir, "Briar/Data/setup.dat");
final File jreFoo = new File(testDir, "Briar/Data/jre/foo");
final File fooJar = new File(testDir, "Briar/Data/foo.jar");
final File fooTtf = new File(testDir, "Briar/Data/foo.ttf");
final File fooXyz = new File(testDir, "Briar/Data/foo.xyz");
createJar();
Mockery context = new Mockery();
final SetupCallback callback = context.mock(SetupCallback.class);
final SetupParameters params = context.mock(SetupParameters.class);
final I18n i18n = context.mock(I18n.class);
context.checking(new Expectations() {{
oneOf(params).getChosenLocation();
will(returnValue(testDir));
allowing(callback).isCancelled();
will(returnValue(false));
oneOf(callback).copyingFile(setupDat);
oneOf(params).getExeHeaderSize();
will(returnValue((long) HEADER_SIZE));
oneOf(callback).extractingFile(jreFoo);
oneOf(callback).extractingFile(fooJar);
oneOf(callback).extractingFile(fooTtf);
oneOf(i18n).saveLocale(new File(testDir, "Briar/Data"));
oneOf(callback).installed(new File(testDir, "Briar"));
}});
new SetupWorker(callback, params, i18n, jar).run();
context.assertIsSatisfied();
assertTrue(setupDat.exists());
assertTrue(setupDat.isFile());
assertEquals(jar.length(), setupDat.length());
assertTrue(jreFoo.exists());
assertTrue(jreFoo.isFile());
assertEquals("one one one".length(), jreFoo.length());
assertTrue(fooJar.exists());
assertTrue(fooJar.isFile());
assertEquals("two two two".length(), fooJar.length());
assertTrue(fooTtf.exists());
assertTrue(fooTtf.isFile());
assertEquals("three three three".length(), fooTtf.length());
assertFalse(fooXyz.exists());
}
private void createJar() throws IOException {
FileOutputStream out = new FileOutputStream(jar);
byte[] header = new byte[HEADER_SIZE];
out.write(header);
ZipOutputStream zip = new ZipOutputStream(out);
File temp = new File(testDir, "temp");
TestUtils.createFile(temp, "one one one");
ZipUtils.copyToZip("jre/foo", temp, zip);
temp.delete();
TestUtils.createFile(temp, "two two two");
ZipUtils.copyToZip("foo.jar", temp, zip);
temp.delete();
TestUtils.createFile(temp, "three three three");
ZipUtils.copyToZip("foo.ttf", temp, zip);
temp.delete();
TestUtils.createFile(temp, "four four four");
ZipUtils.copyToZip("foo.xyz", temp, zip);
temp.delete();
zip.flush();
zip.close();
}
@After
public void tearDown() throws IOException {
TestUtils.delete(testDir);
}
}

View File

@@ -7,6 +7,7 @@ import java.io.InputStream;
import java.util.Scanner;
import junit.framework.TestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.util.FileUtils.Callback;
import org.jmock.Expectations;

View File

@@ -4,6 +4,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
@@ -12,6 +13,7 @@ import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import junit.framework.TestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.util.ZipUtils.Callback;
import org.jmock.Expectations;
@@ -24,6 +26,10 @@ public class ZipUtilsTest extends TestCase {
private final File testDir = new File("test.tmp");
private final File f1 = new File(testDir, "abc/def/1");
private final File f2 = new File(testDir, "abc/def/2");
private final File f3 = new File(testDir, "abc/3");
@Before
public void setUp() {
testDir.mkdirs();
@@ -72,15 +78,12 @@ public class ZipUtilsTest extends TestCase {
@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);
oneOf(callback).processingFile(f1);
oneOf(callback).processingFile(f2);
oneOf(callback).processingFile(f3);
}});
copyRecursively(callback);
@@ -94,10 +97,9 @@ public class ZipUtilsTest extends TestCase {
}
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");
TestUtils.createFile(f1, "one one one");
TestUtils.createFile(f2, "two two two");
TestUtils.createFile(f3, "three three three");
File src = new File(testDir, "abc");
File dest = new File(testDir, "dest");
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(dest));
@@ -113,6 +115,85 @@ public class ZipUtilsTest extends TestCase {
checkZipEntries(dest, expected);
}
@Test
public void testUnzipStream() throws IOException {
Mockery context = new Mockery();
final Callback callback = context.mock(Callback.class);
context.checking(new Expectations() {{
oneOf(callback).processingFile(f1);
oneOf(callback).processingFile(f2);
oneOf(callback).processingFile(f3);
}});
unzipStream(null, callback);
context.assertIsSatisfied();
assertTrue(f1.exists());
assertTrue(f1.isFile());
assertEquals("one one one".length(), f1.length());
assertTrue(f2.exists());
assertTrue(f2.isFile());
assertEquals("two two two".length(), f2.length());
assertTrue(f3.exists());
assertTrue(f3.isFile());
assertEquals("three three three".length(), f3.length());
}
@Test
public void testUnzipStreamWithRegex() throws IOException {
Mockery context = new Mockery();
final Callback callback = context.mock(Callback.class);
context.checking(new Expectations() {{
oneOf(callback).processingFile(f1);
oneOf(callback).processingFile(f2);
}});
unzipStream("^abc/def/.*", callback);
context.assertIsSatisfied();
assertTrue(f1.exists());
assertTrue(f1.isFile());
assertEquals("one one one".length(), f1.length());
assertTrue(f2.exists());
assertTrue(f2.isFile());
assertEquals("two two two".length(), f2.length());
assertFalse(f3.exists());
}
@Test
public void testUnzipStreamNoCallback() throws IOException {
unzipStream(null, null);
assertTrue(f1.exists());
assertTrue(f1.isFile());
assertEquals("one one one".length(), f1.length());
assertTrue(f2.exists());
assertTrue(f2.isFile());
assertEquals("two two two".length(), f2.length());
assertTrue(f3.exists());
assertTrue(f3.isFile());
assertEquals("three three three".length(), f3.length());
}
private void unzipStream(String regex, Callback callback)
throws IOException {
TestUtils.createFile(f1, "one one one");
TestUtils.createFile(f2, "two two two");
TestUtils.createFile(f3, "three three three");
File src = new File(testDir, "abc");
File dest = new File(testDir, "dest");
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(dest));
ZipUtils.copyToZipRecursively(src.getName(), src, zip, null);
zip.flush();
zip.close();
TestUtils.delete(src);
InputStream in = new FileInputStream(dest);
ZipUtils.unzipStream(in, testDir, regex, callback);
}
@After
public void tearDown() throws IOException {
TestUtils.delete(testDir);