Merge with 2.0.0-m3

This commit is contained in:
René Pfeuffer
2018-12-20 10:44:44 +01:00
28 changed files with 313 additions and 146 deletions

View File

@@ -35,55 +35,33 @@ package sonia.scm.store;
//~--- non-JDK imports --------------------------------------------------------
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* In memory configuration store factory for testing purposes.
*
* Use {@link #create()} to get a store that creates the same store on each request.
*
* @author Sebastian Sdorra
*/
public class InMemoryConfigurationStoreFactory implements ConfigurationStoreFactory {
private static final Map<Key, InMemoryConfigurationStore> STORES = new HashMap<>();
private ConfigurationStore store;
public static ConfigurationStoreFactory create() {
return new InMemoryConfigurationStoreFactory(new InMemoryConfigurationStore());
}
public InMemoryConfigurationStoreFactory() {
}
public InMemoryConfigurationStoreFactory(ConfigurationStore store) {
this.store = store;
}
@Override
public ConfigurationStore getStore(TypedStoreParameters storeParameters) {
Key key = new Key(storeParameters.getType(), storeParameters.getName(), storeParameters.getRepository() == null? "-": storeParameters.getRepository().getId());
if (STORES.containsKey(key)) {
return STORES.get(key);
} else {
InMemoryConfigurationStore<Object> store = new InMemoryConfigurationStore<>();
STORES.put(key, store);
if (store != null) {
return store;
}
}
private static class Key {
private final Class type;
private final String name;
private final String id;
public Key(Class type, String name, String id) {
this.type = type;
this.name = name;
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Key key = (Key) o;
return Objects.equals(type, key.type) &&
Objects.equals(name, key.name) &&
Objects.equals(id, key.id);
}
@Override
public int hashCode() {
return Objects.hash(type, name, id);
}
return new InMemoryConfigurationStore<>();
}
}

View File

@@ -0,0 +1,53 @@
package sonia.scm.store;
import sonia.scm.security.KeyGenerator;
import sonia.scm.security.UUIDKeyGenerator;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* In memory store implementation of {@link DataStore}.
*
* @author Sebastian Sdorra
*
* @param <T> type of stored object
*/
public class InMemoryDataStore<T> implements DataStore<T> {
private final Map<String, T> store = new HashMap<>();
private KeyGenerator generator = new UUIDKeyGenerator();
@Override
public String put(T item) {
String key = generator.createKey();
store.put(key, item);
return key;
}
@Override
public void put(String id, T item) {
store.put(id, item);
}
@Override
public Map<String, T> getAll() {
return Collections.unmodifiableMap(store);
}
@Override
public void clear() {
store.clear();
}
@Override
public void remove(String id) {
store.remove(id);
}
@Override
public T get(String id) {
return store.get(id);
}
}

View File

@@ -0,0 +1,26 @@
package sonia.scm.store;
/**
* In memory configuration store factory for testing purposes.
*
* @author Sebastian Sdorra
*/
public class InMemoryDataStoreFactory implements DataStoreFactory {
private InMemoryDataStore store;
public InMemoryDataStoreFactory() {
}
public InMemoryDataStoreFactory(InMemoryDataStore store) {
this.store = store;
}
@Override
public <T> DataStore<T> getStore(TypedStoreParameters<T> storeParameters) {
if (store != null) {
return store;
}
return new InMemoryDataStore<>();
}
}