From a3e24bcfde295ab49cd06961cb09cf2c94f96537 Mon Sep 17 00:00:00 2001 From: lsiepel Date: Sat, 21 Oct 2023 12:03:26 +0200 Subject: [PATCH] adapt to core StringUtils (#15781) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Leo Siepel Signed-off-by: Jørgen Austvik --- .../binding/bluetooth/util/StringUtil.java | 47 ------------------- .../openhab/binding/bluetooth/TestUtils.java | 8 ++-- .../BluetoothDiscoveryServiceTest.java | 20 ++++---- 3 files changed, 14 insertions(+), 61 deletions(-) delete mode 100644 bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/util/StringUtil.java diff --git a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/util/StringUtil.java b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/util/StringUtil.java deleted file mode 100644 index 6365665f662df..0000000000000 --- a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/util/StringUtil.java +++ /dev/null @@ -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"); - } -} diff --git a/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/TestUtils.java b/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/TestUtils.java index 0303ba6818fb8..3b3bc11b138a1 100644 --- a/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/TestUtils.java +++ b/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/TestUtils.java @@ -13,8 +13,8 @@ package org.openhab.binding.bluetooth; import org.eclipse.jdt.annotation.NonNullByDefault; -import org.openhab.binding.bluetooth.util.StringUtil; import org.openhab.core.thing.ThingUID; +import org.openhab.core.util.StringUtils; /** * Contains general utilities used for bluetooth tests @@ -27,14 +27,14 @@ public class TestUtils { public static BluetoothAddress randomAddress() { StringBuilder builder = new StringBuilder(); for (int i = 0; i < 5; i++) { - builder.append(StringUtil.randomHex(2)); + builder.append(StringUtils.getRandomHex(2)); builder.append(":"); } - builder.append(StringUtil.randomHex(2)); + builder.append(StringUtils.getRandomHex(2)); return new BluetoothAddress(builder.toString()); } public static ThingUID randomThingUID() { - return new ThingUID(BluetoothBindingConstants.BINDING_ID, StringUtil.randomAlphabetic(6)); + return new ThingUID(BluetoothBindingConstants.BINDING_ID, StringUtils.getRandomAlphabetic(6)); } } diff --git a/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDiscoveryServiceTest.java b/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDiscoveryServiceTest.java index aae70b1e8595c..2c94607f06f44 100644 --- a/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDiscoveryServiceTest.java +++ b/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDiscoveryServiceTest.java @@ -47,12 +47,12 @@ import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryDevice; import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryParticipant; 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.DiscoveryResult; import org.openhab.core.config.discovery.DiscoveryResultBuilder; import org.openhab.core.thing.ThingTypeUID; import org.openhab.core.thing.ThingUID; +import org.openhab.core.util.StringUtils; /** * Tests {@link BluetoothDiscoveryService}. @@ -196,7 +196,7 @@ public void connectionParticipantTest() { MockBluetoothAdapter mockAdapter1 = new MockBluetoothAdapter(); MockBluetoothDevice mockDevice = mockAdapter1.getDevice(address); - String deviceName = StringUtil.randomAlphanummeric(10); + String deviceName = StringUtils.getRandomAlphanumeric(10); mockDevice.setDeviceName(deviceName); BluetoothDevice device = Mockito.spy(mockDevice); @@ -223,7 +223,7 @@ public void multiDiscoverySingleConnectionTest() { MockBluetoothAdapter mockAdapter2 = new MockBluetoothAdapter(); MockBluetoothDevice mockDevice1 = mockAdapter1.getDevice(address); MockBluetoothDevice mockDevice2 = mockAdapter2.getDevice(address); - String deviceName = StringUtil.randomAlphanummeric(10); + String deviceName = StringUtils.getRandomAlphanumeric(10); mockDevice1.setDeviceName(deviceName); mockDevice2.setDeviceName(deviceName); @@ -261,7 +261,7 @@ public void multiDiscoverySingleConnectionTest() { public void nonConnectionParticipantTest() { MockBluetoothAdapter mockAdapter1 = new MockBluetoothAdapter(); MockBluetoothDevice mockDevice = mockAdapter1.getDevice(TestUtils.randomAddress()); - String deviceName = StringUtil.randomAlphanummeric(10); + String deviceName = StringUtils.getRandomAlphanumeric(10); mockDevice.setDeviceName(deviceName); BluetoothDevice device = Mockito.spy(mockDevice); @@ -418,7 +418,7 @@ public void recursiveFutureTest() throws InterruptedException { MockBluetoothAdapter mockAdapter2 = new MockBluetoothAdapter(); MockBluetoothDevice mockDevice1 = mockAdapter1.getDevice(address); MockBluetoothDevice mockDevice2 = mockAdapter2.getDevice(address); - String deviceName = StringUtil.randomAlphanummeric(10); + String deviceName = StringUtils.getRandomAlphanumeric(10); MockDiscoveryParticipant participant2 = new MockDiscoveryParticipant() { @Override @@ -534,7 +534,7 @@ private class MockDiscoveryParticipant implements BluetoothDiscoveryParticipant private ThingTypeUID typeUID; public MockDiscoveryParticipant() { - this.typeUID = new ThingTypeUID(BluetoothBindingConstants.BINDING_ID, StringUtil.randomAlphabetic(6)); + this.typeUID = new ThingTypeUID(BluetoothBindingConstants.BINDING_ID, StringUtils.getRandomAlphabetic(6)); } @Override @@ -544,20 +544,20 @@ public Set getSupportedThingTypeUIDs() { @Override public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) { - String repProp = StringUtil.randomAlphabetic(6); + String repProp = StringUtils.getRandomAlphabetic(6); ThingUID thingUID = getThingUID(device); if (thingUID == null) { return null; } - return DiscoveryResultBuilder.create(thingUID).withLabel(StringUtil.randomAlphabetic(6)) - .withProperty(repProp, StringUtil.randomAlphabetic(6)).withRepresentationProperty(repProp) + return DiscoveryResultBuilder.create(thingUID).withLabel(StringUtils.getRandomAlphabetic(6)) + .withProperty(repProp, StringUtils.getRandomAlphabetic(6)).withRepresentationProperty(repProp) .withBridge(device.getAdapter().getUID()).build(); } @Override public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) { 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); } }