-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (C) 2023 Information Management Services, Inc. | ||
*/ | ||
package com.imsweb.seerutils; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class SeerLRUCacheTest { | ||
|
||
@Test | ||
public void testCache() { | ||
SeerLRUCache<String, String> cache = new SeerLRUCache<>(3); | ||
|
||
cache.put("1", "A"); | ||
cache.put("2", "B"); | ||
cache.put("3", "C"); | ||
Assert.assertEquals(3, cache.size()); | ||
|
||
cache.put("4", "D"); | ||
Assert.assertEquals(3, cache.size()); | ||
Assert.assertTrue(cache.containsKey("4")); | ||
Assert.assertFalse(cache.containsKey("1")); | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
src/test/java/com/imsweb/seerutils/zip/ZipSecureFileTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright (C) 2023 Information Management Services, Inc. | ||
*/ | ||
package com.imsweb.seerutils.zip; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.StringWriter; | ||
import java.util.Arrays; | ||
import java.util.Enumeration; | ||
|
||
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import com.imsweb.seerutils.SeerUtils; | ||
|
||
public class ZipSecureFileTest { | ||
|
||
@Test | ||
public void testZipSecureFile() throws IOException { | ||
|
||
File dir = new File(System.getProperty("user.dir") + "/build"); | ||
if (!dir.exists() && !dir.mkdir()) | ||
Assert.fail("Unable to create build directory!"); | ||
|
||
File f1 = new File(dir, "f1.txt"); | ||
SeerUtils.writeFile("test1", f1); | ||
File f2 = new File(dir, "f2.txt"); | ||
SeerUtils.writeFile("test2", f2); | ||
|
||
File f = new File(dir, "f.zip"); | ||
SeerUtils.zipFiles(Arrays.asList(f1, f2), f); | ||
|
||
try (ZipSecureFile secureFile = new ZipSecureFile(f)) { | ||
Assert.assertTrue(secureFile.getMaxEntrySize() > 0); | ||
Assert.assertTrue(secureFile.getMinInflateRatio() > 0.0); | ||
|
||
Enumeration<ZipArchiveEntry> entries = secureFile.getEntries(); | ||
while (entries.hasMoreElements()) { | ||
try (InputStreamReader reader = new InputStreamReader(secureFile.getInputStream(entries.nextElement())); StringWriter writer = new StringWriter()) { | ||
SeerUtils.copyReaderToWriter(reader, writer); | ||
Assert.assertTrue(writer.toString().startsWith("test")); | ||
} | ||
} | ||
} | ||
|
||
// test too many entries | ||
try (ZipSecureFile secureFile = new ZipSecureFile(f, 0.0075, 1L)) { | ||
Enumeration<ZipArchiveEntry> entries = secureFile.getEntries(); | ||
while (entries.hasMoreElements()) { | ||
try (InputStreamReader reader = new InputStreamReader(secureFile.getInputStream(entries.nextElement())); StringWriter writer = new StringWriter()) { | ||
SeerUtils.copyReaderToWriter(reader, writer); | ||
} | ||
} | ||
Assert.fail("Should have been an exception here"); | ||
} | ||
catch (ZipEntryTooLargeException e) { | ||
// expected | ||
} | ||
|
||
// I can't test the compression ratio because there is a hard-coded "grace" size, and I would have to create a big file for this... | ||
} | ||
} |