From d84234b5b8f1dbf25327ed1f5c85aff48fc92382 Mon Sep 17 00:00:00 2001 From: John Blum Date: Wed, 17 Jul 2019 02:22:07 -0700 Subject: [PATCH] SGF-864 - Fix unreliable tests. --- ...heClusterConfigurationIntegrationTest.java | 46 +++++-- ...riggeredImportsExportsIntegrationTest.java | 39 ++++-- .../SnapshotServiceFactoryBeanTest.java | 118 ++++++++++++++---- .../gemfire/test/support/StackTraceUtils.java | 7 +- .../gemfire/test/support/ThreadUtils.java | 7 +- .../gemfire/test/support/ThrowableUtils.java | 9 +- 6 files changed, 174 insertions(+), 52 deletions(-) diff --git a/src/test/java/org/springframework/data/gemfire/CacheClusterConfigurationIntegrationTest.java b/src/test/java/org/springframework/data/gemfire/CacheClusterConfigurationIntegrationTest.java index a4a67093..084ded93 100644 --- a/src/test/java/org/springframework/data/gemfire/CacheClusterConfigurationIntegrationTest.java +++ b/src/test/java/org/springframework/data/gemfire/CacheClusterConfigurationIntegrationTest.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.gemfire; import static org.junit.Assert.assertEquals; @@ -28,10 +27,6 @@ import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; -import com.gemstone.gemfire.cache.DataPolicy; -import com.gemstone.gemfire.cache.Region; -import com.gemstone.gemfire.cache.Scope; - import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Ignore; @@ -40,6 +35,11 @@ import org.junit.Test; import org.junit.rules.TestRule; import org.junit.rules.TestWatcher; import org.junit.runner.Description; + +import com.gemstone.gemfire.cache.DataPolicy; +import com.gemstone.gemfire.cache.Region; +import com.gemstone.gemfire.cache.Scope; + import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanInitializationException; import org.springframework.context.ConfigurableApplicationContext; @@ -81,8 +81,10 @@ public class CacheClusterConfigurationIntegrationTest { @Rule public TestRule watchman = new TestWatcher() { + @Override protected void failed(Throwable throwable, Description description) { + System.err.println(String.format("Test '%1$s' failed...", description.getDisplayName())); System.err.println(ThrowableUtils.toString(throwable)); System.err.println("Locator process log file contents were..."); @@ -91,6 +93,7 @@ public class CacheClusterConfigurationIntegrationTest { @Override protected void finished(Description description) { + if (Boolean.valueOf(System.getProperty("spring.gemfire.fork.clean", Boolean.TRUE.toString()))) { try { FileUtils.write(new File(locatorWorkingDirectory.getParent(), @@ -104,7 +107,9 @@ public class CacheClusterConfigurationIntegrationTest { } private String getLocatorProcessOutput(Description description) { + try { + String locatorProcessOutputString = StringUtils.collectionToDelimitedString(locatorProcessOutput, FileUtils.LINE_SEPARATOR, String.format("[%1$s] - ", description.getMethodName()), ""); @@ -121,6 +126,7 @@ public class CacheClusterConfigurationIntegrationTest { @BeforeClass public static void testSuiteSetup() throws IOException { + String locatorName = "ClusterConfigLocator"; locatorWorkingDirectory = new File(System.getProperty("user.dir"), locatorName.toLowerCase()); @@ -133,7 +139,7 @@ public class CacheClusterConfigurationIntegrationTest { arguments.add("-Dgemfire.name=" + locatorName); arguments.add("-Dgemfire.mcast-port=0"); - arguments.add("-Dgemfire.log-level=error"); + arguments.add("-Dgemfire.log-level=info"); arguments.add("-Dspring.gemfire.enable-cluster-configuration=true"); arguments.add("-Dspring.gemfire.load-cluster-configuration=true"); @@ -141,7 +147,9 @@ public class CacheClusterConfigurationIntegrationTest { arguments.toArray(new String[arguments.size()])); locatorProcess.register(new ProcessInputStreamListener() { - @Override public void onInput(final String input) { + + @Override + public void onInput(final String input) { locatorProcessOutput.add(input); } }); @@ -154,9 +162,13 @@ public class CacheClusterConfigurationIntegrationTest { } private static void waitForLocatorStart(final long milliseconds) { + ThreadUtils.timedWait(milliseconds, 500, new ThreadUtils.WaitCondition() { + File pidControlFile = new File(locatorWorkingDirectory, LocatorProcess.getLocatorProcessControlFilename()); - @Override public boolean waiting() { + + @Override + public boolean waiting() { return !pidControlFile.isFile(); } }); @@ -164,7 +176,9 @@ public class CacheClusterConfigurationIntegrationTest { @AfterClass public static void testSuiteTearDown() { + locatorProcess.shutdown(); + if (Boolean.valueOf(System.getProperty("spring.gemfire.fork.clean", Boolean.TRUE.toString()))) { FileSystemUtils.deleteRecursively(locatorWorkingDirectory); } @@ -175,22 +189,28 @@ public class CacheClusterConfigurationIntegrationTest { } protected Region assertRegion(final Region actualRegion, final String expectedRegionName, final String expectedRegionFullPath) { + assertNotNull(String.format("The '%1$s' was not properly configured and initialized!", expectedRegionName), actualRegion); assertEquals(expectedRegionName, actualRegion.getName()); assertEquals(expectedRegionFullPath, actualRegion.getFullPath()); + return actualRegion; } protected Region assertRegionAttributes(final Region actualRegion, final DataPolicy expectedDataPolicy, final Scope expectedScope) { + assertNotNull(actualRegion); assertNotNull(actualRegion.getAttributes()); assertEquals(expectedDataPolicy, actualRegion.getAttributes().getDataPolicy()); assertEquals(expectedScope, actualRegion.getAttributes().getScope()); + return actualRegion; } protected String getLocation(final String configLocation) { + String baseLocation = getClass().getPackage().getName().replace('.', File.separatorChar); + return baseLocation.concat(File.separator).concat(configLocation); } @@ -199,8 +219,11 @@ public class CacheClusterConfigurationIntegrationTest { } protected ConfigurableApplicationContext newApplicationContext(String... configLocations) { + ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext(configLocations); + applicationContext.registerShutdownHook(); + return applicationContext; } @@ -208,8 +231,9 @@ public class CacheClusterConfigurationIntegrationTest { @Ignore // TODO re-enable the test once the GemFire Cluster Configuration Service race condition has been properly fixed! public void clusterConfigurationTest() { - ConfigurableApplicationContext applicationContext = newApplicationContext( - getLocation("cacheUsingClusterConfigurationIntegrationTest.xml")); + + ConfigurableApplicationContext applicationContext = + newApplicationContext(getLocation("cacheUsingClusterConfigurationIntegrationTest.xml")); assertRegionAttributes(assertRegion(getRegion(applicationContext, "ClusterConfigRegion"), "ClusterConfigRegion"), DataPolicy.PARTITION, Scope.DISTRIBUTED_NO_ACK); @@ -229,7 +253,9 @@ public class CacheClusterConfigurationIntegrationTest { @Test public void localConfigurationTest() { + try { + newApplicationContext(getLocation("cacheUsingLocalOnlyConfigurationIntegrationTest.xml")); fail("Loading the 'cacheUsingLocalOnlyConfigurationIntegrationTest.xml' Spring ApplicationContext" diff --git a/src/test/java/org/springframework/data/gemfire/snapshot/SnapshotApplicationEventTriggeredImportsExportsIntegrationTest.java b/src/test/java/org/springframework/data/gemfire/snapshot/SnapshotApplicationEventTriggeredImportsExportsIntegrationTest.java index f894f73b..29f4bf40 100644 --- a/src/test/java/org/springframework/data/gemfire/snapshot/SnapshotApplicationEventTriggeredImportsExportsIntegrationTest.java +++ b/src/test/java/org/springframework/data/gemfire/snapshot/SnapshotApplicationEventTriggeredImportsExportsIntegrationTest.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.gemfire.snapshot; import static org.hamcrest.CoreMatchers.equalTo; @@ -26,12 +25,17 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; + import javax.annotation.Resource; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; + +import com.gemstone.gemfire.cache.Region; +import com.gemstone.gemfire.cache.snapshot.SnapshotFilter; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; import org.springframework.data.gemfire.repository.sample.Person; @@ -46,9 +50,6 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.util.Assert; import org.springframework.util.StringUtils; -import com.gemstone.gemfire.cache.Region; -import com.gemstone.gemfire.cache.snapshot.SnapshotFilter; - /** * The SnapshotApplicationEventTriggeredImportsExportsIntegrationTest class is a test suite of test cases testing * the effects of the SnapshotServiceFactoryBean using Spring ApplicationEvents to trigger imports and exports @@ -93,6 +94,7 @@ 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)); @@ -110,6 +112,7 @@ public class SnapshotApplicationEventTriggeredImportsExportsIntegrationTest { } protected void assertPeople(Region targetRegion, Person... people) { + assertThat(targetRegion.size(), is(equalTo(people.length))); for (Person person : people) { @@ -118,6 +121,7 @@ public class SnapshotApplicationEventTriggeredImportsExportsIntegrationTest { } protected void assertPerson(Person expectedPerson, Person actualPerson) { + assertThat(String.format("Expected (%1$s); but was (%2$s)", expectedPerson, actualPerson), actualPerson, is(notNullValue())); assertThat(actualPerson.getId(), is(equalTo(expectedPerson.getId()))); @@ -130,16 +134,22 @@ public class SnapshotApplicationEventTriggeredImportsExportsIntegrationTest { } 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); + + @Override + public boolean waiting() { + return doe.size() < expectedDoeSize + || everyoneElse.size() < expectedEveryoneSize + || handy.size() < expectedHandySize; } }); } @@ -147,6 +157,7 @@ public class SnapshotApplicationEventTriggeredImportsExportsIntegrationTest { @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")); @@ -171,7 +182,8 @@ public class SnapshotApplicationEventTriggeredImportsExportsIntegrationTest { Person jillHill = put(people, createPerson("Jill", "Hill")); eventPublisher.publishEvent(event); - wait(10, 5, 4, 3); + + wait(5, 5, 4, 3); assertPeople(doe, jonDoe, janeDoe, cookieDoe, pieDoe, sourDoe); assertPeople(everyoneElse, jackBlack, joeDirt, jackHill, jillHill); @@ -195,12 +207,16 @@ public class SnapshotApplicationEventTriggeredImportsExportsIntegrationTest { private final String lastName; public LastNameSnapshotFilter(String lastName) { + Assert.hasText(lastName, "'lastName' must be specified"); + this.lastName = lastName; } protected String getLastName() { + Assert.state(StringUtils.hasText(lastName), "'lastName' was not properly initialized"); + return lastName; } @@ -231,11 +247,13 @@ public class SnapshotApplicationEventTriggeredImportsExportsIntegrationTest { @Autowired private ApplicationEventPublisher eventPublisher; - private static final Map snapshotFileLastModifiedMap = new ConcurrentHashMap(2); + private static final Map snapshotFileLastModifiedMap = + new ConcurrentHashMap(2); @Scheduled(fixedDelay = 1000) @SuppressWarnings("unchecked") public void processSnapshots() { + boolean triggerEvent = false; for (File snapshotFile : nullSafeArray(snapshotsDirectory.listFiles(FileSystemUtils.FileOnlyFilter.INSTANCE))) { @@ -248,10 +266,11 @@ public class SnapshotApplicationEventTriggeredImportsExportsIntegrationTest { } protected File[] nullSafeArray(File... files) { - return (files != null ? files : new File[0]); + return files != null ? files : new File[0]; } protected boolean isUnprocessedSnapshotFile(File snapshotFile) { + Long lastModified = snapshotFile.lastModified(); Long previousLastModified = snapshotFileLastModifiedMap.get(snapshotFile); 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 01896958..aadff68d 100644 --- a/src/test/java/org/springframework/data/gemfire/snapshot/SnapshotServiceFactoryBeanTest.java +++ b/src/test/java/org/springframework/data/gemfire/snapshot/SnapshotServiceFactoryBeanTest.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.gemfire.snapshot; import static com.gemstone.gemfire.cache.snapshot.SnapshotOptions.SnapshotFormat; @@ -48,18 +47,12 @@ import java.io.File; import java.io.IOException; import java.util.Arrays; -import org.apache.commons.logging.Log; import org.junit.After; import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.mockito.Matchers; -import org.springframework.core.io.ClassPathResource; -import org.springframework.data.gemfire.snapshot.event.ExportSnapshotApplicationEvent; -import org.springframework.data.gemfire.snapshot.event.ImportSnapshotApplicationEvent; -import org.springframework.data.gemfire.snapshot.event.SnapshotApplicationEvent; -import org.springframework.data.gemfire.test.support.FileSystemUtils; import com.gemstone.gemfire.cache.Cache; import com.gemstone.gemfire.cache.Region; @@ -68,6 +61,14 @@ import com.gemstone.gemfire.cache.snapshot.RegionSnapshotService; import com.gemstone.gemfire.cache.snapshot.SnapshotFilter; import com.gemstone.gemfire.cache.snapshot.SnapshotOptions; +import org.apache.commons.logging.Log; + +import org.springframework.core.io.ClassPathResource; +import org.springframework.data.gemfire.snapshot.event.ExportSnapshotApplicationEvent; +import org.springframework.data.gemfire.snapshot.event.ImportSnapshotApplicationEvent; +import org.springframework.data.gemfire.snapshot.event.SnapshotApplicationEvent; +import org.springframework.data.gemfire.test.support.FileSystemUtils; + /** * The SnapshotServiceFactoryBeanTest class is a test suite of test cases testing the contract and functionality * of the SnapshotServiceFactoryBean class. @@ -92,10 +93,13 @@ public class SnapshotServiceFactoryBeanTest { 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; } @@ -117,6 +121,7 @@ public class SnapshotServiceFactoryBeanTest { protected SnapshotMetadata newSnapshotMetadata(File location, SnapshotFilter filter, SnapshotFormat format) { + return new SnapshotMetadata(location, filter, format); } @@ -125,6 +130,7 @@ public class SnapshotServiceFactoryBeanTest { } protected String toPathname(String... pathElements) { + StringBuilder pathname = new StringBuilder(); for (String pathElement : pathElements) { @@ -135,7 +141,7 @@ public class SnapshotServiceFactoryBeanTest { } @BeforeClass - public static void setupBeforeClass() throws Exception { + public static void setupBeforeClass() { snapshotDat = mockFile("snapshot.dat"); } @@ -173,12 +179,14 @@ public class SnapshotServiceFactoryBeanTest { @Test public void nullSafeIsFileWithFile() { + assertThat(SnapshotServiceFactoryBean.nullSafeIsFile(FileSystemUtils.JAVA_EXE), is(FileSystemUtils.JAVA_EXE.isFile())); } @Test public void nullSafeIsFileWithNonFiles() { + assertThat(SnapshotServiceFactoryBean.nullSafeIsFile(new File("/path/to/non-existing/file.ext")), is(false)); assertThat(SnapshotServiceFactoryBean.nullSafeIsFile(new File(System.getProperty("user.dir"))), is(false)); } @@ -202,6 +210,7 @@ public class SnapshotServiceFactoryBeanTest { @Test public void setAndGetCacheSuccessfully() { Cache mockCache = mock(Cache.class, "MockCache"); + SnapshotServiceFactoryBean factoryBean = new SnapshotServiceFactoryBean(); factoryBean.setCache(mockCache); @@ -298,7 +307,9 @@ public class SnapshotServiceFactoryBeanTest { "MockSnapshotServiceAdapter"); SnapshotServiceFactoryBean factoryBean = new SnapshotServiceFactoryBean() { - @Override protected SnapshotServiceAdapter create() { + + @Override + protected SnapshotServiceAdapter create() { return mockSnapshotService; } }; @@ -316,7 +327,9 @@ public class SnapshotServiceFactoryBeanTest { final SnapshotServiceAdapter mockSnapshotService = mock(SnapshotServiceAdapter.class, "MockSnapshotServiceAdapter"); SnapshotServiceFactoryBean factoryBean = new SnapshotServiceFactoryBean() { - @Override protected SnapshotServiceAdapter create() { + + @Override + protected SnapshotServiceAdapter create() { return mockSnapshotService; } }; @@ -332,6 +345,7 @@ public class SnapshotServiceFactoryBeanTest { @Test public void createCacheSnapshotService() { Cache mockCache = mock(Cache.class, "MockCache"); + CacheSnapshotService mockCacheSnapshotService = mock(CacheSnapshotService.class, "MockCacheSnapshotService"); when(mockCache.getSnapshotService()).thenReturn(mockCacheSnapshotService); @@ -350,6 +364,7 @@ public class SnapshotServiceFactoryBeanTest { @Test public void createRegionSnapshotService() { Region mockRegion = mock(Region.class, "MockRegion"); + RegionSnapshotService mockRegionSnapshotService = mock(RegionSnapshotService.class, "MockRegionSnapshotService"); when(mockRegion.getSnapshotService()).thenReturn(mockRegionSnapshotService); @@ -389,7 +404,9 @@ public class SnapshotServiceFactoryBeanTest { "MockSnapshotServiceAdapter"); SnapshotServiceFactoryBean factoryBean = new SnapshotServiceFactoryBean() { - @Override public SnapshotServiceAdapter getObject() throws Exception { + + @Override + public SnapshotServiceAdapter getObject() { return mockSnapshotService; } }; @@ -403,7 +420,8 @@ public class SnapshotServiceFactoryBeanTest { } @Test - public void onApplicationEventWhenMatchUsingEventSnapshotMetadataPerformsExport() throws Exception { + public void onApplicationEventWhenMatchUsingEventSnapshotMetadataPerformsExport() { + Region mockRegion = mock(Region.class, "MockRegion"); SnapshotApplicationEvent mockSnapshotEvent = mock(ExportSnapshotApplicationEvent.class, @@ -418,7 +436,9 @@ public class SnapshotServiceFactoryBeanTest { when(mockSnapshotEvent.getSnapshotMetadata()).thenReturn(toArray(eventSnapshotMetadata)); SnapshotServiceFactoryBean factoryBean = new SnapshotServiceFactoryBean() { - @Override public SnapshotServiceAdapter getObject() throws Exception { + + @Override + public SnapshotServiceAdapter getObject() { return mockSnapshotService; } }; @@ -438,7 +458,8 @@ public class SnapshotServiceFactoryBeanTest { } @Test - public void onApplicationEventWhenMatchUsingFactorySnapshotMetadataPerformsImport() throws Exception { + public void onApplicationEventWhenMatchUsingFactorySnapshotMetadataPerformsImport() { + SnapshotApplicationEvent mockSnapshotEvent = mock(ImportSnapshotApplicationEvent.class, "MockImportSnapshotApplicationEvent"); @@ -451,7 +472,9 @@ public class SnapshotServiceFactoryBeanTest { when(mockSnapshotEvent.getSnapshotMetadata()).thenReturn(null); SnapshotServiceFactoryBean factoryBean = new SnapshotServiceFactoryBean() { - @Override public SnapshotServiceAdapter getObject() throws Exception { + + @Override + public SnapshotServiceAdapter getObject() { return mockSnapshotService; } }; @@ -470,7 +493,8 @@ public class SnapshotServiceFactoryBeanTest { } @Test - public void onApplicationEventWhenNoMatchDoesNotPerformExport() throws Exception { + public void onApplicationEventWhenNoMatchDoesNotPerformExport() { + SnapshotApplicationEvent mockSnapshotEvent = mock(ExportSnapshotApplicationEvent.class, "MockExportSnapshotApplicationEvent"); @@ -481,7 +505,9 @@ public class SnapshotServiceFactoryBeanTest { final SnapshotServiceAdapter mockSnapshotService = mock(SnapshotServiceAdapter.class, "MockSnapshotServiceAdapter"); SnapshotServiceFactoryBean factoryBean = new SnapshotServiceFactoryBean() { - @Override public SnapshotServiceAdapter getObject() throws Exception { + + @Override + public SnapshotServiceAdapter getObject() { return mockSnapshotService; } }; @@ -500,7 +526,8 @@ public class SnapshotServiceFactoryBeanTest { } @Test - public void onApplicationEventWhenNoMatchDoesNotPerformImport() throws Exception { + public void onApplicationEventWhenNoMatchDoesNotPerformImport() { + Region mockRegion = mock(Region.class, "MockRegion"); SnapshotApplicationEvent mockSnapshotEvent = mock(ImportSnapshotApplicationEvent.class, @@ -513,7 +540,9 @@ public class SnapshotServiceFactoryBeanTest { when(mockSnapshotEvent.getSnapshotMetadata()).thenReturn(null); SnapshotServiceFactoryBean factoryBean = new SnapshotServiceFactoryBean() { - @Override public SnapshotServiceAdapter getObject() throws Exception { + + @Override + public SnapshotServiceAdapter getObject() { return mockSnapshotService; } }; @@ -534,6 +563,7 @@ public class SnapshotServiceFactoryBeanTest { @Test public void resolveSnapshotMetadataFromEvent() { + SnapshotMetadata eventSnapshotMetadata = newSnapshotMetadata(snapshotDat); SnapshotMetadata factoryExportSnapshotMetadata = newSnapshotMetadata(); SnapshotMetadata factoryImportSnapshotMetadata = newSnapshotMetadata(FileSystemUtils.USER_HOME); @@ -555,6 +585,7 @@ public class SnapshotServiceFactoryBeanTest { @Test public void resolveExportSnapshotMetadataFromFactory() { + SnapshotMetadata factoryExportSnapshotMetadata = newSnapshotMetadata(); SnapshotMetadata factoryImportSnapshotMetadata = newSnapshotMetadata(FileSystemUtils.USER_HOME); @@ -575,6 +606,7 @@ public class SnapshotServiceFactoryBeanTest { @Test public void resolveImportSnapshotMetadataFromFactory() { + SnapshotMetadata factoryExportSnapshotMetadata = newSnapshotMetadata(); SnapshotMetadata factoryImportSnapshotMetadata = newSnapshotMetadata(FileSystemUtils.USER_HOME); @@ -595,6 +627,7 @@ public class SnapshotServiceFactoryBeanTest { @Test public void withCacheBasedSnapshotServiceOnCacheSnapshotEventIsMatch() { + SnapshotApplicationEvent mockSnapshotEvent = mock(SnapshotApplicationEvent.class, "MockSnapshotApplicationEvent"); when(mockSnapshotEvent.isCacheSnapshotEvent()).thenReturn(true); @@ -608,6 +641,7 @@ public class SnapshotServiceFactoryBeanTest { @Test public void withCacheBasedSnapshotServiceOnRegionSnapshotEventIsNotAMatch() { + SnapshotApplicationEvent mockSnapshotEvent = mock(SnapshotApplicationEvent.class, "MockSnapshotApplicationEvent"); when(mockSnapshotEvent.isCacheSnapshotEvent()).thenReturn(false); @@ -622,6 +656,7 @@ public class SnapshotServiceFactoryBeanTest { @Test public void withRegionBasedSnapshotServiceOnCacheSnapshotEventIsMatch() { + SnapshotApplicationEvent mockSnapshotEvent = mock(SnapshotApplicationEvent.class, "MockSnapshotApplicationEvent"); when(mockSnapshotEvent.isCacheSnapshotEvent()).thenReturn(true); @@ -637,6 +672,7 @@ public class SnapshotServiceFactoryBeanTest { @Test public void withRegionBasedSnapshotServiceOnRegionSnapshotEventIsMatch() { + Region mockRegion = mock(Region.class, "MockRegion"); SnapshotApplicationEvent mockSnapshotEvent = mock(SnapshotApplicationEvent.class, "MockSnapshotApplicationEvent"); @@ -655,6 +691,7 @@ public class SnapshotServiceFactoryBeanTest { @Test public void importCacheSnapshotOnInitialization() throws Exception { + Cache mockCache = mock(Cache.class, "MockCache"); CacheSnapshotService mockCacheSnapshotService = mock(CacheSnapshotService.class, "MockCacheSnapshotService"); @@ -705,6 +742,7 @@ public class SnapshotServiceFactoryBeanTest { @Test @SuppressWarnings("unchecked") public void importRegionSnapshotOnInitialization() throws Exception { + Cache mockCache = mock(Cache.class, "MockCache"); Region mockRegion = mock(Region.class, "MockRegion"); @@ -758,6 +796,7 @@ public class SnapshotServiceFactoryBeanTest { @Test public void exportCacheSnapshotOnDestroy() throws Exception { + Cache mockCache = mock(Cache.class, "MockCache"); CacheSnapshotService mockCacheSnapshotService = mock(CacheSnapshotService.class, "MockCacheSnapshotService"); @@ -800,6 +839,7 @@ public class SnapshotServiceFactoryBeanTest { @Test public void exportRegionSnapshotOnDestroy() throws Exception { + Cache mockCache = mock(Cache.class, "MockCache"); Region mockRegion = mock(Region.class, "MockRegion"); @@ -846,6 +886,7 @@ public class SnapshotServiceFactoryBeanTest { @Test public void createOptionsWithFilterOnSnapshotServiceAdapterSupport() { + SnapshotFilter mockSnapshotFilter = mock(SnapshotFilter.class, "MockSnapshotFilter"); final SnapshotOptions mockSnapshotOptions = mock(SnapshotOptions.class, "MockSnapshotOptions"); @@ -853,7 +894,9 @@ public class SnapshotServiceFactoryBeanTest { when(mockSnapshotOptions.setFilter(any(SnapshotFilter.class))).thenReturn(mockSnapshotOptions); TestSnapshotServiceAdapter snapshotService = new TestSnapshotServiceAdapter() { - @Override public SnapshotOptions createOptions() { + + @Override + public SnapshotOptions createOptions() { return mockSnapshotOptions; } }; @@ -867,7 +910,7 @@ public class SnapshotServiceFactoryBeanTest { public void invokeExceptionSuppressingCloseOnSnapshotServiceAdapterSupportIsSuccessful() throws Exception { Closeable mockCloseable = mock(Closeable.class, "MockCloseable"); - assertThat(new TestSnapshotServiceAdapter().exceptionSuppressingClose(mockCloseable), is(true)); + assertThat(new SnapshotServiceFactoryBeanIntegrationTest.TestSnapshotServiceAdapter().exceptionSuppressingClose(mockCloseable), is(true)); verify(mockCloseable, times(1)).close(); } @@ -878,7 +921,7 @@ public class SnapshotServiceFactoryBeanTest { doThrow(new IOException("TEST")).when(mockCloseable).close(); - assertThat(new TestSnapshotServiceAdapter().exceptionSuppressingClose(mockCloseable), is(false)); + assertThat(new SnapshotServiceFactoryBeanIntegrationTest.TestSnapshotServiceAdapter().exceptionSuppressingClose(mockCloseable), is(false)); verify(mockCloseable, times(1)).close(); } @@ -890,7 +933,9 @@ public class SnapshotServiceFactoryBeanTest { when(mockLog.isDebugEnabled()).thenReturn(true); TestSnapshotServiceAdapter snapshotService = new TestSnapshotServiceAdapter() { - @Override Log createLog() { + + @Override + Log createLog() { return mockLog; } }; @@ -905,12 +950,15 @@ public class SnapshotServiceFactoryBeanTest { @Test public void logDebugWhenNotDebugging() { + final Log mockLog = mock(Log.class, "MockLog"); when(mockLog.isDebugEnabled()).thenReturn(false); TestSnapshotServiceAdapter snapshotService = new TestSnapshotServiceAdapter() { - @Override Log createLog() { + + @Override + Log createLog() { return mockLog; } }; @@ -923,6 +971,7 @@ public class SnapshotServiceFactoryBeanTest { @Test public void toSimpleFilenameUsingVariousPathnames() { + TestSnapshotServiceAdapter snapshotService = new TestSnapshotServiceAdapter(); assertThat(snapshotService.toSimpleFilename(toPathname("path", "to", "file.ext")), is(equalTo("file.ext"))); @@ -937,6 +986,7 @@ public class SnapshotServiceFactoryBeanTest { @Test(expected = ImportSnapshotException.class) public void loadCacheSnapshotWithDirectoryAndFormatHandlesExceptionAppropriately() throws Exception { + CacheSnapshotService mockCacheSnapshotService = mock(CacheSnapshotService.class, "MockCacheSnapshotService"); doThrow(new IOException("TEST")).when(mockCacheSnapshotService).load(any(File.class), any(SnapshotFormat.class)); @@ -949,11 +999,13 @@ public class SnapshotServiceFactoryBeanTest { adapter.load(FileSystemUtils.WORKING_DIRECTORY, SnapshotFormat.GEMFIRE); } catch (ImportSnapshotException expected) { + assertThat(expected.getMessage(), is(equalTo(String.format( "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"))); + throw expected; } finally { @@ -964,6 +1016,7 @@ public class SnapshotServiceFactoryBeanTest { @Test(expected = ImportSnapshotException.class) public void loadCacheSnapshotWithFormatOptionsAndSnapshotFilesHandlesExceptionAppropriately() throws Exception { + SnapshotOptions mockSnapshotOptions = mock(SnapshotOptions.class, "MockSnapshotOptions"); CacheSnapshotService mockCacheSnapshotService = mock(CacheSnapshotService.class, "MockCacheSnapshotService"); @@ -979,11 +1032,13 @@ public class SnapshotServiceFactoryBeanTest { adapter.load(SnapshotFormat.GEMFIRE, mockSnapshotOptions, snapshotDat); } catch (ImportSnapshotException expected) { + assertThat(expected.getMessage(), is(equalTo(String.format( "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"))); + throw expected; } finally { @@ -994,6 +1049,7 @@ public class SnapshotServiceFactoryBeanTest { @Test(expected = ExportSnapshotException.class) public void saveCacheSnapshotWithDirectoryAndFormatHandlesExceptionAppropriately() throws Exception { + CacheSnapshotService mockCacheSnapshotService = mock(CacheSnapshotService.class, "MockCacheSnapshotService"); doThrow(new IOException("TEST")).when(mockCacheSnapshotService).save(any(File.class), any(SnapshotFormat.class)); @@ -1006,11 +1062,13 @@ public class SnapshotServiceFactoryBeanTest { adapter.save(FileSystemUtils.WORKING_DIRECTORY, SnapshotFormat.GEMFIRE); } catch (ExportSnapshotException expected) { + assertThat(expected.getMessage(), is(equalTo(String.format( "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"))); + throw expected; } finally { @@ -1036,11 +1094,13 @@ public class SnapshotServiceFactoryBeanTest { adapter.save(FileSystemUtils.USER_HOME, SnapshotFormat.GEMFIRE, mockSnapshotOptions); } 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)", FileSystemUtils.USER_HOME, mockSnapshotOptions)))); assertThat(expected.getCause(), is(instanceOf(ClassCastException.class))); assertThat(expected.getCause().getMessage(), is(equalTo("TEST"))); + throw expected; } finally { @@ -1064,10 +1124,12 @@ public class SnapshotServiceFactoryBeanTest { adapter.load(snapshotDat, SnapshotFormat.GEMFIRE); } catch (ImportSnapshotException expected) { + assertThat(expected.getMessage(), is(equalTo(String.format( "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; } finally { @@ -1092,11 +1154,13 @@ public class SnapshotServiceFactoryBeanTest { adapter.load(SnapshotFormat.GEMFIRE, mockSnapshotOptions, snapshotDat); } catch (ImportSnapshotException expected) { + assertThat(expected.getMessage(), is(equalTo(String.format( "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"))); + throw expected; } finally { @@ -1120,10 +1184,12 @@ public class SnapshotServiceFactoryBeanTest { adapter.save(snapshotDat, SnapshotFormat.GEMFIRE); } catch (ExportSnapshotException expected) { + assertThat(expected.getMessage(), is(equalTo(String.format( "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; } finally { @@ -1163,9 +1229,11 @@ public class SnapshotServiceFactoryBeanTest { @Test public void createSnapshotMetadataWithNullLocation() { + expectedException.expect(IllegalArgumentException.class); expectedException.expectCause(is(nullValue(Throwable.class))); expectedException.expectMessage("Location must not be null"); + new SnapshotMetadata(null, mock(SnapshotFilter.class), SnapshotFormat.GEMFIRE); } @@ -1185,6 +1253,7 @@ public class SnapshotServiceFactoryBeanTest { @Test public void createSnapshotMetadataWithFileNullFilterAndGemFireFormat() throws Exception { + SnapshotMetadata snapshotMetadata = new SnapshotMetadata(snapshotDat, null, SnapshotFormat.GEMFIRE); assertThat(snapshotMetadata.getLocation(), is(equalTo(snapshotDat))); @@ -1197,6 +1266,7 @@ public class SnapshotServiceFactoryBeanTest { @Test public void isJarFileIsTrue() { + // JRE File runtimeDotJar = new File(new File(FileSystemUtils.JAVA_HOME, "lib"), "rt.jar"); diff --git a/src/test/java/org/springframework/data/gemfire/test/support/StackTraceUtils.java b/src/test/java/org/springframework/data/gemfire/test/support/StackTraceUtils.java index 856330de..ae08f1d0 100644 --- a/src/test/java/org/springframework/data/gemfire/test/support/StackTraceUtils.java +++ b/src/test/java/org/springframework/data/gemfire/test/support/StackTraceUtils.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.gemfire.test.support; /** @@ -49,6 +48,7 @@ public abstract class StackTraceUtils extends ThreadUtils { } public static StackTraceElement getTestCaller(final Thread thread) { + for (StackTraceElement stackTraceElement : thread.getStackTrace()) { if (isTestSuiteClass(stackTraceElement) && isTestCaseMethod(stackTraceElement)) { return stackTraceElement; @@ -59,6 +59,7 @@ public abstract class StackTraceUtils extends ThreadUtils { } private static boolean isTestCaseMethod(final StackTraceElement element) { + boolean result = element.getMethodName().toLowerCase().startsWith("test"); try { @@ -71,9 +72,11 @@ public abstract class StackTraceUtils extends ThreadUtils { } private static boolean isTestSuiteClass(final StackTraceElement element) { + boolean result = element.getClass().getSimpleName().toLowerCase().endsWith("test"); + result |= element.getClass().isAssignableFrom(junit.framework.TestCase.class); + return result; } - } diff --git a/src/test/java/org/springframework/data/gemfire/test/support/ThreadUtils.java b/src/test/java/org/springframework/data/gemfire/test/support/ThreadUtils.java index 3358423e..1beb3aa8 100644 --- a/src/test/java/org/springframework/data/gemfire/test/support/ThreadUtils.java +++ b/src/test/java/org/springframework/data/gemfire/test/support/ThreadUtils.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.gemfire.test.support; import java.util.concurrent.TimeUnit; @@ -30,6 +29,7 @@ public abstract class ThreadUtils { /* (non-Javadoc) */ public static boolean sleep(long milliseconds) { + try { Thread.sleep(milliseconds); return true; @@ -45,8 +45,11 @@ public abstract class ThreadUtils { } public static boolean timedWait(long duration, long interval) { + return timedWait(duration, interval, new WaitCondition() { - @Override public boolean waiting() { + + @Override + public boolean waiting() { return true; } }); diff --git a/src/test/java/org/springframework/data/gemfire/test/support/ThrowableUtils.java b/src/test/java/org/springframework/data/gemfire/test/support/ThrowableUtils.java index 7e4a3399..257d8cb5 100644 --- a/src/test/java/org/springframework/data/gemfire/test/support/ThrowableUtils.java +++ b/src/test/java/org/springframework/data/gemfire/test/support/ThrowableUtils.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.gemfire.test.support; import java.io.PrintWriter; @@ -31,10 +30,12 @@ import java.io.StringWriter; @SuppressWarnings("unused") public abstract class ThrowableUtils { - public static String toString(final Throwable t) { + public static String toString(Throwable cause) { + StringWriter writer = new StringWriter(); - t.printStackTrace(new PrintWriter(writer)); + + cause.printStackTrace(new PrintWriter(writer)); + return writer.toString(); } - }