adapt to core StringUtils (#15781)
Signed-off-by: Leo Siepel <leosiepel@gmail.com>
This commit is contained in:
parent
bf0cc2ba0f
commit
7d63504eee
|
@ -1,47 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright (c) 2010-2023 Contributors to the openHAB project
|
|
||||||
*
|
|
||||||
* See the NOTICE file(s) distributed with this work for additional
|
|
||||||
* information.
|
|
||||||
*
|
|
||||||
* This program and the accompanying materials are made available under the
|
|
||||||
* terms of the Eclipse Public License 2.0 which is available at
|
|
||||||
* http://www.eclipse.org/legal/epl-2.0
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: EPL-2.0
|
|
||||||
*/
|
|
||||||
package org.openhab.binding.bluetooth.util;
|
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This is a n string utility class
|
|
||||||
*
|
|
||||||
* @author Leo Siepel - Initial contribution
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@NonNullByDefault
|
|
||||||
public class StringUtil {
|
|
||||||
|
|
||||||
public static String randomString(int length, String charset) {
|
|
||||||
StringBuilder sb = new StringBuilder(length);
|
|
||||||
for (int i = 0; i < length; i++) {
|
|
||||||
int index = (int) (charset.length() * Math.random());
|
|
||||||
sb.append(charset.charAt(index));
|
|
||||||
}
|
|
||||||
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String randomAlphabetic(int length) {
|
|
||||||
return StringUtil.randomString(length, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvxyz");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String randomHex(int length) {
|
|
||||||
return StringUtil.randomString(length, "0123456789ABCDEF");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String randomAlphanummeric(int length) {
|
|
||||||
return StringUtil.randomString(length, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvxyz");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -13,8 +13,8 @@
|
||||||
package org.openhab.binding.bluetooth;
|
package org.openhab.binding.bluetooth;
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.openhab.binding.bluetooth.util.StringUtil;
|
|
||||||
import org.openhab.core.thing.ThingUID;
|
import org.openhab.core.thing.ThingUID;
|
||||||
|
import org.openhab.core.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contains general utilities used for bluetooth tests
|
* Contains general utilities used for bluetooth tests
|
||||||
|
@ -27,14 +27,14 @@ public class TestUtils {
|
||||||
public static BluetoothAddress randomAddress() {
|
public static BluetoothAddress randomAddress() {
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
for (int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
builder.append(StringUtil.randomHex(2));
|
builder.append(StringUtils.getRandomHex(2));
|
||||||
builder.append(":");
|
builder.append(":");
|
||||||
}
|
}
|
||||||
builder.append(StringUtil.randomHex(2));
|
builder.append(StringUtils.getRandomHex(2));
|
||||||
return new BluetoothAddress(builder.toString());
|
return new BluetoothAddress(builder.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ThingUID randomThingUID() {
|
public static ThingUID randomThingUID() {
|
||||||
return new ThingUID(BluetoothBindingConstants.BINDING_ID, StringUtil.randomAlphabetic(6));
|
return new ThingUID(BluetoothBindingConstants.BINDING_ID, StringUtils.getRandomAlphabetic(6));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,12 +47,12 @@ import org.openhab.binding.bluetooth.TestUtils;
|
||||||
import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryDevice;
|
import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryDevice;
|
||||||
import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryParticipant;
|
import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryParticipant;
|
||||||
import org.openhab.binding.bluetooth.notification.BluetoothConnectionStatusNotification;
|
import org.openhab.binding.bluetooth.notification.BluetoothConnectionStatusNotification;
|
||||||
import org.openhab.binding.bluetooth.util.StringUtil;
|
|
||||||
import org.openhab.core.config.discovery.DiscoveryListener;
|
import org.openhab.core.config.discovery.DiscoveryListener;
|
||||||
import org.openhab.core.config.discovery.DiscoveryResult;
|
import org.openhab.core.config.discovery.DiscoveryResult;
|
||||||
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
|
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
|
||||||
import org.openhab.core.thing.ThingTypeUID;
|
import org.openhab.core.thing.ThingTypeUID;
|
||||||
import org.openhab.core.thing.ThingUID;
|
import org.openhab.core.thing.ThingUID;
|
||||||
|
import org.openhab.core.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests {@link BluetoothDiscoveryService}.
|
* Tests {@link BluetoothDiscoveryService}.
|
||||||
|
@ -196,7 +196,7 @@ public class BluetoothDiscoveryServiceTest {
|
||||||
|
|
||||||
MockBluetoothAdapter mockAdapter1 = new MockBluetoothAdapter();
|
MockBluetoothAdapter mockAdapter1 = new MockBluetoothAdapter();
|
||||||
MockBluetoothDevice mockDevice = mockAdapter1.getDevice(address);
|
MockBluetoothDevice mockDevice = mockAdapter1.getDevice(address);
|
||||||
String deviceName = StringUtil.randomAlphanummeric(10);
|
String deviceName = StringUtils.getRandomAlphanumeric(10);
|
||||||
mockDevice.setDeviceName(deviceName);
|
mockDevice.setDeviceName(deviceName);
|
||||||
|
|
||||||
BluetoothDevice device = Mockito.spy(mockDevice);
|
BluetoothDevice device = Mockito.spy(mockDevice);
|
||||||
|
@ -223,7 +223,7 @@ public class BluetoothDiscoveryServiceTest {
|
||||||
MockBluetoothAdapter mockAdapter2 = new MockBluetoothAdapter();
|
MockBluetoothAdapter mockAdapter2 = new MockBluetoothAdapter();
|
||||||
MockBluetoothDevice mockDevice1 = mockAdapter1.getDevice(address);
|
MockBluetoothDevice mockDevice1 = mockAdapter1.getDevice(address);
|
||||||
MockBluetoothDevice mockDevice2 = mockAdapter2.getDevice(address);
|
MockBluetoothDevice mockDevice2 = mockAdapter2.getDevice(address);
|
||||||
String deviceName = StringUtil.randomAlphanummeric(10);
|
String deviceName = StringUtils.getRandomAlphanumeric(10);
|
||||||
mockDevice1.setDeviceName(deviceName);
|
mockDevice1.setDeviceName(deviceName);
|
||||||
mockDevice2.setDeviceName(deviceName);
|
mockDevice2.setDeviceName(deviceName);
|
||||||
|
|
||||||
|
@ -261,7 +261,7 @@ public class BluetoothDiscoveryServiceTest {
|
||||||
public void nonConnectionParticipantTest() {
|
public void nonConnectionParticipantTest() {
|
||||||
MockBluetoothAdapter mockAdapter1 = new MockBluetoothAdapter();
|
MockBluetoothAdapter mockAdapter1 = new MockBluetoothAdapter();
|
||||||
MockBluetoothDevice mockDevice = mockAdapter1.getDevice(TestUtils.randomAddress());
|
MockBluetoothDevice mockDevice = mockAdapter1.getDevice(TestUtils.randomAddress());
|
||||||
String deviceName = StringUtil.randomAlphanummeric(10);
|
String deviceName = StringUtils.getRandomAlphanumeric(10);
|
||||||
mockDevice.setDeviceName(deviceName);
|
mockDevice.setDeviceName(deviceName);
|
||||||
|
|
||||||
BluetoothDevice device = Mockito.spy(mockDevice);
|
BluetoothDevice device = Mockito.spy(mockDevice);
|
||||||
|
@ -418,7 +418,7 @@ public class BluetoothDiscoveryServiceTest {
|
||||||
MockBluetoothAdapter mockAdapter2 = new MockBluetoothAdapter();
|
MockBluetoothAdapter mockAdapter2 = new MockBluetoothAdapter();
|
||||||
MockBluetoothDevice mockDevice1 = mockAdapter1.getDevice(address);
|
MockBluetoothDevice mockDevice1 = mockAdapter1.getDevice(address);
|
||||||
MockBluetoothDevice mockDevice2 = mockAdapter2.getDevice(address);
|
MockBluetoothDevice mockDevice2 = mockAdapter2.getDevice(address);
|
||||||
String deviceName = StringUtil.randomAlphanummeric(10);
|
String deviceName = StringUtils.getRandomAlphanumeric(10);
|
||||||
|
|
||||||
MockDiscoveryParticipant participant2 = new MockDiscoveryParticipant() {
|
MockDiscoveryParticipant participant2 = new MockDiscoveryParticipant() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -534,7 +534,7 @@ public class BluetoothDiscoveryServiceTest {
|
||||||
private ThingTypeUID typeUID;
|
private ThingTypeUID typeUID;
|
||||||
|
|
||||||
public MockDiscoveryParticipant() {
|
public MockDiscoveryParticipant() {
|
||||||
this.typeUID = new ThingTypeUID(BluetoothBindingConstants.BINDING_ID, StringUtil.randomAlphabetic(6));
|
this.typeUID = new ThingTypeUID(BluetoothBindingConstants.BINDING_ID, StringUtils.getRandomAlphabetic(6));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -544,20 +544,20 @@ public class BluetoothDiscoveryServiceTest {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
|
public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
|
||||||
String repProp = StringUtil.randomAlphabetic(6);
|
String repProp = StringUtils.getRandomAlphabetic(6);
|
||||||
ThingUID thingUID = getThingUID(device);
|
ThingUID thingUID = getThingUID(device);
|
||||||
if (thingUID == null) {
|
if (thingUID == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return DiscoveryResultBuilder.create(thingUID).withLabel(StringUtil.randomAlphabetic(6))
|
return DiscoveryResultBuilder.create(thingUID).withLabel(StringUtils.getRandomAlphabetic(6))
|
||||||
.withProperty(repProp, StringUtil.randomAlphabetic(6)).withRepresentationProperty(repProp)
|
.withProperty(repProp, StringUtils.getRandomAlphabetic(6)).withRepresentationProperty(repProp)
|
||||||
.withBridge(device.getAdapter().getUID()).build();
|
.withBridge(device.getAdapter().getUID()).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
|
public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
|
||||||
String deviceName = device.getName();
|
String deviceName = device.getName();
|
||||||
String id = deviceName != null ? deviceName : StringUtil.randomAlphabetic(6);
|
String id = deviceName != null ? deviceName : StringUtils.getRandomAlphabetic(6);
|
||||||
return new ThingUID(typeUID, device.getAdapter().getUID(), id);
|
return new ThingUID(typeUID, device.getAdapter().getUID(), id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue