SGF-408 - Provide support in the SDG XML namespace to load a pre-defined data set using GemFire Snapshot Service for development and testing purposes.

Refactored the SnapshotApplicationEvent into 2 separate ApplicationEvent classes to handle import and exports.
Refactored the SnapshotServiceFactoryBean class to handle import/export SnapshotApplicationEvents in onApplicationEvent(:SnapshotApplicationEvent).
Added integration tests for the archive file handling capabilities of the SnapshotServiceFactoryBean.SnapshotServiceAdapterSupport class.
Added additional unit tests.
(cherry picked from commit 35107aa12dc9d5f133697775c2535c1f9cd3cd87)

Signed-off-by: John Blum <jblum@pivotal.io>
This commit is contained in:
John Blum
2015-08-25 00:53:46 -07:00
parent e78169eda2
commit 447d55efbe
9 changed files with 390 additions and 83 deletions

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2010-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire;
import static org.springframework.data.gemfire.SnapshotServiceFactoryBean.SnapshotMetadata;
/**
* The ExportSnapshotApplicationEvent class is a Spring ApplicationEvent signaling a GemFire Cache or Region 'import'
* snapshot event.
*
* @author John Blum
* @see org.springframework.data.gemfire.SnapshotApplicationEvent
* @since 1.7.0
*/
@SuppressWarnings("unused")
public class ExportSnapshotApplicationEvent<K, V> extends SnapshotApplicationEvent<K, V> {
/**
* Constructs an instance of ExportSnapshotApplicationEvent initialized with an event source and optional meta-data
* describing the data snapshots to be exported.
*
* @param source the source of the ApplicationEvent.
* @param snapshotMetadata an array of SnapshotMetadata containing details for each export.
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean.SnapshotMetadata
* @see #ExportSnapshotApplicationEvent(Object, String, SnapshotMetadata[])
*/
public ExportSnapshotApplicationEvent(Object source, SnapshotMetadata<K, V>... snapshotMetadata) {
super(source, snapshotMetadata);
}
/**
* Constructs an instance of ExportSnapshotApplicationEvent initialized with an event source, a pathname
* of the Region from which data is exported along with meta-data describing the details of the snapshot source.
*
* @param source the source of the ApplicationEvent.
* @param regionPath absolute pathname of the Region.
* @param snapshotMetadata an array of SnapshotMetadata containing details for each export.
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean.SnapshotMetadata
*/
public ExportSnapshotApplicationEvent(Object source, String regionPath, SnapshotMetadata<K, V>... snapshotMetadata) {
super(source, regionPath, snapshotMetadata);
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2010-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire;
import static org.springframework.data.gemfire.SnapshotServiceFactoryBean.SnapshotMetadata;
/**
* The ImportSnapshotApplicationEvent class is a Spring ApplicationEvent signaling a GemFire Cache or Region 'export'
* snapshot event.
*
* @author John Blum
* @see org.springframework.data.gemfire.SnapshotApplicationEvent
* @since 1.7.0
*/
@SuppressWarnings("unused")
public class ImportSnapshotApplicationEvent<K, V> extends SnapshotApplicationEvent<K, V> {
/**
* Constructs an instance of ImportSnapshotApplicationEvent initialized with an event source and optional meta-data
* describing the data snapshots to be imported.
*
* @param source the source of the ApplicationEvent.
* @param snapshotMetadata an array of SnapshotMetadata containing details for each import.
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean.SnapshotMetadata
* @see #ImportSnapshotApplicationEvent(Object, String, SnapshotMetadata[])
*/
public ImportSnapshotApplicationEvent(Object source, SnapshotMetadata<K, V>... snapshotMetadata) {
super(source, snapshotMetadata);
}
/**
* Constructs an instance of ImportSnapshotApplicationEvent initialized with an event source, a pathname
* of the Region in which data is imported along with meta-data describing the details of the snapshot source.
*
* @param source the source of the ApplicationEvent.
* @param regionPath absolute pathname of the Region.
* @param snapshotMetadata an array of SnapshotMetadata containing details for each import/export.
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean.SnapshotMetadata
*/
public ImportSnapshotApplicationEvent(Object source, String regionPath, SnapshotMetadata<K, V>... snapshotMetadata) {
super(source, regionPath, snapshotMetadata);
}
}

View File

@@ -24,8 +24,8 @@ import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.management.internal.cli.util.spring.StringUtils;
/**
* The SnapshotApplicationEvent class is a Spring ApplicationEvent signaling a GemFire Cache Region(s) snapshot event
* triggering a snapshot to occur.
* The SnapshotApplicationEvent class is a Spring ApplicationEvent signaling a GemFire Cache or Region snapshot event,
* used to trigger a snapshot to occur.
*
* @author John Blum
* @see org.springframework.context.ApplicationEvent
@@ -33,36 +33,36 @@ import com.gemstone.gemfire.management.internal.cli.util.spring.StringUtils;
* @since 1.7.0
*/
@SuppressWarnings("unused")
public class SnapshotApplicationEvent<K, V> extends ApplicationEvent {
public abstract class SnapshotApplicationEvent<K, V> extends ApplicationEvent {
private final SnapshotMetadata<K, V>[] snapshotMetadata;
private final String regionPath;
/**
* Constructs an instance of SnapshotApplicationEvent initialized with an event source along with optional meta-data
* describing the data snapshots to be taken.
* Constructs an instance of SnapshotApplicationEvent initialized with an event source and optional meta-data
* describing the data snapshots to be imported/exported.
*
* @param eventSource the source of the ApplicationEvent.
* @param snapshotMetadata an array of SnapshotMetadata containing details of each export.
* @param source the source of the ApplicationEvent.
* @param snapshotMetadata an array of SnapshotMetadata containing details for each import/export.
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean.SnapshotMetadata
* @see #SnapshotApplicationEvent(Object, String, SnapshotMetadata[])
*/
public SnapshotApplicationEvent(Object eventSource, SnapshotMetadata<K, V>... snapshotMetadata) {
this(eventSource, null, snapshotMetadata);
public SnapshotApplicationEvent(Object source, SnapshotMetadata<K, V>... snapshotMetadata) {
this(source, null, snapshotMetadata);
}
/**
* Constructs an instance of SnapshotApplicationEvent initialized with an event source and pathname of the Region
* on which the data snapshot(s) will be taken with details provided by the snapshot meta-data.
* Constructs an instance of SnapshotApplicationEvent initialized with an event source, a pathname of the Region
* which data snapshots are imported/exported along with meta-data describing the details of the snapshot source.
*
* @param eventSource the source of the ApplicationEvent.
* @param regionPath the absolute pathname of the Region.
* @param snapshotMetadata an array of SnapshotMetadata containing details of each export.
* @param source the source of the ApplicationEvent.
* @param regionPath absolute pathname of the Region.
* @param snapshotMetadata an array of SnapshotMetadata containing details for each import/export.
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean.SnapshotMetadata
*/
public SnapshotApplicationEvent(Object eventSource, String regionPath, SnapshotMetadata<K, V>... snapshotMetadata) {
super(eventSource);
public SnapshotApplicationEvent(Object source, String regionPath, SnapshotMetadata<K, V>... snapshotMetadata) {
super(source);
this.snapshotMetadata = snapshotMetadata;
this.regionPath = regionPath;
}

View File

@@ -74,11 +74,11 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
private Region<K, V> region;
private SnapshotServiceAdapter<K, V> snapshotServiceAdapter;
private SnapshotMetadata<K, V>[] exports;
private SnapshotMetadata<K, V>[] imports;
private SnapshotServiceAdapter<K, V> snapshotServiceAdapter;
/* (non-Javadoc) */
@SuppressWarnings("unchecked")
static <K, V> SnapshotMetadata<K, V>[] nullSafeArray(SnapshotMetadata<K, V>[] configurations) {
@@ -231,8 +231,6 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
*/
@Override
@SuppressWarnings("unchecked")
// TODO consider making the "import" on bean initialization asynchronous
// NOTE GemFire may handle asynchronous import and export under the right conditions (need to research)
public void afterPropertiesSet() throws Exception {
snapshotServiceAdapter = create();
snapshotServiceAdapter.doImport(getImports());
@@ -296,22 +294,29 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
}
/**
* Listens for SnapshotApplicationEvents triggering a GemFire Cache-wide or Region data snapshot when
* the details of the event match the criteria of this factory's constructed GemFire SnapshotService.
* Listens for SnapshotApplicationEvents triggering a GemFire Cache-wide or Region data snapshot import/export
* when details of the event match the criteria of this factory's constructed GemFire SnapshotService.
*
* @param event the SnapshotApplicationEvent triggering a GemFire Cache or Region data export.
* @param event the SnapshotApplicationEvent triggering a GemFire Cache or Region data import/export.
* @see org.springframework.data.gemfire.SnapshotApplicationEvent
* @see org.springframework.data.gemfire.ExportSnapshotApplicationEvent
* @see org.springframework.data.gemfire.ImportSnapshotApplicationEvent
* @see #isMatch(SnapshotApplicationEvent)
* @see #resolveSnapshotMetadata(SnapshotApplicationEvent)
* @see #getObject()
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean.SnapshotServiceAdapter#doExport(SnapshotMetadata[])
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean.SnapshotServiceAdapter#doImport(SnapshotMetadata[])
*/
@Override
@SuppressWarnings("unchecked")
public void onApplicationEvent(SnapshotApplicationEvent<K, V> event) {
try {
if (isMatch(event)) {
getObject().doExport(resolveSnapshotMetadata(event));
if (event instanceof ExportSnapshotApplicationEvent) {
getObject().doExport(resolveSnapshotMetadata(event));
}
else {
getObject().doImport(resolveSnapshotMetadata(event));
}
}
}
catch (Exception ignore) {
@@ -335,18 +340,21 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
}
/**
* Resolves the SnapshotMetadata used to perform the GemFire Cache or Region data snapshot (export). If the event
* contains specific SnapshotMetadata, then this is preferred over the factory's own "export" SnapshotMetadata.
* Resolves the SnapshotMetadata used to perform the GemFire Cache or Region data snapshot import/export.
* If the event contains specific SnapshotMetadata, then this is preferred over the factory's own
* "import" or "export" SnapshotMetadata.
*
* @param event the SnapshotApplicationEvent from which to resolve the SnapshotMetadata.
* @return the resolved SnapshotMetadata, either from the event or this factory's configured exports.
* @return the resolved SnapshotMetadata, either from the event or this factory's configured imports/exports.
* @see org.springframework.data.gemfire.SnapshotApplicationEvent#getSnapshotMetadata()
* @see #getExports()
* @see #getImports()
*/
protected SnapshotMetadata<K, V>[] resolveSnapshotMetadata(SnapshotApplicationEvent<K, V> event) {
SnapshotMetadata<K, V>[] eventSnapshotMetadata = event.getSnapshotMetadata();
return (!ObjectUtils.isEmpty(eventSnapshotMetadata) ? eventSnapshotMetadata : getExports());
return (!ObjectUtils.isEmpty(eventSnapshotMetadata) ? eventSnapshotMetadata
: (event instanceof ExportSnapshotApplicationEvent ? getExports() : getImports()));
}
/**
@@ -374,6 +382,14 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
}
/**
* SnapshotServiceAdapterSupport is an abstract base class for all SnapshotServiceAdapter implementations
* encapsulating common reusable functionality.
*
* @param <K> the class type of the Cache Region key.
* @param <V> the class type of the Cache Region value.
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean.SnapshotServiceAdapter
*/
protected static abstract class SnapshotServiceAdapterSupport<K, V> implements SnapshotServiceAdapter<K, V> {
protected final Log log = createLog();
@@ -382,6 +398,11 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
return LogFactory.getLog(getClass());
}
@Override
public SnapshotOptions<K, V> createOptions() {
throw new UnsupportedOperationException("not implemented");
}
protected SnapshotOptions<K, V> createOptions(SnapshotFilter<K, V> filter) {
return createOptions().setFilter(filter);
}
@@ -422,7 +443,7 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
ZipFile zipFile = (ArchiveFileFilter.INSTANCE.isJarFile(file)
? new JarFile(file, false, JarFile.OPEN_READ)
: new ZipFile(file, ZipFile.OPEN_READ));
: new ZipFile(file, ZipFile.OPEN_READ));
for (ZipEntry entry : CollectionUtils.iterable(zipFile.entries())) {
if (!entry.isDirectory()) {
@@ -469,6 +490,26 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
}
}
@Override
public void load(File directory, SnapshotFormat format) {
throw new UnsupportedOperationException("not implemented");
}
@Override
public void load(SnapshotFormat format, SnapshotOptions<K, V> options, File... snapshots) {
throw new UnsupportedOperationException("not implemented");
}
@Override
public void save(File location, SnapshotFormat format) {
throw new UnsupportedOperationException("not implemented");
}
@Override
public void save(File location, SnapshotFormat format, SnapshotOptions<K, V> options) {
throw new UnsupportedOperationException("not implemented");
}
protected String toSimpleFilename(String pathname) {
int pathSeparatorIndex = String.valueOf(pathname).lastIndexOf(File.separator);
pathname = (pathSeparatorIndex > -1 ? pathname.substring(pathSeparatorIndex + 1) : pathname);
@@ -478,6 +519,8 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
/**
* The CacheSnapshotServiceAdapter is a SnapshotServiceAdapter adapting GemFire's CacheSnapshotService.
*
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean.SnapshotServiceAdapterSupport
*/
protected static class CacheSnapshotServiceAdapter extends SnapshotServiceAdapterSupport<Object, Object> {
@@ -554,6 +597,8 @@ public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotSer
/**
* The RegionSnapshotServiceAdapter is a SnapshotServiceAdapter adapting GemFire's RegionSnapshotService.
*
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean.SnapshotServiceAdapterSupport
*/
protected static class RegionSnapshotServiceAdapter<K, V> extends SnapshotServiceAdapterSupport<K, V> {

View File

@@ -238,7 +238,7 @@ both in the same definition) is illegal.
</xsd:any>
</xsd:sequence>
<xsd:attribute name="location" type="xsd:string" use="required"/>
<xsd:attribute name="filter-ref" type="xsd:string" use="optional"/>
<xsd:attribute name="format" type="xsd:string" use="optional" default="GEMFIRE"/>
<xsd:attribute name="filter-ref" type="xsd:string" use="optional"/>
</xsd:complexType>
</xsd:schema>

View File

@@ -64,7 +64,7 @@ public class SnapshotApplicationEventTest {
@Test
public void constructSnapshotApplicationEventWithoutRegionOrSnapshotMetadata() {
SnapshotApplicationEvent event = new SnapshotApplicationEvent(this);
SnapshotApplicationEvent event = new TestSnapshotApplicationEvent(this);
assertThat((SnapshotApplicationEventTest) event.getSource(), is(equalTo(this)));
assertThat(event.getRegionPath(), is(nullValue()));
@@ -78,7 +78,7 @@ public class SnapshotApplicationEventTest {
public void constructSnapshotApplicationEventWithRegionAndSnapshotMetadata() {
SnapshotMetadata eventSnapshotMetadata = newSnapshotMetadata();
SnapshotApplicationEvent event = new SnapshotApplicationEvent(this, "/Example", eventSnapshotMetadata);
SnapshotApplicationEvent event = new TestSnapshotApplicationEvent(this, "/Example", eventSnapshotMetadata);
assertThat((SnapshotApplicationEventTest) event.getSource(), is(equalTo(this)));
assertThat(event.getRegionPath(), is(equalTo("/Example")));
@@ -89,7 +89,7 @@ public class SnapshotApplicationEventTest {
@Test
public void constructSnapshotApplicationEventWithRegionButNoSnapshotMetadata() {
SnapshotApplicationEvent event = new SnapshotApplicationEvent(this, "/Example");
SnapshotApplicationEvent event = new TestSnapshotApplicationEvent(this, "/Example");
assertThat((SnapshotApplicationEventTest) event.getSource(), is(equalTo(this)));
assertThat(event.getRegionPath(), is(equalTo("/Example")));
@@ -104,7 +104,7 @@ public class SnapshotApplicationEventTest {
SnapshotMetadata eventSnapshotMetadataOne = newSnapshotMetadata();
SnapshotMetadata eventSnapshotMetadataTwo = newSnapshotMetadata();
SnapshotApplicationEvent event = new SnapshotApplicationEvent(this, eventSnapshotMetadataOne,
SnapshotApplicationEvent event = new TestSnapshotApplicationEvent(this, eventSnapshotMetadataOne,
eventSnapshotMetadataTwo);
assertThat((SnapshotApplicationEventTest) event.getSource(), is(equalTo(this)));
@@ -117,7 +117,7 @@ public class SnapshotApplicationEventTest {
@Test
public void matchesNullRegionIsFalse() {
assertThat(new SnapshotApplicationEvent(this).matches((Region) null), is(false));
assertThat(new TestSnapshotApplicationEvent(this).matches((Region) null), is(false));
}
@Test
@@ -126,7 +126,7 @@ public class SnapshotApplicationEventTest {
when(mockRegion.getFullPath()).thenReturn("/Example");
assertThat(new SnapshotApplicationEvent(this, "/Prototype").matches(mockRegion), is(false));
assertThat(new TestSnapshotApplicationEvent(this, "/Prototype").matches(mockRegion), is(false));
verify(mockRegion, times(1)).getFullPath();
}
@@ -137,14 +137,14 @@ public class SnapshotApplicationEventTest {
when(mockRegion.getFullPath()).thenReturn("/Example");
assertThat(new SnapshotApplicationEvent(this, "/Example").matches(mockRegion), is(true));
assertThat(new TestSnapshotApplicationEvent(this, "/Example").matches(mockRegion), is(true));
verify(mockRegion, times(1)).getFullPath();
}
@Test
public void matchesNonMatchingRegionPathsIsFalse() {
SnapshotApplicationEvent event = new SnapshotApplicationEvent(this, "/Example");
SnapshotApplicationEvent event = new TestSnapshotApplicationEvent(this, "/Example");
assertThat(event.getRegionPath(), is(equalTo("/Example")));
assertThat(event.matches("/Prototype"), is(false));
@@ -163,11 +163,23 @@ public class SnapshotApplicationEventTest {
@Test
public void matchesMatchingRegionPathsIsTrue() {
assertThat(new SnapshotApplicationEvent(this, "/Example").matches("/Example"), is(true));
assertThat(new SnapshotApplicationEvent(this, "/").matches("/"), is(true));
assertThat(new SnapshotApplicationEvent(this, " ").matches(" "), is(true));
assertThat(new SnapshotApplicationEvent(this, "").matches(""), is(true));
assertThat(new SnapshotApplicationEvent(this, (String) null).matches((String) null), is(true));
assertThat(new TestSnapshotApplicationEvent(this, "/Example").matches("/Example"), is(true));
assertThat(new TestSnapshotApplicationEvent(this, "/").matches("/"), is(true));
assertThat(new TestSnapshotApplicationEvent(this, " ").matches(" "), is(true));
assertThat(new TestSnapshotApplicationEvent(this, "").matches(""), is(true));
assertThat(new TestSnapshotApplicationEvent(this, (String) null).matches((String) null), is(true));
}
protected static final class TestSnapshotApplicationEvent<K, V> extends SnapshotApplicationEvent<K, V> {
public TestSnapshotApplicationEvent(Object source, SnapshotMetadata<K, V>... snapshotMetadata) {
super(source, snapshotMetadata);
}
public TestSnapshotApplicationEvent(Object source, String regionPath,
SnapshotMetadata<K, V>... snapshotMetadata) {
super(source, regionPath, snapshotMetadata);
}
}
}

View File

@@ -16,13 +16,89 @@
package org.springframework.data.gemfire;
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.SnapshotServiceFactoryBean.SnapshotServiceAdapterSupport;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.gemfire.test.support.FileSystemUtils;
/**
* The SnapshotServiceFactoryBeanIntegrationTest class...
* The SnapshotServiceFactoryBeanIntegrationTest class is a test suite of test cases testing the file archive handling
* capabilities of the SnapshotServiceFactoryBean.SnapshotServiceAdpterSupport class.
*
* @author John Blum
* @see org.springframework.data.gemfire.
* @see org.junit.Test
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean.SnapshotServiceAdapterSupport
* @since 1.7.0
*/
public class SnapshotServiceFactoryBeanIntegrationTest {
SnapshotServiceAdapterSupport snapshotService = new TestSnapshotServiceAdapter();
protected List<String> toFilenames(File... files) {
List<String> filenames = new ArrayList<String>(files.length);
for (File file : files) {
filenames.add(file.getName());
}
return filenames;
}
@Test
public void handleNonArchiveFileLocation() {
File expectedFile = new File("/path/to/non-existing/snapshot/file.gfd");
File[] files = snapshotService.handleFileLocation(expectedFile);
assertThat(files, is(notNullValue()));
assertThat(files.length, is(equalTo(1)));
assertThat(files[0], is(equalTo(expectedFile)));
}
@Test
public void handleArchiveFileLocation() throws Exception {
File cacheSnapshotZipDirectory = null;
try {
File cacheSnapshotZip = new ClassPathResource("/cache_snapshot.zip").getFile();
File[] actualSnapshots = snapshotService.handleFileLocation(cacheSnapshotZip);
assertThat(actualSnapshots, is(notNullValue()));
assertThat(actualSnapshots.length, is(equalTo(3)));
assertThat(toFilenames(actualSnapshots).containsAll(Arrays.asList(
"accounts.snapshot", "address.snapshot", "people.snapshot")), is(true));
cacheSnapshotZipDirectory = new File(System.getProperty("java.io.tmpdir"),
cacheSnapshotZip.getName().replaceAll("\\.", "-"));
assertThat(cacheSnapshotZipDirectory.isDirectory(), is(true));
assertThat(cacheSnapshotZipDirectory.listFiles(FileSystemUtils.FileOnlyFilter.INSTANCE),
is(equalTo(actualSnapshots)));
}
finally {
if (cacheSnapshotZipDirectory != null && cacheSnapshotZipDirectory.isDirectory()) {
FileSystemUtils.deleteRecursive(cacheSnapshotZipDirectory);
}
}
}
protected static final class TestSnapshotServiceAdapter<K, V> extends SnapshotServiceAdapterSupport<K, V> {
@Override
protected File[] handleLocation(final SnapshotServiceFactoryBean.SnapshotMetadata<K, V> configuration) {
throw new UnsupportedOperationException("not implemented");
}
}
}

View File

@@ -370,12 +370,12 @@ public class SnapshotServiceFactoryBeanTest {
SnapshotMetadata eventSnapshotMetadata = newSnapshotMetadata(snapshotDat);
SnapshotApplicationEvent event = new SnapshotApplicationEvent(this, "/Example", eventSnapshotMetadata);
SnapshotApplicationEvent event = new ExportSnapshotApplicationEvent(this, "/Example", eventSnapshotMetadata);
assertThat(event.isRegionSnapshotEvent(), is(true));
assertThat(event.getSnapshotMetadata()[0], is(sameInstance(eventSnapshotMetadata)));
assertThat(factoryBean.getExports()[0], is(not(sameInstance(eventSnapshotMetadata))));
assertThat(factoryBean.getRegion(), is(sameInstance(mockRegion)));
assertThat(event.isRegionSnapshotEvent(), is(true));
assertThat(event.getSnapshotMetadata()[0], is(sameInstance(eventSnapshotMetadata)));
factoryBean.onApplicationEvent(event);
@@ -386,6 +386,40 @@ public class SnapshotServiceFactoryBeanTest {
eq(eventSnapshotMetadata.getFormat()), eq(mockSnapshotOptions));
}
@Test
public void onApplicationEventWhenMatchUsingFactorySnapshotMetadataPerformsImport() throws Exception {
final CacheSnapshotService mockSnapshotService = mock(CacheSnapshotService.class, "MockCacheSnapshotService");
SnapshotOptions mockSnapshotOptions = mock(SnapshotOptions.class, "MockSnapshotOptions");
when(mockSnapshotService.createOptions()).thenReturn(mockSnapshotOptions);
when(mockSnapshotOptions.setFilter(any(SnapshotFilter.class))).thenReturn(mockSnapshotOptions);
SnapshotServiceFactoryBean factoryBean = new SnapshotServiceFactoryBean() {
@Override public SnapshotServiceAdapter getObject() throws Exception {
return new CacheSnapshotServiceAdapter(mockSnapshotService);
}
};
SnapshotMetadata expectedSnapshotMetadata = newSnapshotMetadata(snapshotDat);
factoryBean.setImports(toArray(expectedSnapshotMetadata));
SnapshotApplicationEvent event = new ImportSnapshotApplicationEvent(this);
assertThat(factoryBean.getImports()[0], is(equalTo(expectedSnapshotMetadata)));
assertThat(factoryBean.getRegion(), is(nullValue()));
assertThat(event.isCacheSnapshotEvent(), is(true));
assertThat(event.getSnapshotMetadata().length, is(equalTo(0)));
factoryBean.onApplicationEvent(event);
verify(mockSnapshotOptions, times(1)).setFilter(isNull(SnapshotFilter.class));
verify(mockSnapshotService, times(1)).createOptions();
verify(mockSnapshotService, times(1)).load(eq(new File[] { expectedSnapshotMetadata.getLocation() }),
eq(expectedSnapshotMetadata.getFormat()), eq(mockSnapshotOptions));
}
@Test
public void onApplicationEventWhenNoMatchDoesNotPerformExport() throws Exception {
final CacheSnapshotService mockSnapshotService = mock(CacheSnapshotService.class, "MockCacheSnapshotService");
@@ -396,10 +430,13 @@ public class SnapshotServiceFactoryBeanTest {
}
};
SnapshotApplicationEvent event = new SnapshotApplicationEvent(this, "/Example");
factoryBean.setExports(toArray(newSnapshotMetadata()));
assertThat(event.isRegionSnapshotEvent(), is(true));
SnapshotApplicationEvent event = new ExportSnapshotApplicationEvent(this, "/Example");
assertThat(factoryBean.getExports().length, is(equalTo(1)));
assertThat(factoryBean.getRegion(), is(nullValue()));
assertThat(event.isRegionSnapshotEvent(), is(true));
factoryBean.onApplicationEvent(event);
@@ -409,32 +446,78 @@ public class SnapshotServiceFactoryBeanTest {
any(SnapshotOptions.class));
}
@Test
public void onApplicationEventWhenNoMatchDoesNotPerformImport() throws Exception {
Region mockRegion = mock(Region.class, "MockRegion");
when(mockRegion.getFullPath()).thenReturn("/Example");
final RegionSnapshotService mockSnapshotService = mock(RegionSnapshotService.class, "MockRegionSnapshotService");
SnapshotServiceFactoryBean factoryBean = new SnapshotServiceFactoryBean() {
@Override public SnapshotServiceAdapter getObject() throws Exception {
return new RegionSnapshotServiceAdapter(mockSnapshotService);
}
};
factoryBean.setImports(toArray(newSnapshotMetadata()));
factoryBean.setRegion(mockRegion);
SnapshotApplicationEvent event = new ImportSnapshotApplicationEvent(this, "/Test");
assertThat(factoryBean.getImports().length, is(equalTo(1)));
assertThat(factoryBean.getRegion(), is(equalTo(mockRegion)));
assertThat(event.isRegionSnapshotEvent(), is(true));
factoryBean.onApplicationEvent(event);
verify(mockRegion, times(1)).getFullPath();
verify(mockSnapshotService, never()).createOptions();
verify(mockSnapshotService, never()).load(any(File.class), any(SnapshotFormat.class));
verify(mockSnapshotService, never()).load(any(File.class), any(SnapshotFormat.class),
any(SnapshotOptions.class));
}
@Test
public void resolveSnapshotMetadataFromEvent() {
SnapshotMetadata eventSnapshotMetadata = newSnapshotMetadata(snapshotDat);
SnapshotMetadata exportSnapshotMetadata = newSnapshotMetadata();
SnapshotMetadata importSnapshotMetadata = newSnapshotMetadata(FileSystemUtils.USER_HOME);
factoryBean.setExports(toArray(exportSnapshotMetadata));
factoryBean.setImports(toArray(importSnapshotMetadata));
assertThat(factoryBean.getExports()[0], is(equalTo(exportSnapshotMetadata)));
assertThat(factoryBean.resolveSnapshotMetadata(new SnapshotApplicationEvent(
assertThat(factoryBean.getImports()[0], is(equalTo(importSnapshotMetadata)));
assertThat(factoryBean.resolveSnapshotMetadata(new ExportSnapshotApplicationEvent(
this, eventSnapshotMetadata))[0], is(equalTo(eventSnapshotMetadata)));
}
@Test
public void resolveSnapshotMetadataFromFactory() {
public void resolveExportSnapshotMetadataFromFactory() {
SnapshotMetadata exportSnapshotMetadata = newSnapshotMetadata();
factoryBean.setExports(toArray(exportSnapshotMetadata));
assertThat(factoryBean.getExports()[0], is(equalTo(exportSnapshotMetadata)));
assertThat(factoryBean.resolveSnapshotMetadata(new SnapshotApplicationEvent(this))[0],
assertThat(factoryBean.resolveSnapshotMetadata(new ExportSnapshotApplicationEvent(this))[0],
is(equalTo(exportSnapshotMetadata)));
}
@Test
public void resolveImportSnapshotMetadataFromFactory() {
SnapshotMetadata exportSnapshotMetadata = newSnapshotMetadata();
factoryBean.setImports(toArray(exportSnapshotMetadata));
assertThat(factoryBean.getImports()[0], is(equalTo(exportSnapshotMetadata)));
assertThat(factoryBean.resolveSnapshotMetadata(new ImportSnapshotApplicationEvent(this))[0],
is(equalTo(exportSnapshotMetadata)));
}
@Test
public void withCacheBasedSnapshotServiceOnCacheSnapshotEventIsMatch() {
SnapshotApplicationEvent event = new SnapshotApplicationEvent(this);
SnapshotApplicationEvent event = new ExportSnapshotApplicationEvent(this);
assertThat(event.isCacheSnapshotEvent(), is(true));
assertThat(event.isRegionSnapshotEvent(), is(false));
@@ -443,7 +526,7 @@ public class SnapshotServiceFactoryBeanTest {
@Test
public void withCacheBasedSnapshotServiceOnRegionSnapshotEventIsNotAMatch() {
SnapshotApplicationEvent event = new SnapshotApplicationEvent(this, "/Example");
SnapshotApplicationEvent event = new ImportSnapshotApplicationEvent(this, "/Example");
assertThat(event.isCacheSnapshotEvent(), is(false));
assertThat(event.isRegionSnapshotEvent(), is(true));
@@ -452,7 +535,7 @@ public class SnapshotServiceFactoryBeanTest {
@Test
public void withRegionBasedSnapshotServiceOnRegionSnapshotEventIsMatch() {
SnapshotApplicationEvent event = new SnapshotApplicationEvent(this, "/Example");
SnapshotApplicationEvent event = new ExportSnapshotApplicationEvent(this, "/Example");
assertThat(event.isCacheSnapshotEvent(), is(false));
assertThat(event.isRegionSnapshotEvent(), is(true));
@@ -471,7 +554,7 @@ public class SnapshotServiceFactoryBeanTest {
@Test
public void withRegionBasedSnapshotServiceOnCacheSnapshotEventIsNotAMatch() {
SnapshotApplicationEvent event = new SnapshotApplicationEvent(this);
SnapshotApplicationEvent event = new ImportSnapshotApplicationEvent(this);
assertThat(event.isCacheSnapshotEvent(), is(true));
assertThat(event.isRegionSnapshotEvent(), is(false));
@@ -1074,35 +1157,10 @@ public class SnapshotServiceFactoryBeanTest {
protected static class TestSnapshotServiceAdapter extends SnapshotServiceAdapterSupport<Object, Object> {
@Override
public SnapshotOptions<Object, Object> createOptions() {
throw new UnsupportedOperationException("not implemented");
}
@Override
protected File[] handleLocation(final SnapshotMetadata<Object, Object> configuration) {
throw new UnsupportedOperationException("not implemented");
}
@Override
public void load(final File directory, final SnapshotFormat format) {
throw new UnsupportedOperationException("not implemented");
}
@Override
public void load(SnapshotFormat format, SnapshotOptions<Object, Object> options, File... snapshots) {
throw new UnsupportedOperationException("not implemented");
}
@Override
public void save(final File location, final SnapshotFormat format) {
throw new UnsupportedOperationException("not implemented");
}
@Override
public void save(File location, SnapshotFormat format, SnapshotOptions<Object, Object> options) {
throw new UnsupportedOperationException("not implemented");
}
}
}

Binary file not shown.