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 ceca73b..239a06e 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
@@ -15,11 +15,18 @@
*/
package com.avanza.gs.test;
+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:
*
@@ -39,8 +46,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());
@@ -58,6 +67,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");
@@ -82,5 +107,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);
+ }
+ }
}
}