ARTEMIS-3576 Fix toString methods throwing exceptions
This commit is contained in:
parent
5f5f683a85
commit
fcd512f9de
|
@ -45,6 +45,8 @@ import org.apache.activemq.artemis.utils.Env;
|
||||||
import org.apache.activemq.artemis.utils.IPV6Util;
|
import org.apache.activemq.artemis.utils.IPV6Util;
|
||||||
import org.jboss.logging.Logger;
|
import org.jboss.logging.Logger;
|
||||||
|
|
||||||
|
import static org.apache.activemq.artemis.utils.Preconditions.checkNotNull;
|
||||||
|
|
||||||
public class NettyConnection implements Connection {
|
public class NettyConnection implements Connection {
|
||||||
|
|
||||||
private static final Logger logger = Logger.getLogger(NettyConnection.class);
|
private static final Logger logger = Logger.getLogger(NettyConnection.class);
|
||||||
|
@ -74,6 +76,8 @@ public class NettyConnection implements Connection {
|
||||||
final BaseConnectionLifeCycleListener<?> listener,
|
final BaseConnectionLifeCycleListener<?> listener,
|
||||||
boolean batchingEnabled,
|
boolean batchingEnabled,
|
||||||
boolean directDeliver) {
|
boolean directDeliver) {
|
||||||
|
checkNotNull(channel);
|
||||||
|
|
||||||
this.configuration = configuration;
|
this.configuration = configuration;
|
||||||
|
|
||||||
this.channel = channel;
|
this.channel = channel;
|
||||||
|
|
|
@ -54,6 +54,7 @@ import static org.apache.activemq.artemis.jms.client.ActiveMQDestination.QUEUE_Q
|
||||||
import static org.apache.activemq.artemis.jms.client.ActiveMQDestination.TEMP_QUEUE_QUALIFED_PREFIX;
|
import static org.apache.activemq.artemis.jms.client.ActiveMQDestination.TEMP_QUEUE_QUALIFED_PREFIX;
|
||||||
import static org.apache.activemq.artemis.jms.client.ActiveMQDestination.TEMP_TOPIC_QUALIFED_PREFIX;
|
import static org.apache.activemq.artemis.jms.client.ActiveMQDestination.TEMP_TOPIC_QUALIFED_PREFIX;
|
||||||
import static org.apache.activemq.artemis.jms.client.ActiveMQDestination.TOPIC_QUALIFIED_PREFIX;
|
import static org.apache.activemq.artemis.jms.client.ActiveMQDestination.TOPIC_QUALIFIED_PREFIX;
|
||||||
|
import static org.apache.activemq.artemis.utils.Preconditions.checkNotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ActiveMQ Artemis implementation of a JMS Message.
|
* ActiveMQ Artemis implementation of a JMS Message.
|
||||||
|
@ -219,6 +220,8 @@ public class ActiveMQMessage implements javax.jms.Message {
|
||||||
* Constructor for when receiving a message from the server
|
* Constructor for when receiving a message from the server
|
||||||
*/
|
*/
|
||||||
public ActiveMQMessage(final ClientMessage message, final ClientSession session) {
|
public ActiveMQMessage(final ClientMessage message, final ClientSession session) {
|
||||||
|
checkNotNull(message);
|
||||||
|
|
||||||
this.message = message;
|
this.message = message;
|
||||||
|
|
||||||
readOnly = true;
|
readOnly = true;
|
||||||
|
@ -853,10 +856,14 @@ public class ActiveMQMessage implements javax.jms.Message {
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuffer sb = new StringBuffer("ActiveMQMessage[");
|
StringBuffer sb = new StringBuffer("ActiveMQMessage[");
|
||||||
sb.append(getJMSMessageID());
|
if (message != null) {
|
||||||
sb.append("]:");
|
sb.append(getJMSMessageID());
|
||||||
sb.append(message.isDurable() ? "PERSISTENT" : "NON-PERSISTENT");
|
sb.append("]:");
|
||||||
sb.append("/" + message.toString());
|
sb.append(message.isDurable() ? "PERSISTENT" : "NON-PERSISTENT");
|
||||||
|
sb.append("/" + message.toString());
|
||||||
|
} else {
|
||||||
|
sb.append("]");
|
||||||
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,8 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.activemq.artemis.core.journal.impl;
|
package org.apache.activemq.artemis.core.journal.impl;
|
||||||
|
|
||||||
|
import static org.apache.activemq.artemis.utils.Preconditions.checkNotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This holds the relationship a record has with other files in regard to reference counting.
|
* This holds the relationship a record has with other files in regard to reference counting.
|
||||||
* Note: This class used to be called PosFiles
|
* Note: This class used to be called PosFiles
|
||||||
|
@ -35,6 +37,8 @@ public class JournalRecord {
|
||||||
private ObjIntIntArrayList<JournalFile> fileUpdates;
|
private ObjIntIntArrayList<JournalFile> fileUpdates;
|
||||||
|
|
||||||
public JournalRecord(final JournalFile addFile, final int size) {
|
public JournalRecord(final JournalFile addFile, final int size) {
|
||||||
|
checkNotNull(addFile);
|
||||||
|
|
||||||
this.addFile = addFile;
|
this.addFile = addFile;
|
||||||
|
|
||||||
this.size = size;
|
this.size = size;
|
||||||
|
|
|
@ -1779,11 +1779,16 @@ public abstract class AMQPMessage extends RefCountMessage implements org.apache.
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
MessageDataScanningStatus scanningStatus = getDataScanningStatus();
|
||||||
|
Map<String, Object> applicationProperties = scanningStatus == MessageDataScanningStatus.SCANNED ?
|
||||||
|
getApplicationPropertiesMap(false) : Collections.EMPTY_MAP;
|
||||||
|
|
||||||
return this.getClass().getSimpleName() + "( [durable=" + isDurable() +
|
return this.getClass().getSimpleName() + "( [durable=" + isDurable() +
|
||||||
", messageID=" + getMessageID() +
|
", messageID=" + getMessageID() +
|
||||||
", address=" + getAddress() +
|
", address=" + getAddress() +
|
||||||
", size=" + getEncodeSize() +
|
", size=" + getEncodeSize() +
|
||||||
", applicationProperties=" + getApplicationPropertiesMap(false) +
|
", scanningStatus=" + scanningStatus +
|
||||||
|
", applicationProperties=" + applicationProperties +
|
||||||
", messageAnnotations=" + getMessageAnnotationsMap(false) +
|
", messageAnnotations=" + getMessageAnnotationsMap(false) +
|
||||||
", properties=" + properties +
|
", properties=" + properties +
|
||||||
", extraProperties = " + getExtraProperties() +
|
", extraProperties = " + getExtraProperties() +
|
||||||
|
|
|
@ -26,6 +26,8 @@ import org.apache.qpid.proton.codec.WritableBuffer;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
|
|
||||||
|
import static org.apache.activemq.artemis.utils.Preconditions.checkNotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link ReadableBuffer} implementation that wraps a Netty {@link ByteBuf} to
|
* {@link ReadableBuffer} implementation that wraps a Netty {@link ByteBuf} to
|
||||||
* allow use of Netty buffers to be used when decoding AMQP messages.
|
* allow use of Netty buffers to be used when decoding AMQP messages.
|
||||||
|
@ -37,6 +39,8 @@ public class NettyReadable implements ReadableBuffer {
|
||||||
private final ByteBuf buffer;
|
private final ByteBuf buffer;
|
||||||
|
|
||||||
public NettyReadable(ByteBuf buffer) {
|
public NettyReadable(ByteBuf buffer) {
|
||||||
|
checkNotNull(buffer);
|
||||||
|
|
||||||
this.buffer = buffer;
|
this.buffer = buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -332,7 +332,7 @@ public final class ActiveMQRAManagedConnectionFactory implements ManagedConnecti
|
||||||
}
|
}
|
||||||
|
|
||||||
int hash = mcfProperties.hashCode();
|
int hash = mcfProperties.hashCode();
|
||||||
hash += 31 * ra.hashCode();
|
hash += 31 * (ra != null ? ra.hashCode() : 0);
|
||||||
|
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,8 @@ import javax.jms.Message;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
|
|
||||||
|
import static org.apache.activemq.artemis.utils.Preconditions.checkNotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A wrapper for a message
|
* A wrapper for a message
|
||||||
*/
|
*/
|
||||||
|
@ -44,6 +46,9 @@ public class ActiveMQRAMessage implements Message {
|
||||||
* @param session the session
|
* @param session the session
|
||||||
*/
|
*/
|
||||||
public ActiveMQRAMessage(final Message message, final ActiveMQRASession session) {
|
public ActiveMQRAMessage(final Message message, final ActiveMQRASession session) {
|
||||||
|
checkNotNull(message);
|
||||||
|
checkNotNull(session);
|
||||||
|
|
||||||
if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
|
if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
|
||||||
ActiveMQRALogger.LOGGER.trace("constructor(" + message + ", " + session + ")");
|
ActiveMQRALogger.LOGGER.trace("constructor(" + message + ", " + session + ")");
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,8 @@ import org.apache.activemq.artemis.core.paging.cursor.PagePosition;
|
||||||
import org.apache.activemq.artemis.core.paging.impl.Page;
|
import org.apache.activemq.artemis.core.paging.impl.Page;
|
||||||
import org.jboss.logging.Logger;
|
import org.jboss.logging.Logger;
|
||||||
|
|
||||||
|
import static org.apache.activemq.artemis.utils.Preconditions.checkNotNull;
|
||||||
|
|
||||||
public class PageReader implements PageCache {
|
public class PageReader implements PageCache {
|
||||||
private static final Logger logger = Logger.getLogger(PageReader.class);
|
private static final Logger logger = Logger.getLogger(PageReader.class);
|
||||||
|
|
||||||
|
@ -31,6 +33,8 @@ public class PageReader implements PageCache {
|
||||||
private final int numberOfMessages;
|
private final int numberOfMessages;
|
||||||
|
|
||||||
public PageReader(Page page, int numberOfMessages) {
|
public PageReader(Page page, int numberOfMessages) {
|
||||||
|
checkNotNull(page);
|
||||||
|
|
||||||
this.page = page;
|
this.page = page;
|
||||||
this.numberOfMessages = numberOfMessages;
|
this.numberOfMessages = numberOfMessages;
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,8 @@ import org.apache.activemq.artemis.core.journal.EncodingSupport;
|
||||||
import org.apache.activemq.artemis.utils.BufferHelper;
|
import org.apache.activemq.artemis.utils.BufferHelper;
|
||||||
import org.apache.activemq.artemis.utils.DataConstants;
|
import org.apache.activemq.artemis.utils.DataConstants;
|
||||||
|
|
||||||
|
import static org.apache.activemq.artemis.utils.Preconditions.checkNotNull;
|
||||||
|
|
||||||
public class PersistedRole implements EncodingSupport {
|
public class PersistedRole implements EncodingSupport {
|
||||||
|
|
||||||
private long storeId;
|
private long storeId;
|
||||||
|
@ -36,6 +38,9 @@ public class PersistedRole implements EncodingSupport {
|
||||||
}
|
}
|
||||||
|
|
||||||
public PersistedRole(String username, List<String> roles) {
|
public PersistedRole(String username, List<String> roles) {
|
||||||
|
checkNotNull(username);
|
||||||
|
checkNotNull(roles);
|
||||||
|
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.roles = roles;
|
this.roles = roles;
|
||||||
}
|
}
|
||||||
|
@ -95,10 +100,12 @@ public class PersistedRole implements EncodingSupport {
|
||||||
result.append("PersistedRole [storeId=").append(storeId);
|
result.append("PersistedRole [storeId=").append(storeId);
|
||||||
result.append(", username=").append(username);
|
result.append(", username=").append(username);
|
||||||
result.append(", roles [");
|
result.append(", roles [");
|
||||||
for (int i = 0; i < roles.size(); i++) {
|
if (roles != null) {
|
||||||
result.append(roles.get(i));
|
for (int i = 0; i < roles.size(); i++) {
|
||||||
if (i < roles.size() - 1) {
|
result.append(roles.get(i));
|
||||||
result.append(", ");
|
if (i < roles.size() - 1) {
|
||||||
|
result.append(", ");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result.append("]]");
|
result.append("]]");
|
||||||
|
|
|
@ -25,6 +25,8 @@ import org.apache.activemq.artemis.utils.ByteUtil;
|
||||||
import org.apache.activemq.artemis.utils.DataConstants;
|
import org.apache.activemq.artemis.utils.DataConstants;
|
||||||
import org.apache.activemq.artemis.utils.UUID;
|
import org.apache.activemq.artemis.utils.UUID;
|
||||||
|
|
||||||
|
import static org.apache.activemq.artemis.utils.Preconditions.checkNotNull;
|
||||||
|
|
||||||
public class DuplicateIDEncoding implements EncodingSupport {
|
public class DuplicateIDEncoding implements EncodingSupport {
|
||||||
|
|
||||||
public SimpleString address;
|
public SimpleString address;
|
||||||
|
@ -32,6 +34,9 @@ public class DuplicateIDEncoding implements EncodingSupport {
|
||||||
public byte[] duplID;
|
public byte[] duplID;
|
||||||
|
|
||||||
public DuplicateIDEncoding(final SimpleString address, final byte[] duplID) {
|
public DuplicateIDEncoding(final SimpleString address, final byte[] duplID) {
|
||||||
|
checkNotNull(address);
|
||||||
|
checkNotNull(duplID);
|
||||||
|
|
||||||
this.address = address;
|
this.address = address;
|
||||||
|
|
||||||
this.duplID = duplID;
|
this.duplID = duplID;
|
||||||
|
@ -78,7 +83,7 @@ public class DuplicateIDEncoding implements EncodingSupport {
|
||||||
|
|
||||||
// The bridge will generate IDs on these terms:
|
// The bridge will generate IDs on these terms:
|
||||||
// This will make them easier to read
|
// This will make them easier to read
|
||||||
if (address.toString().startsWith("BRIDGE") && duplID.length == 24) {
|
if (address != null && address.toString().startsWith("BRIDGE") && duplID.length == 24) {
|
||||||
try {
|
try {
|
||||||
ByteBuffer buff = ByteBuffer.wrap(duplID);
|
ByteBuffer buff = ByteBuffer.wrap(duplID);
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,8 @@ import org.apache.activemq.artemis.core.persistence.AddressBindingInfo;
|
||||||
import org.apache.activemq.artemis.api.core.RoutingType;
|
import org.apache.activemq.artemis.api.core.RoutingType;
|
||||||
import org.apache.activemq.artemis.utils.DataConstants;
|
import org.apache.activemq.artemis.utils.DataConstants;
|
||||||
|
|
||||||
|
import static org.apache.activemq.artemis.utils.Preconditions.checkNotNull;
|
||||||
|
|
||||||
public class PersistentAddressBindingEncoding implements EncodingSupport, AddressBindingInfo {
|
public class PersistentAddressBindingEncoding implements EncodingSupport, AddressBindingInfo {
|
||||||
|
|
||||||
public long id;
|
public long id;
|
||||||
|
@ -59,6 +61,9 @@ public class PersistentAddressBindingEncoding implements EncodingSupport, Addres
|
||||||
public PersistentAddressBindingEncoding(final SimpleString name,
|
public PersistentAddressBindingEncoding(final SimpleString name,
|
||||||
final EnumSet<RoutingType> routingTypes,
|
final EnumSet<RoutingType> routingTypes,
|
||||||
final boolean autoCreated) {
|
final boolean autoCreated) {
|
||||||
|
checkNotNull(name);
|
||||||
|
checkNotNull(routingTypes);
|
||||||
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.routingTypes = routingTypes;
|
this.routingTypes = routingTypes;
|
||||||
this.autoCreated = autoCreated;
|
this.autoCreated = autoCreated;
|
||||||
|
|
|
@ -125,7 +125,7 @@ class NullStorageLargeServerMessage extends CoreMessage implements CoreLargeServ
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "NullStorageLargeServerMessage[messageID=" + messageID + ", durable=" + durable + ", address=" + getAddress() + ",properties=" + properties.toString() + "]";
|
return "NullStorageLargeServerMessage[messageID=" + messageID + ", durable=" + durable + ", address=" + getAddress() + ",properties=" + properties + "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -24,6 +24,8 @@ import java.util.Map;
|
||||||
import org.apache.activemq.artemis.api.core.ActiveMQBuffer;
|
import org.apache.activemq.artemis.api.core.ActiveMQBuffer;
|
||||||
import org.apache.activemq.artemis.core.protocol.core.impl.PacketImpl;
|
import org.apache.activemq.artemis.core.protocol.core.impl.PacketImpl;
|
||||||
|
|
||||||
|
import static org.apache.activemq.artemis.utils.Preconditions.checkNotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Informs the Backup trying to start replicating of an error.
|
* Informs the Backup trying to start replicating of an error.
|
||||||
*/
|
*/
|
||||||
|
@ -57,6 +59,9 @@ public final class BackupReplicationStartFailedMessage extends PacketImpl {
|
||||||
|
|
||||||
public BackupReplicationStartFailedMessage(BackupRegistrationProblem registrationProblem) {
|
public BackupReplicationStartFailedMessage(BackupRegistrationProblem registrationProblem) {
|
||||||
super(BACKUP_REGISTRATION_FAILED);
|
super(BACKUP_REGISTRATION_FAILED);
|
||||||
|
|
||||||
|
checkNotNull(registrationProblem);
|
||||||
|
|
||||||
problem = registrationProblem;
|
problem = registrationProblem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,6 +109,6 @@ public final class BackupReplicationStartFailedMessage extends PacketImpl {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getPacketString() {
|
protected String getPacketString() {
|
||||||
return super.getPacketString() + ", problem=" + problem.name();
|
return super.getPacketString() + ", problem=" + (problem != null ? problem.name() : null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,8 @@ import org.apache.activemq.artemis.core.server.ActiveMQServerLogger;
|
||||||
import org.apache.activemq.artemis.core.server.cluster.ClusterControl;
|
import org.apache.activemq.artemis.core.server.cluster.ClusterControl;
|
||||||
import org.apache.activemq.artemis.core.server.cluster.ClusterController;
|
import org.apache.activemq.artemis.core.server.cluster.ClusterController;
|
||||||
|
|
||||||
|
import static org.apache.activemq.artemis.utils.Preconditions.checkNotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A QourumManager can be used to register a {@link org.apache.activemq.artemis.core.server.cluster.qourum.Quorum} to receive notifications
|
* A QourumManager can be used to register a {@link org.apache.activemq.artemis.core.server.cluster.qourum.Quorum} to receive notifications
|
||||||
* about changes to the cluster. A {@link org.apache.activemq.artemis.core.server.cluster.qourum.Quorum} can then issue a vote to the
|
* about changes to the cluster. A {@link org.apache.activemq.artemis.core.server.cluster.qourum.Quorum} can then issue a vote to the
|
||||||
|
@ -73,6 +75,9 @@ public final class QuorumManager implements ClusterTopologyListener, ActiveMQCom
|
||||||
private int maxClusterSize = 0;
|
private int maxClusterSize = 0;
|
||||||
|
|
||||||
public QuorumManager(ExecutorService threadPool, ClusterController clusterController) {
|
public QuorumManager(ExecutorService threadPool, ClusterController clusterController) {
|
||||||
|
checkNotNull(threadPool);
|
||||||
|
checkNotNull(clusterController);
|
||||||
|
|
||||||
this.clusterController = clusterController;
|
this.clusterController = clusterController;
|
||||||
this.executor = threadPool;
|
this.executor = threadPool;
|
||||||
}
|
}
|
||||||
|
|
|
@ -319,11 +319,16 @@ public class MessageReferenceImpl extends LinkedListImpl.Node<MessageReferenceIm
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Reference[" + getMessage().getMessageID() +
|
Message message = getMessage();
|
||||||
"]:" +
|
if (message != null) {
|
||||||
(getMessage().isDurable() ? "RELIABLE" : "NON-RELIABLE") +
|
return "Reference[" + message.getMessageID() +
|
||||||
":" +
|
"]:" +
|
||||||
getMessage();
|
(message.isDurable() ? "RELIABLE" : "NON-RELIABLE") +
|
||||||
|
":" +
|
||||||
|
message;
|
||||||
|
} else {
|
||||||
|
return "Reference[]";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -174,6 +174,10 @@
|
||||||
<groupId>com.google.errorprone</groupId>
|
<groupId>com.google.errorprone</groupId>
|
||||||
<artifactId>error_prone_core</artifactId>
|
<artifactId>error_prone_core</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,138 @@
|
||||||
|
/**
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
* <p>
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* <p>
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.activemq.artemis.tests.unit;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableSet;
|
||||||
|
import com.google.common.reflect.ClassPath;
|
||||||
|
import org.apache.activemq.artemis.tests.util.RandomUtil;
|
||||||
|
import org.jboss.logging.Logger;
|
||||||
|
import org.junit.Assume;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.Parameterized;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.lang.reflect.Modifier;
|
||||||
|
import java.lang.reflect.Parameter;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RunWith(value = Parameterized.class)
|
||||||
|
public class AllClassesTest {
|
||||||
|
private static final Logger log = Logger.getLogger(AllClassesTest.class);
|
||||||
|
|
||||||
|
private static ClassLoader classLoader = AllClassesTest.class.getClassLoader();
|
||||||
|
|
||||||
|
@Parameterized.Parameters(name = "classInfo={0}")
|
||||||
|
public static Collection getParameters() {
|
||||||
|
List<Class> parameters = new ArrayList<>();
|
||||||
|
ClassLoader classLoader = AllClassesTest.class.getClassLoader();
|
||||||
|
|
||||||
|
try {
|
||||||
|
ClassPath classPath = ClassPath.from(classLoader);
|
||||||
|
ImmutableSet<ClassPath.ClassInfo> classInfos = classPath.getTopLevelClassesRecursive("org.apache.activemq.artemis");
|
||||||
|
|
||||||
|
for (ClassPath.ClassInfo classInfo : classInfos) {
|
||||||
|
if (!classInfo.getPackageName().contains("tests")) {
|
||||||
|
try {
|
||||||
|
Class loadedClass = classInfo.load();
|
||||||
|
if (!loadedClass.isEnum() && !loadedClass.isInterface() && !Modifier.isAbstract(loadedClass.getModifiers())) {
|
||||||
|
parameters.add(loadedClass);
|
||||||
|
}
|
||||||
|
} catch (Throwable loadThrowable) {
|
||||||
|
log.debug("cannot load " + classInfo.getName() + ": " + loadThrowable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return parameters;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("Exception on loading all classes: " + e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return parameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Class targetClass;
|
||||||
|
|
||||||
|
public AllClassesTest(Class targetClass) {
|
||||||
|
this.targetClass = targetClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToString() {
|
||||||
|
Object targetInstance = newInstance(targetClass);
|
||||||
|
Assume.assumeTrue("cannot create " + targetClass.getName(), targetInstance != null);
|
||||||
|
|
||||||
|
String targetOutput = targetInstance.toString();
|
||||||
|
log.debug("targetOutput: " + targetOutput);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object newInstance(Class targetClass) {
|
||||||
|
|
||||||
|
Constructor[] targetConstructors = targetClass.getDeclaredConstructors();
|
||||||
|
Arrays.sort(targetConstructors, (c1, c2) -> c2.getParameterCount() - c1.getParameterCount());
|
||||||
|
for (Constructor targetConstructor : targetConstructors) {
|
||||||
|
List<Object> initArgs = new ArrayList<>();
|
||||||
|
Parameter[] constructorParameters = targetConstructor.getParameters();
|
||||||
|
|
||||||
|
for (Parameter constructorParameter : constructorParameters) {
|
||||||
|
Object initArg = null;
|
||||||
|
if (constructorParameter.getType().isAssignableFrom(byte.class)) {
|
||||||
|
initArg = RandomUtil.randomByte();
|
||||||
|
} else if (constructorParameter.getType().isAssignableFrom(byte[].class)) {
|
||||||
|
initArg = RandomUtil.randomBytes();
|
||||||
|
} else if (constructorParameter.getType().isAssignableFrom(boolean.class)) {
|
||||||
|
initArg = RandomUtil.randomBoolean();
|
||||||
|
} else if (constructorParameter.getType().isAssignableFrom(char.class)) {
|
||||||
|
initArg = RandomUtil.randomChar();
|
||||||
|
} else if (constructorParameter.getType().isAssignableFrom(double.class)) {
|
||||||
|
initArg = RandomUtil.randomDouble();
|
||||||
|
} else if (constructorParameter.getType().isAssignableFrom(float.class)) {
|
||||||
|
initArg = RandomUtil.randomFloat();
|
||||||
|
} else if (constructorParameter.getType().isAssignableFrom(int.class)) {
|
||||||
|
initArg = RandomUtil.randomInt() / 1024;
|
||||||
|
} else if (constructorParameter.getType().isAssignableFrom(long.class)) {
|
||||||
|
initArg = RandomUtil.randomLong();
|
||||||
|
} else if (constructorParameter.getType().isAssignableFrom(String.class)) {
|
||||||
|
initArg = RandomUtil.randomString();
|
||||||
|
} else {
|
||||||
|
initArg = null;
|
||||||
|
}
|
||||||
|
initArgs.add(initArg);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return targetConstructor.newInstance(initArgs.toArray());
|
||||||
|
} catch (Throwable createThrowable) {
|
||||||
|
log.debug("cannot construct " + targetClass.getName() + ": " + createThrowable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return Mockito.spy(targetClass);
|
||||||
|
} catch (Throwable spyThrowable) {
|
||||||
|
log.debug("cannot spy " + targetClass.getName() + ": " + spyThrowable);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue