Skip to content

Commit

Permalink
Add more null annotations (#2742)
Browse files Browse the repository at this point in the history
* Add more null annotations
* Fix mock name

Adds null annotations to most of the tests as well as a few other classes.
Also fixes a few other SAT findings.
Fixes ~300 SAT findings in total.


Signed-off-by: Wouter Born <github@maindrain.net>
  • Loading branch information
wborn authored Feb 14, 2022
1 parent b5bf0b0 commit ad936cd
Show file tree
Hide file tree
Showing 296 changed files with 2,873 additions and 2,323 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.Map;
import java.util.Random;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.addon.Addon;
import org.openhab.core.addon.AddonEventFactory;
import org.openhab.core.addon.AddonService;
Expand All @@ -40,26 +42,23 @@
* @author Kai Kreuzer - Initial contribution
*/
@Component
@NonNullByDefault
public class SampleAddonService implements AddonService {

private static final String LOREM_IPSUM = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";

private static final String[] COLOR_VALUES = new String[] { "80", "C8", "FF" };

private EventPublisher eventPublisher;
private final EventPublisher eventPublisher;

List<AddonType> types = new ArrayList<>(3);
Map<String, Addon> extensions = new HashMap<>(30);
private List<AddonType> types = new ArrayList<>(3);
private Map<String, Addon> extensions = new HashMap<>(30);

@Reference
protected void setEventPublisher(EventPublisher eventPublisher) {
@Activate
public SampleAddonService(final @Reference EventPublisher eventPublisher) {
this.eventPublisher = eventPublisher;
}

protected void unsetEventPublisher(EventPublisher eventPublisher) {
this.eventPublisher = null;
}

@Activate
protected void activate() {
types.add(new AddonType("binding", "Bindings"));
Expand Down Expand Up @@ -149,36 +148,32 @@ public void uninstall(String id) {
}

@Override
public List<Addon> getAddons(Locale locale) {
public List<Addon> getAddons(@Nullable Locale locale) {
return new ArrayList<>(extensions.values());
}

@Override
public Addon getAddon(String id, Locale locale) {
public @Nullable Addon getAddon(String id, @Nullable Locale locale) {
return extensions.get(id);
}

@Override
public List<AddonType> getTypes(Locale locale) {
public List<AddonType> getTypes(@Nullable Locale locale) {
return types;
}

@Override
public String getAddonId(URI extensionURI) {
public @Nullable String getAddonId(URI extensionURI) {
return null;
}

private void postInstalledEvent(String extensionId) {
if (eventPublisher != null) {
Event event = AddonEventFactory.createAddonInstalledEvent(extensionId);
eventPublisher.post(event);
}
Event event = AddonEventFactory.createAddonInstalledEvent(extensionId);
eventPublisher.post(event);
}

private void postUninstalledEvent(String extensionId) {
if (eventPublisher != null) {
Event event = AddonEventFactory.createAddonUninstalledEvent(extensionId);
eventPublisher.post(event);
}
Event event = AddonEventFactory.createAddonUninstalledEvent(extensionId);
eventPublisher.post(event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -170,7 +171,8 @@ private void playOnSink(@Nullable String sinkId, String fileName, @Nullable Perc
try {
audioManager.playFile(fileName, sinkId, volume);
} catch (AudioException e) {
console.println(e.getMessage());
console.println(Objects.requireNonNullElse(e.getMessage(),
String.format("An error occurred while playing '%s' on sink '%s'", fileName, sinkId)));
}
}

Expand All @@ -197,7 +199,8 @@ private void streamOnSink(@Nullable String sinkId, String url, Console console)
try {
audioManager.stream(url, sinkId);
} catch (AudioException e) {
console.println(e.getMessage());
console.println(Objects.requireNonNullElse(e.getMessage(),
String.format("An error occurred while streaming '%s' to sink '%s'", url, sinkId)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class AudioWaveUtils {
/**
* This "magic" packet marks the beginning of the read data
*/
private final static int DATA_MAGIC = 0x64617461;
private static final int DATA_MAGIC = 0x64617461;

private static final AudioFormat DEFAULT_WAVE_AUDIO_FORMAT = new AudioFormat(AudioFormat.CONTAINER_WAVE,
AudioFormat.CODEC_PCM_SIGNED, false, 16, 705600, 44100L, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import java.util.concurrent.CompletableFuture;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
Expand Down Expand Up @@ -45,22 +47,21 @@
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.WARN)
@NonNullByDefault
public abstract class AbstractAudioServletTest extends JavaTest {

protected AudioServlet audioServlet;

private int port;
private TestServer server;

private static final String AUDIO_SERVLET_PROTOCOL = "http";
private static final String AUDIO_SERVLET_HOSTNAME = "localhost";

private CompletableFuture<Boolean> serverStarted;
protected @NonNullByDefault({}) AudioServlet audioServlet;

private HttpClient httpClient;
private int port;
private @NonNullByDefault({}) TestServer server;
private @NonNullByDefault({}) HttpClient httpClient;
private @NonNullByDefault({}) CompletableFuture<Boolean> serverStarted;

private @Mock HttpService httpServiceMock;
private @Mock HttpContext httpContextMock;
private @Mock @NonNullByDefault({}) HttpService httpServiceMock;
private @Mock @NonNullByDefault({}) HttpContext httpContextMock;

@BeforeEach
public void setupServerAndClient() {
Expand Down Expand Up @@ -115,7 +116,7 @@ protected Request getHttpRequest(String url) {
return httpClient.newRequest(url).method(HttpMethod.GET);
}

protected String serveStream(AudioStream stream, Integer timeInterval) throws Exception {
protected String serveStream(AudioStream stream, @Nullable Integer timeInterval) throws Exception {
serverStarted.get(); // wait for the server thread to be started

String path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.util.Locale;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -41,19 +42,16 @@
* @author Christoph Weitkamp - Added parameter to adjust the volume
* @author Wouter Born - Migrate tests from Groovy to Java
*/
@NonNullByDefault
public class AudioConsoleTest extends AbstractAudioServletTest {

private BundledSoundFileHandler fileHandler;

private AudioConsoleCommandExtension audioConsoleCommandExtension;

private AudioManagerImpl audioManager;

private AudioSinkFake audioSink;
private @NonNullByDefault({}) AudioConsoleCommandExtension audioConsoleCommandExtension;
private @NonNullByDefault({}) AudioManagerImpl audioManager;
private @NonNullByDefault({}) AudioSinkFake audioSink;
private @NonNullByDefault({}) String consoleOutput;
private @NonNullByDefault({}) BundledSoundFileHandler fileHandler;

private final byte[] testByteArray = new byte[] { 0, 1, 2 };

private String consoleOutput;
private final Console consoleMock = new Console() {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.HashSet;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.openhab.core.audio.AudioFormat;

Expand All @@ -27,6 +28,7 @@
* @author Petar Valchev - Initial contribution
* @author Wouter Born - Migrate tests from Groovy to Java
*/
@NonNullByDefault
public class AudioFormatTest {
private final String testContainer = AudioFormat.CONTAINER_WAVE;
private final String testCodec = AudioFormat.CODEC_PCM_SIGNED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.core.audio.AudioFormat;
Expand All @@ -31,11 +33,11 @@
* @author Wouter Born - Migrate tests from Groovy to Java
* @author Henning Treu - extract servlet tests
*/
@NonNullByDefault
public class AudioManagerServletTest extends AbstractAudioServletTest {

private AudioManagerImpl audioManager;

private AudioSinkFake audioSink;
private @NonNullByDefault({}) AudioManagerImpl audioManager;
private @NonNullByDefault({}) AudioSinkFake audioSink;

@BeforeEach
public void setup() {
Expand All @@ -62,7 +64,7 @@ public void audioManagerDoesNotProcessStreamsIfThereIsNoRegisteredSink() throws
assertServedStream(streamTimeout);
}

private void assertServedStream(Integer timeInterval) throws Exception {
private void assertServedStream(@Nullable Integer timeInterval) throws Exception {
AudioStream audioStream = getByteArrayAudioStream(AudioFormat.CONTAINER_WAVE, AudioFormat.CODEC_PCM_SIGNED);
String url = serveStream(audioStream, timeInterval);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiFunction;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -50,14 +52,13 @@
* @author Wouter Born - Migrate tests from Groovy to Java
* @author Henning Treu - Convert to plain java tests
*/
@NonNullByDefault
public class AudioManagerTest {

private BundledSoundFileHandler fileHandler;

private AudioManagerImpl audioManager;

private AudioSinkFake audioSink;
private AudioSource audioSource;
private @NonNullByDefault({}) AudioManagerImpl audioManager;
private @NonNullByDefault({}) AudioSinkFake audioSink;
private @NonNullByDefault({}) AudioSource audioSource;
private @NonNullByDefault({}) BundledSoundFileHandler fileHandler;

@BeforeEach
public void setup() throws IOException {
Expand Down Expand Up @@ -283,12 +284,12 @@ private void assertAddedParameterOption(String param, Locale locale) {
case AudioManagerImpl.CONFIG_DEFAULT_SINK:
audioManager.addAudioSink(audioSink);
id = audioSink.getId();
label = audioSink.getLabel(locale);
label = Objects.requireNonNull(audioSink.getLabel(locale));
break;
case AudioManagerImpl.CONFIG_DEFAULT_SOURCE:
audioManager.addAudioSource(audioSource);
id = audioSource.getId();
label = audioSource.getLabel(locale);
label = Objects.requireNonNull(audioSource.getLabel(locale));
break;
default:
fail("The parameter must be either default sink or default source");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.File;
import java.util.concurrent.TimeUnit;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.http.HttpStatus;
import org.junit.jupiter.api.Test;
Expand All @@ -32,6 +33,7 @@
* @author Petar Valchev - Initial contribution
* @author Wouter Born - Migrate tests from Groovy to Java
*/
@NonNullByDefault
public class AudioServletTest extends AbstractAudioServletTest {

private static final String MEDIA_TYPE_AUDIO_WAV = "audio/wav";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.nio.file.Path;
import java.util.Comparator;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.OpenHAB;
import org.openhab.core.audio.internal.AudioManagerTest;
import org.slf4j.Logger;
Expand All @@ -32,6 +33,7 @@
*
* @author Markus Rathgeb - Initial contribution
*/
@NonNullByDefault
public class BundledSoundFileHandler implements Closeable {
private static final String MP3_FILE_NAME = "mp3AudioFile.mp3";
private static final String WAV_FILE_NAME = "wavAudioFile.wav";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ protected boolean createAndLoad(ScriptFileReference ref) {
} else {
logger.error("Script loading error, ignoring file: {}", fileName);
}

} catch (IOException e) {
logger.error("Failed to load file '{}': {}", ref.getScriptFileURL().getFile(), e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
*/
package org.openhab.core.automation;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* Marker interface for RuleActions
*
* Every method in the implementation should provide annotations which are used to create the ModuleTypes
*
* @author Stefan Triller - Initial contribution
*/
@NonNullByDefault
public interface AnnotatedActions {

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.openhab.core.automation;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* This enumeration is used to present the main status of a {@link Rule}.
* <table>
Expand Down Expand Up @@ -73,6 +75,7 @@
* @author Kai Kreuzer - Refactored to match ThingStatus implementation
* @author Ana Dimova - add java doc
*/
@NonNullByDefault
public enum RuleStatus {
UNINITIALIZED(1),
INITIALIZING(2),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.openhab.core.automation;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* This enumeration is used to represent a detail of a {@link RuleStatus}. It can be considered as a sub-status.
* It shows the specific reasons why the status of the rule is like as is.
Expand Down Expand Up @@ -81,6 +83,7 @@
* @author Kai Kreuzer - Refactored to match ThingStatusDetail implementation
* @author Ana Dimova - add java doc
*/
@NonNullByDefault
public enum RuleStatusDetail {
NONE(0),
HANDLER_MISSING_ERROR(1),
Expand Down
Loading

0 comments on commit ad936cd

Please sign in to comment.