NIHVIVO-160 Complete the FileStorageHelper, with tests.
This commit is contained in:
parent
c4709265c4
commit
544432acc1
9 changed files with 807 additions and 60 deletions
|
@ -3,6 +3,7 @@
|
|||
package edu.cornell.mannlib.vitro.webapp.utils.filestorage;
|
||||
|
||||
import static edu.cornell.mannlib.vitro.webapp.utils.filestorage.FileStorageFactory.PROPERTY_IMPLEMETATION_CLASSNAME;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
|
@ -28,7 +29,8 @@ import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
|
|||
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* TODO
|
||||
* This just checks the interaction between the configuration properties, the
|
||||
* system properties, and the implementation of {@link FileStorage}.
|
||||
*/
|
||||
public class FileStorageFactoryTest extends AbstractTestClass {
|
||||
private static final String configProperties = "#mock config properties file\n";
|
||||
|
@ -62,7 +64,7 @@ public class FileStorageFactoryTest extends AbstractTestClass {
|
|||
@Test
|
||||
public void createDefaultImplementation() throws IOException {
|
||||
setConfigurationProperties(tempDir.getPath(),
|
||||
"http://vivo.myDomain.edu/individual/");
|
||||
"http://vivo.myDomain.edu/individual/", "50M");
|
||||
FileStorage fs = FileStorageFactory.getFileStorage();
|
||||
assertEquals("implementation class", FileStorageImpl.class, fs
|
||||
.getClass());
|
||||
|
@ -80,13 +82,20 @@ public class FileStorageFactoryTest extends AbstractTestClass {
|
|||
@Test(expected = IllegalArgumentException.class)
|
||||
public void baseDirectoryDoesntExist() throws IOException {
|
||||
setConfigurationProperties("/bogus/Directory",
|
||||
"http://vivo.myDomain.edu/individual/");
|
||||
"http://vivo.myDomain.edu/individual/", "50M");
|
||||
FileStorageFactory.getFileStorage();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void defaultNamespaceIsBogus() throws IOException {
|
||||
setConfigurationProperties(tempDir.getPath(), "namespace");
|
||||
setConfigurationProperties(tempDir.getPath(), "namespace", "50M");
|
||||
FileStorageFactory.getFileStorage();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void invalidMaximumFileSize() throws IOException {
|
||||
setConfigurationProperties(tempDir.getPath(),
|
||||
"http://vivo.myDomain.edu/individual/", "50X");
|
||||
FileStorageFactory.getFileStorage();
|
||||
}
|
||||
|
||||
|
@ -115,10 +124,11 @@ public class FileStorageFactoryTest extends AbstractTestClass {
|
|||
// ----------------------------------------------------------------------
|
||||
|
||||
private void setConfigurationProperties(String baseDir,
|
||||
String defaultNamespace) {
|
||||
String defaultNamespace, String maxFileSize) {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
map.put("upload.directory", baseDir);
|
||||
map.put("Vitro.defaultNamespace", defaultNamespace);
|
||||
map.put(FileStorage.PROPERTY_FILE_STORAGE_BASE_DIR, baseDir);
|
||||
map.put(FileStorage.PROPERTY_DEFAULT_NAMESPACE, defaultNamespace);
|
||||
map.put(FileStorage.PROPERTY_FILE_MAXIMUM_SIZE, maxFileSize);
|
||||
|
||||
try {
|
||||
Field f = ConfigurationProperties.class.getDeclaredField("theMap");
|
||||
|
@ -147,7 +157,7 @@ public class FileStorageFactoryTest extends AbstractTestClass {
|
|||
return "filename";
|
||||
}
|
||||
|
||||
public byte[] getfile(String id, String filename)
|
||||
public byte[] getFile(String id, String filename)
|
||||
throws FileNotFoundException, IOException {
|
||||
return new byte[0];
|
||||
}
|
||||
|
@ -171,7 +181,7 @@ public class FileStorageFactoryTest extends AbstractTestClass {
|
|||
return "filename";
|
||||
}
|
||||
|
||||
public byte[] getfile(String id, String filename)
|
||||
public byte[] getFile(String id, String filename)
|
||||
throws FileNotFoundException, IOException {
|
||||
return new byte[0];
|
||||
}
|
||||
|
|
|
@ -0,0 +1,239 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.utils.filestorage;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class FileStorageHelperTest {
|
||||
private static String RAW_NAME_1 = "simpleName";
|
||||
private static String ENCODED_NAME_1 = "simpleName";
|
||||
private static String RAW_NAME_2 = "common:/Chars.pdf";
|
||||
private static String ENCODED_NAME_2 = "common+=Chars.pdf";
|
||||
private static String RAW_NAME_3 = "rare\"+~chars";
|
||||
private static String ENCODED_NAME_3 = "rare^22^2b^7echars";
|
||||
private static String RAW_NAME_4 = "combination+<of^:both";
|
||||
private static String ENCODED_NAME_4 = "combination^2b^3cof^5e+both";
|
||||
private static String RAW_NAME_5 = " invisibles\u0001\u007f";
|
||||
private static String ENCODED_NAME_5 = "^20invisibles^01^7f";
|
||||
private static String RAW_NAME_6 = "out of range\u0101";
|
||||
|
||||
private static String ID_1 = "simpleName";
|
||||
private static String RELATIVE_PATH_1 = "sim/ple/Nam/e";
|
||||
private static String ID_2 = "combination+<of^:both";
|
||||
private static String RELATIVE_PATH_2 = "com/bin/ati/on^/2b^/3co/f^5/e+b/oth";
|
||||
private static String ID_3 = "http://vivo.myDomain.edu/file/n3234";
|
||||
private static String RELATIVE_PATH_3 = "htt/p+=/=vi/vo,/myD/oma/in,/edu/=fi/le=/n32/34";
|
||||
private static String RELATIVE_PREFIXED_PATH_3 = "b~n/323/4";
|
||||
|
||||
private static File ROOT_DIR_1 = new File("/root");
|
||||
private static File ABSOLUTE_PATH_1 = new File("/root/sim/ple/Nam/e");
|
||||
private static File ROOT_DIR_2 = new File("/this/that/slash/");
|
||||
private static File ABSOLUTE_PATH_2 = new File(
|
||||
"/this/that/slash/sim/ple/Nam/e");
|
||||
|
||||
private static String FULL_NAME = "myPhoto.jpg";
|
||||
private static String FULL_ID = "http://vivo.myDomain.edu/file/n3234.XXX";
|
||||
private static File FULL_ROOT = new File(
|
||||
"/usr/local/vivo/uploads/file_storage_root");
|
||||
private static File FULL_RESULT_PATH = new File(
|
||||
"/usr/local/vivo/uploads/file_storage_root/b~n/323/4,X/XX/myPhoto.jpg");
|
||||
|
||||
private static Map<Character, String> EMPTY_NAMESPACES = Collections
|
||||
.emptyMap();
|
||||
private static Map<Character, String> NAMESPACES = initPrefixMap();
|
||||
|
||||
private static Map<Character, String> initPrefixMap() {
|
||||
Map<Character, String> map = new HashMap<Character, String>();
|
||||
map.put('a', "junk");
|
||||
map.put('b', "http://vivo.myDomain.edu/file/");
|
||||
return map;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// encodeName
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
public void encodeName1() {
|
||||
assertNameEncoding(RAW_NAME_1, ENCODED_NAME_1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeName2() {
|
||||
assertNameEncoding(RAW_NAME_2, ENCODED_NAME_2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeName3() {
|
||||
assertNameEncoding(RAW_NAME_3, ENCODED_NAME_3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeName4() {
|
||||
assertNameEncoding(RAW_NAME_4, ENCODED_NAME_4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeName5() {
|
||||
assertNameEncoding(RAW_NAME_5, ENCODED_NAME_5);
|
||||
}
|
||||
|
||||
@Test(expected = InvalidCharacterException.class)
|
||||
public void encodeName6() {
|
||||
FileStorageHelper.encodeName(RAW_NAME_6);
|
||||
}
|
||||
|
||||
private void assertNameEncoding(String rawName, String expected) {
|
||||
String encoded = FileStorageHelper.encodeName(rawName);
|
||||
assertEquals("encoded name", expected, encoded);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// decodeName
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
public void decodeName1() {
|
||||
assertNameDecoding(ENCODED_NAME_1, RAW_NAME_1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeName2() {
|
||||
assertNameDecoding(ENCODED_NAME_2, RAW_NAME_2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeName3() {
|
||||
assertNameDecoding(ENCODED_NAME_3, RAW_NAME_3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeName4() {
|
||||
assertNameDecoding(ENCODED_NAME_4, RAW_NAME_4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeName5() {
|
||||
assertNameDecoding(ENCODED_NAME_5, RAW_NAME_5);
|
||||
}
|
||||
|
||||
private void assertNameDecoding(String encodedName, String expected) {
|
||||
String decoded = FileStorageHelper.decodeName(encodedName);
|
||||
assertEquals("decodedName", expected, decoded);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// idToPath
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
public void idToPath1() {
|
||||
assertIdToPath(ID_1, EMPTY_NAMESPACES, RELATIVE_PATH_1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void idToPath2() {
|
||||
assertIdToPath(ID_2, EMPTY_NAMESPACES, RELATIVE_PATH_2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void idToPath3() {
|
||||
assertIdToPath(ID_3, EMPTY_NAMESPACES, RELATIVE_PATH_3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void idToPath3WithNamespace() {
|
||||
assertIdToPath(ID_3, NAMESPACES, RELATIVE_PREFIXED_PATH_3);
|
||||
}
|
||||
|
||||
private void assertIdToPath(String id, Map<Character, String> namespaces,
|
||||
String expected) {
|
||||
String adjustedExpected = expected.replace('/', File.separatorChar);
|
||||
String relativePath = FileStorageHelper.id2Path(id, namespaces);
|
||||
assertEquals("idToPath", adjustedExpected, relativePath);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// getPathToIdDirectory
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
public void getPathToIdDirectory1() {
|
||||
assertPathToIdDirectory(ID_1, EMPTY_NAMESPACES, ROOT_DIR_1,
|
||||
ABSOLUTE_PATH_1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPathToIdDirectory2() {
|
||||
assertPathToIdDirectory(ID_1, EMPTY_NAMESPACES, ROOT_DIR_2,
|
||||
ABSOLUTE_PATH_2);
|
||||
}
|
||||
|
||||
private void assertPathToIdDirectory(String id,
|
||||
Map<Character, String> namespaces, File rootDir, File expected) {
|
||||
File actual = FileStorageHelper.getPathToIdDirectory(id, namespaces,
|
||||
rootDir);
|
||||
File adjustedExpected = new File(expected.getPath().replace('/',
|
||||
File.separatorChar));
|
||||
assertEquals("pathToIdDirectory", adjustedExpected, actual);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// getFullPath
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
public void getFullPath() {
|
||||
File actual = FileStorageHelper.getFullPath(FULL_ROOT, FULL_ID,
|
||||
FULL_NAME, NAMESPACES);
|
||||
assertEquals("fullPath", FULL_RESULT_PATH, actual);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// parseMaximumFileSize
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
public void parseMaximumFileSizeBare() {
|
||||
long size = FileStorageHelper.parseMaximumFileSize("1467898");
|
||||
assertEquals("", 1467898, size);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseMaximumFileSizeWithSuffixes() {
|
||||
long size = FileStorageHelper.parseMaximumFileSize("152K");
|
||||
assertEquals("", 152L * 1024L, size);
|
||||
|
||||
size = FileStorageHelper.parseMaximumFileSize("47M");
|
||||
assertEquals("", 47L * 1024L * 1024L, size);
|
||||
|
||||
size = FileStorageHelper.parseMaximumFileSize("3G");
|
||||
assertEquals("", 3L * 1024L * 1024L * 1024L, size);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void parseMaximumFileSizeInvalidSuffix() {
|
||||
FileStorageHelper.parseMaximumFileSize("152X");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void parseMaximumFileSizeNegativeNumber() {
|
||||
FileStorageHelper.parseMaximumFileSize("-3K");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void parseMaximumFileSizeEmbeddedBadCharacter() {
|
||||
FileStorageHelper.parseMaximumFileSize("1G52K");
|
||||
}
|
||||
|
||||
}
|
|
@ -2,16 +2,18 @@
|
|||
|
||||
package edu.cornell.mannlib.vitro.webapp.utils.filestorage;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
|
||||
|
||||
/**
|
||||
* Test the FileStorage methods. The zero-argument constructor was tested in
|
||||
* {@link FileStorageFactoryTest}.
|
||||
*/
|
||||
public class FileStorageImplTest {
|
||||
public class FileStorageImplTest extends AbstractTestClass {
|
||||
@Ignore
|
||||
@Test
|
||||
public void baseDirDoesntExist() {
|
||||
|
@ -48,4 +50,69 @@ public class FileStorageImplTest {
|
|||
fail("initializedNamespacesDontMatch not implemented");
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void createFileOriginal() {
|
||||
fail("createFileOriginal not implemented");
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void createFileOverwrite() {
|
||||
fail("createFileOverwrite not implemented");
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void createFileConflictingName() {
|
||||
fail("createFileConflictingName not implemented");
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void getFilenameExists() {
|
||||
fail("getFilenameExists not implemented");
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void getFilenameDoesntExist() {
|
||||
fail("getFilenameDoesntExist not implemented");
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void getFilenameMultipleFiles() {
|
||||
fail("getFilenameMultipleFiles not implemented");
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void getFileFound() {
|
||||
fail("getFilenameFound not implemented");
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void getFileNotFound() {
|
||||
fail("getFileNotFound not implemented");
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void getFileTooLarge() {
|
||||
fail("getFileTooLarge not implemented");
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void deleteFileExists() {
|
||||
fail("deleteFileExists not implemented");
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void deleteFileDoesntExist() {
|
||||
fail("deleteFileDoesntExist not implemented");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue