Add a test utility

This commit is contained in:
jamesagnew 2020-03-18 17:33:39 -04:00
parent 44ef5a4f6c
commit ecd2bcf492
1 changed files with 25 additions and 19 deletions

View File

@ -34,6 +34,7 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -44,26 +45,29 @@ public class PointcutLatch implements IAnonymousInterceptor, IPointcutLatch {
private static final FhirObjectPrinter ourFhirObjectToStringMapper = new FhirObjectPrinter(); private static final FhirObjectPrinter ourFhirObjectToStringMapper = new FhirObjectPrinter();
private final String name; private final String name;
private final AtomicLong myLastInvoke = new AtomicLong();
private final AtomicReference<CountDownLatch> myCountdownLatch = new AtomicReference<>(); private final AtomicReference<CountDownLatch> myCountdownLatch = new AtomicReference<>();
private final AtomicReference<List<String>> myFailures = new AtomicReference<>(); private final AtomicReference<List<String>> myFailures = new AtomicReference<>();
private final AtomicReference<List<HookParams>> myCalledWith = new AtomicReference<>(); private final AtomicReference<List<HookParams>> myCalledWith = new AtomicReference<>();
private int myDefaultTimeoutSeconds = DEFAULT_TIMEOUT_SECONDS;
private final Pointcut myPointcut; private final Pointcut myPointcut;
private int myDefaultTimeoutSeconds = DEFAULT_TIMEOUT_SECONDS;
private int myInitialCount; private int myInitialCount;
private boolean myExactMatch; private boolean myExactMatch;
public PointcutLatch(Pointcut thePointcut) { public PointcutLatch(Pointcut thePointcut) {
this.name = thePointcut.name(); this.name = thePointcut.name();
myPointcut = thePointcut; myPointcut = thePointcut;
} }
public PointcutLatch(String theName) { public PointcutLatch(String theName) {
this.name = theName; this.name = theName;
myPointcut = null; myPointcut = null;
} }
public long getLastInvoke() {
return myLastInvoke.get();
}
public PointcutLatch setDefaultTimeoutSeconds(int theDefaultTimeoutSeconds) { public PointcutLatch setDefaultTimeoutSeconds(int theDefaultTimeoutSeconds) {
myDefaultTimeoutSeconds = theDefaultTimeoutSeconds; myDefaultTimeoutSeconds = theDefaultTimeoutSeconds;
return this; return this;
@ -166,6 +170,8 @@ public class PointcutLatch implements IAnonymousInterceptor, IPointcutLatch {
@Override @Override
public void invoke(Pointcut thePointcut, HookParams theArgs) { public void invoke(Pointcut thePointcut, HookParams theArgs) {
myLastInvoke.set(System.currentTimeMillis());
CountDownLatch latch = myCountdownLatch.get(); CountDownLatch latch = myCountdownLatch.get();
if (myExactMatch) { if (myExactMatch) {
if (latch == null) { if (latch == null) {
@ -189,6 +195,21 @@ public class PointcutLatch implements IAnonymousInterceptor, IPointcutLatch {
this.invoke(myPointcut, new HookParams(arg)); this.invoke(myPointcut, new HookParams(arg));
} }
@Override
public String toString() {
return new ToStringBuilder(this)
.append("name", name)
.append("myCountdownLatch", myCountdownLatch)
// .append("myFailures", myFailures)
// .append("myCalledWith", myCalledWith)
.append("myInitialCount", myInitialCount)
.toString();
}
public Object getLatchInvocationParameter() {
return getLatchInvocationParameter(myCalledWith.get());
}
private class PointcutLatchException extends IllegalStateException { private class PointcutLatchException extends IllegalStateException {
private static final long serialVersionUID = 1372636272233536829L; private static final long serialVersionUID = 1372636272233536829L;
@ -205,21 +226,6 @@ public class PointcutLatch implements IAnonymousInterceptor, IPointcutLatch {
return hookParams.values().stream().map(ourFhirObjectToStringMapper).collect(Collectors.joining(", ")); return hookParams.values().stream().map(ourFhirObjectToStringMapper).collect(Collectors.joining(", "));
} }
@Override
public String toString() {
return new ToStringBuilder(this)
.append("name", name)
.append("myCountdownLatch", myCountdownLatch)
// .append("myFailures", myFailures)
// .append("myCalledWith", myCalledWith)
.append("myInitialCount", myInitialCount)
.toString();
}
public Object getLatchInvocationParameter() {
return getLatchInvocationParameter(myCalledWith.get());
}
public static Object getLatchInvocationParameter(List<HookParams> theHookParams) { public static Object getLatchInvocationParameter(List<HookParams> theHookParams) {
Validate.notNull(theHookParams); Validate.notNull(theHookParams);
Validate.isTrue(theHookParams.size() == 1, "Expected Pointcut to be invoked 1 time"); Validate.isTrue(theHookParams.size() == 1, "Expected Pointcut to be invoked 1 time");