Skip to content

Commit

Permalink
Add test cases for FileSizeChecker.isAllowedFileSize IQSS#5634
Browse files Browse the repository at this point in the history
  • Loading branch information
rschlaefli committed Mar 16, 2019
1 parent bc500f2 commit 8b3fbbf
Showing 1 changed file with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,40 @@
package edu.harvard.iq.dataverse.datasetutility;

import static edu.harvard.iq.dataverse.datasetutility.FileSizeChecker.bytesToHumanReadable;

import edu.harvard.iq.dataverse.datasetutility.FileSizeChecker.FileSizeResponse;
import edu.harvard.iq.dataverse.util.BundleUtil;
import edu.harvard.iq.dataverse.util.SystemConfig;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
*
* @author oscardssmith
*/
public class FileSizeCheckerTest {

private FileSizeChecker fileSizeChecker;

@Before
public void setUp() {
// initialize a system config and instantiate a file size checker
// override the max file upload side to allow for testing
fileSizeChecker = new FileSizeChecker(new SystemConfig() {
@Override
public Long getMaxFileUploadSize() {
return 1000L;
}
});
}

@Test
public void testBytesToHumanReadable() {
long[] sizes = {1L, 1023L, 1986L, 125707L, 2759516000L, 12039650000000L};
Expand All @@ -35,4 +56,40 @@ public void testBytesToHumanReadable() {
assertEquals(expLongAns, longAns);
}

@Test(expected = NullPointerException.class)
public void testIsAllowedFileSize_throwsOnNull() {
fileSizeChecker.isAllowedFileSize(null);
}

@Test
public void testIsAllowedFileSize_allowsSmallerFileSize() {
FileSizeResponse response = fileSizeChecker.isAllowedFileSize(999L);
assertTrue(response.fileSizeOK);
}

@Test
public void testIsAllowedFileSize_allowsEqualFileSize() {
FileSizeResponse response = fileSizeChecker.isAllowedFileSize(1000L);
assertTrue(response.fileSizeOK);
}

@Test
public void testIsAllowedFileSize_rejectsBiggerFileSize() {
FileSizeResponse response = fileSizeChecker.isAllowedFileSize(1001L);
assertFalse(response.fileSizeOK);
}

@Test(expected = NullPointerException.class)
public void testIsAllowedFileSize_allowsOnUnboundedFileSize() {
// initialize a system config and instantiate a file size checker
// ensure that a max filesize is not set
FileSizeChecker unboundedFileSizeChecker = new FileSizeChecker(new SystemConfig() {
@Override
public Long getMaxFileUploadSize() {
return null;
}
});
FileSizeResponse response = unboundedFileSizeChecker.isAllowedFileSize(Long.MAX_VALUE);
assertTrue(response.fileSizeOK);
}
}

0 comments on commit 8b3fbbf

Please sign in to comment.