Last active
May 12, 2023 18:16
-
-
Save falkoschumann/d03a2af48012cc6ca54d74da0fc01a41 to your computer and use it in GitHub Desktop.
Java test helper classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.Queue; | |
public class ConfigurableResponses<T> { | |
private final Object response; | |
private ConfigurableResponses(Object value) { | |
response = value; | |
} | |
public static <T> ConfigurableResponses<T> empty() { | |
return sequence(List.of()); | |
} | |
public static <T> ConfigurableResponses<T> always(T value) { | |
return new ConfigurableResponses<>(value); | |
} | |
public static ConfigurableResponses<?> always(Exception exception) { | |
return new ConfigurableResponses<>(exception); | |
} | |
public static <T> ConfigurableResponses<T> sequence(List<?> values) { | |
return new ConfigurableResponses<>(new LinkedList<>(values)); | |
} | |
public static <T> ConfigurableResponses<T> sequence(Object... values) { | |
return sequence(List.of(values)); | |
} | |
@SuppressWarnings("unchecked") | |
public T next() { | |
if (response instanceof Queue<?> q) { | |
var v = q.poll(); | |
if (v == null) { | |
throw new IllegalStateException("No more values configured."); | |
} else if (v instanceof RuntimeException e) { | |
throw e; | |
} else { | |
return (T) v; | |
} | |
} else if (response instanceof RuntimeException e) { | |
throw e; | |
} else { | |
return (T) response; | |
} | |
} | |
@SuppressWarnings("unchecked") | |
public T tryNext() throws Exception { | |
if (response instanceof Queue<?> q) { | |
var v = q.poll(); | |
if (v == null) { | |
throw new IllegalStateException("No more values configured."); | |
} else if (v instanceof Exception e) { | |
throw e; | |
} else { | |
return (T) v; | |
} | |
} else if (response instanceof Exception e) { | |
throw e; | |
} else { | |
return (T) response; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.function.Consumer; | |
public class OutputTracker<T> { | |
private final List<T> data = new ArrayList<>(); | |
private final Consumer<T> tracker = data::add; | |
private final EventEmitter<T> emitter; | |
public OutputTracker(EventEmitter<T> emitter) { | |
this.emitter = emitter; | |
emitter.addListener(tracker); | |
} | |
public List<T> data() { | |
return List.copyOf(data); | |
} | |
public List<T> clear() { | |
var result = data(); | |
data.clear(); | |
return result; | |
} | |
public void stop() { | |
emitter.removeListener(tracker); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment