This closes #869

This commit is contained in:
Clebert Suconic 2017-03-09 17:27:51 -05:00
commit 0b1e48a7f4
2 changed files with 36 additions and 39 deletions

View File

@ -16,6 +16,8 @@
*/
package org.apache.activemq.transport.amqp.client;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.qpid.proton.amqp.Binary;
import org.apache.qpid.proton.amqp.transport.Attach;
import org.apache.qpid.proton.amqp.transport.Begin;
@ -29,12 +31,11 @@ import org.apache.qpid.proton.amqp.transport.Transfer;
/**
* Abstract base for a validation hook that is used in tests to check
* the state of a remote resource after a variety of lifecycle events.
* the values of incoming or outgoing AMQP frames.
*/
public class AmqpFrameValidator {
private boolean valid = true;
private String errorMessage;
private AtomicReference<String> errorMessage = new AtomicReference<>();
public void inspectOpen(Open open, Binary encoded) {
@ -73,31 +74,29 @@ public class AmqpFrameValidator {
}
public boolean isValid() {
return valid;
return errorMessage.get() != null;
}
protected void setValid(boolean valid) {
this.valid = valid;
public final void clearErrorMessage() {
errorMessage.set(null);
}
public String getErrorMessage() {
return errorMessage;
public final String getErrorMessage() {
return errorMessage.get();
}
protected void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
protected void markAsInvalid(String errorMessage) {
if (valid) {
setValid(false);
setErrorMessage(errorMessage);
protected final boolean markAsInvalid(String message) {
if (message == null) {
throw new NullPointerException("Provided error message cannot be null!");
}
return errorMessage.compareAndSet(null, message);
}
public void assertValid() {
if (!isValid()) {
throw new AssertionError(errorMessage);
public final void assertValid() {
final String assertionErrorMessage = errorMessage.get();
if (assertionErrorMessage != null) {
throw new AssertionError(assertionErrorMessage);
}
}
}

View File

@ -16,6 +16,8 @@
*/
package org.apache.activemq.transport.amqp.client;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.qpid.proton.engine.Connection;
import org.apache.qpid.proton.engine.Delivery;
import org.apache.qpid.proton.engine.Receiver;
@ -28,8 +30,7 @@ import org.apache.qpid.proton.engine.Session;
*/
public class AmqpValidator {
private boolean valid = true;
private String errorMessage;
private AtomicReference<String> errorMessage = new AtomicReference<>();
public void inspectOpenedResource(Connection connection) {
@ -76,32 +77,29 @@ public class AmqpValidator {
}
public boolean isValid() {
return valid;
return this.errorMessage.get() == null;
}
protected void setValid(boolean valid) {
this.valid = valid;
public final void clearErrorMessage() {
errorMessage.set(null);
}
public String getErrorMessage() {
return errorMessage;
public final String getErrorMessage() {
return errorMessage.get();
}
protected void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
protected final boolean markAsInvalid(String message) {
if (message == null) {
throw new NullPointerException("Provided error message cannot be null!");
}
return errorMessage.compareAndSet(null, message);
}
protected void markAsInvalid(String errorMessage) {
if (valid) {
setValid(false);
setErrorMessage(errorMessage);
public final void assertValid() {
final String assertionErrorMessage = errorMessage.get();
if (assertionErrorMessage != null) {
throw new AssertionError(assertionErrorMessage);
}
}
public void assertValid() {
if (!isValid()) {
throw new AssertionError(errorMessage);
}
}
}