Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dependencies {
// Use JUnit Jupiter for testing.
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2'
testImplementation 'org.mockito:mockito-inline:4.8.0'
testImplementation 'com.github.tomakehurst:wiremock-jre8:2.35.0'

implementation 'org.apache.commons:commons-math3:3.6.1'
implementation 'com.google.guava:guava:33.3.1-jre'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public class NativeJavaGbFeatureRepository implements IGBFeaturesRepository {
/**
* Flag to know whether GBFeatureRepository is initialized
*/
@Getter
private final AtomicBoolean initialized = new AtomicBoolean(false);

/**
Expand Down
105 changes: 105 additions & 0 deletions lib/src/test/java/growthbook/sdk/java/CacheManagerFactoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package growthbook.sdk.java;

import growthbook.sdk.java.sandbox.CacheManagerFactory;
import growthbook.sdk.java.sandbox.CacheMode;
import growthbook.sdk.java.sandbox.FileCachingManagerImpl;
import growthbook.sdk.java.sandbox.GbCacheManager;
import growthbook.sdk.java.sandbox.InMemoryCachingManagerImpl;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;

import static org.junit.jupiter.api.Assertions.*;

class CacheManagerFactoryTest {

@TempDir
File tempDir;

@Test
void noneMode_returnsNull() {
GbCacheManager result = CacheManagerFactory.create(CacheMode.NONE, null);
assertNull(result);
}

@Test
void memoryMode_returnsInMemoryManager() {
GbCacheManager result = CacheManagerFactory.create(CacheMode.MEMORY, null);
assertInstanceOf(InMemoryCachingManagerImpl.class, result);
}

@Test
void memoryMode_explicitDirIsIgnored() {
GbCacheManager result = CacheManagerFactory.create(CacheMode.MEMORY, tempDir.getAbsolutePath());
assertInstanceOf(InMemoryCachingManagerImpl.class, result);
}

@Test
void fileMode_withWritableDir_returnsFileCachingManager() {
GbCacheManager result = CacheManagerFactory.create(CacheMode.FILE, tempDir.getAbsolutePath());
assertInstanceOf(FileCachingManagerImpl.class, result);
}

@Test
void fileMode_withUnusableDir_returnsNonNullManager() {
// A path rooted under a plain file cannot be used as a cache directory.
// The factory will try system fallback locations (tmpdir, XDG, ~/Library/Caches…)
// before falling back to InMemoryCachingManagerImpl — so we only assert non-null.
File plainFile = new File(tempDir, "notadir.txt");
try { plainFile.createNewFile(); } catch (Exception ignored) {}
String impossiblePath = plainFile.getAbsolutePath() + File.separator + "sub";

GbCacheManager result = CacheManagerFactory.create(CacheMode.FILE, impossiblePath);
assertNotNull(result);
}

@Test
void fileMode_withNullDir_usesSystemFallbackOrMemory() {
// Should never throw; returns either File or Memory manager.
GbCacheManager result = CacheManagerFactory.create(CacheMode.FILE, null);
assertNotNull(result);
assertTrue(result instanceof FileCachingManagerImpl || result instanceof InMemoryCachingManagerImpl);
}

@Test
void autoMode_withWritableDir_returnsFileCachingManager() {
GbCacheManager result = CacheManagerFactory.create(CacheMode.AUTO, tempDir.getAbsolutePath());
assertInstanceOf(FileCachingManagerImpl.class, result);
}

@Test
void autoMode_withUnusableDir_returnsNonNullManager() {
File plainFile = new File(tempDir, "notadir2.txt");
try { plainFile.createNewFile(); } catch (Exception ignored) {}
String impossiblePath = plainFile.getAbsolutePath() + File.separator + "sub";

// The factory tries multiple system fallback locations before giving up.
GbCacheManager result = CacheManagerFactory.create(CacheMode.AUTO, impossiblePath);
assertNotNull(result);
}

@Test
void autoMode_withNullDir_returnsNonNullManager() {
GbCacheManager result = CacheManagerFactory.create(CacheMode.AUTO, null);
assertNotNull(result);
}

@Test
void fileManager_createdByFactory_canSaveAndLoad() {
GbCacheManager manager = CacheManagerFactory.create(CacheMode.FILE, tempDir.getAbsolutePath());
assertInstanceOf(FileCachingManagerImpl.class, manager);

manager.saveContent("smoke.json", "{\"flag\":true}");
assertEquals("{\"flag\":true}", manager.loadCache("smoke.json"));
}

@Test
void memoryManager_createdByFactory_canSaveAndLoad() {
GbCacheManager manager = CacheManagerFactory.create(CacheMode.MEMORY, null);

assertNotNull(manager);
manager.saveContent("key", "value");
assertEquals("value", manager.loadCache("key"));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package growthbook.sdk.java;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -126,4 +127,41 @@ void shouldHandleMultipleFilesSeparately() {
assertEquals("Content 1", fileCachingManagerImpl.loadCache("file1.txt"));
assertEquals("Content 2", fileCachingManagerImpl.loadCache("file2.txt"));
}

@Test
void shouldClearAllCachedFiles() {
fileCachingManagerImpl.saveContent("a.txt", "aaa");
fileCachingManagerImpl.saveContent("b.txt", "bbb");

fileCachingManagerImpl.clearCache();

Assertions.assertNull(fileCachingManagerImpl.loadCache("a.txt"));
Assertions.assertNull(fileCachingManagerImpl.loadCache("b.txt"));
assertEquals(0, tempDir.listFiles().length);
}

@Test
void shouldCreateCacheDirIfNotExists() {
File subDir = new File(tempDir, "nested/cache");
assertFalse(subDir.exists());

FileCachingManagerImpl manager = new FileCachingManagerImpl(subDir.getAbsolutePath());
manager.saveContent("x.txt", "hello");

assertEquals("hello", manager.loadCache("x.txt"));
}

@Test
void shouldThrowWhenCacheDirectoryCannotBeCreated() {
File file = new File(tempDir, "notadir.txt");
try {
file.createNewFile();
} catch (IOException e) {
fail("Precondition setup failed");
}
// A path *inside* a plain file cannot be a directory.
String impossiblePath = file.getAbsolutePath() + File.separator + "subdir";

assertThrows(RuntimeException.class, () -> new FileCachingManagerImpl(impossiblePath));
}
}
Loading
Loading