NIHVIVO-157 manual merge of the changes from branch NIHVIVO-157-file-storage

This commit is contained in:
jeb228 2010-06-20 19:23:19 +00:00
parent 083aa4e530
commit fb7d5bbba9
51 changed files with 3535 additions and 1198 deletions

View file

@ -2,6 +2,8 @@
package edu.cornell.mannlib.vitro.webapp.dao.jena;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.StringReader;
import junit.framework.Assert;
@ -15,7 +17,6 @@ import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.ontology.Restriction;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;
@ -110,12 +111,8 @@ public class JenaBaseDaoTest {
isDependentRelation ;
Model expectedModel = (ModelFactory.createOntologyModel()).read(new StringReader(expected), "", "N3");
//modtime times make it difficult to compare graphs
wipeOutModTime(expectedModel);
wipeOutModTime(ontModel);
Assert.assertTrue( ontModel.isIsomorphicWith(expectedModel) );
assertEquivalentModels(expectedModel, ontModel);
} catch (InsertException e) {
Assert.fail(e.getMessage());
}
@ -192,11 +189,7 @@ public class JenaBaseDaoTest {
Model expectedModel = (ModelFactory.createOntologyModel()).read(new StringReader(expected), "", "N3");
//modtime times make it difficult to compare graphs
wipeOutModTime(expectedModel);
wipeOutModTime(model);
Assert.assertTrue( model.isIsomorphicWith(expectedModel));
assertEquivalentModels(expectedModel, model);
}
@ -256,11 +249,7 @@ public class JenaBaseDaoTest {
Model expectedModel = (ModelFactory.createOntologyModel()).read(new StringReader(expected), "", "N3");
//modtime times make it difficult to compare graphs
wipeOutModTime(expectedModel);
wipeOutModTime(ontModel);
Assert.assertTrue( ontModel.isIsomorphicWith(expectedModel) );
assertEquivalentModels(expectedModel, ontModel);
} catch (InsertException e) {
Assert.fail(e.getMessage());
}
@ -367,17 +356,7 @@ public class JenaBaseDaoTest {
// wipeOutModTime(model);
// Assert.assertTrue( model.isIsomorphicWith(expectedModel));
// }
void printModels(Model expected, Model result){
System.out.println("Expected:");
expected.write(System.out);
System.out.println("Result:");
result.write(System.out);
}
void wipeOutModTime(Model model){
model.removeAll(null, model.createProperty(VitroVocabulary.MODTIME), null);
}
@Test
/**
* Tests that any statements with a property as predicate are removed
@ -478,5 +457,32 @@ public class JenaBaseDaoTest {
Assert.assertEquals(m.size(), 2); // just rdf:type for Class1 and Prop
}
/**
* Compare the contents of the expected model with the actual model (not counting modification times).
*/
private void assertEquivalentModels(Model expected, Model actual) {
// modtime times make it difficult to compare graphs
wipeOutModTime(expected);
wipeOutModTime(actual);
if (actual.isIsomorphicWith(expected)) {
return;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintStream p = new PrintStream(out, true);
p.println("Models do not match: expected <");
expected.write(out);
p.println("> but was <");
actual.write(out);
p.println(">");
Assert.fail(out.toString());
}
private void wipeOutModTime(Model model){
model.removeAll(null, model.createProperty(VitroVocabulary.MODTIME), null);
}
}

View file

@ -0,0 +1,120 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.filestorage;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.File;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import javax.naming.InitialContext;
import org.apache.log4j.Level;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import stubs.javax.naming.InitialContextStub;
import stubs.javax.naming.spi.InitialContextFactoryStub;
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.filestorage.backend.FileStorageSetup;
/**
*/
public class FileServingHelperTest extends AbstractTestClass {
private static final String DEFAULT_NAMESPACE = "http://some.crazy.domain/individual/";
private static final String CONFIG_PROPERTIES = "#mock config properties file\n";
private static File tempDir;
// ----------------------------------------------------------------------
// framework
// ----------------------------------------------------------------------
/**
* Use a mock {@link InitialContext} to create an empty
* {@link ConfigurationProperties} object. Each test can use
* {@link #setConfigurationProperties(String, String)} to populate it as
* they choose.
*/
@BeforeClass
public static void createConfigurationProperties() throws Exception {
tempDir = createTempDirectory("FileServingHelperTest");
File propsFile = createFile(tempDir, "config.properties",
CONFIG_PROPERTIES);
System.setProperty(InitialContext.INITIAL_CONTEXT_FACTORY,
InitialContextFactoryStub.class.getName());
InitialContextStub.reset();
new InitialContext().bind("java:comp/env/path.configuration", propsFile
.getPath());
setConfigurationProperties(DEFAULT_NAMESPACE);
}
@AfterClass
public static void cleanup() {
purgeDirectoryRecursively(tempDir);
}
// ----------------------------------------------------------------------
// tests
// ----------------------------------------------------------------------
@Test
public void nullUri() {
assertCorrectUrl(null, "somefilename.ext", null);
}
@Test
public void nullFilename() {
assertCorrectUrl("http://some.crazy.domain/individual/n4324", null,
null);
}
@Test
public void notInDefaultNamespace() {
setLoggerLevel(FileServingHelper.class, Level.ERROR);
assertCorrectUrl("notInTheNamespace",
"somefilename.ext", "notInTheNamespace");
}
@Test
public void inDefaultNamespaceNoTrailingSlash() {
assertCorrectUrl("http://some.crazy.domain/individual/n4324",
"somefilename.ext", "/file/n4324/somefilename.ext");
}
@Test
public void inDefaultNamespaceTrailingSlash() {
assertCorrectUrl("http://some.crazy.domain/individual/n4324/",
"somefilename.ext", "/file/n4324/somefilename.ext");
}
// ----------------------------------------------------------------------
// Helper methods
// ----------------------------------------------------------------------
private static void setConfigurationProperties(String defaultNamespace) {
Map<String, String> map = new HashMap<String, String>();
map.put(FileStorageSetup.PROPERTY_DEFAULT_NAMESPACE, defaultNamespace);
try {
Field f = ConfigurationProperties.class.getDeclaredField("theMap");
f.setAccessible(true);
f.set(null, map);
} catch (Exception e) {
fail("Exception while setting config properties: " + e);
}
}
private void assertCorrectUrl(String uri, String filename, String expected) {
String actual = FileServingHelper.getBytestreamAliasUrl(uri, filename);
assertEquals("url", expected, actual);
}
}