diff --git a/gs-test-core/src/main/java/com/avanza/gs/test/InMemoryGigaSpacesManager.java b/gs-test-core/src/main/java/com/avanza/gs/test/InMemoryGigaSpacesManager.java
index 21f0189..bcaac6d 100644
--- a/gs-test-core/src/main/java/com/avanza/gs/test/InMemoryGigaSpacesManager.java
+++ b/gs-test-core/src/main/java/com/avanza/gs/test/InMemoryGigaSpacesManager.java
@@ -17,11 +17,18 @@
import static com.gigaspaces.start.SystemInfo.LOOKUP_LOCATORS_SYS_PROP;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.file.Files;
import java.nio.file.Path;
+import java.util.Comparator;
+import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.gigaspaces.CommonSystemProperties;
+
/**
* This is intended to simulate an in-memory version of GigaSpaces Manager, and starts the following components:
*
@@ -41,8 +48,10 @@ final class InMemoryGigaSpacesManager implements AutoCloseable {
private final InMemoryZooKeeper zooKeeper;
private final InMemoryLus lus;
+ private final Path gsHome;
public InMemoryGigaSpacesManager() {
+ this.gsHome = setupGsHome();
if (shouldStartZooKeeper()) {
this.zooKeeper = new InMemoryZooKeeper();
setGigaSpacesManagerProperties(zooKeeper.getZooKeeperConfig());
@@ -60,6 +69,22 @@ private static boolean shouldStartZooKeeper() {
return !Boolean.getBoolean(DISABLE_ZOOKEEPER_PROPERTY);
}
+ /**
+ * Sets up temporary GS_HOME in order to avoid files being created in project directory during tests.
+ */
+ private Path setupGsHome() {
+ if (System.getProperty(CommonSystemProperties.GS_HOME) != null) {
+ return null;
+ }
+ try {
+ Path tmpPath = Files.createTempDirectory(getClass().getSimpleName());
+ System.setProperty(CommonSystemProperties.GS_HOME, tmpPath.toString());
+ return tmpPath;
+ } catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
+ }
+
private static void setGigaSpacesManagerProperties(Path zooKeeperConfig) {
// Set manager to be available with ZooKeeper on localhost
System.setProperty("com.gs.manager.servers", "localhost");
@@ -94,5 +119,20 @@ public void close() {
LOG.warn("Error while closing ZooKeeper server", e);
}
}
+ // Delete temporary GS_HOME
+ if (gsHome != null) {
+ try (Stream s = Files.walk(gsHome)) {
+ s.sorted(Comparator.reverseOrder())
+ .forEach(p -> {
+ try {
+ Files.delete(p);
+ } catch (IOException e) {
+ throw new UncheckedIOException("Failed to delete " + p, e);
+ }
+ });
+ } catch (Exception e) {
+ LOG.warn("Failed deleting GS_HOME at {}", gsHome, e);
+ }
+ }
}
}