Skip to content

Commit

Permalink
Added more unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
depryf committed Oct 19, 2023
1 parent 83a5f68 commit 240cb8b
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/test/java/com/imsweb/seerutils/SeerLRUCacheTest.java
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 src/test/java/com/imsweb/seerutils/zip/ZipSecureFileTest.java
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...
}
}

0 comments on commit 240cb8b

Please sign in to comment.