unit tests

This commit is contained in:
sergiorussia
2019-05-05 18:06:52 +03:00
parent 4973da5e45
commit 7cf4ce0b2f
4 changed files with 76 additions and 33 deletions
@@ -6,6 +6,8 @@
package com.threerings.getdown.cache; package com.threerings.getdown.cache;
import java.io.File; import java.io.File;
import com.threerings.getdown.data.Resource;
import com.threerings.getdown.util.FileUtil; import com.threerings.getdown.util.FileUtil;
/** /**
@@ -55,9 +57,9 @@ public class GarbageCollector
if (subdirs != null) { if (subdirs != null) {
for (File dir : subdirs) { for (File dir : subdirs) {
if (dir.isDirectory()) { if (dir.isDirectory()) {
// Get all the native jars in the directory (there should only be one) // Get all the native jars or zips in the directory (there should only be one)
for (File file : dir.listFiles()) { for (File file : dir.listFiles()) {
if (!file.getName().endsWith(".jar")) { if (!Resource.isJar(file) && !Resource.isZip(file)) {
continue; continue;
} }
File cachedFile = getCachedFile(file); File cachedFile = getCachedFile(file);
@@ -62,25 +62,25 @@ public class Resource implements Comparable<Resource>
byte[] buffer = new byte[DIGEST_BUFFER_SIZE]; byte[] buffer = new byte[DIGEST_BUFFER_SIZE];
int read; int read;
boolean isJar = isJar(target); boolean isZip = isJar(target) || isZip(target); // jar is a zip too
boolean isPacked200Jar = isPacked200Jar(target); boolean isPacked200Jar = isPacked200Jar(target);
// if this is a jar, we need to compute the digest in a "timestamp and file order" agnostic // if this is a jar, we need to compute the digest in a "timestamp and file order" agnostic
// manner to properly correlate jardiff patched jars with their unpatched originals // manner to properly correlate jardiff patched jars with their unpatched originals
if (isJar || isPacked200Jar){ if (isPacked200Jar || isZip){
File tmpJarFile = null; File tmpJarFile = null;
ZipFile jar = null; ZipFile zip = null;
try { try {
// if this is a compressed jar file, uncompress it to compute the jar file digest // if this is a compressed zip file, uncompress it to compute the zip file digest
if (isPacked200Jar){ if (isPacked200Jar){
tmpJarFile = new File(target.getPath() + ".tmp"); tmpJarFile = new File(target.getPath() + ".tmp");
tmpJarFile.deleteOnExit(); tmpJarFile.deleteOnExit();
jar = FileUtil.unpackPacked200Jar(target, tmpJarFile); zip = FileUtil.unpackPacked200Jar(target, tmpJarFile);
} else{ } else{
jar = new ZipFile(target); zip = new ZipFile(target);
} }
List<? extends ZipEntry> entries = Collections.list(jar.entries()); List<? extends ZipEntry> entries = Collections.list(zip.entries());
Collections.sort(entries, ENTRY_COMP); Collections.sort(entries, ENTRY_COMP);
int eidx = 0; int eidx = 0;
@@ -93,7 +93,7 @@ public class Resource implements Comparable<Resource>
} }
} }
try (InputStream in = jar.getInputStream(entry)) { try (InputStream in = zip.getInputStream(entry)) {
while ((read = in.read(buffer)) != -1) { while ((read = in.read(buffer)) != -1) {
md.update(buffer, 0, read); md.update(buffer, 0, read);
} }
@@ -103,11 +103,11 @@ public class Resource implements Comparable<Resource>
} }
} finally { } finally {
if (jar != null) { if (zip != null) {
try { try {
jar.close(); zip.close();
} catch (IOException ioe) { } catch (IOException ioe) {
log.warning("Error closing jar", "path", target, "jar", jar, "error", ioe); log.warning("Error closing zip", "path", target, "zip", zip, "error", ioe);
} }
} }
if (tmpJarFile != null) { if (tmpJarFile != null) {
@@ -140,10 +140,10 @@ public class Resource implements Comparable<Resource>
_marker = new File(_local.getPath() + "v"); _marker = new File(_local.getPath() + "v");
_attrs = attrs; _attrs = attrs;
_isJar = isJar(local); _isZip = isJar(local) || isZip(local);
_isPacked200Jar = isPacked200Jar(local); _isPacked200Jar = isPacked200Jar(local);
boolean unpack = attrs.contains(Attr.UNPACK); boolean unpack = attrs.contains(Attr.UNPACK);
if (unpack && _isJar) { if (unpack && _isZip) {
_unpacked = _local.getParentFile(); _unpacked = _local.getParentFile();
} else if(unpack && _isPacked200Jar) { } else if(unpack && _isPacked200Jar) {
String dotJar = ".jar", lname = _local.getName(); String dotJar = ".jar", lname = _local.getName();
@@ -299,10 +299,10 @@ public class Resource implements Comparable<Resource>
public void unpack () throws IOException public void unpack () throws IOException
{ {
// sanity check // sanity check
if (!_isJar && !_isPacked200Jar) { if (!_isZip && !_isPacked200Jar) {
throw new IOException("Requested to unpack non-jar file '" + _local + "'."); throw new IOException("Requested to unpack non-jar file '" + _local + "'.");
} }
if (_isJar) { if (_isZip) {
try (ZipFile jar = new ZipFile(_local)) { try (ZipFile jar = new ZipFile(_local)) {
FileUtil.unpackJar(jar, _unpacked, _attrs.contains(Attr.CLEAN)); FileUtil.unpackJar(jar, _unpacked, _attrs.contains(Attr.CLEAN));
} }
@@ -367,25 +367,30 @@ public class Resource implements Comparable<Resource>
} }
} }
protected static boolean isJar (File file) public static boolean isJar (File file)
{ {
String path = file.getName(); String path = file.getName();
return path.endsWith(".jar") || path.endsWith(".jar_new") || return path.endsWith(".jar") || path.endsWith(".jar_new");
path.endsWith(".zip") || path.endsWith(".zip_new");
} }
protected static boolean isPacked200Jar (File file) public static boolean isPacked200Jar (File file)
{ {
String path = file.getName(); String path = file.getName();
return path.endsWith(".jar.pack") || path.endsWith(".jar.pack_new") || return path.endsWith(".jar.pack") || path.endsWith(".jar.pack_new") ||
path.endsWith(".jar.pack.gz")|| path.endsWith(".jar.pack.gz_new"); path.endsWith(".jar.pack.gz")|| path.endsWith(".jar.pack.gz_new");
} }
public static boolean isZip (File file)
{
String path = file.getName();
return path.endsWith(".zip") || path.endsWith(".zip_new");
}
protected String _path; protected String _path;
protected URL _remote; protected URL _remote;
protected File _local, _localNew, _marker, _unpacked; protected File _local, _localNew, _marker, _unpacked;
protected EnumSet<Attr> _attrs; protected EnumSet<Attr> _attrs;
protected boolean _isJar, _isPacked200Jar; protected boolean _isZip, _isPacked200Jar;
/** Used to sort the entries in a jar file. */ /** Used to sort the entries in a jar file. */
protected static final Comparator<ZipEntry> ENTRY_COMP = new Comparator<ZipEntry>() { protected static final Comparator<ZipEntry> ENTRY_COMP = new Comparator<ZipEntry>() {
@@ -7,23 +7,41 @@ package com.threerings.getdown.cache;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.junit.*; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import static org.junit.Assert.*; import org.junit.runners.Parameterized;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue; import static org.junit.Assume.assumeTrue;
/** /**
* Validates that cache garbage is collected and deleted correctly. * Validates that cache garbage is collected and deleted correctly.
*/ */
@RunWith(Parameterized.class)
public class GarbageCollectorTest public class GarbageCollectorTest
{ {
@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ ".jar" },
{ ".zip" }
});
}
@Parameterized.Parameter
public String extension;
@Before public void setupFiles () throws IOException @Before public void setupFiles () throws IOException
{ {
_cachedFile = _folder.newFile("abc123.jar"); _cachedFile = _folder.newFile("abc123" + extension);
_lastAccessedFile = _folder.newFile("abc123.jar" + ResourceCache.LAST_ACCESSED_FILE_SUFFIX); _lastAccessedFile = _folder.newFile("abc123" + extension + ResourceCache.LAST_ACCESSED_FILE_SUFFIX);
} }
@Test public void shouldDeleteCacheEntryIfRetentionPeriodIsReached () @Test public void shouldDeleteCacheEntryIfRetentionPeriodIsReached ()
@@ -7,26 +7,44 @@ package com.threerings.getdown.cache;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.junit.*; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import static org.junit.Assert.*; import org.junit.runners.Parameterized;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/** /**
* Asserts the correct functionality of the {@link ResourceCache}. * Asserts the correct functionality of the {@link ResourceCache}.
*/ */
@RunWith(Parameterized.class)
public class ResourceCacheTest public class ResourceCacheTest
{ {
@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ ".jar" },
{ ".zip" }
});
}
@Parameterized.Parameter
public String extension;
@Before public void setupCache () throws IOException { @Before public void setupCache () throws IOException {
_fileToCache = _folder.newFile("filetocache.jar"); _fileToCache = _folder.newFile("filetocache" + extension);
_cache = new ResourceCache(_folder.newFolder(".cache")); _cache = new ResourceCache(_folder.newFolder(".cache"));
} }
@Test public void shouldCacheFile () throws IOException @Test public void shouldCacheFile () throws IOException
{ {
assertEquals("abc123.jar", cacheFile().getName()); assertEquals("abc123" + extension, cacheFile().getName());
} }
private File cacheFile() throws IOException private File cacheFile() throws IOException
@@ -36,7 +54,7 @@ public class ResourceCacheTest
@Test public void shouldTrackFileUsage () throws IOException @Test public void shouldTrackFileUsage () throws IOException
{ {
String name = "abc123.jar" + ResourceCache.LAST_ACCESSED_FILE_SUFFIX; String name = "abc123" + extension + ResourceCache.LAST_ACCESSED_FILE_SUFFIX;
File lastAccessedFile = new File(cacheFile().getParentFile(), name); File lastAccessedFile = new File(cacheFile().getParentFile(), name);
assertTrue(lastAccessedFile.exists()); assertTrue(lastAccessedFile.exists());
} }