Skip to content
Snippets Groups Projects
Commit 37fda307 authored by felmer's avatar felmer
Browse files

SP-475, BIS-255: more unit tests

SVN: 28183
parent a0041ccc
No related branches found
No related tags found
No related merge requests found
...@@ -20,6 +20,7 @@ import java.io.File; ...@@ -20,6 +20,7 @@ import java.io.File;
import java.util.HashMap; import java.util.HashMap;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.log4j.Level;
import org.jmock.Expectations; import org.jmock.Expectations;
import org.jmock.Mockery; import org.jmock.Mockery;
import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterMethod;
...@@ -28,6 +29,7 @@ import org.testng.annotations.BeforeMethod; ...@@ -28,6 +29,7 @@ import org.testng.annotations.BeforeMethod;
import ch.systemsx.cisd.base.tests.AbstractFileSystemTestCase; import ch.systemsx.cisd.base.tests.AbstractFileSystemTestCase;
import ch.systemsx.cisd.common.filesystem.FileUtilities; import ch.systemsx.cisd.common.filesystem.FileUtilities;
import ch.systemsx.cisd.common.filesystem.IFileOperations; import ch.systemsx.cisd.common.filesystem.IFileOperations;
import ch.systemsx.cisd.common.logging.BufferedAppender;
import ch.systemsx.cisd.common.utilities.ITimeProvider; import ch.systemsx.cisd.common.utilities.ITimeProvider;
import ch.systemsx.cisd.common.utilities.MockTimeProvider; import ch.systemsx.cisd.common.utilities.MockTimeProvider;
import ch.systemsx.cisd.openbis.dss.generic.shared.ISingleDataSetPathInfoProvider; import ch.systemsx.cisd.openbis.dss.generic.shared.ISingleDataSetPathInfoProvider;
...@@ -61,6 +63,8 @@ public abstract class AbstractRemoteHierarchicalContentTestCase extends Abstract ...@@ -61,6 +63,8 @@ public abstract class AbstractRemoteHierarchicalContentTestCase extends Abstract
protected static final String FILE2_CONTENT = "hello file two"; protected static final String FILE2_CONTENT = "hello file two";
protected BufferedAppender logRecorder;
protected Mockery context; protected Mockery context;
protected IFileOperations fileOperations; protected IFileOperations fileOperations;
...@@ -84,11 +88,13 @@ public abstract class AbstractRemoteHierarchicalContentTestCase extends Abstract ...@@ -84,11 +88,13 @@ public abstract class AbstractRemoteHierarchicalContentTestCase extends Abstract
protected IPersistenceManager persistenceManager; protected IPersistenceManager persistenceManager;
protected HashMap<String, DataSetInfo> dataSetInfos; protected HashMap<String, DataSetInfo> dataSetInfos;
@BeforeMethod @BeforeMethod
public void setUpBasicFixture() public void setUpBasicFixture()
{ {
context = new Mockery(); context = new Mockery();
logRecorder = new BufferedAppender(Level.INFO);
fileOperations = context.mock(IFileOperations.class); fileOperations = context.mock(IFileOperations.class);
serviceFactory = context.mock(IDssServiceRpcGenericFactory.class); serviceFactory = context.mock(IDssServiceRpcGenericFactory.class);
remoteDss = context.mock(IDssServiceRpcGeneric.class, "remote DSS"); remoteDss = context.mock(IDssServiceRpcGeneric.class, "remote DSS");
...@@ -121,6 +127,7 @@ public abstract class AbstractRemoteHierarchicalContentTestCase extends Abstract ...@@ -121,6 +127,7 @@ public abstract class AbstractRemoteHierarchicalContentTestCase extends Abstract
@AfterMethod @AfterMethod
public void tearDown() public void tearDown()
{ {
logRecorder.reset();
// To following line of code should also be called at the end of each test method. // To following line of code should also be called at the end of each test method.
// Otherwise one do not known which test failed. // Otherwise one do not known which test failed.
context.assertIsSatisfied(); context.assertIsSatisfied();
......
...@@ -16,11 +16,16 @@ ...@@ -16,11 +16,16 @@
package ch.systemsx.cisd.openbis.dss.generic.shared.content; package ch.systemsx.cisd.openbis.dss.generic.shared.content;
import static ch.systemsx.cisd.openbis.dss.generic.shared.content.ContentCache.CACHE_FOLDER;
import static ch.systemsx.cisd.openbis.dss.generic.shared.content.ContentCache.DOWNLOADING_FOLDER;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.jmock.Expectations; import org.jmock.Expectations;
...@@ -41,6 +46,39 @@ import ch.systemsx.cisd.openbis.dss.generic.shared.dto.DataSetPathInfo; ...@@ -41,6 +46,39 @@ import ch.systemsx.cisd.openbis.dss.generic.shared.dto.DataSetPathInfo;
*/ */
public class ContentCacheTest extends AbstractRemoteHierarchicalContentTestCase public class ContentCacheTest extends AbstractRemoteHierarchicalContentTestCase
{ {
@Test
public void testCreateCacheInstanceForEmptyCache()
{
createCache();
assertEquals("Content cache created. Workspace: " + workSpace.getAbsolutePath()
+ "\nContent cache initialized. It contains 0 bytes from 0 data sets.",
logRecorder.getLogContent());
assertEquals("{}", dataSetInfos.toString());
context.assertIsSatisfied();
}
@Test
public void testCreateCacheInstanceForOneDataSetInCache()
{
File dataSetFolder = new File(workSpace, CACHE_FOLDER + "/" + DATA_SET_CODE);
File file = new File(dataSetFolder, "abc.txt");
file.getParentFile().mkdirs();
FileUtilities.writeToFile(file, FILE1_CONTENT);
dataSetFolder.setLastModified(42000);
prepareRequestPersistence(1);
createCache();
int size = (OSUtilities.isMacOS() ? 1 : 2) * 4;
assertEquals("Content cache created. Workspace: " + workSpace.getAbsolutePath()
+ "\nData set info recreated for data set " + DATA_SET_CODE
+ ".\nContent cache initialized. It contains " + size + ".00 KB from 1 data sets.",
logRecorder.getLogContent());
assertDataSetInfos(DATA_SET_CODE, 1, 1, 42000);
context.assertIsSatisfied();
}
@Test @Test
public void testDataSetLocking() public void testDataSetLocking()
{ {
...@@ -86,7 +124,7 @@ public class ContentCacheTest extends AbstractRemoteHierarchicalContentTestCase ...@@ -86,7 +124,7 @@ public class ContentCacheTest extends AbstractRemoteHierarchicalContentTestCase
DataSetPathInfo pathInfo = new DataSetPathInfo(); DataSetPathInfo pathInfo = new DataSetPathInfo();
pathInfo.setRelativePath(remoteFile1.getName()); pathInfo.setRelativePath(remoteFile1.getName());
pathInfo.setDirectory(false); pathInfo.setDirectory(false);
File fileInCache = new File(workSpace, ContentCache.CACHE_FOLDER + "/" File fileInCache = new File(workSpace, CACHE_FOLDER + "/"
+ DATA_SET_CODE + "/" + pathInfo.getRelativePath()); + DATA_SET_CODE + "/" + pathInfo.getRelativePath());
fileInCache.getParentFile().mkdirs(); fileInCache.getParentFile().mkdirs();
FileUtilities.writeToFile(fileInCache, FILE1_CONTENT); FileUtilities.writeToFile(fileInCache, FILE1_CONTENT);
...@@ -167,7 +205,7 @@ public class ContentCacheTest extends AbstractRemoteHierarchicalContentTestCase ...@@ -167,7 +205,7 @@ public class ContentCacheTest extends AbstractRemoteHierarchicalContentTestCase
DataSetPathInfo pathInfo = new DataSetPathInfo(); DataSetPathInfo pathInfo = new DataSetPathInfo();
pathInfo.setRelativePath(remoteFile1.getName()); pathInfo.setRelativePath(remoteFile1.getName());
pathInfo.setDirectory(false); pathInfo.setDirectory(false);
File fileInCache = new File(workSpace, ContentCache.CACHE_FOLDER + "/" File fileInCache = new File(workSpace, CACHE_FOLDER + "/"
+ DATA_SET_CODE + "/" + pathInfo.getRelativePath()); + DATA_SET_CODE + "/" + pathInfo.getRelativePath());
fileInCache.getParentFile().mkdirs(); fileInCache.getParentFile().mkdirs();
FileUtilities.writeToFile(fileInCache, FILE1_CONTENT); FileUtilities.writeToFile(fileInCache, FILE1_CONTENT);
...@@ -187,12 +225,16 @@ public class ContentCacheTest extends AbstractRemoteHierarchicalContentTestCase ...@@ -187,12 +225,16 @@ public class ContentCacheTest extends AbstractRemoteHierarchicalContentTestCase
final DataSetPathInfo pathInfo1 = prepareForDownloading(remoteFile1); final DataSetPathInfo pathInfo1 = prepareForDownloading(remoteFile1);
final DataSetPathInfo pathInfo2 = prepareForDownloading(remoteFile2); final DataSetPathInfo pathInfo2 = prepareForDownloading(remoteFile2);
prepareRequestPersistence(4); prepareRequestPersistence(4);
ContentCache cache = createCache(); ContentCache cache = createCache();
File downloadingFolder = new File(workSpace, DOWNLOADING_FOLDER);
assertEquals(false, downloadingFolder.exists());
InputStream inputStream1 = InputStream inputStream1 =
cache.getInputStream(SESSION_TOKEN, DATA_SET_LOCATION, pathInfo1); cache.getInputStream(SESSION_TOKEN, DATA_SET_LOCATION, pathInfo1);
assertEquals(1, new File(workSpace, DOWNLOADING_FOLDER).list().length);
InputStream inputStream2 = InputStream inputStream2 =
cache.getInputStream(SESSION_TOKEN, DATA_SET_LOCATION, pathInfo2); cache.getInputStream(SESSION_TOKEN, DATA_SET_LOCATION, pathInfo2);
assertEquals(2, downloadingFolder.list().length);
byte[] bytes1 = new byte[100]; byte[] bytes1 = new byte[100];
byte[] bytes2 = new byte[100]; byte[] bytes2 = new byte[100];
...@@ -201,7 +243,9 @@ public class ContentCacheTest extends AbstractRemoteHierarchicalContentTestCase ...@@ -201,7 +243,9 @@ public class ContentCacheTest extends AbstractRemoteHierarchicalContentTestCase
assertEquals(3, inputStream1.read(bytes1, 11, 100 - 11)); assertEquals(3, inputStream1.read(bytes1, 11, 100 - 11));
assertEquals(3, inputStream2.read(bytes2, 11, 100 - 11)); assertEquals(3, inputStream2.read(bytes2, 11, 100 - 11));
inputStream1.close(); inputStream1.close();
assertEquals(1, new File(workSpace, DOWNLOADING_FOLDER).list().length);
inputStream2.close(); inputStream2.close();
assertEquals(0, new File(workSpace, DOWNLOADING_FOLDER).list().length);
assertEquals(FILE1_CONTENT, new String(bytes1, 0, FILE1_CONTENT.length())); assertEquals(FILE1_CONTENT, new String(bytes1, 0, FILE1_CONTENT.length()));
assertEquals(FILE2_CONTENT, new String(bytes2, 0, FILE2_CONTENT.length())); assertEquals(FILE2_CONTENT, new String(bytes2, 0, FILE2_CONTENT.length()));
...@@ -230,7 +274,7 @@ public class ContentCacheTest extends AbstractRemoteHierarchicalContentTestCase ...@@ -230,7 +274,7 @@ public class ContentCacheTest extends AbstractRemoteHierarchicalContentTestCase
new MessageChannelBuilder(10000).name("3").logger(logger).getChannel(); new MessageChannelBuilder(10000).name("3").logger(logger).getChannel();
final ContentCache cache = createCache(); final ContentCache cache = createCache();
File fileInCache = File fileInCache =
new File(workSpace, ContentCache.CACHE_FOLDER + "/" + DATA_SET_CODE + "/" new File(workSpace, CACHE_FOLDER + "/" + DATA_SET_CODE + "/"
+ remoteFile1.getName()); + remoteFile1.getName());
assertEquals(false, fileInCache.exists()); assertEquals(false, fileInCache.exists());
prepareRequestPersistence(3); prepareRequestPersistence(3);
...@@ -304,7 +348,7 @@ public class ContentCacheTest extends AbstractRemoteHierarchicalContentTestCase ...@@ -304,7 +348,7 @@ public class ContentCacheTest extends AbstractRemoteHierarchicalContentTestCase
prepareRequestPersistence(3); prepareRequestPersistence(3);
File fileInCache = File fileInCache =
new File(workSpace, ContentCache.CACHE_FOLDER + "/" + DATA_SET_CODE + "/" new File(workSpace, CACHE_FOLDER + "/" + DATA_SET_CODE + "/"
+ remoteFile1.getName()); + remoteFile1.getName());
assertEquals(false, fileInCache.exists()); assertEquals(false, fileInCache.exists());
...@@ -373,7 +417,7 @@ public class ContentCacheTest extends AbstractRemoteHierarchicalContentTestCase ...@@ -373,7 +417,7 @@ public class ContentCacheTest extends AbstractRemoteHierarchicalContentTestCase
}); });
prepareRequestPersistence(3); prepareRequestPersistence(3);
File fileInCache = File fileInCache =
new File(workSpace, ContentCache.CACHE_FOLDER + "/" + DATA_SET_CODE + "/" new File(workSpace, CACHE_FOLDER + "/" + DATA_SET_CODE + "/"
+ remoteFile1.getName()); + remoteFile1.getName());
assertEquals(false, fileInCache.exists()); assertEquals(false, fileInCache.exists());
...@@ -409,7 +453,7 @@ public class ContentCacheTest extends AbstractRemoteHierarchicalContentTestCase ...@@ -409,7 +453,7 @@ public class ContentCacheTest extends AbstractRemoteHierarchicalContentTestCase
assertEquals(fileInCache.getAbsolutePath(), fileFromCache.getAbsolutePath()); assertEquals(fileInCache.getAbsolutePath(), fileFromCache.getAbsolutePath());
context.assertIsSatisfied(); context.assertIsSatisfied();
} }
private void assertDataSetInfos(String dataSetCode, int expectedNumberOfSmallFiles, int expectedNumberOfFolders, private void assertDataSetInfos(String dataSetCode, int expectedNumberOfSmallFiles, int expectedNumberOfFolders,
long expectedLastModified) long expectedLastModified)
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment