Skip to content

Commit

Permalink
A number of code style improvments
Browse files Browse the repository at this point in the history
Mainly Sonar related issues.
  • Loading branch information
Hilbrand committed Jan 10, 2025
1 parent c4a1378 commit 30bf348
Show file tree
Hide file tree
Showing 18 changed files with 50 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private Connection openConnection() {
delayRetry(retryTime);
}
}
localConnection.addShutdownListener((cause) -> {
localConnection.addShutdownListener(cause -> {
if (cause.isInitiatedByApplication()) {
LOG.info("Connection was shut down (by application)");
} else {
Expand All @@ -114,7 +114,7 @@ private Connection openConnection() {
return localConnection;
}

private void delayRetry(final int retryTime) {
private static void delayRetry(final int retryTime) {
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(retryTime));
} catch (final InterruptedException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ public class TaskManagerClientSender implements TaskWrapperSender {

private static final Logger LOG = LoggerFactory.getLogger(TaskManagerClientSender.class);

private static final boolean QUEUE_DURABLE = true;
private static final boolean QUEUE_EXCLUSIVE = false;
private static final boolean QUEUE_AUTO_DELETE = false;

private static final int DELIVERY_MODE_NON_PERSISTENT = 1;
private static final int DELIVERY_MODE_PERSISTENT = 2;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,15 @@ public ConnectionConfiguration build() {
return connectionConfiguration;
}

private void checkBlank(final String name, final String value) {
private static void checkBlank(final String name, final String value) {
if (value == null || value.isEmpty()) {
throw new IllegalArgumentException(name + " not allowed to be null or empty.");
}
}

public ConnectionConfiguration.Builder brokerHost(final String brokerHost) {
if (brokerHost == null) {
throw new NullPointerException("Null brokerHost");
throw new IllegalArgumentException("brokerHost null");
}
this.brokerHost = brokerHost;
return this;
Expand All @@ -248,23 +248,23 @@ public ConnectionConfiguration.Builder brokerPort(final int brokerPort) {

public ConnectionConfiguration.Builder brokerUsername(final String brokerUsername) {
if (brokerUsername == null) {
throw new NullPointerException("Null brokerUsername");
throw new IllegalArgumentException("brokerUsername null");
}
this.brokerUsername = brokerUsername;
return this;
}

public ConnectionConfiguration.Builder brokerPassword(final String brokerPassword) {
if (brokerPassword == null) {
throw new NullPointerException("Null brokerPassword");
throw new IllegalArgumentException("brokerPassword null");
}
this.brokerPassword = brokerPassword;
return this;
}

public ConnectionConfiguration.Builder brokerVirtualHost(final String brokerVirtualHost) {
if (brokerVirtualHost == null) {
throw new NullPointerException("Null brokerVirtualHost");
throw new IllegalArgumentException("brokerVirtualHost null");
}
this.brokerVirtualHost = brokerVirtualHost;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private void handleDelivery(final AMQP.BasicProperties properties) {
* @param param param to return the value for
* @return value of param or null if not present
*/
private String getParam(final Map<String, Object> headers, final String param) {
private static String getParam(final Map<String, Object> headers, final String param) {
final Object value = headers.get(param);

return value == null ? null : value.toString();
Expand All @@ -206,7 +206,7 @@ private String getParam(final Map<String, Object> headers, final String param) {
* @param other if param is not present this value is returned
* @return int value of param or other if not present
*/
private int getParamInt(final Map<String, Object> headers, final String param, final int other) {
private static int getParamInt(final Map<String, Object> headers, final String param, final int other) {
final String value = getParam(headers, param);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.BasicProperties;
import com.rabbitmq.client.LongString;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand Down Expand Up @@ -46,7 +45,7 @@ static void afterClass() {
}

@Test
void testTaskManagerClientWithoutBrokerHost() throws IOException {
void testTaskManagerClientWithoutBrokerHost() {
assertThrows(NullPointerException.class, () -> {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder();
builder.brokerHost(null);
Expand All @@ -55,7 +54,7 @@ void testTaskManagerClientWithoutBrokerHost() throws IOException {
}

@Test
void testTaskManagerClientWithEmptyBrokerHost() throws IOException {
void testTaskManagerClientWithEmptyBrokerHost() {
assertThrows(IllegalArgumentException.class, () -> {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder();
builder.brokerHost("");
Expand All @@ -64,7 +63,7 @@ void testTaskManagerClientWithEmptyBrokerHost() throws IOException {
}

@Test
void testTaskManagerClientWithoutBrokerUsername() throws IOException {
void testTaskManagerClientWithoutBrokerUsername() {
assertThrows(NullPointerException.class, () -> {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder();
builder.brokerUsername(null);
Expand All @@ -73,7 +72,7 @@ void testTaskManagerClientWithoutBrokerUsername() throws IOException {
}

@Test
void testTaskManagerClientWithEmptyBrokerUsername() throws IOException {
void testTaskManagerClientWithEmptyBrokerUsername() {
assertThrows(IllegalArgumentException.class, () -> {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder();
builder.brokerUsername("");
Expand All @@ -82,7 +81,7 @@ void testTaskManagerClientWithEmptyBrokerUsername() throws IOException {
}

@Test
void testTaskManagerClientWithoutBrokerPassword() throws IOException {
void testTaskManagerClientWithoutBrokerPassword() {
assertThrows(NullPointerException.class, () -> {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder();
builder.brokerPassword(null);
Expand All @@ -91,7 +90,7 @@ void testTaskManagerClientWithoutBrokerPassword() throws IOException {
}

@Test
void testTaskManagerClientWithEmptyBrokerPassword() throws IOException {
void testTaskManagerClientWithEmptyBrokerPassword() {
assertThrows(IllegalArgumentException.class, () -> {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder();
builder.brokerPassword("");
Expand All @@ -100,7 +99,7 @@ void testTaskManagerClientWithEmptyBrokerPassword() throws IOException {
}

@Test
void testTaskManagerClientWithoutBrokerVirtualHost() throws IOException {
void testTaskManagerClientWithoutBrokerVirtualHost() {
assertThrows(NullPointerException.class, () -> {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder();
builder.brokerVirtualHost(null);
Expand All @@ -109,7 +108,7 @@ void testTaskManagerClientWithoutBrokerVirtualHost() throws IOException {
}

@Test
void testTaskManagerClientWithEmptyBrokerVirtualHost() throws IOException {
void testTaskManagerClientWithEmptyBrokerVirtualHost() {
assertThrows(IllegalArgumentException.class, () -> {
final ConnectionConfiguration.Builder builder = getFullConnectionConfigurationBuilder();
builder.brokerVirtualHost("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

import nl.aerius.taskmanager.client.TaskResultCallback;

/**
*
* Util class to more easily mock a {@link TaskResultCallback}.
*/
class MockTaskResultHandler implements TaskResultCallback {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,26 @@ class TaskManagerClientTest {
private static final WorkerQueueType WORKER_TYPE_TEST = new WorkerQueueType("TEST");
private static final String NORMAL_TASK_ID = "SomeTaskId";
private static final String TASK_QUEUE_NAME = "taskmanagerclienttest.task";
private static ExecutorService EXECUTOR;
private static ExecutorService executor;
private WorkerQueueType workerType;
private TaskManagerClientSender taskManagerClient;
private MockTaskResultHandler mockTaskResultHandler;

@BeforeAll
static void setupClass() {
EXECUTOR = Executors.newSingleThreadExecutor();
executor = Executors.newSingleThreadExecutor();
}

@AfterAll
static void afterClass() {
EXECUTOR.shutdown();
executor.shutdown();
}

@BeforeEach
void setUp() throws Exception {
void setUp() {
mockTaskResultHandler = new MockTaskResultHandler();
workerType = WORKER_TYPE_TEST;
taskManagerClient = new TaskManagerClientSender(new BrokerConnectionFactory(EXECUTOR) {
taskManagerClient = new TaskManagerClientSender(new BrokerConnectionFactory(executor) {
@Override
protected Connection createNewConnection() throws IOException {
return new MockConnection();
Expand All @@ -76,7 +76,7 @@ protected Connection createNewConnection() throws IOException {
}

@AfterEach
void tearDown() throws Exception {
void tearDown() {
taskManagerClient.shutdown();
}

Expand Down Expand Up @@ -104,7 +104,7 @@ void testSendTasks() throws IOException {
}

@Test
void testSendTasksWithNullId() throws IOException, InterruptedException {
void testSendTasksWithNullId() throws IOException {
taskManagerClient.sendTask(new MockTaskInput(), null, mockTaskResultHandler, workerType, TASK_QUEUE_NAME);
assertTrue(taskManagerClient.isUsable(), "Taskmanagerclient should still be usable.");
}
Expand All @@ -131,7 +131,7 @@ void testTaskManagerClientWithConnectionConfigurationBean() throws IOException,
* @throws InterruptedException
*/
@Test
void testSendUnserializableTask() throws IOException, InterruptedException {
void testSendUnserializableTask() {
assertThrows(NotSerializableException.class, () -> {
//anonymous inner type isn't serializable (even if the type is Serializable).
final Serializable input = new Serializable() {
Expand All @@ -158,7 +158,7 @@ void testExit() throws IOException, InterruptedException {
* @throws InterruptedException
*/
@Test
void testSendTaskAfterExit() throws IOException, InterruptedException {
void testSendTaskAfterExit() {
assertThrows(IllegalStateException.class, () -> {
taskManagerClient.shutdown();
testSendTask();
Expand All @@ -169,7 +169,7 @@ void testSendTaskAfterExit() throws IOException, InterruptedException {
* Test method for {@link TaskManagerClientSender#sendTask(Object, String, String)}.
*/
@Test
void testSendTaskToNullQueue() throws IOException {
void testSendTaskToNullQueue() {
assertThrows(IllegalArgumentException.class,
() -> taskManagerClient.sendTask(new MockTaskInput(), NORMAL_TASK_ID, mockTaskResultHandler, workerType, null));
}
Expand All @@ -178,7 +178,7 @@ void testSendTaskToNullQueue() throws IOException {
* Test method for {@link TaskManagerClientSender#sendTask(Object, String, String)}.
*/
@Test
void testSendNullObjectAsTask() throws IOException {
void testSendNullObjectAsTask() {
assertThrows(IllegalArgumentException.class, () -> taskManagerClient.sendTask(null, null, mockTaskResultHandler, workerType, TASK_QUEUE_NAME));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,17 @@
*
* This class keeps some state
*/
public class MockedChannelFactory {
public final class MockedChannelFactory {

private static final ExecutorService EXECUTOR = Executors.newCachedThreadPool();
private static final Map<String, PriorityBlockingQueue<Body>> QUEUES = new ConcurrentHashMap<>();
private static final Map<Long, Body> QUEUED = new ConcurrentHashMap<>();
private static byte[] received;

private MockedChannelFactory() {
// Util class
}

/**
* Creates a new mocked channel.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*/
final class ConfigurationManager {

private static final Logger LOG = LoggerFactory.getLogger(TaskManagerConfiguration.class);
private static final Logger LOG = LoggerFactory.getLogger(ConfigurationManager.class);

private ConfigurationManager() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/
class FIFOTaskScheduler implements TaskScheduler<PriorityTaskQueue> {

private final BlockingQueue<Task> tasks = new LinkedBlockingQueue<Task>();
private final BlockingQueue<Task> tasks = new LinkedBlockingQueue<>();

@Override
public void addTask(final Task task) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void updateWorkerQueueState(final String queueName, final WorkerSizeObser
// Use RabbitMQ HTTP-API.
// URL: [host]:[port]/api/queues/[virtualHost]/[QueueName]
final String virtualHost = configuration.getBrokerVirtualHost().replace("/", "%2f");
final String apiPath = "/api/queues/" + virtualHost + "/" + queueName;
final String apiPath = String.format("/api/queues/%s/%s", virtualHost, queueName);

try {
final JsonNode jsonObject = getJsonResultFromApi(apiPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void before() throws IOException {

@Test
@Timeout(2000)
void testLoadConfiguration() throws IOException {
void testLoadConfiguration() {
final TaskManagerConfiguration tmc = ConfigurationManager.loadConfiguration(properties);

assertNotNull(tmc.getBrokerConfiguration().getBrokerUsername(), "No username could be read from configuration file");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ void testGetTask() throws InterruptedException, ExecutionException {
assertEquals(0, chkCounter.intValue(), "Counter should still be zero when 1 slot priorty available");
assertFalse(receivedTask.isDone(), "Should not be done yet");
scheduler.onTaskFinished(task1a.getMessage().getMetaData().getQueueName());
await().atMost(1, TimeUnit.SECONDS).until(() -> receivedTask.isDone());
await().atMost(1, TimeUnit.SECONDS).until(receivedTask::isDone);
assertNotNull(receivedTask.get(), "Received task");
// task1a finished, now task1b may be executed.
assertEquals(1, chkCounter.intValue(), "Counter should still be 1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
class QueueWatchDogTest {

@Test
void testIsItDead() throws InterruptedException {
void testIsItDead() {
final AtomicReference<LocalDateTime> now = new AtomicReference<>(LocalDateTime.now());
final QueueWatchDog qwd = new QueueWatchDog() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void after() throws InterruptedException {

@Test
@Timeout(3000)
void testNoFreeWorkers() throws InterruptedException {
void testNoFreeWorkers() {
// Add Worker which will unlock
workerPool.onNumberOfWorkersUpdate(1, 0);
executor.execute(dispatcher);
Expand All @@ -89,21 +89,21 @@ void testNoFreeWorkers() throws InterruptedException {

@Test
@Timeout(3000)
void testForwardTest() throws InterruptedException {
void testForwardTest() {
final Task task = createTask();
final Future<?> future = forwardTaskAsync(task, null);
executor.execute(dispatcher);
await().until(() -> dispatcher.isLocked(task));
workerPool.onNumberOfWorkersUpdate(1, 0); //add worker which will unlock
await().until(() -> dispatcher.getState() == State.WAIT_FOR_WORKER);
await().until(() -> future.isDone());
await().until(future::isDone);
assertFalse(future.isCancelled(), "Taskconsumer must be unlocked at this point without error");
}

@Disabled("TaskAlreadySendexception error willl not be thrown")
@Test
@Timeout(3000)
void testForwardDuplicateTask() throws InterruptedException {
void testForwardDuplicateTask() {
final Task task = createTask();
executor.execute(dispatcher);
final Future<?> future = forwardTaskAsync(task, null);
Expand All @@ -124,7 +124,7 @@ void testForwardDuplicateTask() throws InterruptedException {

@Test
@Timeout(3000)
void testExceptionDuringForward() throws InterruptedException {
void testExceptionDuringForward() {
workerProducer.setShutdownExceptionOnForward(true);
final Task task = createTask();
executor.execute(dispatcher);
Expand Down
Loading

0 comments on commit 30bf348

Please sign in to comment.