Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
remove: Potential NP access for getChannel(String) (#4155)
Browse files Browse the repository at this point in the history
Signed-off-by: Markus Rathgeb <maggu2810@gmail.com>
  • Loading branch information
maggu2810 authored and kaikreuzer committed Aug 30, 2017
1 parent 29b3acf commit ceface5
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.List;
import java.util.Locale;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.smarthome.config.core.Configuration;
import org.eclipse.smarthome.core.common.registry.ProviderChangeListener;
import org.eclipse.smarthome.core.items.Item;
Expand All @@ -28,6 +29,7 @@
import org.eclipse.smarthome.core.library.items.NumberItem;
import org.eclipse.smarthome.core.library.items.StringItem;
import org.eclipse.smarthome.core.library.items.SwitchItem;
import org.eclipse.smarthome.core.thing.Channel;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.ManagedThingProvider;
import org.eclipse.smarthome.core.thing.Thing;
Expand Down Expand Up @@ -182,6 +184,16 @@ public void teardown() {
});
}

private static @NonNull Channel getChannel(final @NonNull Thing thing, final @NonNull String channelId) {
final Channel channel = thing.getChannel(channelId);
if (channel == null) {
throw new IllegalArgumentException(String.format("The thing '%s' does not seems to contain a channel '%s'.",
thing.getUID(), channelId));
} else {
return channel;
}
}

/**
* Assert that item's state description is present.
*/
Expand All @@ -194,17 +206,17 @@ public void presentItemStateDescription() throws ItemNotFoundException {
null, "test thing", new Configuration());
assertNotNull(thing);
managedThingProvider.add(thing);
ItemChannelLink link = new ItemChannelLink("TestItem", thing.getChannel("1").getUID());
ItemChannelLink link = new ItemChannelLink("TestItem", getChannel(thing, "1").getUID());
linkRegistry.add(link);
link = new ItemChannelLink("TestItem2", thing.getChannel("2").getUID());
link = new ItemChannelLink("TestItem2", getChannel(thing, "2").getUID());
linkRegistry.add(link);
link = new ItemChannelLink("TestItem3", thing.getChannel("3").getUID());
link = new ItemChannelLink("TestItem3", getChannel(thing, "3").getUID());
linkRegistry.add(link);
link = new ItemChannelLink("TestItem4", thing.getChannel("4").getUID());
link = new ItemChannelLink("TestItem4", getChannel(thing, "4").getUID());
linkRegistry.add(link);
link = new ItemChannelLink("TestItem5", thing.getChannel("5").getUID());
link = new ItemChannelLink("TestItem5", getChannel(thing, "5").getUID());
linkRegistry.add(link);
link = new ItemChannelLink("TestItem6", thing.getChannel("6").getUID());
link = new ItemChannelLink("TestItem6", getChannel(thing, "6").getUID());
linkRegistry.add(link);
//
final Collection<Item> items = itemRegistry.getItems();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,13 @@ public void publishPlanet() {
*/
public void publishChannelIfLinked(ChannelUID channelUID) {
if (isLinked(channelUID.getId()) && getPlanet() != null) {
final Channel channel = getThing().getChannel(channelUID.getId());
if (channel == null) {
logger.error("Cannot find channel for {}", channelUID);
return;
}
try {
AstroChannelConfig config = getThing().getChannel(channelUID.getId()).getConfiguration()
.as(AstroChannelConfig.class);
AstroChannelConfig config = channel.getConfiguration().as(AstroChannelConfig.class);
updateState(channelUID, PropertyUtils.getState(channelUID, config, getPlanet()));
} catch (Exception ex) {
logger.error("Can't update state for channel {} : {}", channelUID, ex.getMessage(), ex);
Expand Down Expand Up @@ -263,11 +267,12 @@ && isLinked(channel.getUID().getId())) {
* Emits an event for the given channel.
*/
public void triggerEvent(String channelId, String event) {
if (getThing().getChannel(channelId) == null) {
final Channel channel = getThing().getChannel(channelId);
if (channel == null) {
logger.warn("Event {} in thing {} does not exist, please recreate the thing", event, getThing().getUID());
return;
}
triggerChannel(getThing().getChannel(channelId).getUID(), event);
triggerChannel(channel.getUID(), event);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,12 @@ public static void scheduleEvent(String thingUID, AstroThingHandler astroHandler
}
final Calendar instant;
if (!configAlreadyApplied) {
AstroChannelConfig config = astroHandler.getThing().getChannel(channelId).getConfiguration()
.as(AstroChannelConfig.class);
final Channel channel = astroHandler.getThing().getChannel(channelId);
if (channel == null) {
logger.warn("Cannot find channel '{}' for thing '{}'.", channelId, astroHandler.getThing().getUID());
return;
}
AstroChannelConfig config = channel.getConfiguration().as(AstroChannelConfig.class);
instant = applyConfig(eventAt, config);
} else {
instant = eventAt;
Expand Down Expand Up @@ -132,8 +136,12 @@ public static void scheduleRange(String thingUID, AstroThingHandler astroHandler
return;
}

AstroChannelConfig config = astroHandler.getThing().getChannel(channelId).getConfiguration()
.as(AstroChannelConfig.class);
final Channel channel = astroHandler.getThing().getChannel(channelId);
if (channel == null) {
logger.warn("Cannot find channel '{}' for thing '{}'.", channelId, astroHandler.getThing().getUID());
return;
}
AstroChannelConfig config = channel.getConfiguration().as(AstroChannelConfig.class);
Calendar configStart = applyConfig(start, config);
Calendar configEnd = applyConfig(end, config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import javax.servlet.ServletException;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.smarthome.binding.fsinternetradio.FSInternetRadioBindingConstants;
import org.eclipse.smarthome.binding.fsinternetradio.handler.FSInternetRadioHandler;
import org.eclipse.smarthome.binding.fsinternetradio.handler.HandlerUtils;
Expand Down Expand Up @@ -55,6 +56,7 @@
import org.eclipse.smarthome.core.types.UnDefType;
import org.eclipse.smarthome.test.java.JavaOSGiTest;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -145,6 +147,18 @@ public void tearDown() {
clearLinks();
}

private static @NonNull Channel getChannel(final @NonNull Thing thing, final @NonNull String channelId) {
final Channel channel = thing.getChannel(channelId);
Assert.assertNotNull(channel);
return channel;
}

private static @NonNull ChannelUID getChannelUID(final @NonNull Thing thing, final @NonNull String channelId) {
final ChannelUID channelUID = getChannel(thing, channelId).getUID();
Assert.assertNotNull(channelUID);
return channelUID;
}

/**
* Verify OFFLINE Thing status when the IP is NULL.
*/
Expand Down Expand Up @@ -210,7 +224,7 @@ public void offlineIfParseError() {
// turn-on the radio
turnTheRadioOn(radioThing);

ChannelUID modeChannelUID = radioThing.getChannel(modeChannelID).getUID();
ChannelUID modeChannelUID = getChannelUID(radioThing, modeChannelID);

/*
* Setting the isInvalidResponseExpected variable to true
Expand Down Expand Up @@ -267,7 +281,7 @@ public void httpStatusNokHandling() {
*/
servlet.setOKAnswerExpected(false);

ChannelUID modeChannelUID = radioThing.getChannel(modeChannelID).getUID();
ChannelUID modeChannelUID = getChannelUID(radioThing, modeChannelID);
Item modeTestItem = initializeItem(modeChannelUID, "mode", acceptedItemType);

servlet.setInvalidResponseExpected(true);
Expand Down Expand Up @@ -337,7 +351,7 @@ public void muteChhannelUpdated() {

turnTheRadioOn(radioThing);

ChannelUID muteChannelUID = radioThing.getChannel(FSInternetRadioBindingConstants.CHANNEL_MUTE).getUID();
ChannelUID muteChannelUID = getChannelUID(radioThing, FSInternetRadioBindingConstants.CHANNEL_MUTE);
Item muteTestItem = initializeItem(muteChannelUID, DEFAULT_TEST_ITEM_NAME, acceptedItemType);

radioHandler.handleCommand(muteChannelUID, OnOffType.ON);
Expand Down Expand Up @@ -376,7 +390,7 @@ public void modeChannelUdpated() {

turnTheRadioOn(radioThing);

ChannelUID modeChannelUID = radioThing.getChannel(modeChannelID).getUID();
ChannelUID modeChannelUID = getChannelUID(radioThing, modeChannelID);
Item modeTestItem = initializeItem(modeChannelUID, DEFAULT_TEST_ITEM_NAME, acceptedItemType);

radioHandler.handleCommand(modeChannelUID, DecimalType.valueOf("1"));
Expand Down Expand Up @@ -410,7 +424,7 @@ public void volumechannelUpdatedAbsIncDec() {

turnTheRadioOn(radioThing);

ChannelUID absoluteVolumeChannelUID = radioThing.getChannel(absoluteVolumeChannelID).getUID();
ChannelUID absoluteVolumeChannelUID = getChannelUID(radioThing, absoluteVolumeChannelID);
Item volumeTestItem = initializeItem(absoluteVolumeChannelUID, DEFAULT_TEST_ITEM_NAME,
absoluteAcceptedItemType);

Expand All @@ -431,7 +445,7 @@ public void volumeChannelUpdatedAbsUpDown() {

turnTheRadioOn(radioThing);

ChannelUID absoluteVolumeChannelUID = radioThing.getChannel(absoluteVolumeChannelID).getUID();
ChannelUID absoluteVolumeChannelUID = getChannelUID(radioThing, absoluteVolumeChannelID);
Item volumeTestItem = initializeItem(absoluteVolumeChannelUID, DEFAULT_TEST_ITEM_NAME,
absoluteAcceptedItemType);

Expand All @@ -452,7 +466,7 @@ public void invalidAbsVolumeValues() {

turnTheRadioOn(radioThing);

ChannelUID absoluteVolumeChannelUID = radioThing.getChannel(absoluteVolumeChannelID).getUID();
ChannelUID absoluteVolumeChannelUID = getChannelUID(radioThing, absoluteVolumeChannelID);
Item volumeTestItem = initializeItem(absoluteVolumeChannelUID, DEFAULT_TEST_ITEM_NAME,
absoluteAcceptedItemType);

Expand Down Expand Up @@ -520,11 +534,11 @@ public void volumeChannelUpdatedPercIncDec() {

turnTheRadioOn(radioThing);

ChannelUID absoluteVolumeChannelUID = radioThing.getChannel(absoluteVolumeChannelID).getUID();
ChannelUID absoluteVolumeChannelUID = getChannelUID(radioThing, absoluteVolumeChannelID);
Item volumeTestItem = initializeItem(absoluteVolumeChannelUID, DEFAULT_TEST_ITEM_NAME,
absoluteAcceptedItemType);

ChannelUID percentVolumeChannelUID = radioThing.getChannel(percentVolumeChannelID).getUID();
ChannelUID percentVolumeChannelUID = getChannelUID(radioThing, percentVolumeChannelID);

testChannelWithINCREASEAndDECREASECommands(percentVolumeChannelUID, volumeTestItem);
}
Expand Down Expand Up @@ -553,11 +567,11 @@ public void volumeChannelUpdatedPercUpDown() {

turnTheRadioOn(radioThing);

ChannelUID absoluteVolumeChannelUID = radioThing.getChannel(absoluteVolumeChannelID).getUID();
ChannelUID absoluteVolumeChannelUID = getChannelUID(radioThing, absoluteVolumeChannelID);
Item volumeTestItem = initializeItem(absoluteVolumeChannelUID, DEFAULT_TEST_ITEM_NAME,
absoluteAcceptedItemType);

ChannelUID percentVolumeChannelUID = radioThing.getChannel(percentVolumeChannelID).getUID();
ChannelUID percentVolumeChannelUID = getChannelUID(radioThing, percentVolumeChannelID);

testChannelWithUPAndDOWNCommands(percentVolumeChannelUID, volumeTestItem);
}
Expand All @@ -581,11 +595,11 @@ public void validInvalidPercVolume() {

turnTheRadioOn(radioThing);

ChannelUID absoluteVolumeChannelUID = radioThing.getChannel(absoluteVolumeChannelID).getUID();
ChannelUID absoluteVolumeChannelUID = getChannelUID(radioThing, absoluteVolumeChannelID);
Item volumeTestItem = initializeItem(absoluteVolumeChannelUID, DEFAULT_TEST_ITEM_NAME,
absoluteAcceptedItemType);

ChannelUID percentVolumeChannelUID = radioThing.getChannel(percentVolumeChannelID).getUID();
ChannelUID percentVolumeChannelUID = getChannelUID(radioThing, percentVolumeChannelID);

/*
* Giving the handler a valid percent value. According to the FrontierSiliconRadio's
Expand Down Expand Up @@ -676,7 +690,7 @@ public void presetChannelUpdated() {
testRadioThingConsideringConfiguration(radioThing);
turnTheRadioOn(radioThing);

ChannelUID presetChannelUID = radioThing.getChannel(FSInternetRadioBindingConstants.CHANNEL_PRESET).getUID();
ChannelUID presetChannelUID = getChannelUID(radioThing, FSInternetRadioBindingConstants.CHANNEL_PRESET);
Item presetTestItem = initializeItem(presetChannelUID, DEFAULT_TEST_ITEM_NAME, acceptedItemType);

radioHandler.handleCommand(presetChannelUID, DecimalType.valueOf("100"));
Expand All @@ -699,8 +713,8 @@ public void playInfoNameChannelUpdated() {

turnTheRadioOn(radioThing);

ChannelUID playInfoNameChannelUID = radioThing
.getChannel(FSInternetRadioBindingConstants.CHANNEL_PLAY_INFO_NAME).getUID();
ChannelUID playInfoNameChannelUID = getChannelUID(radioThing,
FSInternetRadioBindingConstants.CHANNEL_PLAY_INFO_NAME);
Item playInfoNameTestItem = initializeItem(playInfoNameChannelUID, DEFAULT_TEST_ITEM_NAME, acceptedItemType);

waitForAssert(() -> {
Expand All @@ -722,8 +736,8 @@ public void playInfoTextChannelUpdated() {

turnTheRadioOn(radioThing);

ChannelUID playInfoTextChannelUID = radioThing
.getChannel(FSInternetRadioBindingConstants.CHANNEL_PLAY_INFO_TEXT).getUID();
ChannelUID playInfoTextChannelUID = getChannelUID(radioThing,
FSInternetRadioBindingConstants.CHANNEL_PLAY_INFO_TEXT);
Item playInfoTextTestItem = initializeItem(playInfoTextChannelUID, DEFAULT_TEST_ITEM_NAME, acceptedItemType);

waitForAssert(() -> {
Expand Down Expand Up @@ -905,7 +919,7 @@ private Thing initializeRadioThing(Configuration config) {
}

private void turnTheRadioOn(Thing radioThing) {
radioHandler.handleCommand(radioThing.getChannel(FSInternetRadioBindingConstants.CHANNEL_POWER).getUID(),
radioHandler.handleCommand(getChannelUID(radioThing, FSInternetRadioBindingConstants.CHANNEL_POWER),
OnOffType.ON);

final FrontierSiliconRadio radio = HandlerUtils.getRadio(radioHandler);
Expand Down

0 comments on commit ceface5

Please sign in to comment.