From 747a05e48e6155ee4006adf5c98c426c76a2b7a1 Mon Sep 17 00:00:00 2001 From: John Blum Date: Sat, 11 Nov 2017 18:46:25 -0800 Subject: [PATCH] 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. --- .../snapshot/SnapshotServiceFactoryBean.java | 262 ++++++++++-------- ...riggeredImportsExportsIntegrationTest.java | 79 +++--- .../SnapshotServiceFactoryBeanTest.java | 199 +++++++++---- ...hotServiceImportExportIntegrationTest.java | 59 ++-- ...dImportsExportsIntegrationTest-context.xml | 11 +- ...iceImportExportIntegrationTest-context.xml | 4 +- 6 files changed, 366 insertions(+), 248 deletions(-) diff --git a/src/main/java/org/springframework/data/gemfire/snapshot/SnapshotServiceFactoryBean.java b/src/main/java/org/springframework/data/gemfire/snapshot/SnapshotServiceFactoryBean.java index eab53042..ac2ce083 100644 --- a/src/main/java/org/springframework/data/gemfire/snapshot/SnapshotServiceFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/snapshot/SnapshotServiceFactoryBean.java @@ -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 implements FactoryBean>, - InitializingBean, DisposableBean, ApplicationListener> { +public class SnapshotServiceFactoryBean extends AbstractFactoryBeanSupport> + implements InitializingBean, DisposableBean, ApplicationListener> { protected static final SnapshotMetadata[] EMPTY_ARRAY = new SnapshotMetadata[0]; @@ -100,6 +104,73 @@ public class SnapshotServiceFactoryBean implements FactoryBeanmap(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 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 wrap(RegionSnapshotService regionSnapshotService) { + return new RegionSnapshotServiceAdapter(regionSnapshotService); + } + /** * Sets a reference to the GemFire Cache for which the snapshot will be taken. * @@ -109,8 +180,8 @@ public class SnapshotServiceFactoryBean implements FactoryBean newIllegalArgumentException("The GemFire Cache must not be null")); } /** @@ -122,8 +193,8 @@ public class SnapshotServiceFactoryBean implements FactoryBean newIllegalStateException("The GemFire Cache was not properly initialized")); } /** @@ -220,7 +291,7 @@ public class SnapshotServiceFactoryBean implements FactoryBean getObject() throws Exception { - return snapshotServiceAdapter; + return this.snapshotServiceAdapter; } /** @@ -232,8 +303,10 @@ public class SnapshotServiceFactoryBean implements FactoryBean 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 implements FactoryBean 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 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 wrap(RegionSnapshotService regionSnapshotService) { - return new RegionSnapshotServiceAdapter(regionSnapshotService); - } - /** * Performs an export of the GemFire Cache or Region if configured. * @@ -319,6 +328,7 @@ public class SnapshotServiceFactoryBean implements FactoryBean implements FactoryBean event) { + try { if (isMatch(event)) { if (event instanceof ExportSnapshotApplicationEvent) { @@ -377,6 +389,7 @@ public class SnapshotServiceFactoryBean implements FactoryBean[] resolveSnapshotMetadata(SnapshotApplicationEvent event) { + SnapshotMetadata[] eventSnapshotMetadata = event.getSnapshotMetadata(); return (!ObjectUtils.isEmpty(eventSnapshotMetadata) ? eventSnapshotMetadata @@ -394,8 +407,10 @@ public class SnapshotServiceFactoryBean implements FactoryBean createOptions(); + @SuppressWarnings("unchecked") void doExport(SnapshotMetadata... configurations); + @SuppressWarnings("unchecked") void doImport(SnapshotMetadata... configurations); void load(File directory, SnapshotFormat format); @@ -436,33 +451,35 @@ public class SnapshotServiceFactoryBean implements FactoryBean... configurations) { - for (SnapshotMetadata 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... configurations) { - for (SnapshotMetadata 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 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 implements FactoryBean implements FactoryBean implements FactoryBean implements FactoryBean implements FactoryBean 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 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 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 implements FactoryBean getSnapshotService() { - return snapshotService; + return this.snapshotService; } @Override @@ -652,51 +677,55 @@ public class SnapshotServiceFactoryBean implements FactoryBean 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 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 implements FactoryBean 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 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(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(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); } } - } diff --git a/src/test/java/org/springframework/data/gemfire/snapshot/SnapshotServiceFactoryBeanTest.java b/src/test/java/org/springframework/data/gemfire/snapshot/SnapshotServiceFactoryBeanTest.java index 6fdf0d8f..093007c5 100644 --- a/src/test/java/org/springframework/data/gemfire/snapshot/SnapshotServiceFactoryBeanTest.java +++ b/src/test/java/org/springframework/data/gemfire/snapshot/SnapshotServiceFactoryBeanTest.java @@ -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 SnapshotMetadata newSnapshotMetadata(File location, SnapshotFilter filter, SnapshotFormat format) { + return new SnapshotMetadata(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"); } } - } diff --git a/src/test/java/org/springframework/data/gemfire/snapshot/SnapshotServiceImportExportIntegrationTest.java b/src/test/java/org/springframework/data/gemfire/snapshot/SnapshotServiceImportExportIntegrationTest.java index 86a82da7..239da258 100644 --- a/src/test/java/org/springframework/data/gemfire/snapshot/SnapshotServiceImportExportIntegrationTest.java +++ b/src/test/java/org/springframework/data/gemfire/snapshot/SnapshotServiceImportExportIntegrationTest.java @@ -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 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")); } - } diff --git a/src/test/resources/org/springframework/data/gemfire/snapshot/SnapshotApplicationEventTriggeredImportsExportsIntegrationTest-context.xml b/src/test/resources/org/springframework/data/gemfire/snapshot/SnapshotApplicationEventTriggeredImportsExportsIntegrationTest-context.xml index fec7e734..941181d9 100644 --- a/src/test/resources/org/springframework/data/gemfire/snapshot/SnapshotApplicationEventTriggeredImportsExportsIntegrationTest-context.xml +++ b/src/test/resources/org/springframework/data/gemfire/snapshot/SnapshotApplicationEventTriggeredImportsExportsIntegrationTest-context.xml @@ -18,7 +18,6 @@ SnapshotApplicationEventTriggeredImportsExportsIntegrationTest - 0 warning @@ -47,22 +46,22 @@ - - + + - + - + - + diff --git a/src/test/resources/org/springframework/data/gemfire/snapshot/SnapshotServiceImportExportIntegrationTest-context.xml b/src/test/resources/org/springframework/data/gemfire/snapshot/SnapshotServiceImportExportIntegrationTest-context.xml index 739d960b..17125e1b 100644 --- a/src/test/resources/org/springframework/data/gemfire/snapshot/SnapshotServiceImportExportIntegrationTest-context.xml +++ b/src/test/resources/org/springframework/data/gemfire/snapshot/SnapshotServiceImportExportIntegrationTest-context.xml @@ -22,8 +22,8 @@ - - + +