DATAGEODE-55 - Adapt to Apache Geode SnapshotService API and behavioral/functional changes.
Changed Apache Gedoe Cache and Region snapshot files to have a .gfd extension.
This commit is contained in:
@@ -16,8 +16,11 @@
|
||||
|
||||
package org.springframework.data.gemfire.snapshot;
|
||||
|
||||
import static java.util.Arrays.stream;
|
||||
import static org.apache.geode.cache.snapshot.SnapshotOptions.SnapshotFormat;
|
||||
import static org.springframework.data.gemfire.snapshot.SnapshotServiceFactoryBean.SnapshotServiceAdapter;
|
||||
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalArgumentException;
|
||||
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.DataInputStream;
|
||||
@@ -28,6 +31,7 @@ import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
@@ -41,11 +45,11 @@ import org.apache.geode.cache.snapshot.RegionSnapshotService;
|
||||
import org.apache.geode.cache.snapshot.SnapshotFilter;
|
||||
import org.apache.geode.cache.snapshot.SnapshotOptions;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.data.gemfire.snapshot.event.ExportSnapshotApplicationEvent;
|
||||
import org.springframework.data.gemfire.snapshot.event.SnapshotApplicationEvent;
|
||||
import org.springframework.data.gemfire.support.AbstractFactoryBeanSupport;
|
||||
import org.springframework.data.gemfire.util.CollectionUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
@@ -68,8 +72,8 @@ import org.springframework.util.StringUtils;
|
||||
* @since 1.7.0
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotServiceAdapter<K, V>>,
|
||||
InitializingBean, DisposableBean, ApplicationListener<SnapshotApplicationEvent<K, V>> {
|
||||
public class SnapshotServiceFactoryBean<K, V> extends AbstractFactoryBeanSupport<SnapshotServiceAdapter<K, V>>
|
||||
implements InitializingBean, DisposableBean, ApplicationListener<SnapshotApplicationEvent<K, V>> {
|
||||
|
||||
protected static final SnapshotMetadata[] EMPTY_ARRAY = new SnapshotMetadata[0];
|
||||
|
||||
@@ -100,6 +104,73 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
|
||||
return (file != null && file.isFile());
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs and initializes the GemFire Snapshot Service used to take a snapshot of the configured Cache
|
||||
* or Region if initialized. In addition, this initialization method will perform the actual import.
|
||||
*
|
||||
* @throws Exception if the construction and initialization of the GemFire Snapshot Service fails.
|
||||
* @see org.springframework.data.gemfire.snapshot.SnapshotServiceFactoryBean.SnapshotServiceAdapter
|
||||
* @see #getSuppressImportOnInit()
|
||||
* @see #getImports()
|
||||
* @see #create()
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
|
||||
this.snapshotServiceAdapter = create();
|
||||
|
||||
if (!getSuppressImportOnInit()) {
|
||||
this.snapshotServiceAdapter.doImport(getImports());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an appropriate instance of the SnapshotServiceAdapter based on the FactoryBean configuration. If
|
||||
* a Region has not been specified, then a GemFire Snapshot Service for the Cache is constructed, otherwise
|
||||
* the GemFire Snapshot Service for the configured Region is used.
|
||||
*
|
||||
* @return a SnapshotServiceAdapter wrapping the appropriate GemFire Snapshot Service (either Cache or Region)
|
||||
* depending on the FactoryBean configuration.
|
||||
* @see #wrap(CacheSnapshotService)
|
||||
* @see #wrap(RegionSnapshotService)
|
||||
* @see #getRegion()
|
||||
*/
|
||||
protected SnapshotServiceAdapter create() {
|
||||
|
||||
return Optional.ofNullable(getRegion())
|
||||
.<SnapshotServiceAdapter>map(region -> wrap(region.getSnapshotService()))
|
||||
.orElseGet(() -> wrap(getCache().getSnapshotService()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps the GemFire CacheSnapshotService into an appropriate Adapter to uniformly access snapshot operations
|
||||
* on the Cache and Regions alike.
|
||||
*
|
||||
* @param cacheSnapshotService the GemFire CacheSnapshotService to wrap.
|
||||
* @return a SnapshotServiceAdapter wrapping the GemFire CacheSnapshotService.
|
||||
* @see SnapshotServiceFactoryBean.SnapshotServiceAdapter
|
||||
* @see SnapshotServiceFactoryBean.CacheSnapshotServiceAdapter
|
||||
* @see org.apache.geode.cache.snapshot.CacheSnapshotService
|
||||
*/
|
||||
protected SnapshotServiceAdapter<Object, Object> wrap(CacheSnapshotService cacheSnapshotService) {
|
||||
return new CacheSnapshotServiceAdapter(cacheSnapshotService);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps GemFire's RegionSnapshotService into an appropriate Adapter to uniformly access snapshot operations
|
||||
* on the Cache and Regions alike.
|
||||
*
|
||||
* @param regionSnapshotService the GemFire RegionSnapshotService to wrap.
|
||||
* @return a SnapshotServiceAdapter wrapping the GemFire RegionSnapshotService.
|
||||
* @see SnapshotServiceFactoryBean.SnapshotServiceAdapter
|
||||
* @see SnapshotServiceFactoryBean.RegionSnapshotServiceAdapter
|
||||
* @see org.apache.geode.cache.snapshot.RegionSnapshotService
|
||||
*/
|
||||
protected SnapshotServiceAdapter<K, V> wrap(RegionSnapshotService<K, V> regionSnapshotService) {
|
||||
return new RegionSnapshotServiceAdapter<K, V>(regionSnapshotService);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a reference to the GemFire Cache for which the snapshot will be taken.
|
||||
*
|
||||
@@ -109,8 +180,8 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
|
||||
* @see #getCache()
|
||||
*/
|
||||
public void setCache(Cache cache) {
|
||||
Assert.notNull(cache, "The GemFire Cache must not be null");
|
||||
this.cache = cache;
|
||||
this.cache = Optional.ofNullable(cache)
|
||||
.orElseThrow(() -> newIllegalArgumentException("The GemFire Cache must not be null"));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -122,8 +193,8 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
|
||||
* @see #setCache(Cache)
|
||||
*/
|
||||
protected Cache getCache() {
|
||||
Assert.state(cache != null, "The GemFire Cache was not properly initialized");
|
||||
return cache;
|
||||
return Optional.ofNullable(this.cache)
|
||||
.orElseThrow(() -> newIllegalStateException("The GemFire Cache was not properly initialized"));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -220,7 +291,7 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
|
||||
*/
|
||||
@Override
|
||||
public SnapshotServiceAdapter<K, V> getObject() throws Exception {
|
||||
return snapshotServiceAdapter;
|
||||
return this.snapshotServiceAdapter;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -232,8 +303,10 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
|
||||
* @see SnapshotServiceFactoryBean.RegionSnapshotServiceAdapter
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Class<?> getObjectType() {
|
||||
return (snapshotServiceAdapter != null ? snapshotServiceAdapter.getClass() : SnapshotServiceAdapter.class);
|
||||
return Optional.ofNullable(this.snapshotServiceAdapter).map(Object::getClass)
|
||||
.orElse((Class) SnapshotServiceAdapter.class);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -246,70 +319,6 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs and initializes the GemFire Snapshot Service used to take a snapshot of the configured Cache
|
||||
* or Region if initialized. In addition, this initialization method will perform the actual import.
|
||||
*
|
||||
* @throws Exception if the construction and initialization of the GemFire Snapshot Service fails.
|
||||
* @see org.springframework.data.gemfire.snapshot.SnapshotServiceFactoryBean.SnapshotServiceAdapter
|
||||
* @see #getSuppressImportOnInit()
|
||||
* @see #getImports()
|
||||
* @see #create()
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
snapshotServiceAdapter = create();
|
||||
|
||||
if (!getSuppressImportOnInit()) {
|
||||
snapshotServiceAdapter.doImport(getImports());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an appropriate instance of the SnapshotServiceAdapter based on the FactoryBean configuration. If
|
||||
* a Region has not been specified, then a GemFire Snapshot Service for the Cache is constructed, otherwise
|
||||
* the GemFire Snapshot Service for the configured Region is used.
|
||||
*
|
||||
* @return a SnapshotServiceAdapter wrapping the appropriate GemFire Snapshot Service (either Cache or Region)
|
||||
* depending on the FactoryBean configuration.
|
||||
* @see #wrap(CacheSnapshotService)
|
||||
* @see #wrap(RegionSnapshotService)
|
||||
* @see #getRegion()
|
||||
*/
|
||||
protected SnapshotServiceAdapter create() {
|
||||
Region<K, V> region = getRegion();
|
||||
return (region != null ? wrap(region.getSnapshotService()) : wrap(getCache().getSnapshotService()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps the GemFire CacheSnapshotService into an appropriate Adapter to uniformly access snapshot operations
|
||||
* on the Cache and Regions alike.
|
||||
*
|
||||
* @param cacheSnapshotService the GemFire CacheSnapshotService to wrap.
|
||||
* @return a SnapshotServiceAdapter wrapping the GemFire CacheSnapshotService.
|
||||
* @see SnapshotServiceFactoryBean.SnapshotServiceAdapter
|
||||
* @see SnapshotServiceFactoryBean.CacheSnapshotServiceAdapter
|
||||
* @see org.apache.geode.cache.snapshot.CacheSnapshotService
|
||||
*/
|
||||
protected SnapshotServiceAdapter<Object, Object> wrap(CacheSnapshotService cacheSnapshotService) {
|
||||
return new CacheSnapshotServiceAdapter(cacheSnapshotService);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps GemFire's RegionSnapshotService into an appropriate Adapter to uniformly access snapshot operations
|
||||
* on the Cache and Regions alike.
|
||||
*
|
||||
* @param regionSnapshotService the GemFire RegionSnapshotService to wrap.
|
||||
* @return a SnapshotServiceAdapter wrapping the GemFire RegionSnapshotService.
|
||||
* @see SnapshotServiceFactoryBean.SnapshotServiceAdapter
|
||||
* @see SnapshotServiceFactoryBean.RegionSnapshotServiceAdapter
|
||||
* @see org.apache.geode.cache.snapshot.RegionSnapshotService
|
||||
*/
|
||||
protected SnapshotServiceAdapter<K, V> wrap(RegionSnapshotService<K, V> regionSnapshotService) {
|
||||
return new RegionSnapshotServiceAdapter<K, V>(regionSnapshotService);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs an export of the GemFire Cache or Region if configured.
|
||||
*
|
||||
@@ -319,6 +328,7 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
|
||||
* @see #getObject()
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("all")
|
||||
public void destroy() throws Exception {
|
||||
getObject().doExport(getExports());
|
||||
}
|
||||
@@ -337,7 +347,9 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
|
||||
* @see #getObject()
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("all")
|
||||
public void onApplicationEvent(SnapshotApplicationEvent<K, V> event) {
|
||||
|
||||
try {
|
||||
if (isMatch(event)) {
|
||||
if (event instanceof ExportSnapshotApplicationEvent) {
|
||||
@@ -377,6 +389,7 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
|
||||
* @see #getImports()
|
||||
*/
|
||||
protected SnapshotMetadata<K, V>[] resolveSnapshotMetadata(SnapshotApplicationEvent<K, V> event) {
|
||||
|
||||
SnapshotMetadata<K, V>[] eventSnapshotMetadata = event.getSnapshotMetadata();
|
||||
|
||||
return (!ObjectUtils.isEmpty(eventSnapshotMetadata) ? eventSnapshotMetadata
|
||||
@@ -394,8 +407,10 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
|
||||
|
||||
SnapshotOptions<K, V> createOptions();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
void doExport(SnapshotMetadata<K, V>... configurations);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
void doImport(SnapshotMetadata<K, V>... configurations);
|
||||
|
||||
void load(File directory, SnapshotFormat format);
|
||||
@@ -436,33 +451,35 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void doExport(SnapshotMetadata<K, V>... configurations) {
|
||||
for (SnapshotMetadata<K, V> configuration : nullSafeArray(configurations)) {
|
||||
save(configuration.getLocation(), configuration.getFormat(), createOptions(configuration.getFilter()));
|
||||
}
|
||||
|
||||
stream(nullSafeArray(configurations)).forEach(configuration ->
|
||||
save(configuration.getLocation(), configuration.getFormat(), createOptions(configuration.getFilter())));
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void doImport(SnapshotMetadata<K, V>... configurations) {
|
||||
for (SnapshotMetadata<K, V> configuration : nullSafeArray(configurations)) {
|
||||
load(configuration.getFormat(), createOptions(configuration.getFilter()), handleLocation(configuration));
|
||||
}
|
||||
|
||||
stream(nullSafeArray(configurations)).forEach(configuration ->
|
||||
load(configuration.getFormat(), createOptions(configuration.getFilter()),
|
||||
handleLocation(configuration)));
|
||||
}
|
||||
|
||||
protected abstract File[] handleLocation(SnapshotMetadata<K, V> configuration);
|
||||
|
||||
protected File[] handleDirectoryLocation(File directory) {
|
||||
return directory.listFiles(new FileFilter() {
|
||||
@Override public boolean accept(File pathname) {
|
||||
return nullSafeIsFile(pathname);
|
||||
}
|
||||
});
|
||||
return directory.listFiles(pathname -> nullSafeIsFile(pathname));
|
||||
}
|
||||
|
||||
protected File[] handleFileLocation(File file) {
|
||||
|
||||
if (ArchiveFileFilter.INSTANCE.accept(file)) {
|
||||
try {
|
||||
File extractedArchiveDirectory = new File(TEMPORARY_DIRECTORY, file.getName().replaceAll("\\.", "-"));
|
||||
|
||||
File extractedArchiveDirectory =
|
||||
new File(TEMPORARY_DIRECTORY, file.getName().replaceAll("\\.", "-"));
|
||||
|
||||
Assert.state(extractedArchiveDirectory.isDirectory() || extractedArchiveDirectory.mkdirs(),
|
||||
String.format("Failed create directory (%1$s) in which to extract archive (%2$s)",
|
||||
@@ -474,6 +491,7 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
|
||||
|
||||
for (ZipEntry entry : CollectionUtils.iterable(zipFile.entries())) {
|
||||
if (!entry.isDirectory()) {
|
||||
|
||||
DataInputStream entryInputStream = new DataInputStream(zipFile.getInputStream(entry));
|
||||
|
||||
DataOutputStream entryOutputStream = new DataOutputStream(new FileOutputStream(
|
||||
@@ -491,9 +509,9 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
|
||||
|
||||
return handleDirectoryLocation(extractedArchiveDirectory);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
throw new ImportSnapshotException(String.format(
|
||||
"Failed to extract archive (%1$s) to import", file), t);
|
||||
catch (Throwable cause) {
|
||||
throw new ImportSnapshotException(
|
||||
String.format("Failed to extract archive [%1$s] to import", file), cause);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -501,12 +519,13 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
|
||||
}
|
||||
|
||||
protected boolean exceptionSuppressingClose(Closeable closeable) {
|
||||
|
||||
try {
|
||||
closeable.close();
|
||||
return true;
|
||||
}
|
||||
catch (IOException ignore) {
|
||||
logDebug(ignore, "Failed to close (%1$s)", closeable);
|
||||
logDebug(ignore, "Failed to close [%s]", closeable);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -559,7 +578,7 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
|
||||
}
|
||||
|
||||
protected CacheSnapshotService getSnapshotService() {
|
||||
return snapshotService;
|
||||
return this.snapshotService;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -569,55 +588,61 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
|
||||
|
||||
@Override
|
||||
protected File[] handleLocation(SnapshotMetadata<Object, Object> configuration) {
|
||||
return (configuration.isFile() ? handleFileLocation(configuration.getLocation())
|
||||
|
||||
return (configuration.isFile()
|
||||
? handleFileLocation(configuration.getLocation())
|
||||
: handleDirectoryLocation(configuration.getLocation()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(File directory, SnapshotFormat format) {
|
||||
|
||||
try {
|
||||
getSnapshotService().load(directory, format);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
catch (Throwable cause) {
|
||||
throw new ImportSnapshotException(String.format(
|
||||
"Failed to load snapshots from directory (%1$s) in format (%2$s)",
|
||||
directory, format), t);
|
||||
"Failed to load snapshots from directory [%1$s] in format [%2$s]",
|
||||
directory, format), cause);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(SnapshotFormat format, SnapshotOptions<Object, Object> options, File... snapshots) {
|
||||
|
||||
try {
|
||||
getSnapshotService().load(snapshots, format, options);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
catch (Throwable cause) {
|
||||
throw new ImportSnapshotException(String.format(
|
||||
"Failed to load snapshots (%1$s) in format (%2$s) using options (%3$s)",
|
||||
Arrays.toString(snapshots), format, options), t);
|
||||
"Failed to load snapshots [%1$s] in format [%2$s] using options [%3$s]",
|
||||
Arrays.toString(snapshots), format, options), cause);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(File directory, SnapshotFormat format) {
|
||||
|
||||
try {
|
||||
getSnapshotService().save(directory, format);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
catch (Throwable cause) {
|
||||
throw new ExportSnapshotException(String.format(
|
||||
"Failed to save snapshots to directory (%1$s) in format (%2$s)",
|
||||
directory, format), t);
|
||||
"Failed to save snapshots to directory [%1$s] in format [%2$s]",
|
||||
directory, format), cause);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(File directory, SnapshotFormat format, SnapshotOptions<Object, Object> options) {
|
||||
|
||||
try {
|
||||
getSnapshotService().save(directory, format, options);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
catch (Throwable cause) {
|
||||
throw new ExportSnapshotException(String.format(
|
||||
"Failed to save snapshots to directory (%1$s) in format (%2$s) using options (%3$s)",
|
||||
directory, format, options), t);
|
||||
"Failed to save snapshots to directory [%1$s] in format [%2$s] using options [%3$s]",
|
||||
directory, format, options), cause);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -637,7 +662,7 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
|
||||
}
|
||||
|
||||
protected RegionSnapshotService<K, V> getSnapshotService() {
|
||||
return snapshotService;
|
||||
return this.snapshotService;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -652,51 +677,55 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
|
||||
|
||||
@Override
|
||||
public void load(File snapshot, SnapshotFormat format) {
|
||||
|
||||
try {
|
||||
getSnapshotService().load(snapshot, format);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
catch (Throwable cause) {
|
||||
throw new ImportSnapshotException(String.format(
|
||||
"Failed to load snapshot from file (%1$s) in format (%2$s)",
|
||||
snapshot, format), t);
|
||||
"Failed to load snapshot from file [%1$s] in format [%2$s]",
|
||||
snapshot, format), cause);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(SnapshotFormat format, SnapshotOptions<K, V> options, File... snapshots) {
|
||||
|
||||
try {
|
||||
for (File snapshot : snapshots) {
|
||||
getSnapshotService().load(snapshot, format, options);
|
||||
}
|
||||
}
|
||||
catch (Throwable t) {
|
||||
catch (Throwable cause) {
|
||||
throw new ImportSnapshotException(String.format(
|
||||
"Failed to load snapshots (%1$s) in format (%2$s) using options (%3$s)",
|
||||
Arrays.toString(snapshots), format, options), t);
|
||||
"Failed to load snapshots [%1$s] in format [%2$s] using options [%3$s]",
|
||||
Arrays.toString(snapshots), format, options), cause);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(File snapshot, SnapshotFormat format) {
|
||||
|
||||
try {
|
||||
getSnapshotService().save(snapshot, format);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
catch (Throwable cause) {
|
||||
throw new ExportSnapshotException(String.format(
|
||||
"Failed to save snapshot to file (%1$s) in format (%2$s)",
|
||||
snapshot, format), t);
|
||||
"Failed to save snapshot to file [%1$s] in format [%2$s]",
|
||||
snapshot, format), cause);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(File snapshot, SnapshotFormat format, SnapshotOptions<K, V> options) {
|
||||
|
||||
try {
|
||||
getSnapshotService().save(snapshot, format, options);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
catch (Throwable cause) {
|
||||
throw new ExportSnapshotException(String.format(
|
||||
"Failed to save snapshot to file (%1$s) in format (%2$s) using options (%3$s)",
|
||||
snapshot, format, options), t);
|
||||
"Failed to save snapshot to file [%1$s] in format [%2$s] using options [%3$s]",
|
||||
snapshot, format, options), cause);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -795,5 +824,4 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
|
||||
return ACCEPTED_FILE_EXTENSIONS.contains(getFileExtension(pathname));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,10 +16,12 @@
|
||||
|
||||
package org.springframework.data.gemfire.snapshot;
|
||||
|
||||
import static java.util.Arrays.stream;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.springframework.data.gemfire.util.ArrayUtils.nullSafeArray;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
@@ -72,7 +74,7 @@ import org.springframework.util.StringUtils;
|
||||
@SuppressWarnings("unused")
|
||||
public class SnapshotApplicationEventTriggeredImportsExportsIntegrationTest {
|
||||
|
||||
protected static final AtomicLong ID_SEQUENCE = new AtomicLong(0l);
|
||||
protected static final AtomicLong ID_SEQUENCE = new AtomicLong(0L);
|
||||
|
||||
protected static File snapshotsDirectory;
|
||||
|
||||
@@ -93,12 +95,13 @@ public class SnapshotApplicationEventTriggeredImportsExportsIntegrationTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void setupBeforeClass() throws Exception {
|
||||
|
||||
snapshotsDirectory = new File(new File(FileSystemUtils.WORKING_DIRECTORY, "gemfire"), "snapshots");
|
||||
|
||||
assertThat(snapshotsDirectory.isDirectory() || snapshotsDirectory.mkdirs(), is(true));
|
||||
|
||||
File peopleSnapshotFile = new File(snapshotsDirectory, "people.snapshot");
|
||||
File nonHandyNonDoeSnapshotFile = new File(snapshotsDirectory, "nonHandyNonDoePeople.snapshot");
|
||||
File peopleSnapshotFile = new File(snapshotsDirectory, "people-snapshot.gfd");
|
||||
File nonHandyNonDoeSnapshotFile = new File(snapshotsDirectory, "nonHandyNonDoePeople-snapshot.gfd");
|
||||
|
||||
assertThat(peopleSnapshotFile.isFile() || peopleSnapshotFile.createNewFile(), is(true));
|
||||
assertThat(nonHandyNonDoeSnapshotFile.isFile() || nonHandyNonDoeSnapshotFile.createNewFile(), is(true));
|
||||
@@ -106,83 +109,88 @@ public class SnapshotApplicationEventTriggeredImportsExportsIntegrationTest {
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownAfterClass() {
|
||||
FileSystemUtils.deleteRecursive(snapshotsDirectory.getParentFile());
|
||||
//FileSystemUtils.deleteRecursive(snapshotsDirectory.getParentFile());
|
||||
}
|
||||
|
||||
protected void assertPeople(Region<Long, Person> targetRegion, Person... people) {
|
||||
|
||||
assertThat(targetRegion.size(), is(equalTo(people.length)));
|
||||
|
||||
for (Person person : people) {
|
||||
assertPerson(person, targetRegion.get(person.getId()));
|
||||
}
|
||||
stream(nullSafeArray(people, Person.class))
|
||||
.forEach(person -> assertPerson(person, targetRegion.get(person.getId())));
|
||||
}
|
||||
|
||||
protected void assertPerson(Person expectedPerson, Person actualPerson) {
|
||||
assertThat(String.format("Expected (%1$s); but was (%2$s)", expectedPerson, actualPerson),
|
||||
|
||||
assertThat(String.format("Expected [%1$s]; but was [%2$s]", expectedPerson, actualPerson),
|
||||
actualPerson, is(notNullValue()));
|
||||
assertThat(actualPerson.getId(), is(equalTo(expectedPerson.getId())));
|
||||
assertThat(actualPerson.getFirstname(), is(equalTo(expectedPerson.getFirstname())));
|
||||
assertThat(actualPerson.getLastname(), is(equalTo(expectedPerson.getLastname())));
|
||||
}
|
||||
|
||||
protected Person createPerson(String firstName, String lastName) {
|
||||
protected Person newPerson(String firstName, String lastName) {
|
||||
return new Person(ID_SEQUENCE.incrementAndGet(), firstName, lastName);
|
||||
}
|
||||
|
||||
protected Person put(Region<Long, Person> targetRegion, Person person) {
|
||||
|
||||
targetRegion.putIfAbsent(person.getId(), person);
|
||||
|
||||
return person;
|
||||
}
|
||||
|
||||
protected void wait(final int seconds, final int expectedDoeSize, final int expectedEveryoneSize,
|
||||
final int expectedHandySize) {
|
||||
ThreadUtils.timedWait(TimeUnit.SECONDS.toMillis(seconds), 500, new ThreadUtils.WaitCondition() {
|
||||
@Override public boolean waiting() {
|
||||
return (doe.size() < expectedDoeSize && everyoneElse.size() < expectedEveryoneSize
|
||||
&& handy.size() < expectedHandySize);
|
||||
}
|
||||
});
|
||||
protected void wait(int seconds, int expectedDoeSize, int expectedEveryoneSize, int expectedHandySize) {
|
||||
|
||||
ThreadUtils.timedWait(TimeUnit.SECONDS.toMillis(seconds), 500,
|
||||
() -> (doe.size() < expectedDoeSize && everyoneElse.size() <
|
||||
expectedEveryoneSize && handy.size() < expectedHandySize));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void exportsTriggeringImportsOnSnapshotApplicationEvents() {
|
||||
Person jonDoe = put(people, createPerson("Jon", "Doe"));
|
||||
Person janeDoe = put(people, createPerson("Jane", "Doe"));
|
||||
Person jackBlack = put(people, createPerson("Jack", "Black"));
|
||||
Person jackHandy = put(people, createPerson("Jack", "Handy"));
|
||||
Person joeDirt = put(people, createPerson("Joe", "Dirt"));
|
||||
|
||||
SnapshotApplicationEvent event = new ExportSnapshotApplicationEvent<Long, Person>(this, people.getFullPath());
|
||||
Person jonDoe = put(people, newPerson("Jon", "Doe"));
|
||||
Person janeDoe = put(people, newPerson("Jane", "Doe"));
|
||||
Person jackBlack = put(people, newPerson("Jack", "Black"));
|
||||
Person jackHandy = put(people, newPerson("Jack", "Handy"));
|
||||
Person joeDirt = put(people, newPerson("Joe", "Dirt"));
|
||||
|
||||
SnapshotApplicationEvent event =
|
||||
new ExportSnapshotApplicationEvent<Long, Person>(this, people.getFullPath());
|
||||
|
||||
eventPublisher.publishEvent(event);
|
||||
|
||||
wait(5, 2, 2, 1);
|
||||
|
||||
assertPeople(doe, jonDoe, janeDoe);
|
||||
assertPeople(everyoneElse, jackBlack, joeDirt);
|
||||
assertPeople(handy, jackHandy);
|
||||
|
||||
Person cookieDoe = put(people, createPerson("Cookie", "Doe"));
|
||||
Person pieDoe = put(people, createPerson("Pie", "Doe"));
|
||||
Person sourDoe = put(people, createPerson("Sour", "Doe"));
|
||||
Person randyHandy = put(people, createPerson("Randy", "Handy"));
|
||||
Person sandyHandy = put(people, createPerson("Sandy", "Handy"));
|
||||
Person jackHill = put(people, createPerson("Jack", "Hill"));
|
||||
Person jillHill = put(people, createPerson("Jill", "Hill"));
|
||||
Person cookieDoe = put(people, newPerson("Cookie", "Doe"));
|
||||
Person pieDoe = put(people, newPerson("Pie", "Doe"));
|
||||
Person sourDoe = put(people, newPerson("Sour", "Doe"));
|
||||
Person randyHandy = put(people, newPerson("Randy", "Handy"));
|
||||
Person sandyHandy = put(people, newPerson("Sandy", "Handy"));
|
||||
Person jackHill = put(people, newPerson("Jack", "Hill"));
|
||||
Person jillHill = put(people, newPerson("Jill", "Hill"));
|
||||
|
||||
eventPublisher.publishEvent(event);
|
||||
|
||||
wait(10, 5, 4, 3);
|
||||
|
||||
assertPeople(doe, jonDoe, janeDoe, cookieDoe, pieDoe, sourDoe);
|
||||
assertPeople(everyoneElse, jackBlack, joeDirt, jackHill, jillHill);
|
||||
assertPeople(handy, jackHandy, randyHandy, sandyHandy);
|
||||
|
||||
Person bobDoe = put(people, createPerson("Bob", "Doe"));
|
||||
Person mandyHandy = put(people, createPerson("Mandy", "Handy"));
|
||||
Person imaPigg = put(people, createPerson("Ima", "Pigg"));
|
||||
Person benDover = put(people, createPerson("Ben", "Dover"));
|
||||
Person bobDoe = put(people, newPerson("Bob", "Doe"));
|
||||
Person mandyHandy = put(people, newPerson("Mandy", "Handy"));
|
||||
Person imaPigg = put(people, newPerson("Ima", "Pigg"));
|
||||
Person benDover = put(people, newPerson("Ben", "Dover"));
|
||||
|
||||
eventPublisher.publishEvent(event);
|
||||
|
||||
wait(15, 6, 6, 4);
|
||||
|
||||
assertPeople(doe, jonDoe, janeDoe, cookieDoe, pieDoe, sourDoe, bobDoe);
|
||||
@@ -236,6 +244,7 @@ public class SnapshotApplicationEventTriggeredImportsExportsIntegrationTest {
|
||||
@Scheduled(fixedDelay = 1000)
|
||||
@SuppressWarnings("unchecked")
|
||||
public void processSnapshots() {
|
||||
|
||||
boolean triggerEvent = false;
|
||||
|
||||
for (File snapshotFile : nullSafeArray(snapshotsDirectory.listFiles(FileSystemUtils.FileOnlyFilter.INSTANCE))) {
|
||||
@@ -252,6 +261,7 @@ public class SnapshotApplicationEventTriggeredImportsExportsIntegrationTest {
|
||||
}
|
||||
|
||||
protected boolean isUnprocessedSnapshotFile(File snapshotFile) {
|
||||
|
||||
Long lastModified = snapshotFile.lastModified();
|
||||
Long previousLastModified = snapshotFileLastModifiedMap.get(snapshotFile);
|
||||
|
||||
@@ -262,5 +272,4 @@ public class SnapshotApplicationEventTriggeredImportsExportsIntegrationTest {
|
||||
return !previousLastModified.equals(lastModified);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -86,15 +86,18 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
private static File snapshotDat;
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedException = ExpectedException.none();
|
||||
public ExpectedException exception = ExpectedException.none();
|
||||
|
||||
private SnapshotServiceFactoryBean factoryBean = new SnapshotServiceFactoryBean();
|
||||
|
||||
protected static File mockFile(String filename) {
|
||||
|
||||
File mockFile = mock(File.class, filename);
|
||||
|
||||
when(mockFile.isFile()).thenReturn(true);
|
||||
when(mockFile.getAbsolutePath()).thenReturn(String.format("/path/to/%1$s", filename));
|
||||
when(mockFile.getName()).thenReturn(filename);
|
||||
|
||||
return mockFile;
|
||||
}
|
||||
|
||||
@@ -116,6 +119,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
protected <K, V> SnapshotMetadata<K, V> newSnapshotMetadata(File location, SnapshotFilter<K, V> filter,
|
||||
SnapshotFormat format) {
|
||||
|
||||
return new SnapshotMetadata<K, V>(location, filter, format);
|
||||
}
|
||||
|
||||
@@ -124,6 +128,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
}
|
||||
|
||||
protected String toPathname(String... pathElements) {
|
||||
|
||||
StringBuilder pathname = new StringBuilder();
|
||||
|
||||
for (String pathElement : pathElements) {
|
||||
@@ -147,6 +152,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void nullSafeArrayWithNonNullArray() {
|
||||
|
||||
SnapshotMetadata[] expectedConfigurations = new SnapshotMetadata[0];
|
||||
|
||||
assertThat(SnapshotServiceFactoryBean.nullSafeArray(expectedConfigurations),
|
||||
@@ -165,8 +171,10 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void nullSafeIsDirectoryWithNonDirectories() {
|
||||
|
||||
assertThat(SnapshotServiceFactoryBean.nullSafeIsDirectory(new File("path/to/non-existing/directory")),
|
||||
is(false));
|
||||
|
||||
assertThat(SnapshotServiceFactoryBean.nullSafeIsDirectory(FileSystemUtils.JAVA_EXE), is(false));
|
||||
}
|
||||
|
||||
@@ -184,22 +192,27 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void setCacheToNull() {
|
||||
expectedException.expect(IllegalArgumentException.class);
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
expectedException.expectMessage("The GemFire Cache must not be null");
|
||||
|
||||
exception.expect(IllegalArgumentException.class);
|
||||
exception.expectCause(is(nullValue(Throwable.class)));
|
||||
exception.expectMessage("The GemFire Cache must not be null");
|
||||
|
||||
factoryBean.setCache(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCacheWhenUninitialized() {
|
||||
expectedException.expect(IllegalStateException.class);
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
expectedException.expectMessage("The GemFire Cache was not properly initialized");
|
||||
|
||||
exception.expect(IllegalStateException.class);
|
||||
exception.expectCause(is(nullValue(Throwable.class)));
|
||||
exception.expectMessage("The GemFire Cache was not properly initialized");
|
||||
|
||||
factoryBean.getCache();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setAndGetCacheSuccessfully() {
|
||||
|
||||
Cache mockCache = mock(Cache.class, "MockCache");
|
||||
SnapshotServiceFactoryBean factoryBean = new SnapshotServiceFactoryBean();
|
||||
|
||||
@@ -210,6 +223,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void setAndGetExports() {
|
||||
|
||||
SnapshotMetadata[] actualExports = factoryBean.getExports();
|
||||
|
||||
assertThat(actualExports, is(notNullValue()));
|
||||
@@ -232,6 +246,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void setAndGetImports() {
|
||||
|
||||
SnapshotMetadata[] actualImports = factoryBean.getImports();
|
||||
|
||||
assertThat(actualImports, is(notNullValue()));
|
||||
@@ -254,6 +269,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void setAndGetRegionSuccessfully() {
|
||||
|
||||
assertThat(factoryBean.getRegion(), is(nullValue()));
|
||||
|
||||
Region mockRegion = mock(Region.class, "MockRegion");
|
||||
@@ -269,6 +285,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void setAndGetSuppressImportOnInitSuccessfully() {
|
||||
|
||||
assertThat(factoryBean.getSuppressImportOnInit(), is(false));
|
||||
|
||||
factoryBean.setSuppressImportOnInit(true);
|
||||
@@ -291,9 +308,10 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void afterPropertiesSetCreatesSnapshotServiceAdapterAndDoesImportWithConfiguredImports() throws Exception {
|
||||
|
||||
SnapshotMetadata expectedSnapshotMetadata = newSnapshotMetadata();
|
||||
|
||||
final SnapshotServiceAdapter mockSnapshotService = mock(SnapshotServiceAdapter.class,
|
||||
SnapshotServiceAdapter mockSnapshotService = mock(SnapshotServiceAdapter.class,
|
||||
"MockSnapshotServiceAdapter");
|
||||
|
||||
SnapshotServiceFactoryBean factoryBean = new SnapshotServiceFactoryBean() {
|
||||
@@ -312,7 +330,9 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void afterPropertiesSetCreatesSnapshotServiceAdapterButSuppressesImportOnInit() throws Exception {
|
||||
final SnapshotServiceAdapter mockSnapshotService = mock(SnapshotServiceAdapter.class, "MockSnapshotServiceAdapter");
|
||||
|
||||
SnapshotServiceAdapter mockSnapshotService = mock(SnapshotServiceAdapter.class,
|
||||
"MockSnapshotServiceAdapter");
|
||||
|
||||
SnapshotServiceFactoryBean factoryBean = new SnapshotServiceFactoryBean() {
|
||||
@Override protected SnapshotServiceAdapter create() {
|
||||
@@ -330,6 +350,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void createCacheSnapshotService() {
|
||||
|
||||
Cache mockCache = mock(Cache.class, "MockCache");
|
||||
CacheSnapshotService mockCacheSnapshotService = mock(CacheSnapshotService.class, "MockCacheSnapshotService");
|
||||
|
||||
@@ -348,6 +369,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void createRegionSnapshotService() {
|
||||
|
||||
Region mockRegion = mock(Region.class, "MockRegion");
|
||||
RegionSnapshotService mockRegionSnapshotService = mock(RegionSnapshotService.class, "MockRegionSnapshotService");
|
||||
|
||||
@@ -366,25 +388,30 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void wrapNullCacheSnapshotService() {
|
||||
expectedException.expect(IllegalArgumentException.class);
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
expectedException.expectMessage("The backing CacheSnapshotService must not be null");
|
||||
|
||||
exception.expect(IllegalArgumentException.class);
|
||||
exception.expectCause(is(nullValue(Throwable.class)));
|
||||
exception.expectMessage("The backing CacheSnapshotService must not be null");
|
||||
|
||||
factoryBean.wrap((CacheSnapshotService) null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wrapNullRegionSnapshotService() {
|
||||
expectedException.expect(IllegalArgumentException.class);
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
expectedException.expectMessage("The backing RegionSnapshotService must not be null");
|
||||
|
||||
exception.expect(IllegalArgumentException.class);
|
||||
exception.expectCause(is(nullValue(Throwable.class)));
|
||||
exception.expectMessage("The backing RegionSnapshotService must not be null");
|
||||
|
||||
factoryBean.wrap((RegionSnapshotService) null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void destroyPerformsExportWithConfiguredExports() throws Exception {
|
||||
|
||||
SnapshotMetadata expectedSnapshotMetadata = newSnapshotMetadata();
|
||||
|
||||
final SnapshotServiceAdapter mockSnapshotService = mock(SnapshotServiceAdapter.class,
|
||||
SnapshotServiceAdapter mockSnapshotService = mock(SnapshotServiceAdapter.class,
|
||||
"MockSnapshotServiceAdapter");
|
||||
|
||||
SnapshotServiceFactoryBean factoryBean = new SnapshotServiceFactoryBean() {
|
||||
@@ -403,6 +430,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void onApplicationEventWhenMatchUsingEventSnapshotMetadataPerformsExport() throws Exception {
|
||||
|
||||
Region mockRegion = mock(Region.class, "MockRegion");
|
||||
|
||||
SnapshotApplicationEvent mockSnapshotEvent = mock(ExportSnapshotApplicationEvent.class,
|
||||
@@ -410,7 +438,8 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
SnapshotMetadata eventSnapshotMetadata = newSnapshotMetadata(snapshotDat);
|
||||
|
||||
final SnapshotServiceAdapter mockSnapshotService = mock(SnapshotServiceAdapter.class, "MockSnapshotServiceAdapter");
|
||||
SnapshotServiceAdapter mockSnapshotService =
|
||||
mock(SnapshotServiceAdapter.class, "MockSnapshotServiceAdapter");
|
||||
|
||||
when(mockSnapshotEvent.isCacheSnapshotEvent()).thenReturn(false);
|
||||
when(mockSnapshotEvent.matches(eq(mockRegion))).thenReturn(true);
|
||||
@@ -438,12 +467,14 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void onApplicationEventWhenMatchUsingFactorySnapshotMetadataPerformsImport() throws Exception {
|
||||
|
||||
SnapshotApplicationEvent mockSnapshotEvent = mock(ImportSnapshotApplicationEvent.class,
|
||||
"MockImportSnapshotApplicationEvent");
|
||||
|
||||
SnapshotMetadata factorySnapshotMetadata = newSnapshotMetadata(snapshotDat);
|
||||
|
||||
final SnapshotServiceAdapter mockSnapshotService = mock(SnapshotServiceAdapter.class, "MockSnapshotServiceAdapter");
|
||||
SnapshotServiceAdapter mockSnapshotService =
|
||||
mock(SnapshotServiceAdapter.class, "MockSnapshotServiceAdapter");
|
||||
|
||||
when(mockSnapshotEvent.isCacheSnapshotEvent()).thenReturn(true);
|
||||
when(mockSnapshotEvent.matches(any(Region.class))).thenReturn(false);
|
||||
@@ -470,6 +501,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void onApplicationEventWhenNoMatchDoesNotPerformExport() throws Exception {
|
||||
|
||||
SnapshotApplicationEvent mockSnapshotEvent = mock(ExportSnapshotApplicationEvent.class,
|
||||
"MockExportSnapshotApplicationEvent");
|
||||
|
||||
@@ -477,7 +509,8 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
when(mockSnapshotEvent.matches(any(Region.class))).thenReturn(false);
|
||||
when(mockSnapshotEvent.getSnapshotMetadata()).thenReturn(null);
|
||||
|
||||
final SnapshotServiceAdapter mockSnapshotService = mock(SnapshotServiceAdapter.class, "MockSnapshotServiceAdapter");
|
||||
SnapshotServiceAdapter mockSnapshotService =
|
||||
mock(SnapshotServiceAdapter.class, "MockSnapshotServiceAdapter");
|
||||
|
||||
SnapshotServiceFactoryBean factoryBean = new SnapshotServiceFactoryBean() {
|
||||
@Override public SnapshotServiceAdapter getObject() throws Exception {
|
||||
@@ -500,12 +533,14 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void onApplicationEventWhenNoMatchDoesNotPerformImport() throws Exception {
|
||||
|
||||
Region mockRegion = mock(Region.class, "MockRegion");
|
||||
|
||||
SnapshotApplicationEvent mockSnapshotEvent = mock(ImportSnapshotApplicationEvent.class,
|
||||
"MockImportSnapshotApplicationEvent");
|
||||
|
||||
final SnapshotServiceAdapter mockSnapshotService = mock(SnapshotServiceAdapter.class, "MockSnapshotServiceAdapter");
|
||||
SnapshotServiceAdapter mockSnapshotService =
|
||||
mock(SnapshotServiceAdapter.class, "MockSnapshotServiceAdapter");
|
||||
|
||||
when(mockSnapshotEvent.isCacheSnapshotEvent()).thenReturn(false);
|
||||
when(mockSnapshotEvent.matches(any(Region.class))).thenReturn(false);
|
||||
@@ -533,12 +568,13 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void resolveSnapshotMetadataFromEvent() {
|
||||
|
||||
SnapshotMetadata eventSnapshotMetadata = newSnapshotMetadata(snapshotDat);
|
||||
SnapshotMetadata factoryExportSnapshotMetadata = newSnapshotMetadata();
|
||||
SnapshotMetadata factoryImportSnapshotMetadata = newSnapshotMetadata(FileSystemUtils.USER_HOME);
|
||||
|
||||
SnapshotApplicationEvent mockSnapshotEvent = mock(SnapshotApplicationEvent.class,
|
||||
"MockSnapshotApplicationEvent");
|
||||
SnapshotApplicationEvent mockSnapshotEvent =
|
||||
mock(SnapshotApplicationEvent.class, "MockSnapshotApplicationEvent");
|
||||
|
||||
when(mockSnapshotEvent.getSnapshotMetadata()).thenReturn(toArray(eventSnapshotMetadata));
|
||||
|
||||
@@ -554,11 +590,12 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void resolveExportSnapshotMetadataFromFactory() {
|
||||
|
||||
SnapshotMetadata factoryExportSnapshotMetadata = newSnapshotMetadata();
|
||||
SnapshotMetadata factoryImportSnapshotMetadata = newSnapshotMetadata(FileSystemUtils.USER_HOME);
|
||||
|
||||
SnapshotApplicationEvent mockSnapshotEvent = mock(ExportSnapshotApplicationEvent.class,
|
||||
"MockExportSnapshotApplicationEvent");
|
||||
SnapshotApplicationEvent mockSnapshotEvent =
|
||||
mock(ExportSnapshotApplicationEvent.class,"MockExportSnapshotApplicationEvent");
|
||||
|
||||
when(mockSnapshotEvent.getSnapshotMetadata()).thenReturn(null);
|
||||
|
||||
@@ -574,11 +611,12 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void resolveImportSnapshotMetadataFromFactory() {
|
||||
|
||||
SnapshotMetadata factoryExportSnapshotMetadata = newSnapshotMetadata();
|
||||
SnapshotMetadata factoryImportSnapshotMetadata = newSnapshotMetadata(FileSystemUtils.USER_HOME);
|
||||
|
||||
SnapshotApplicationEvent mockSnapshotEvent = mock(ImportSnapshotApplicationEvent.class,
|
||||
"MockImportSnapshotApplicationEvent");
|
||||
SnapshotApplicationEvent mockSnapshotEvent =
|
||||
mock(ImportSnapshotApplicationEvent.class, "MockImportSnapshotApplicationEvent");
|
||||
|
||||
when(mockSnapshotEvent.getSnapshotMetadata()).thenReturn(toArray());
|
||||
|
||||
@@ -594,7 +632,9 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void withCacheBasedSnapshotServiceOnCacheSnapshotEventIsMatch() {
|
||||
SnapshotApplicationEvent mockSnapshotEvent = mock(SnapshotApplicationEvent.class, "MockSnapshotApplicationEvent");
|
||||
|
||||
SnapshotApplicationEvent mockSnapshotEvent =
|
||||
mock(SnapshotApplicationEvent.class, "MockSnapshotApplicationEvent");
|
||||
|
||||
when(mockSnapshotEvent.isCacheSnapshotEvent()).thenReturn(true);
|
||||
|
||||
@@ -607,7 +647,9 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void withCacheBasedSnapshotServiceOnRegionSnapshotEventIsNotAMatch() {
|
||||
SnapshotApplicationEvent mockSnapshotEvent = mock(SnapshotApplicationEvent.class, "MockSnapshotApplicationEvent");
|
||||
|
||||
SnapshotApplicationEvent mockSnapshotEvent =
|
||||
mock(SnapshotApplicationEvent.class, "MockSnapshotApplicationEvent");
|
||||
|
||||
when(mockSnapshotEvent.isCacheSnapshotEvent()).thenReturn(false);
|
||||
when(mockSnapshotEvent.matches(any(Region.class))).thenReturn(false);
|
||||
@@ -621,7 +663,9 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void withRegionBasedSnapshotServiceOnCacheSnapshotEventIsMatch() {
|
||||
SnapshotApplicationEvent mockSnapshotEvent = mock(SnapshotApplicationEvent.class, "MockSnapshotApplicationEvent");
|
||||
|
||||
SnapshotApplicationEvent mockSnapshotEvent =
|
||||
mock(SnapshotApplicationEvent.class, "MockSnapshotApplicationEvent");
|
||||
|
||||
when(mockSnapshotEvent.isCacheSnapshotEvent()).thenReturn(true);
|
||||
|
||||
@@ -636,9 +680,11 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void withRegionBasedSnapshotServiceOnRegionSnapshotEventIsMatch() {
|
||||
|
||||
Region mockRegion = mock(Region.class, "MockRegion");
|
||||
|
||||
SnapshotApplicationEvent mockSnapshotEvent = mock(SnapshotApplicationEvent.class, "MockSnapshotApplicationEvent");
|
||||
SnapshotApplicationEvent mockSnapshotEvent =
|
||||
mock(SnapshotApplicationEvent.class, "MockSnapshotApplicationEvent");
|
||||
|
||||
when(mockSnapshotEvent.isCacheSnapshotEvent()).thenReturn(false);
|
||||
when(mockSnapshotEvent.matches(eq(mockRegion))).thenReturn(true);
|
||||
@@ -654,9 +700,11 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void importCacheSnapshotOnInitialization() throws Exception {
|
||||
|
||||
Cache mockCache = mock(Cache.class, "MockCache");
|
||||
|
||||
CacheSnapshotService mockCacheSnapshotService = mock(CacheSnapshotService.class, "MockCacheSnapshotService");
|
||||
CacheSnapshotService mockCacheSnapshotService =
|
||||
mock(CacheSnapshotService.class, "MockCacheSnapshotService");
|
||||
|
||||
SnapshotFilter mockSnapshotFilterOne = mock(SnapshotFilter.class, "MockSnapshotFilterOne");
|
||||
SnapshotFilter mockSnapshotFilterTwo = mock(SnapshotFilter.class, "MockSnapshotFilterTwo");
|
||||
@@ -704,11 +752,13 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void importRegionSnapshotOnInitialization() throws Exception {
|
||||
|
||||
Cache mockCache = mock(Cache.class, "MockCache");
|
||||
|
||||
Region mockRegion = mock(Region.class, "MockRegion");
|
||||
|
||||
RegionSnapshotService mockRegionSnapshotService = mock(RegionSnapshotService.class, "MockRegionSnapshotService");
|
||||
RegionSnapshotService mockRegionSnapshotService =
|
||||
mock(RegionSnapshotService.class, "MockRegionSnapshotService");
|
||||
|
||||
SnapshotFilter mockSnapshotFilterOne = mock(SnapshotFilter.class, "MockSnapshotFilterOne");
|
||||
SnapshotFilter mockSnapshotFilterTwo = mock(SnapshotFilter.class, "MockSnapshotFilterTwo");
|
||||
@@ -757,9 +807,11 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void exportCacheSnapshotOnDestroy() throws Exception {
|
||||
|
||||
Cache mockCache = mock(Cache.class, "MockCache");
|
||||
|
||||
CacheSnapshotService mockCacheSnapshotService = mock(CacheSnapshotService.class, "MockCacheSnapshotService");
|
||||
CacheSnapshotService mockCacheSnapshotService =
|
||||
mock(CacheSnapshotService.class, "MockCacheSnapshotService");
|
||||
|
||||
SnapshotFilter mockSnapshotFilterOne = mock(SnapshotFilter.class, "MockSnapshotFilterOne");
|
||||
SnapshotFilter mockSnapshotFilterTwo = mock(SnapshotFilter.class, "MockSnapshotFilterTwo");
|
||||
@@ -799,11 +851,13 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void exportRegionSnapshotOnDestroy() throws Exception {
|
||||
|
||||
Cache mockCache = mock(Cache.class, "MockCache");
|
||||
|
||||
Region mockRegion = mock(Region.class, "MockRegion");
|
||||
|
||||
RegionSnapshotService mockRegionSnapshotService = mock(RegionSnapshotService.class, "MockRegionSnapshotService");
|
||||
RegionSnapshotService mockRegionSnapshotService =
|
||||
mock(RegionSnapshotService.class, "MockRegionSnapshotService");
|
||||
|
||||
SnapshotFilter mockSnapshotFilterOne = mock(SnapshotFilter.class, "MockSnapshotFilterOne");
|
||||
SnapshotFilter mockSnapshotFilterTwo = mock(SnapshotFilter.class, "MockSnapshotFilterTwo");
|
||||
@@ -845,9 +899,10 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void createOptionsWithFilterOnSnapshotServiceAdapterSupport() {
|
||||
|
||||
SnapshotFilter mockSnapshotFilter = mock(SnapshotFilter.class, "MockSnapshotFilter");
|
||||
|
||||
final SnapshotOptions mockSnapshotOptions = mock(SnapshotOptions.class, "MockSnapshotOptions");
|
||||
SnapshotOptions mockSnapshotOptions = mock(SnapshotOptions.class, "MockSnapshotOptions");
|
||||
|
||||
when(mockSnapshotOptions.setFilter(any(SnapshotFilter.class))).thenReturn(mockSnapshotOptions);
|
||||
|
||||
@@ -864,6 +919,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void invokeExceptionSuppressingCloseOnSnapshotServiceAdapterSupportIsSuccessful() throws Exception {
|
||||
|
||||
Closeable mockCloseable = mock(Closeable.class, "MockCloseable");
|
||||
|
||||
assertThat(new TestSnapshotServiceAdapter().exceptionSuppressingClose(mockCloseable), is(true));
|
||||
@@ -873,6 +929,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void invokeExceptionSuppressingCloseOnSnapshotServiceAdapterSupportIsUnsuccessful() throws Exception {
|
||||
|
||||
Closeable mockCloseable = mock(Closeable.class, "MockCloseable");
|
||||
|
||||
doThrow(new IOException("TEST")).when(mockCloseable).close();
|
||||
@@ -884,7 +941,8 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void logDebugWhenDebugging() {
|
||||
final Log mockLog = mock(Log.class, "MockLog");
|
||||
|
||||
Log mockLog = mock(Log.class, "MockLog");
|
||||
|
||||
when(mockLog.isDebugEnabled()).thenReturn(true);
|
||||
|
||||
@@ -904,7 +962,8 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void logDebugWhenNotDebugging() {
|
||||
final Log mockLog = mock(Log.class, "MockLog");
|
||||
|
||||
Log mockLog = mock(Log.class, "MockLog");
|
||||
|
||||
when(mockLog.isDebugEnabled()).thenReturn(false);
|
||||
|
||||
@@ -922,6 +981,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void toSimpleFilenameUsingVariousPathnames() {
|
||||
|
||||
TestSnapshotServiceAdapter snapshotService = new TestSnapshotServiceAdapter();
|
||||
|
||||
assertThat(snapshotService.toSimpleFilename(toPathname("path", "to", "file.ext")), is(equalTo("file.ext")));
|
||||
@@ -936,7 +996,9 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test(expected = ImportSnapshotException.class)
|
||||
public void loadCacheSnapshotWithDirectoryAndFormatHandlesExceptionAppropriately() throws Exception {
|
||||
CacheSnapshotService mockCacheSnapshotService = mock(CacheSnapshotService.class, "MockCacheSnapshotService");
|
||||
|
||||
CacheSnapshotService mockCacheSnapshotService =
|
||||
mock(CacheSnapshotService.class, "MockCacheSnapshotService");
|
||||
|
||||
doThrow(new IOException("TEST")).when(mockCacheSnapshotService).load(any(File.class), any(SnapshotFormat.class));
|
||||
|
||||
@@ -949,7 +1011,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
}
|
||||
catch (ImportSnapshotException expected) {
|
||||
assertThat(expected.getMessage(), is(equalTo(String.format(
|
||||
"Failed to load snapshots from directory (%1$s) in format (GEMFIRE)",
|
||||
"Failed to load snapshots from directory [%1$s] in format [GEMFIRE]",
|
||||
FileSystemUtils.WORKING_DIRECTORY))));
|
||||
assertThat(expected.getCause(), is(instanceOf(IOException.class)));
|
||||
assertThat(expected.getCause().getMessage(), is(equalTo("TEST")));
|
||||
@@ -963,9 +1025,11 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test(expected = ImportSnapshotException.class)
|
||||
public void loadCacheSnapshotWithFormatOptionsAndSnapshotFilesHandlesExceptionAppropriately() throws Exception {
|
||||
|
||||
SnapshotOptions mockSnapshotOptions = mock(SnapshotOptions.class, "MockSnapshotOptions");
|
||||
|
||||
CacheSnapshotService mockCacheSnapshotService = mock(CacheSnapshotService.class, "MockCacheSnapshotService");
|
||||
CacheSnapshotService mockCacheSnapshotService =
|
||||
mock(CacheSnapshotService.class, "MockCacheSnapshotService");
|
||||
|
||||
doThrow(new ClassCastException("TEST")).when(mockCacheSnapshotService).load(any(File[].class),
|
||||
any(SnapshotFormat.class), any(SnapshotOptions.class));
|
||||
@@ -979,7 +1043,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
}
|
||||
catch (ImportSnapshotException expected) {
|
||||
assertThat(expected.getMessage(), is(equalTo(String.format(
|
||||
"Failed to load snapshots (%1$s) in format (GEMFIRE) using options (%2$s)",
|
||||
"Failed to load snapshots [%1$s] in format [GEMFIRE] using options [%2$s]",
|
||||
Arrays.toString(new File[] { snapshotDat }), mockSnapshotOptions))));
|
||||
assertThat(expected.getCause(), is(instanceOf(ClassCastException.class)));
|
||||
assertThat(expected.getCause().getMessage(), is(equalTo("TEST")));
|
||||
@@ -993,7 +1057,9 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test(expected = ExportSnapshotException.class)
|
||||
public void saveCacheSnapshotWithDirectoryAndFormatHandlesExceptionAppropriately() throws Exception {
|
||||
CacheSnapshotService mockCacheSnapshotService = mock(CacheSnapshotService.class, "MockCacheSnapshotService");
|
||||
|
||||
CacheSnapshotService mockCacheSnapshotService =
|
||||
mock(CacheSnapshotService.class, "MockCacheSnapshotService");
|
||||
|
||||
doThrow(new IOException("TEST")).when(mockCacheSnapshotService).save(any(File.class), any(SnapshotFormat.class));
|
||||
|
||||
@@ -1006,7 +1072,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
}
|
||||
catch (ExportSnapshotException expected) {
|
||||
assertThat(expected.getMessage(), is(equalTo(String.format(
|
||||
"Failed to save snapshots to directory (%1$s) in format (GEMFIRE)",
|
||||
"Failed to save snapshots to directory [%1$s] in format [GEMFIRE]",
|
||||
FileSystemUtils.WORKING_DIRECTORY))));
|
||||
assertThat(expected.getCause(), is(instanceOf(IOException.class)));
|
||||
assertThat(expected.getCause().getMessage(), is(equalTo("TEST")));
|
||||
@@ -1020,9 +1086,11 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test(expected = ExportSnapshotException.class)
|
||||
public void saveCacheSnapshotWithDirectoryFormatAndOptionsHandlesExceptionAppropriately() throws Exception {
|
||||
|
||||
SnapshotOptions mockSnapshotOptions = mock(SnapshotOptions.class, "MockSnapshotOptions");
|
||||
|
||||
CacheSnapshotService mockCacheSnapshotService = mock(CacheSnapshotService.class, "MockCacheSnapshotService");
|
||||
CacheSnapshotService mockCacheSnapshotService =
|
||||
mock(CacheSnapshotService.class, "MockCacheSnapshotService");
|
||||
|
||||
doThrow(new ClassCastException("TEST")).when(mockCacheSnapshotService).save(any(File.class),
|
||||
any(SnapshotFormat.class), any(SnapshotOptions.class));
|
||||
@@ -1036,7 +1104,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
}
|
||||
catch (ExportSnapshotException expected) {
|
||||
assertThat(expected.getMessage(), is(equalTo(String.format(
|
||||
"Failed to save snapshots to directory (%1$s) in format (GEMFIRE) using options (%2$s)",
|
||||
"Failed to save snapshots to directory [%1$s] in format [GEMFIRE] using options [%2$s]",
|
||||
FileSystemUtils.USER_HOME, mockSnapshotOptions))));
|
||||
assertThat(expected.getCause(), is(instanceOf(ClassCastException.class)));
|
||||
assertThat(expected.getCause().getMessage(), is(equalTo("TEST")));
|
||||
@@ -1050,7 +1118,9 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test(expected = ImportSnapshotException.class)
|
||||
public void loadRegionSnapshotWithSnapshotFileAndFormatHandlesExceptionAppropriately() throws Exception {
|
||||
RegionSnapshotService mockRegionSnapshotService = mock(RegionSnapshotService.class, "MockRegionSnapshotService");
|
||||
|
||||
RegionSnapshotService mockRegionSnapshotService =
|
||||
mock(RegionSnapshotService.class, "MockRegionSnapshotService");
|
||||
|
||||
doThrow(new IOException("TEST")).when(mockRegionSnapshotService).load(any(File.class),
|
||||
any(SnapshotFormat.class));
|
||||
@@ -1064,7 +1134,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
}
|
||||
catch (ImportSnapshotException expected) {
|
||||
assertThat(expected.getMessage(), is(equalTo(String.format(
|
||||
"Failed to load snapshot from file (%1$s) in format (GEMFIRE)", snapshotDat))));
|
||||
"Failed to load snapshot from file [%1$s] in format [GEMFIRE]", snapshotDat))));
|
||||
assertThat(expected.getCause(), is(instanceOf(IOException.class)));
|
||||
assertThat(expected.getCause().getMessage(), is(equalTo("TEST")));
|
||||
throw expected;
|
||||
@@ -1076,9 +1146,11 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test(expected = ImportSnapshotException.class)
|
||||
public void loadRegionSnapshotWithFormatOptionsAndSnapshotFilesHandlesExceptionAppropriately() throws Exception {
|
||||
|
||||
SnapshotOptions mockSnapshotOptions = mock(SnapshotOptions.class, "MockSnapshotOptions");
|
||||
|
||||
RegionSnapshotService mockRegionSnapshotService = mock(RegionSnapshotService.class, "MockRegionSnapshotService");
|
||||
RegionSnapshotService mockRegionSnapshotService =
|
||||
mock(RegionSnapshotService.class, "MockRegionSnapshotService");
|
||||
|
||||
doThrow(new ClassCastException("TEST")).when(mockRegionSnapshotService).load(
|
||||
any(File.class), any(SnapshotFormat.class), any(SnapshotOptions.class));
|
||||
@@ -1092,7 +1164,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
}
|
||||
catch (ImportSnapshotException expected) {
|
||||
assertThat(expected.getMessage(), is(equalTo(String.format(
|
||||
"Failed to load snapshots (%1$s) in format (GEMFIRE) using options (%2$s)",
|
||||
"Failed to load snapshots [%1$s] in format [GEMFIRE] using options [%2$s]",
|
||||
Arrays.toString(new File[] { snapshotDat }), mockSnapshotOptions))));
|
||||
assertThat(expected.getCause(), is(instanceOf(ClassCastException.class)));
|
||||
assertThat(expected.getCause().getMessage(), is(equalTo("TEST")));
|
||||
@@ -1106,7 +1178,9 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test(expected = ExportSnapshotException.class)
|
||||
public void saveRegionSnapshotWithSnapshotFileAndFormatHandlesExceptionAppropriately() throws Exception {
|
||||
RegionSnapshotService mockRegionSnapshotService = mock(RegionSnapshotService.class, "MockRegionSnapshotService");
|
||||
|
||||
RegionSnapshotService mockRegionSnapshotService =
|
||||
mock(RegionSnapshotService.class, "MockRegionSnapshotService");
|
||||
|
||||
doThrow(new IOException("TEST")).when(mockRegionSnapshotService).save(any(File.class),
|
||||
any(SnapshotFormat.class));
|
||||
@@ -1120,7 +1194,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
}
|
||||
catch (ExportSnapshotException expected) {
|
||||
assertThat(expected.getMessage(), is(equalTo(String.format(
|
||||
"Failed to save snapshot to file (%1$s) in format (GEMFIRE)", snapshotDat))));
|
||||
"Failed to save snapshot to file [%1$s] in format [GEMFIRE]", snapshotDat))));
|
||||
assertThat(expected.getCause(), is(instanceOf(IOException.class)));
|
||||
assertThat(expected.getCause().getMessage(), is(equalTo("TEST")));
|
||||
throw expected;
|
||||
@@ -1132,7 +1206,9 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test(expected = ExportSnapshotException.class)
|
||||
public void saveRegionSnapshotWithSnapshotFileFormatAndOptionsHandlesExceptionAppropriately() throws Exception {
|
||||
SnapshotOptions mockSnapshotOptions = mock(SnapshotOptions.class, "MockSnapahotOptions");
|
||||
|
||||
SnapshotOptions mockSnapshotOptions =
|
||||
mock(SnapshotOptions.class, "MockSnapahotOptions");
|
||||
|
||||
RegionSnapshotService mockRegionSnapshotService = mock(RegionSnapshotService.class, "MockRegionSnapshotService");
|
||||
|
||||
@@ -1148,7 +1224,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
}
|
||||
catch (ExportSnapshotException expected) {
|
||||
assertThat(expected.getMessage(), is(equalTo(String.format(
|
||||
"Failed to save snapshot to file (%1$s) in format (GEMFIRE) using options (%2$s)",
|
||||
"Failed to save snapshot to file [%1$s] in format [GEMFIRE] using options [%2$s]",
|
||||
snapshotDat, mockSnapshotOptions))));
|
||||
assertThat(expected.getCause(), is(instanceOf(ClassCastException.class)));
|
||||
assertThat(expected.getCause().getMessage(), is(equalTo("TEST")));
|
||||
@@ -1162,17 +1238,21 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void createSnapshotMetadataWithNullLocation() {
|
||||
expectedException.expect(IllegalArgumentException.class);
|
||||
expectedException.expectCause(is(nullValue(Throwable.class)));
|
||||
expectedException.expectMessage("Location must not be null");
|
||||
|
||||
exception.expect(IllegalArgumentException.class);
|
||||
exception.expectCause(is(nullValue(Throwable.class)));
|
||||
exception.expectMessage("Location must not be null");
|
||||
|
||||
new SnapshotMetadata(null, mock(SnapshotFilter.class), SnapshotFormat.GEMFIRE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createSnapshotMetadataWithDirectoryFilterAndUnspecifiedFormat() {
|
||||
|
||||
SnapshotFilter mockSnapshotFilter = mock(SnapshotFilter.class, "MockSnapshotFilter");
|
||||
|
||||
SnapshotMetadata metadata = new SnapshotMetadata(FileSystemUtils.WORKING_DIRECTORY, mockSnapshotFilter, null);
|
||||
SnapshotMetadata metadata =
|
||||
new SnapshotMetadata(FileSystemUtils.WORKING_DIRECTORY, mockSnapshotFilter, null);
|
||||
|
||||
assertThat(metadata.getLocation(), is(equalTo(FileSystemUtils.WORKING_DIRECTORY)));
|
||||
assertThat(metadata.isDirectory(), is(true));
|
||||
@@ -1184,6 +1264,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void createSnapshotMetadataWithFileNullFilterAndGemFireFormat() throws Exception {
|
||||
|
||||
SnapshotMetadata snapshotMetadata = new SnapshotMetadata(snapshotDat, null, SnapshotFormat.GEMFIRE);
|
||||
|
||||
assertThat(snapshotMetadata.getLocation(), is(equalTo(snapshotDat)));
|
||||
@@ -1196,6 +1277,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void isJarFileIsTrue() {
|
||||
|
||||
// JRE
|
||||
File runtimeDotJar = new File(new File(FileSystemUtils.JAVA_HOME, "lib"), "rt.jar");
|
||||
|
||||
@@ -1210,6 +1292,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void isJarFileIsFalse() throws Exception {
|
||||
|
||||
assertThat(ArchiveFileFilter.INSTANCE.isJarFile(new File("/path/to/non-existing/file.jar")), is(false));
|
||||
assertThat(ArchiveFileFilter.INSTANCE.isJarFile(new ClassPathResource("/cluster_config.zip").getFile()), is(false));
|
||||
assertThat(ArchiveFileFilter.INSTANCE.isJarFile(new File("to/file.tar")), is(false));
|
||||
@@ -1221,6 +1304,7 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
|
||||
@Test
|
||||
public void getFileExtensionOfVariousFiles() throws Exception {
|
||||
|
||||
assertThat(ArchiveFileFilter.INSTANCE.getFileExtension(new ClassPathResource("/cluster_config.zip").getFile()), is(equalTo("zip")));
|
||||
assertThat(ArchiveFileFilter.INSTANCE.getFileExtension(new File("/path/to/non-existing/file.jar")), is(equalTo("")));
|
||||
assertThat(ArchiveFileFilter.INSTANCE.getFileExtension(new File("to/non-existing/file.tar")), is(equalTo("")));
|
||||
@@ -1247,5 +1331,4 @@ public class SnapshotServiceFactoryBeanTest {
|
||||
throw new UnsupportedOperationException("not implemented");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ import org.springframework.util.FileCopyUtils;
|
||||
@SuppressWarnings("unused")
|
||||
public class SnapshotServiceImportExportIntegrationTest {
|
||||
|
||||
private static final AtomicLong ID_SEQUENCE = new AtomicLong(0l);
|
||||
private static final AtomicLong ID_SEQUENCE = new AtomicLong(0L);
|
||||
|
||||
private static ConfigurableApplicationContext applicationContext;
|
||||
|
||||
@@ -64,6 +64,7 @@ public class SnapshotServiceImportExportIntegrationTest {
|
||||
private static Region<Long, Person> people;
|
||||
|
||||
protected static void assertPerson(Person expectedPerson, Person actualPerson) {
|
||||
|
||||
assertThat(actualPerson, is(notNullValue()));
|
||||
assertThat(actualPerson.getId(), is(equalTo(expectedPerson.getId())));
|
||||
assertThat(actualPerson.getFirstname(), is(equalTo(expectedPerson.getFirstname())));
|
||||
@@ -71,23 +72,25 @@ public class SnapshotServiceImportExportIntegrationTest {
|
||||
}
|
||||
|
||||
protected static void assertRegion(Region<?, ?> actualRegion, String expectedName, int expectedSize) {
|
||||
|
||||
assertThat(actualRegion, is(notNullValue()));
|
||||
assertThat(actualRegion.getName(), is(equalTo("People")));
|
||||
assertThat(actualRegion.getFullPath(), is(equalTo(String.format("%1$s%2$s", Region.SEPARATOR, expectedName))));
|
||||
assertThat(actualRegion.size(), is(expectedSize));
|
||||
}
|
||||
|
||||
protected static Person createPerson(String firstName, String lastName) {
|
||||
return createPerson(ID_SEQUENCE.incrementAndGet(), firstName, lastName);
|
||||
protected static Person newPerson(String firstName, String lastName) {
|
||||
return newPerson(ID_SEQUENCE.incrementAndGet(), firstName, lastName);
|
||||
}
|
||||
|
||||
protected static Person createPerson(Long id, String firstName, String lastName) {
|
||||
protected static Person newPerson(Long id, String firstName, String lastName) {
|
||||
return new Person(id, firstName, lastName);
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void setupBeforeClass() throws Exception {
|
||||
|
||||
snapshotsDirectory = new File(new File(FileSystemUtils.WORKING_DIRECTORY, "gemfire"), "snapshots");
|
||||
|
||||
File exportDirectory = new File(snapshotsDirectory, "export");
|
||||
@@ -96,7 +99,7 @@ public class SnapshotServiceImportExportIntegrationTest {
|
||||
assertThat(exportDirectory.isDirectory() || exportDirectory.mkdirs(), is(true));
|
||||
assertThat(importDirectory.isDirectory() || importDirectory.mkdirs(), is(true));
|
||||
|
||||
importPeopleSnapshot = new File(importDirectory, "people.snapshot");
|
||||
importPeopleSnapshot = new File(importDirectory, "people-snapshot.gfd");
|
||||
|
||||
FileCopyUtils.copy(new ClassPathResource("/people.snapshot").getFile(), importPeopleSnapshot);
|
||||
|
||||
@@ -113,9 +116,10 @@ public class SnapshotServiceImportExportIntegrationTest {
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownAfterClass() {
|
||||
|
||||
applicationContext.close();
|
||||
|
||||
File exportPeopleSnapshot = new File(new File(snapshotsDirectory, "export"), "people.snapshot");
|
||||
File exportPeopleSnapshot = new File(new File(snapshotsDirectory, "export"), "people-snapshot.gfd");
|
||||
|
||||
assertThat(exportPeopleSnapshot.isFile(), is(true));
|
||||
assertThat(exportPeopleSnapshot.length(), is(equalTo(importPeopleSnapshot.length())));
|
||||
@@ -126,23 +130,19 @@ public class SnapshotServiceImportExportIntegrationTest {
|
||||
@Before
|
||||
public void setup() {
|
||||
//setupPeople();
|
||||
ThreadUtils.timedWait(TimeUnit.SECONDS.toMillis(5), 500, new ThreadUtils.WaitCondition() {
|
||||
@Override public boolean waiting() {
|
||||
return !(people.size() > 0);
|
||||
}
|
||||
});
|
||||
ThreadUtils.timedWait(TimeUnit.SECONDS.toMillis(5), 500, () -> !(people.size() > 0));
|
||||
}
|
||||
|
||||
protected void setupPeople() {
|
||||
put(createPerson("Jon", "Doe"));
|
||||
put(createPerson("Jane", "Doe"));
|
||||
put(createPerson("Cookie", "Doe"));
|
||||
put(createPerson("Fro", "Doe"));
|
||||
put(createPerson("Joe", "Doe"));
|
||||
put(createPerson("Lan", "Doe"));
|
||||
put(createPerson("Pie", "Doe"));
|
||||
put(createPerson("Play", "Doe"));
|
||||
put(createPerson("Sour", "Doe"));
|
||||
put(newPerson("Jon", "Doe"));
|
||||
put(newPerson("Jane", "Doe"));
|
||||
put(newPerson("Cookie", "Doe"));
|
||||
put(newPerson("Fro", "Doe"));
|
||||
put(newPerson("Joe", "Doe"));
|
||||
put(newPerson("Lan", "Doe"));
|
||||
put(newPerson("Pie", "Doe"));
|
||||
put(newPerson("Play", "Doe"));
|
||||
put(newPerson("Sour", "Doe"));
|
||||
}
|
||||
|
||||
protected Person put(Person person) {
|
||||
@@ -153,15 +153,14 @@ public class SnapshotServiceImportExportIntegrationTest {
|
||||
@Test
|
||||
public void peopleRegionIsLoaded() {
|
||||
assertRegion(people, "People", 9);
|
||||
assertPerson(people.get(1l), createPerson(1l, "Jon", "Doe"));
|
||||
assertPerson(people.get(2l), createPerson(2l, "Jane", "Doe"));
|
||||
assertPerson(people.get(3l), createPerson(3l, "Cookie", "Doe"));
|
||||
assertPerson(people.get(4l), createPerson(4l, "Fro", "Doe"));
|
||||
assertPerson(people.get(5l), createPerson(5l, "Joe", "Doe"));
|
||||
assertPerson(people.get(6l), createPerson(6l, "Lan", "Doe"));
|
||||
assertPerson(people.get(7l), createPerson(7l, "Pie", "Doe"));
|
||||
assertPerson(people.get(8l), createPerson(8l, "Play", "Doe"));
|
||||
assertPerson(people.get(9l), createPerson(9l, "Sour", "Doe"));
|
||||
assertPerson(people.get(1L), newPerson(1L, "Jon", "Doe"));
|
||||
assertPerson(people.get(2L), newPerson(2L, "Jane", "Doe"));
|
||||
assertPerson(people.get(3L), newPerson(3L, "Cookie", "Doe"));
|
||||
assertPerson(people.get(4L), newPerson(4L, "Fro", "Doe"));
|
||||
assertPerson(people.get(5L), newPerson(5L, "Joe", "Doe"));
|
||||
assertPerson(people.get(6L), newPerson(6L, "Lan", "Doe"));
|
||||
assertPerson(people.get(7L), newPerson(7L, "Pie", "Doe"));
|
||||
assertPerson(people.get(8L), newPerson(8L, "Play", "Doe"));
|
||||
assertPerson(people.get(9L), newPerson(9L, "Sour", "Doe"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
|
||||
<util:properties id="gemfireProperties">
|
||||
<prop key="name">SnapshotApplicationEventTriggeredImportsExportsIntegrationTest</prop>
|
||||
<prop key="mcast-port">0</prop>
|
||||
<prop key="log-level">warning</prop>
|
||||
</util:properties>
|
||||
|
||||
@@ -47,22 +46,22 @@
|
||||
<task:annotation-driven scheduler="snapshotImportsMonitorScheduler"/>
|
||||
|
||||
<gfe-data:snapshot-service id="peopleSnapshotService" region-ref="People">
|
||||
<gfe-data:snapshot-export location="gemfire/snapshots/people.snapshot"/>
|
||||
<gfe-data:snapshot-export location="gemfire/snapshots/nonHandyNonDoePeople.snapshot" filter-ref="nonHandyNonDoeSnapshotFilter"/>
|
||||
<gfe-data:snapshot-export location="gemfire/snapshots/people-snapshot.gfd"/>
|
||||
<gfe-data:snapshot-export location="gemfire/snapshots/nonHandyNonDoePeople-snapshot.gfd" filter-ref="nonHandyNonDoeSnapshotFilter"/>
|
||||
</gfe-data:snapshot-service>
|
||||
|
||||
<gfe-data:snapshot-service id="doeSnapshotService" region-ref="Doe" suppress-import-on-init="true">
|
||||
<gfe-data:snapshot-import location="gemfire/snapshots/people.snapshot">
|
||||
<gfe-data:snapshot-import location="gemfire/snapshots/people-snapshot.gfd">
|
||||
<bean class="org.springframework.data.gemfire.snapshot.SnapshotApplicationEventTriggeredImportsExportsIntegrationTest.LastNameSnapshotFilter" c:lastName="Doe"/>
|
||||
</gfe-data:snapshot-import>
|
||||
</gfe-data:snapshot-service>
|
||||
|
||||
<gfe-data:snapshot-service id="everyoneElseSnapshotService" region-ref="EveryoneElse" suppress-import-on-init="true">
|
||||
<gfe-data:snapshot-import location="gemfire/snapshots/nonHandyNonDoePeople.snapshot"/>
|
||||
<gfe-data:snapshot-import location="gemfire/snapshots/nonHandyNonDoePeople-snapshot.gfd"/>
|
||||
</gfe-data:snapshot-service>
|
||||
|
||||
<gfe-data:snapshot-service id="handySnapshotService" region-ref="Handy" suppress-import-on-init="true">
|
||||
<gfe-data:snapshot-import location="gemfire/snapshots/people.snapshot">
|
||||
<gfe-data:snapshot-import location="gemfire/snapshots/people-snapshot.gfd">
|
||||
<bean class="org.springframework.data.gemfire.snapshot.SnapshotApplicationEventTriggeredImportsExportsIntegrationTest.LastNameSnapshotFilter" c:lastName="Handy"/>
|
||||
</gfe-data:snapshot-import>
|
||||
</gfe-data:snapshot-service>
|
||||
|
||||
@@ -22,8 +22,8 @@
|
||||
<gfe:partitioned-region id="People" persistent="false"/>
|
||||
|
||||
<gfe-data:snapshot-service id="peopleSnapshotService" region-ref="People">
|
||||
<gfe-data:snapshot-import location="gemfire/snapshots/import/people.snapshot"/>
|
||||
<gfe-data:snapshot-export location="gemfire/snapshots/export/people.snapshot" format="GEMFIRE"/>
|
||||
<gfe-data:snapshot-import location="gemfire/snapshots/import/people-snapshot.gfd"/>
|
||||
<gfe-data:snapshot-export location="gemfire/snapshots/export/people-snapshot.gfd" format="GEMFIRE"/>
|
||||
</gfe-data:snapshot-service>
|
||||
|
||||
</beans>
|
||||
|
||||
Reference in New Issue
Block a user