From 787e378a3c594e52725e8cf67a65d423ff550d59 Mon Sep 17 00:00:00 2001 From: jeb228 Date: Fri, 21 May 2010 17:42:03 +0000 Subject: [PATCH] NIHVIVO-160 Start writing unit tests as the implementation progresses. --- .../filestorage/FileStorageFactoryTest.java | 183 ++++++++++++++++++ .../filestorage/FileStorageImplTest.java | 51 +++++ 2 files changed, 234 insertions(+) create mode 100644 webapp/test/edu/cornell/mannlib/vitro/webapp/utils/filestorage/FileStorageFactoryTest.java create mode 100644 webapp/test/edu/cornell/mannlib/vitro/webapp/utils/filestorage/FileStorageImplTest.java diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/filestorage/FileStorageFactoryTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/filestorage/FileStorageFactoryTest.java new file mode 100644 index 000000000..578084ab8 --- /dev/null +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/filestorage/FileStorageFactoryTest.java @@ -0,0 +1,183 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +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.assertEquals; +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; + +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import org.junit.After; +import org.junit.Before; +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; + +/** + * TODO + */ +public class FileStorageFactoryTest extends AbstractTestClass { + private static final String configProperties = "#mock config properties file\n"; + private static File tempDir; + private static File propsFile; + + @Before + public void setup() throws Exception { + tempDir = createTempDirectory("FileStorageFactoryTest"); + + propsFile = createFile(tempDir, "config.properties", configProperties); + + System.setProperty(InitialContext.INITIAL_CONTEXT_FACTORY, + InitialContextFactoryStub.class.getName()); + InitialContextStub.reset(); + new InitialContext().bind("java:comp/env/path.configuration", propsFile + .getPath()); + } + + @Before + public void initializeConfigurationProperties() throws NamingException, + SecurityException, NoSuchMethodException, IllegalArgumentException, + IllegalAccessException, InvocationTargetException { + } + + @After + public void cleanup() { + purgeDirectoryRecursively(tempDir); + } + + @Test + public void createDefaultImplementation() throws IOException { + setConfigurationProperties(tempDir.getPath(), + "http://vivo.myDomain.edu/individual/"); + FileStorage fs = FileStorageFactory.getFileStorage(); + assertEquals("implementation class", FileStorageImpl.class, fs + .getClass()); + } + + @Test + public void createAlternateImplementation() throws IOException { + System.setProperty(PROPERTY_IMPLEMETATION_CLASSNAME, + FileStorageStub.class.getName()); + FileStorage fs = FileStorageFactory.getFileStorage(); + assertEquals("implementation class", FileStorageStub.class, fs + .getClass()); + } + + @Test(expected = IllegalArgumentException.class) + public void baseDirectoryDoesntExist() throws IOException { + setConfigurationProperties("/bogus/Directory", + "http://vivo.myDomain.edu/individual/"); + FileStorageFactory.getFileStorage(); + } + + @Test(expected = IllegalArgumentException.class) + public void defaultNamespaceIsBogus() throws IOException { + setConfigurationProperties(tempDir.getPath(), "namespace"); + FileStorageFactory.getFileStorage(); + } + + @Test(expected = IllegalArgumentException.class) + public void noSuchClass() throws IOException { + System.setProperty(PROPERTY_IMPLEMETATION_CLASSNAME, "bogus.Classname"); + FileStorageFactory.getFileStorage(); + } + + @Test(expected = IllegalArgumentException.class) + public void doesntImplement() throws IOException { + System.setProperty(PROPERTY_IMPLEMETATION_CLASSNAME, + NotFileStorage.class.getName()); + FileStorageFactory.getFileStorage(); + } + + @Test(expected = IllegalArgumentException.class) + public void noZeroArgsConstructor() throws IOException { + System.setProperty(PROPERTY_IMPLEMETATION_CLASSNAME, + NoConstructor.class.getName()); + FileStorageFactory.getFileStorage(); + } + + // ---------------------------------------------------------------------- + // Helper methods + // ---------------------------------------------------------------------- + + private void setConfigurationProperties(String baseDir, + String defaultNamespace) { + Map map = new HashMap(); + map.put("upload.directory", baseDir); + map.put("Vitro.defaultNamespace", 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); + } + } + + // ---------------------------------------------------------------------- + // Helper classes + // ---------------------------------------------------------------------- + + /** An alternative implementation of FileStorage. */ + public static class FileStorageStub implements FileStorage { + public void createFile(String id, String filename, InputStream bytes) + throws FileAlreadyExistsException, IOException { + } + + public boolean deleteFile(String id) throws IOException { + return true; + } + + public String getFilename(String id) throws IOException { + return "filename"; + } + + public byte[] getfile(String id, String filename) + throws FileNotFoundException, IOException { + return new byte[0]; + } + } + + /** This class has no zero-argument constructor. */ + public static class NoConstructor implements FileStorage { + @SuppressWarnings("unused") + public NoConstructor(String string) { + } + + public void createFile(String id, String filename, InputStream bytes) + throws FileAlreadyExistsException, IOException { + } + + public boolean deleteFile(String id) throws IOException { + return true; + } + + public String getFilename(String id) throws IOException { + return "filename"; + } + + public byte[] getfile(String id, String filename) + throws FileNotFoundException, IOException { + return new byte[0]; + } + } + + /** This class does not implement the FileStorage interface. */ + public static class NotFileStorage { + } +} diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/filestorage/FileStorageImplTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/filestorage/FileStorageImplTest.java new file mode 100644 index 000000000..91dad1f32 --- /dev/null +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/utils/filestorage/FileStorageImplTest.java @@ -0,0 +1,51 @@ +/* $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.*; + +import org.junit.Ignore; +import org.junit.Test; + +/** + * Test the FileStorage methods. The zero-argument constructor was tested in + * {@link FileStorageFactoryTest}. + */ +public class FileStorageImplTest { + @Ignore + @Test + public void baseDirDoesntExist() { + fail("baseDirDoesntExist not implemented"); + } + + @Ignore + @Test + public void partialInitializationRoot() { + fail("partialInitializationRoot not implemented"); + } + + @Ignore + @Test + public void partialInitializationNamespaces() { + fail("partialInitializationNamespaces not implemented"); + } + + @Ignore + @Test + public void notInitialized() { + fail("notInitialized not implemented"); + } + + @Ignore + @Test + public void initializedOK() { + fail("initializedOK not implemented"); + } + + @Ignore + @Test + public void initializedNamespacesDontMatch() { + fail("initializedNamespacesDontMatch not implemented"); + } + +}