Skip to content
Snippets Groups Projects
Commit dad1b4fc authored by akwizgran's avatar akwizgran
Browse files

More unit tests.

parent eb1c8552
No related branches found
No related tags found
No related merge requests found
...@@ -7,4 +7,6 @@ public interface SetupParameters { ...@@ -7,4 +7,6 @@ public interface SetupParameters {
File getChosenLocation(); File getChosenLocation();
String[] getBundledFontFilenames(); String[] getBundledFontFilenames();
long getExeHeaderSize();
} }
package net.sf.briar.setup;
import net.sf.briar.api.setup.SetupWorkerFactory;
import com.google.inject.AbstractModule;
public class SetupModule extends AbstractModule {
@Override
protected void configure() {
bind(SetupWorkerFactory.class).to(SetupWorkerFactoryImpl.class);
}
}
...@@ -5,7 +5,6 @@ import java.io.FileInputStream; ...@@ -5,7 +5,6 @@ import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.security.CodeSource;
import net.sf.briar.api.i18n.I18n; import net.sf.briar.api.i18n.I18n;
import net.sf.briar.api.setup.SetupCallback; import net.sf.briar.api.setup.SetupCallback;
...@@ -18,18 +17,19 @@ class SetupWorker implements Runnable { ...@@ -18,18 +17,19 @@ class SetupWorker implements Runnable {
private static final String MAIN_CLASS = private static final String MAIN_CLASS =
"net.sf.briar.ui.invitation.InvitationMain"; "net.sf.briar.ui.invitation.InvitationMain";
private static final int EXE_HEADER_SIZE = 62976;
private final SetupCallback callback; private final SetupCallback callback;
private final SetupParameters parameters; private final SetupParameters parameters;
private final I18n i18n; private final I18n i18n;
private final File jar;
private final ZipUtils.Callback unzipCallback; private final ZipUtils.Callback unzipCallback;
SetupWorker(final SetupCallback callback, SetupParameters parameters, SetupWorker(final SetupCallback callback, SetupParameters parameters,
I18n i18n) { I18n i18n, File jar) {
this.parameters = parameters; this.parameters = parameters;
this.callback = callback; this.callback = callback;
this.i18n = i18n; this.i18n = i18n;
this.jar = jar;
unzipCallback = new ZipUtils.Callback() { unzipCallback = new ZipUtils.Callback() {
public void processingFile(File f) { public void processingFile(File f) {
callback.extractingFile(f); callback.extractingFile(f);
...@@ -38,6 +38,10 @@ class SetupWorker implements Runnable { ...@@ -38,6 +38,10 @@ class SetupWorker implements Runnable {
} }
public void run() { public void run() {
if(!jar.isFile()) {
callback.error("Not running from jar");
return;
}
File dir = parameters.getChosenLocation(); File dir = parameters.getChosenLocation();
assert dir != null; assert dir != null;
if(!dir.exists()) { if(!dir.exists()) {
...@@ -66,8 +70,6 @@ class SetupWorker implements Runnable { ...@@ -66,8 +70,6 @@ class SetupWorker implements Runnable {
return; return;
} }
try { try {
if(callback.isCancelled()) return;
File jar = getJar();
if(callback.isCancelled()) return; if(callback.isCancelled()) return;
copyInstaller(jar, data); copyInstaller(jar, data);
if(callback.isCancelled()) return; if(callback.isCancelled()) return;
...@@ -86,14 +88,6 @@ class SetupWorker implements Runnable { ...@@ -86,14 +88,6 @@ class SetupWorker implements Runnable {
callback.installed(dir); callback.installed(dir);
} }
private File getJar() throws IOException {
CodeSource c = FileUtils.class.getProtectionDomain().getCodeSource();
File jar = new File(c.getLocation().getPath());
assert jar.exists();
if(!jar.isFile()) throw new IOException("Not running from a jar");
return jar;
}
private void copyInstaller(File jar, File dir) throws IOException { private void copyInstaller(File jar, File dir) throws IOException {
File dest = new File(dir, "setup.dat"); File dest = new File(dir, "setup.dat");
callback.copyingFile(dest); callback.copyingFile(dest);
...@@ -103,7 +97,7 @@ class SetupWorker implements Runnable { ...@@ -103,7 +97,7 @@ class SetupWorker implements Runnable {
private void extractFiles(File jar, File dir, String regex) private void extractFiles(File jar, File dir, String regex)
throws IOException { throws IOException {
FileInputStream in = new FileInputStream(jar); FileInputStream in = new FileInputStream(jar);
in.skip(EXE_HEADER_SIZE); in.skip(parameters.getExeHeaderSize());
ZipUtils.unzipStream(in, dir, regex, unzipCallback); ZipUtils.unzipStream(in, dir, regex, unzipCallback);
} }
......
package net.sf.briar.setup; package net.sf.briar.setup;
import java.io.File;
import java.security.CodeSource;
import net.sf.briar.api.i18n.I18n; import net.sf.briar.api.i18n.I18n;
import net.sf.briar.api.setup.SetupCallback; import net.sf.briar.api.setup.SetupCallback;
import net.sf.briar.api.setup.SetupParameters; import net.sf.briar.api.setup.SetupParameters;
import net.sf.briar.api.setup.SetupWorkerFactory; import net.sf.briar.api.setup.SetupWorkerFactory;
import net.sf.briar.util.FileUtils;
import com.google.inject.Inject;
public class SetupWorkerFactoryImpl implements SetupWorkerFactory { public class SetupWorkerFactoryImpl implements SetupWorkerFactory {
private final I18n i18n; private final I18n i18n;
@Inject
public SetupWorkerFactoryImpl(I18n i18n) { public SetupWorkerFactoryImpl(I18n i18n) {
this.i18n = i18n; this.i18n = i18n;
} }
public Runnable createWorker(SetupCallback callback, public Runnable createWorker(SetupCallback callback,
SetupParameters parameters) { SetupParameters parameters) {
return new SetupWorker(callback, parameters, i18n); CodeSource c = FileUtils.class.getProtectionDomain().getCodeSource();
File jar = new File(c.getLocation().getPath());
assert jar.exists();
return new SetupWorker(callback, parameters, i18n, jar);
} }
} }
<project name='test' default='test'> <project name='test' default='test'>
<import file='../build-common.xml'/> <import file='../build-common.xml'/>
<target name='test' depends='depend'> <target name='test' depends='depend'>
<junit haltonfailure='true' printsummary='on' showoutput='true'> <junit printsummary='on'>
<classpath> <classpath>
<fileset refid='bundled-jars'/> <fileset refid='bundled-jars'/>
<fileset refid='test-jars'/> <fileset refid='test-jars'/>
...@@ -10,7 +10,10 @@ ...@@ -10,7 +10,10 @@
<path refid='test-classes'/> <path refid='test-classes'/>
<path refid='util-classes'/> <path refid='util-classes'/>
</classpath> </classpath>
<test name='net.sf.briar.setup.SetupWorkerTest'/>
<test name='net.sf.briar.util.FileUtilsTest'/> <test name='net.sf.briar.util.FileUtilsTest'/>
<test name='net.sf.briar.util.StringUtilsTest'/>
<test name='net.sf.briar.util.ZipUtilsTest'/>
</junit> </junit>
</target> </target>
</project> </project>
package net.sf.briar.util; package net.sf.briar;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; 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); if(f.isDirectory()) for(File child : f.listFiles()) delete(child);
f.delete(); f.delete();
} }
static void createFile(File f, String s) throws IOException { public static void createFile(File f, String s) throws IOException {
f.getParentFile().mkdirs(); f.getParentFile().mkdirs();
PrintStream out = new PrintStream(new FileOutputStream(f)); PrintStream out = new PrintStream(new FileOutputStream(f));
out.print(s); out.print(s);
......
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);
}
}
...@@ -7,6 +7,7 @@ import java.io.InputStream; ...@@ -7,6 +7,7 @@ import java.io.InputStream;
import java.util.Scanner; import java.util.Scanner;
import junit.framework.TestCase; import junit.framework.TestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.util.FileUtils.Callback; import net.sf.briar.util.FileUtils.Callback;
import org.jmock.Expectations; import org.jmock.Expectations;
......
...@@ -4,6 +4,7 @@ import java.io.File; ...@@ -4,6 +4,7 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.Map; import java.util.Map;
import java.util.Scanner; import java.util.Scanner;
import java.util.TreeMap; import java.util.TreeMap;
...@@ -12,6 +13,7 @@ import java.util.zip.ZipInputStream; ...@@ -12,6 +13,7 @@ import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import junit.framework.TestCase; import junit.framework.TestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.util.ZipUtils.Callback; import net.sf.briar.util.ZipUtils.Callback;
import org.jmock.Expectations; import org.jmock.Expectations;
...@@ -24,6 +26,10 @@ public class ZipUtilsTest extends TestCase { ...@@ -24,6 +26,10 @@ public class ZipUtilsTest extends TestCase {
private final File testDir = new File("test.tmp"); 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 @Before
public void setUp() { public void setUp() {
testDir.mkdirs(); testDir.mkdirs();
...@@ -72,15 +78,12 @@ public class ZipUtilsTest extends TestCase { ...@@ -72,15 +78,12 @@ public class ZipUtilsTest extends TestCase {
@Test @Test
public void testCopyToZipRecursively() throws IOException { 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(); Mockery context = new Mockery();
final Callback callback = context.mock(Callback.class); final Callback callback = context.mock(Callback.class);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(callback).processingFile(src1); oneOf(callback).processingFile(f1);
oneOf(callback).processingFile(src2); oneOf(callback).processingFile(f2);
oneOf(callback).processingFile(src3); oneOf(callback).processingFile(f3);
}}); }});
copyRecursively(callback); copyRecursively(callback);
...@@ -94,10 +97,9 @@ public class ZipUtilsTest extends TestCase { ...@@ -94,10 +97,9 @@ public class ZipUtilsTest extends TestCase {
} }
private void copyRecursively(Callback callback) throws IOException { private void copyRecursively(Callback callback) throws IOException {
TestUtils.createFile(new File(testDir, "abc/def/1"), "one one one"); TestUtils.createFile(f1, "one one one");
TestUtils.createFile(new File(testDir, "abc/def/2"), "two two two"); TestUtils.createFile(f2, "two two two");
TestUtils.createFile(new File(testDir, "abc/3"), "three three three"); TestUtils.createFile(f3, "three three three");
File src = new File(testDir, "abc"); File src = new File(testDir, "abc");
File dest = new File(testDir, "dest"); File dest = new File(testDir, "dest");
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(dest)); ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(dest));
...@@ -113,6 +115,85 @@ public class ZipUtilsTest extends TestCase { ...@@ -113,6 +115,85 @@ public class ZipUtilsTest extends TestCase {
checkZipEntries(dest, expected); 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 @After
public void tearDown() throws IOException { public void tearDown() throws IOException {
TestUtils.delete(testDir); TestUtils.delete(testDir);
......
...@@ -7,6 +7,8 @@ import net.sf.briar.api.setup.SetupParameters; ...@@ -7,6 +7,8 @@ import net.sf.briar.api.setup.SetupParameters;
class SetupParametersImpl implements SetupParameters { class SetupParametersImpl implements SetupParameters {
private static final int EXE_HEADER_SIZE = 62976;
private final LocationPanel locationPanel; private final LocationPanel locationPanel;
private final FontManager fontManager; private final FontManager fontManager;
...@@ -22,4 +24,8 @@ class SetupParametersImpl implements SetupParameters { ...@@ -22,4 +24,8 @@ class SetupParametersImpl implements SetupParameters {
public String[] getBundledFontFilenames() { public String[] getBundledFontFilenames() {
return fontManager.getBundledFontFilenames(); return fontManager.getBundledFontFilenames();
} }
public long getExeHeaderSize() {
return EXE_HEADER_SIZE;
}
} }
...@@ -54,18 +54,18 @@ public class ZipUtils { ...@@ -54,18 +54,18 @@ public class ZipUtils {
/** /**
* Unzips the given stream to the given directory, skipping any zip entries * 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 * that don't match the given regex (a null regex matches all entries). If
* called once for each file extracted. * the callback is not null it's called once for each file extracted.
*/ */
public static void unzipStream(InputStream in, File dir, String regex, public static void unzipStream(InputStream in, File dir, String regex,
Callback callback) throws IOException { Callback callback) throws IOException {
String path = dir.getCanonicalPath(); String path = dir.getPath();
ZipInputStream zip = new ZipInputStream(in); ZipInputStream zip = new ZipInputStream(in);
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
ZipEntry entry; ZipEntry entry;
while((entry = zip.getNextEntry()) != null) { while((entry = zip.getNextEntry()) != null) {
String name = entry.getName(); String name = entry.getName();
if(name.matches(regex)) { if(regex == null || name.matches(regex)) {
File file = new File(path + "/" + name); File file = new File(path + "/" + name);
if(callback != null) callback.processingFile(file); if(callback != null) callback.processingFile(file);
if(entry.isDirectory()) { if(entry.isDirectory()) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment