From 7888da16849949e5d78348cfe019c37d9a1df57f Mon Sep 17 00:00:00 2001 From: Wouter Born Date: Thu, 22 Dec 2022 15:11:49 +0100 Subject: [PATCH] Fix some recently introduced SAT and NPE issues (#3251) Signed-off-by: Wouter Born --- .../openhab/core/audio/utils/ToneSynthesizer.java | 13 ++++++------- .../media/internal/SynthesizeActionHandler.java | 4 ---- .../internal/action/ScriptExecutionImpl.java | 9 +-------- .../console/karaf/internal/CompleterWrapper.java | 15 ++++++++++----- .../io/rest/auth/internal/AuthFilterTest.java | 12 +++++------- .../org/openhab/core/io/websocket/EventDTO.java | 6 ++++-- .../openhab/core/io/websocket/EventWebSocket.java | 3 ++- .../core/thing/events/ThingStatusInfoEvent.java | 8 ++++---- .../core/library/items/NumberItemTest.java | 7 +++---- .../config/discovery/internal/InboxOSGiTest.java | 1 - .../core/thing/link/ItemChannelLinkOSGiTest.java | 11 ++++------- 11 files changed, 39 insertions(+), 50 deletions(-) diff --git a/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/utils/ToneSynthesizer.java b/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/utils/ToneSynthesizer.java index 2160bd3b5..b3d5a0055 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/utils/ToneSynthesizer.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/utils/ToneSynthesizer.java @@ -59,7 +59,7 @@ public class ToneSynthesizer { * You can optionally add the character "'" to increase the note one octave. * You can optionally add ":ms" where ms is an int value to customize the note/silence milliseconds duration * (defaults to 200ms). - * + * * @param melody to be parsed. * @return list of {@link Tone} instances. * @throws ParseException if melody can not be played. @@ -87,7 +87,7 @@ public class ToneSynthesizer { if (noteObj.isPresent()) { melodySounds.add(noteTone(noteObj.get(), soundMillis, octaves)); break; - } else if (note.equals("O") || note.equals("0")) { + } else if ("O".equals(note) || "0".equals(note)) { melodySounds.add(silenceTone(soundMillis)); break; } @@ -127,21 +127,20 @@ public class ToneSynthesizer { /** * Synthesize a list of {@link Tone} into a wav audio stream - * + * * @param tones the list of {@link Tone} * @return an audio stream with the synthesized tones * @throws IOException in case of problems writing the audio stream */ public AudioStream getStream(List tones) throws IOException { int byteRate = (int) (sampleRate * bitDepth * channels / 8); - byte[] audioBuffer = new byte[0]; + byte[] audioBuffer = {}; var fixedTones = new ArrayList<>(tones); fixedTones.add(silenceTone(100)); for (var sound : fixedTones) { var frequency = sound.frequency; var millis = sound.millis; int samplesPerChannel = (int) Math.ceil(sampleRate * (((double) millis) / 1000)); - int byteSize = (int) Math.ceil(byteRate * ((double) millis / 1000)); byte[] audioPart = getAudioBytes(frequency, samplesPerChannel); audioBuffer = ByteBuffer.allocate(audioBuffer.length + audioPart.length).put(audioBuffer).put(audioPart) .array(); @@ -193,10 +192,10 @@ public class ToneSynthesizer { audioBuffer.putShort(sample); break; case 24: - putInt24Bits(audioBuffer, ((int) sample) << 8); + putInt24Bits(audioBuffer, (sample) << 8); break; case 32: - audioBuffer.putInt(((int) sample) << 16); + audioBuffer.putInt((sample) << 16); break; } } diff --git a/bundles/org.openhab.core.automation.module.media/src/main/java/org/openhab/core/automation/module/media/internal/SynthesizeActionHandler.java b/bundles/org.openhab.core.automation.module.media/src/main/java/org/openhab/core/automation/module/media/internal/SynthesizeActionHandler.java index 58e4d3954..6940dc9b2 100644 --- a/bundles/org.openhab.core.automation.module.media/src/main/java/org/openhab/core/automation/module/media/internal/SynthesizeActionHandler.java +++ b/bundles/org.openhab.core.automation.module.media/src/main/java/org/openhab/core/automation/module/media/internal/SynthesizeActionHandler.java @@ -21,8 +21,6 @@ import org.openhab.core.audio.AudioManager; import org.openhab.core.automation.Action; import org.openhab.core.automation.handler.BaseActionModuleHandler; import org.openhab.core.library.types.PercentType; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * This is an ModuleHandler implementation for Actions that synthesize a tone melody. @@ -37,8 +35,6 @@ public class SynthesizeActionHandler extends BaseActionModuleHandler { public static final String PARAM_SINK = "sink"; public static final String PARAM_VOLUME = "volume"; - private final Logger logger = LoggerFactory.getLogger(SynthesizeActionHandler.class); - private final AudioManager audioManager; private final String melody; private final @Nullable String sink; diff --git a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/action/ScriptExecutionImpl.java b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/action/ScriptExecutionImpl.java index 8a3ca3722..29513bed8 100644 --- a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/action/ScriptExecutionImpl.java +++ b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/action/ScriptExecutionImpl.java @@ -17,8 +17,6 @@ import java.util.function.Consumer; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; -import org.openhab.core.automation.RuleManager; -import org.openhab.core.automation.RuleRegistry; import org.openhab.core.automation.module.script.action.ScriptExecution; import org.openhab.core.automation.module.script.action.Timer; import org.openhab.core.scheduler.Scheduler; @@ -37,15 +35,10 @@ import org.osgi.service.component.annotations.Reference; public class ScriptExecutionImpl implements ScriptExecution { private final Scheduler scheduler; - private final RuleManager ruleManager; - private final RuleRegistry ruleRegistry; @Activate - public ScriptExecutionImpl(@Reference RuleRegistry ruleRegistry, @Reference RuleManager ruleManager, - @Reference Scheduler scheduler) { - this.ruleRegistry = ruleRegistry; + public ScriptExecutionImpl(@Reference Scheduler scheduler) { this.scheduler = scheduler; - this.ruleManager = ruleManager; } @Override diff --git a/bundles/org.openhab.core.io.console.karaf/src/main/java/org/openhab/core/io/console/karaf/internal/CompleterWrapper.java b/bundles/org.openhab.core.io.console.karaf/src/main/java/org/openhab/core/io/console/karaf/internal/CompleterWrapper.java index 0bff5f6f2..e348068f8 100644 --- a/bundles/org.openhab.core.io.console.karaf/src/main/java/org/openhab/core/io/console/karaf/internal/CompleterWrapper.java +++ b/bundles/org.openhab.core.io.console.karaf/src/main/java/org/openhab/core/io/console/karaf/internal/CompleterWrapper.java @@ -62,16 +62,19 @@ public class CompleterWrapper implements Completer { if (commandLine.getArguments().length > 1) { String arg = commandLine.getArguments()[0]; - if (!arg.equals(command) && !arg.equals(localGlobalCommand)) + if (!arg.equals(command) && !arg.equals(localGlobalCommand)) { return -1; + } } - if (commandLine.getCursorArgumentIndex() < 0) + if (commandLine.getCursorArgumentIndex() < 0) { return -1; + } var localCompleter = completer; - if (localCompleter == null) + if (localCompleter == null) { return -1; + } String[] args = commandLine.getArguments(); boolean result = localCompleter.complete(Arrays.copyOfRange(args, 1, args.length), @@ -86,12 +89,14 @@ public class CompleterWrapper implements Completer { String arg = commandLine.getArguments()[0]; arg = arg.substring(0, commandLine.getArgumentPosition()); - if (command.startsWith(arg)) + if (command.startsWith(arg)) { candidates.add(new Candidate(command, command, null, commandDescription, null, null, true)); + } String localGlobalCommand = globalCommand; - if (localGlobalCommand != null && localGlobalCommand.startsWith(arg)) + if (localGlobalCommand != null && localGlobalCommand.startsWith(arg)) { candidates.add(new Candidate(localGlobalCommand, localGlobalCommand, null, commandDescription, null, null, true)); + } return; } diff --git a/bundles/org.openhab.core.io.rest.auth/src/test/java/org/openhab/core/io/rest/auth/internal/AuthFilterTest.java b/bundles/org.openhab.core.io.rest.auth/src/test/java/org/openhab/core/io/rest/auth/internal/AuthFilterTest.java index 5e5c38240..862ddd551 100644 --- a/bundles/org.openhab.core.io.rest.auth/src/test/java/org/openhab/core/io/rest/auth/internal/AuthFilterTest.java +++ b/bundles/org.openhab.core.io.rest.auth/src/test/java/org/openhab/core/io/rest/auth/internal/AuthFilterTest.java @@ -13,9 +13,7 @@ package org.openhab.core.io.rest.auth.internal; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; import java.io.IOException; import java.util.Map; @@ -48,16 +46,16 @@ public class AuthFilterTest { @InjectMocks private @NonNullByDefault({}) AuthFilter authFilter; - private @Mock @NonNullByDefault({}) JwtHelper jwtHelperMock; - private @Mock @NonNullByDefault({}) UserRegistry userRegistryMock; + // These mocks are inject into authFilter during setup + public @Mock @NonNullByDefault({}) JwtHelper jwtHelperMock; + public @Mock @NonNullByDefault({}) UserRegistry userRegistryMock; private @Mock @NonNullByDefault({}) ContainerRequestContext containerRequestContext; - private @Mock @NonNullByDefault({}) HttpServletRequest servletRequest; @BeforeEach public void setup() { - MockitoAnnotations.initMocks(this); + MockitoAnnotations.openMocks(this); when(servletRequest.getRemoteAddr()).thenReturn("192.168.0.100"); } diff --git a/bundles/org.openhab.core.io.websocket/src/main/java/org/openhab/core/io/websocket/EventDTO.java b/bundles/org.openhab.core.io.websocket/src/main/java/org/openhab/core/io/websocket/EventDTO.java index 8270664ce..3c235af46 100644 --- a/bundles/org.openhab.core.io.websocket/src/main/java/org/openhab/core/io/websocket/EventDTO.java +++ b/bundles/org.openhab.core.io.websocket/src/main/java/org/openhab/core/io/websocket/EventDTO.java @@ -53,10 +53,12 @@ public class EventDTO { @Override public boolean equals(@Nullable Object o) { - if (this == o) + if (this == o) { return true; - if (o == null || getClass() != o.getClass()) + } + if (o == null || getClass() != o.getClass()) { return false; + } EventDTO eventDTO = (EventDTO) o; return Objects.equals(type, eventDTO.type) && Objects.equals(topic, eventDTO.topic) && Objects.equals(payload, eventDTO.payload) && Objects.equals(source, eventDTO.source) diff --git a/bundles/org.openhab.core.io.websocket/src/main/java/org/openhab/core/io/websocket/EventWebSocket.java b/bundles/org.openhab.core.io.websocket/src/main/java/org/openhab/core/io/websocket/EventWebSocket.java index bf9efcf6f..d4378738d 100644 --- a/bundles/org.openhab.core.io.websocket/src/main/java/org/openhab/core/io/websocket/EventWebSocket.java +++ b/bundles/org.openhab.core.io.websocket/src/main/java/org/openhab/core/io/websocket/EventWebSocket.java @@ -179,7 +179,8 @@ public class EventWebSocket { if (session != null) { session.close(); } - String message = error == null ? "" : error.getMessage(); + + String message = error == null ? "" : Objects.requireNonNullElse(error.getMessage(), ""); logger.info("WebSocket error: {}", message); onClose(StatusCode.NO_CODE, message); } diff --git a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/events/ThingStatusInfoEvent.java b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/events/ThingStatusInfoEvent.java index 464b9a692..9813bf9e3 100644 --- a/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/events/ThingStatusInfoEvent.java +++ b/bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/events/ThingStatusInfoEvent.java @@ -82,12 +82,12 @@ public class ThingStatusInfoEvent extends AbstractEvent { @Override public boolean equals(@Nullable Object o) { - if (this == o) + if (this == o) { return true; - if (o == null || getClass() != o.getClass()) - return false; - if (!super.equals(o)) + } + if (o == null || getClass() != o.getClass() || !super.equals(o)) { return false; + } ThingStatusInfoEvent that = (ThingStatusInfoEvent) o; return thingUID.equals(that.thingUID) && thingStatusInfo.equals(that.thingStatusInfo); } diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/items/NumberItemTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/items/NumberItemTest.java index 6a325f165..95529bdb5 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/items/NumberItemTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/items/NumberItemTest.java @@ -12,8 +12,7 @@ */ package org.openhab.core.library.items; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; @@ -147,13 +146,13 @@ public class NumberItemTest { item.setEventPublisher(eventPublisher); QuantityType command = new QuantityType<>("15 °C"); - item.send(new QuantityType<>("15 °C")); + item.send(command); ArgumentCaptor captor = ArgumentCaptor.forClass(ItemCommandEvent.class); verify(eventPublisher).post(captor.capture()); ItemCommandEvent event = captor.getValue(); - assertThat(event.getItemCommand(), is(new QuantityType<>("15 °C"))); + assertThat(event.getItemCommand(), is(command)); } @Test diff --git a/itests/org.openhab.core.config.discovery.tests/src/main/java/org/openhab/core/config/discovery/internal/InboxOSGiTest.java b/itests/org.openhab.core.config.discovery.tests/src/main/java/org/openhab/core/config/discovery/internal/InboxOSGiTest.java index 4a8073666..45071e77e 100644 --- a/itests/org.openhab.core.config.discovery.tests/src/main/java/org/openhab/core/config/discovery/internal/InboxOSGiTest.java +++ b/itests/org.openhab.core.config.discovery.tests/src/main/java/org/openhab/core/config/discovery/internal/InboxOSGiTest.java @@ -957,7 +957,6 @@ public class InboxOSGiTest extends JavaOSGiTest { @Test @SuppressWarnings("null") public void assertThatApproveSetsTheDiscoveredLabelIfNoOtherIsGiven() { - inbox.add(testDiscoveryResult); Thing approvedThing = inbox.approve(testThing.getUID(), null, null); Thing addedThing = registry.get(testThing.getUID()); diff --git a/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/link/ItemChannelLinkOSGiTest.java b/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/link/ItemChannelLinkOSGiTest.java index ddf2fb489..4f4042b8c 100644 --- a/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/link/ItemChannelLinkOSGiTest.java +++ b/itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/link/ItemChannelLinkOSGiTest.java @@ -13,10 +13,7 @@ package org.openhab.core.thing.link; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.*; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; @@ -54,9 +51,9 @@ public class ItemChannelLinkOSGiTest extends JavaOSGiTest { private static final String BULK_BASE_THING_UID = "binding:type:thing"; private static final String BULK_BASE_ITEM_NAME = "item"; - private static int BULK_ITEM_COUNT = 3; - private static int BULK_THING_COUNT = 3; - private static int BULK_CHANNEL_COUNT = 3; + private static final int BULK_ITEM_COUNT = 3; + private static final int BULK_THING_COUNT = 3; + private static final int BULK_CHANNEL_COUNT = 3; private static final String ITEM = "item"; private static final ThingTypeUID THING_TYPE_UID = new ThingTypeUID("binding:thing");