mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-02-03 22:11:52 +01:00
Tests attempting to solve #655
This commit is contained in:
parent
abeb841378
commit
68a2947f70
@ -30,6 +30,9 @@ public class DataContainer extends HashMap<Key, Supplier> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public <T> void putSupplier(Key<T> key, Supplier<T> supplier) {
|
public <T> void putSupplier(Key<T> key, Supplier<T> supplier) {
|
||||||
|
if (supplier == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
super.put(key, new CachingSupplier<>(supplier));
|
super.put(key, new CachingSupplier<>(supplier));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,7 +76,7 @@ public class DataContainer extends HashMap<Key, Supplier> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public <T> T getUnsafe(Key<T> key) {
|
public <T> T getUnsafe(Key<T> key) {
|
||||||
Supplier supplier = get(key);
|
Supplier supplier = super.get(key);
|
||||||
if (supplier == null) {
|
if (supplier == null) {
|
||||||
throw new IllegalArgumentException("Unsupported Key: " + key.getKeyName());
|
throw new IllegalArgumentException("Unsupported Key: " + key.getKeyName());
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.djrapitops.plan.data.container;
|
||||||
|
|
||||||
|
import com.djrapitops.plan.data.store.keys.SessionKeys;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link Session} {@link com.djrapitops.plan.data.store.containers.DataContainer}.
|
||||||
|
*
|
||||||
|
* @author Rsl1122
|
||||||
|
*/
|
||||||
|
public class SessionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void safeStartKeyConstructor() {
|
||||||
|
for (int i = 0; i < 10000; i++) {
|
||||||
|
Session session = new Session(null, System.currentTimeMillis(), null, null);
|
||||||
|
|
||||||
|
// Should not throw
|
||||||
|
session.getUnsafe(SessionKeys.START);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void safeStartKeyDBConstructor() {
|
||||||
|
for (int i = 0; i < 10000; i++) {
|
||||||
|
long time = System.currentTimeMillis();
|
||||||
|
Session session = new Session(-1, null, null, time, time + 1, 0, 0, 0);
|
||||||
|
|
||||||
|
// Should not throw
|
||||||
|
session.getUnsafe(SessionKeys.START);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,88 @@
|
|||||||
|
package com.djrapitops.plan.data.store.containers;
|
||||||
|
|
||||||
|
import com.djrapitops.plan.data.store.Key;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link DataContainer} programming errors.
|
||||||
|
*
|
||||||
|
* @author Rsl1122
|
||||||
|
*/
|
||||||
|
public class DataContainerTest {
|
||||||
|
|
||||||
|
private static final Key<String> TEST_KEY = new Key<>(String.class, "TEST_KEY");
|
||||||
|
private static final Key<String> TEST_KEY_COPY = new Key<>(String.class, "TEST_KEY");
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void safeUnsafeKeySupplierSameObject() {
|
||||||
|
DataContainer container = new DataContainer();
|
||||||
|
container.putSupplier(TEST_KEY, () -> "Success");
|
||||||
|
|
||||||
|
// Test twice for CachingSupplier
|
||||||
|
assertEquals("Success", container.getUnsafe(TEST_KEY));
|
||||||
|
assertEquals("Success", container.getUnsafe(TEST_KEY));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void safeUnsafeKeySupplierDifferentObject() {
|
||||||
|
DataContainer container = new DataContainer();
|
||||||
|
container.putSupplier(TEST_KEY, () -> "Success");
|
||||||
|
|
||||||
|
// Test twice for CachingSupplier
|
||||||
|
assertEquals("Success", container.getUnsafe(TEST_KEY_COPY));
|
||||||
|
assertEquals("Success", container.getUnsafe(TEST_KEY_COPY));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void safeUnsafeKeyRawSameObject() {
|
||||||
|
DataContainer container = new DataContainer();
|
||||||
|
container.putRawData(TEST_KEY, "Success");
|
||||||
|
|
||||||
|
// Test twice for CachingSupplier
|
||||||
|
assertEquals("Success", container.getUnsafe(TEST_KEY));
|
||||||
|
assertEquals("Success", container.getUnsafe(TEST_KEY));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void safeUnsafeKeyRawDifferentObject() {
|
||||||
|
DataContainer container = new DataContainer();
|
||||||
|
container.putRawData(TEST_KEY, "Success");
|
||||||
|
|
||||||
|
// Test twice for CachingSupplier
|
||||||
|
assertEquals("Success", container.getUnsafe(TEST_KEY_COPY));
|
||||||
|
assertEquals("Success", container.getUnsafe(TEST_KEY_COPY));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void safeUnsafeKeyRawNull() {
|
||||||
|
DataContainer container = new DataContainer();
|
||||||
|
container.putRawData(TEST_KEY, null);
|
||||||
|
|
||||||
|
// Test twice for CachingSupplier
|
||||||
|
assertTrue(container.supports(TEST_KEY));
|
||||||
|
assertNull(container.getUnsafe(TEST_KEY));
|
||||||
|
assertNull(container.getUnsafe(TEST_KEY));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void safeUnsafeKeyNullSupplier() {
|
||||||
|
DataContainer container = new DataContainer();
|
||||||
|
container.putSupplier(TEST_KEY, null);
|
||||||
|
|
||||||
|
assertFalse(container.supports(TEST_KEY));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void safeUnsafeKeySupplierNull() {
|
||||||
|
DataContainer container = new DataContainer();
|
||||||
|
container.putSupplier(TEST_KEY, () -> null);
|
||||||
|
|
||||||
|
// Test twice for CachingSupplier
|
||||||
|
assertTrue(container.supports(TEST_KEY));
|
||||||
|
assertNull(container.getUnsafe(TEST_KEY));
|
||||||
|
assertNull(container.getUnsafe(TEST_KEY));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user