Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5218 arbitrary filetype index #5219

Merged
merged 6 commits into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 22 additions & 10 deletions src/main/java/edu/harvard/iq/dataverse/util/BundleUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

public class BundleUtil {
Expand All @@ -27,19 +28,30 @@ public static String getStringFromBundle(String key, List<String> arguments) {
ResourceBundle bundle = getResourceBundle(defaultBundleFile );
return getStringFromBundle(key, arguments, bundle);
}

public static String getStringFromBundle(String key, List<String> arguments, ResourceBundle bundle) {
if (key == null || key.isEmpty()) {
return null;
}
String stringFromBundle = null;
try {
stringFromBundle = bundle.getString(key);
logger.fine("string found: " + stringFromBundle);
return getStringFromBundleNoMissingCheck(key, arguments, bundle);
} catch (MissingResourceException ex) {
logger.warning("Could not find key \"" + key + "\" in bundle file.");
logger.warning("Could not find key \"" + key + "\" in bundle file: ");
logger.log(Level.CONFIG, ex.getMessage(), ex);
return null;
}
}

/**
* This call was added to allow bypassing the exception catch, for filetype indexing needs the exception to bubble up
* --MAD 4.9.4
*/
private static String getStringFromBundleNoMissingCheck(String key, List<String> arguments, ResourceBundle bundle) throws MissingResourceException {
if (key == null || key.isEmpty()) {
return null;
}
String stringFromBundle = null;

stringFromBundle = bundle.getString(key);
logger.fine("string found: " + stringFromBundle);

if (arguments != null) {
Object[] argArray = new String[arguments.size()];
argArray = arguments.toArray(argArray);
Expand All @@ -49,9 +61,9 @@ public static String getStringFromBundle(String key, List<String> arguments, Res
}
}

public static String getStringFromPropertyFile(String key, String propertyFileName ) {
public static String getStringFromPropertyFile(String key, String propertyFileName ) throws MissingResourceException {
ResourceBundle bundle = getResourceBundle(propertyFileName);
return getStringFromBundle(key, null, bundle);
return getStringFromBundleNoMissingCheck(key, null, bundle);
}

public static ResourceBundle getResourceBundle(String propertyFileName)
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/edu/harvard/iq/dataverse/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,15 @@ public static String getFacetFileType(DataFile dataFile) {
String typeClass = fileType.split("/")[0];
return Character.toUpperCase(typeClass.charAt(0)) + typeClass.substring(1);
}
} else {
try {
return BundleUtil.getStringFromPropertyFile("application/octet-stream","MimeTypeFacets" );
} catch (MissingResourceException ex) {
logger.warning("Could not find \"" + fileType + "\" in bundle file: ");
logger.log(Level.CONFIG, ex.getMessage(), ex);
return null;
}
}

return BundleUtil.getStringFromPropertyFile("application/octet-stream","MimeTypeFacets" );
}

public static String getUserFriendlyOriginalType(DataFile dataFile) {
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/util/BundleUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Arrays;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -70,5 +71,21 @@ public void testGetStringFromBundleWithArgumentsAndSpecificBundle() {
assertEquals(null, BundleUtil.getStringFromBundle(null, null, null));
assertEquals("Search", BundleUtil.getStringFromBundle("search", null, ResourceBundle.getBundle("Bundle", Locale.US)));
}

@Test
public void testStringFromPropertyFile() {
assertEquals("ZIP", BundleUtil.getStringFromPropertyFile("application/zip","MimeTypeFacets"));
}

//To assure that the MissingResourceException bubble up from this call
@Test(expected = MissingResourceException.class)
public void testStringFromPropertyFileException() {
BundleUtil.getStringFromPropertyFile("FAKE","MimeTypeFacets");
}

//To assure MissingResourceException is caught when calling normal bundle calls
@Test
public void testNoErrorNonExistentStringBundle() {
BundleUtil.getStringFromBundle("FAKE", null, BundleUtil.getResourceBundle("MimeTypeFacets"));
}
}