From 7cf4ce0b2f725fe0fce61e5c383d8fac6f6f70e8 Mon Sep 17 00:00:00 2001 From: sergiorussia <22471371+sergiorussia@users.noreply.github.com> Date: Sun, 5 May 2019 18:06:52 +0300 Subject: [PATCH] unit tests --- .../getdown/cache/GarbageCollector.java | 6 ++- .../com/threerings/getdown/data/Resource.java | 45 ++++++++++--------- .../getdown/cache/GarbageCollectorTest.java | 28 +++++++++--- .../getdown/cache/ResourceCacheTest.java | 30 ++++++++++--- 4 files changed, 76 insertions(+), 33 deletions(-) diff --git a/core/src/main/java/com/threerings/getdown/cache/GarbageCollector.java b/core/src/main/java/com/threerings/getdown/cache/GarbageCollector.java index 67ea645..ae18990 100644 --- a/core/src/main/java/com/threerings/getdown/cache/GarbageCollector.java +++ b/core/src/main/java/com/threerings/getdown/cache/GarbageCollector.java @@ -6,6 +6,8 @@ package com.threerings.getdown.cache; import java.io.File; + +import com.threerings.getdown.data.Resource; import com.threerings.getdown.util.FileUtil; /** @@ -55,9 +57,9 @@ public class GarbageCollector if (subdirs != null) { for (File dir : subdirs) { 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()) { - if (!file.getName().endsWith(".jar")) { + if (!Resource.isJar(file) && !Resource.isZip(file)) { continue; } File cachedFile = getCachedFile(file); diff --git a/core/src/main/java/com/threerings/getdown/data/Resource.java b/core/src/main/java/com/threerings/getdown/data/Resource.java index 78ca844..b45e326 100644 --- a/core/src/main/java/com/threerings/getdown/data/Resource.java +++ b/core/src/main/java/com/threerings/getdown/data/Resource.java @@ -62,25 +62,25 @@ public class Resource implements Comparable byte[] buffer = new byte[DIGEST_BUFFER_SIZE]; int read; - boolean isJar = isJar(target); + boolean isZip = isJar(target) || isZip(target); // jar is a zip too boolean isPacked200Jar = isPacked200Jar(target); // 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 - if (isJar || isPacked200Jar){ + if (isPacked200Jar || isZip){ File tmpJarFile = null; - ZipFile jar = null; + ZipFile zip = null; 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){ tmpJarFile = new File(target.getPath() + ".tmp"); tmpJarFile.deleteOnExit(); - jar = FileUtil.unpackPacked200Jar(target, tmpJarFile); + zip = FileUtil.unpackPacked200Jar(target, tmpJarFile); } else{ - jar = new ZipFile(target); + zip = new ZipFile(target); } - List entries = Collections.list(jar.entries()); + List entries = Collections.list(zip.entries()); Collections.sort(entries, ENTRY_COMP); int eidx = 0; @@ -93,7 +93,7 @@ public class Resource implements Comparable } } - try (InputStream in = jar.getInputStream(entry)) { + try (InputStream in = zip.getInputStream(entry)) { while ((read = in.read(buffer)) != -1) { md.update(buffer, 0, read); } @@ -103,11 +103,11 @@ public class Resource implements Comparable } } finally { - if (jar != null) { + if (zip != null) { try { - jar.close(); + zip.close(); } 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) { @@ -140,10 +140,10 @@ public class Resource implements Comparable _marker = new File(_local.getPath() + "v"); _attrs = attrs; - _isJar = isJar(local); + _isZip = isJar(local) || isZip(local); _isPacked200Jar = isPacked200Jar(local); boolean unpack = attrs.contains(Attr.UNPACK); - if (unpack && _isJar) { + if (unpack && _isZip) { _unpacked = _local.getParentFile(); } else if(unpack && _isPacked200Jar) { String dotJar = ".jar", lname = _local.getName(); @@ -299,10 +299,10 @@ public class Resource implements Comparable public void unpack () throws IOException { // sanity check - if (!_isJar && !_isPacked200Jar) { + if (!_isZip && !_isPacked200Jar) { throw new IOException("Requested to unpack non-jar file '" + _local + "'."); } - if (_isJar) { + if (_isZip) { try (ZipFile jar = new ZipFile(_local)) { FileUtil.unpackJar(jar, _unpacked, _attrs.contains(Attr.CLEAN)); } @@ -367,25 +367,30 @@ public class Resource implements Comparable } } - protected static boolean isJar (File file) + public static boolean isJar (File file) { String path = file.getName(); - return path.endsWith(".jar") || path.endsWith(".jar_new") || - path.endsWith(".zip") || path.endsWith(".zip_new"); + return path.endsWith(".jar") || path.endsWith(".jar_new"); } - protected static boolean isPacked200Jar (File file) + public static boolean isPacked200Jar (File file) { String path = file.getName(); return path.endsWith(".jar.pack") || path.endsWith(".jar.pack_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 URL _remote; protected File _local, _localNew, _marker, _unpacked; protected EnumSet _attrs; - protected boolean _isJar, _isPacked200Jar; + protected boolean _isZip, _isPacked200Jar; /** Used to sort the entries in a jar file. */ protected static final Comparator ENTRY_COMP = new Comparator() { diff --git a/core/src/test/java/com/threerings/getdown/cache/GarbageCollectorTest.java b/core/src/test/java/com/threerings/getdown/cache/GarbageCollectorTest.java index d5a3937..6834ff9 100644 --- a/core/src/test/java/com/threerings/getdown/cache/GarbageCollectorTest.java +++ b/core/src/test/java/com/threerings/getdown/cache/GarbageCollectorTest.java @@ -7,23 +7,41 @@ package com.threerings.getdown.cache; import java.io.File; import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; 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 static org.junit.Assert.*; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeTrue; /** * Validates that cache garbage is collected and deleted correctly. */ +@RunWith(Parameterized.class) public class GarbageCollectorTest { + @Parameterized.Parameters(name = "{0}") + public static Collection data() { + return Arrays.asList(new Object[][] { + { ".jar" }, + { ".zip" } + }); + } + + @Parameterized.Parameter + public String extension; + @Before public void setupFiles () throws IOException { - _cachedFile = _folder.newFile("abc123.jar"); - _lastAccessedFile = _folder.newFile("abc123.jar" + ResourceCache.LAST_ACCESSED_FILE_SUFFIX); + _cachedFile = _folder.newFile("abc123" + extension); + _lastAccessedFile = _folder.newFile("abc123" + extension + ResourceCache.LAST_ACCESSED_FILE_SUFFIX); } @Test public void shouldDeleteCacheEntryIfRetentionPeriodIsReached () diff --git a/core/src/test/java/com/threerings/getdown/cache/ResourceCacheTest.java b/core/src/test/java/com/threerings/getdown/cache/ResourceCacheTest.java index 860c72a..93ad683 100644 --- a/core/src/test/java/com/threerings/getdown/cache/ResourceCacheTest.java +++ b/core/src/test/java/com/threerings/getdown/cache/ResourceCacheTest.java @@ -7,26 +7,44 @@ package com.threerings.getdown.cache; import java.io.File; import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; 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 static org.junit.Assert.*; +import org.junit.runner.RunWith; +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}. */ +@RunWith(Parameterized.class) public class ResourceCacheTest { + @Parameterized.Parameters(name = "{0}") + public static Collection data() { + return Arrays.asList(new Object[][] { + { ".jar" }, + { ".zip" } + }); + } + + @Parameterized.Parameter + public String extension; + @Before public void setupCache () throws IOException { - _fileToCache = _folder.newFile("filetocache.jar"); + _fileToCache = _folder.newFile("filetocache" + extension); _cache = new ResourceCache(_folder.newFolder(".cache")); } @Test public void shouldCacheFile () throws IOException { - assertEquals("abc123.jar", cacheFile().getName()); + assertEquals("abc123" + extension, cacheFile().getName()); } private File cacheFile() throws IOException @@ -36,7 +54,7 @@ public class ResourceCacheTest @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); assertTrue(lastAccessedFile.exists()); }