Improve test stability and OS compatibility (#2878)

This fixes the build being broken when building on Windows.
It also contains many fixes for timing issues which seem to impact macOS and Windows more than Linux.

* Fix .gitattributes and add *.xml_gen to fix line ending issues on Windows
* Derive fork count from CPU details in org.openhab.core tests for more stable tests on machines with fewer cores
* Adjust SafeCallerImplTest timings
* Increase ExecUtilTest timeout
* Increase SchedulerImplTest timeouts
* Increase AudioConsoleTest serveStream timeout
* Increase AudioServletTest serveStream timeout
* Increase SchedulerImplTest test timeouts
* Increase ExpireManagerTest timeout used for checking published events
* Increase PeriodicSchedulerImplTest max allowed delta
* Increase SchedulerImplTest timeouts
* Fix BundleInfoReader file stream not closed causing temp dir deletion issues on Windows
* Fix GenerateDefaultTranslationsMojoTest Windows line endings issues
* Fix GenerateDefaultTranslationsMojoTest Windows temp dir deletion problem
* Fix GenericItemProviderTest tearDown sometimes fails because of queued events
* Fix ChannelLinkNotifierOSGiTest wait for channel link events
* Fix ChannelCommandDescriptionProviderOSGiTest fails if provider not immediately registered
* Fix ChannelStateDescriptionProviderOSGiTest fails if provider not immediately registered
* Fix GenericItemChannelLinkProviderTest not waiting for async updated state to become true
* Fix GenericThingProviderTest failing due to events of previous test
* Fix InboxOSGiTest sometimes fails because of queued events
* Fix ScriptEngineOSGiTest failing because items are not yet added to registry
* Fix ThingManagerOSGiTest failing due to async handleRemoval call

Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
Wouter Born 2022-04-14 23:00:33 +02:00 committed by GitHub
parent a4353ee5ca
commit 05fdc81b27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 397 additions and 239 deletions

5
.gitattributes vendored
View File

@ -1,2 +1,3 @@
.java text=auto *.java text=auto
.xml text=auto *.xml text=auto
*.xml_gen text=auto

View File

@ -69,7 +69,7 @@ public class AudioConsoleTest extends AbstractAudioServletTest {
} }
}; };
private final int testTimeout = 1; private final int testTimeout = 5;
@BeforeEach @BeforeEach
public void setUp() throws IOException { public void setUp() throws IOException {
@ -100,7 +100,7 @@ public class AudioConsoleTest extends AbstractAudioServletTest {
public void audioConsolePlaysFile() throws AudioException, IOException { public void audioConsolePlaysFile() throws AudioException, IOException {
AudioStream audioStream = new FileAudioStream(new File(fileHandler.wavFilePath())); AudioStream audioStream = new FileAudioStream(new File(fileHandler.wavFilePath()));
String[] args = new String[] { AudioConsoleCommandExtension.SUBCMD_PLAY, fileHandler.wavFileName() }; String[] args = { AudioConsoleCommandExtension.SUBCMD_PLAY, fileHandler.wavFileName() };
audioConsoleCommandExtension.execute(args, consoleMock); audioConsoleCommandExtension.execute(args, consoleMock);
assertThat(audioSink.audioFormat.isCompatible(audioStream.getFormat()), is(true)); assertThat(audioSink.audioFormat.isCompatible(audioStream.getFormat()), is(true));
@ -111,8 +111,7 @@ public class AudioConsoleTest extends AbstractAudioServletTest {
public void audioConsolePlaysFileForASpecifiedSink() throws AudioException, IOException { public void audioConsolePlaysFileForASpecifiedSink() throws AudioException, IOException {
AudioStream audioStream = new FileAudioStream(new File(fileHandler.wavFilePath())); AudioStream audioStream = new FileAudioStream(new File(fileHandler.wavFilePath()));
String[] args = new String[] { AudioConsoleCommandExtension.SUBCMD_PLAY, audioSink.getId(), String[] args = { AudioConsoleCommandExtension.SUBCMD_PLAY, audioSink.getId(), fileHandler.wavFileName() };
fileHandler.wavFileName() };
audioConsoleCommandExtension.execute(args, consoleMock); audioConsoleCommandExtension.execute(args, consoleMock);
assertThat(audioSink.audioFormat.isCompatible(audioStream.getFormat()), is(true)); assertThat(audioSink.audioFormat.isCompatible(audioStream.getFormat()), is(true));
@ -123,8 +122,8 @@ public class AudioConsoleTest extends AbstractAudioServletTest {
public void audioConsolePlaysFileForASpecifiedSinkWithASpecifiedVolume() throws AudioException, IOException { public void audioConsolePlaysFileForASpecifiedSinkWithASpecifiedVolume() throws AudioException, IOException {
AudioStream audioStream = new FileAudioStream(new File(fileHandler.wavFilePath())); AudioStream audioStream = new FileAudioStream(new File(fileHandler.wavFilePath()));
String[] args = new String[] { AudioConsoleCommandExtension.SUBCMD_PLAY, audioSink.getId(), String[] args = { AudioConsoleCommandExtension.SUBCMD_PLAY, audioSink.getId(), fileHandler.wavFileName(),
fileHandler.wavFileName(), "25" }; "25" };
audioConsoleCommandExtension.execute(args, consoleMock); audioConsoleCommandExtension.execute(args, consoleMock);
assertThat(audioSink.audioFormat.isCompatible(audioStream.getFormat()), is(true)); assertThat(audioSink.audioFormat.isCompatible(audioStream.getFormat()), is(true));
@ -133,8 +132,8 @@ public class AudioConsoleTest extends AbstractAudioServletTest {
@Test @Test
public void audioConsolePlaysFileForASpecifiedSinkWithAnInvalidVolume() { public void audioConsolePlaysFileForASpecifiedSinkWithAnInvalidVolume() {
String[] args = new String[] { AudioConsoleCommandExtension.SUBCMD_PLAY, audioSink.getId(), String[] args = { AudioConsoleCommandExtension.SUBCMD_PLAY, audioSink.getId(), fileHandler.wavFileName(),
fileHandler.wavFileName(), "invalid" }; "invalid" };
audioConsoleCommandExtension.execute(args, consoleMock); audioConsoleCommandExtension.execute(args, consoleMock);
waitForAssert(() -> assertThat("The given volume was invalid", consoleOutput, waitForAssert(() -> assertThat("The given volume was invalid", consoleOutput,
@ -148,7 +147,7 @@ public class AudioConsoleTest extends AbstractAudioServletTest {
String url = serveStream(audioStream, testTimeout); String url = serveStream(audioStream, testTimeout);
String[] args = new String[] { AudioConsoleCommandExtension.SUBCMD_STREAM, url }; String[] args = { AudioConsoleCommandExtension.SUBCMD_STREAM, url };
audioConsoleCommandExtension.execute(args, consoleMock); audioConsoleCommandExtension.execute(args, consoleMock);
assertThat("The streamed URL was not as expected", ((URLAudioStream) audioSink.audioStream).getURL(), is(url)); assertThat("The streamed URL was not as expected", ((URLAudioStream) audioSink.audioStream).getURL(), is(url));
@ -161,7 +160,7 @@ public class AudioConsoleTest extends AbstractAudioServletTest {
String url = serveStream(audioStream, testTimeout); String url = serveStream(audioStream, testTimeout);
String[] args = new String[] { AudioConsoleCommandExtension.SUBCMD_STREAM, audioSink.getId(), url }; String[] args = { AudioConsoleCommandExtension.SUBCMD_STREAM, audioSink.getId(), url };
audioConsoleCommandExtension.execute(args, consoleMock); audioConsoleCommandExtension.execute(args, consoleMock);
assertThat("The streamed URL was not as expected", ((URLAudioStream) audioSink.audioStream).getURL(), is(url)); assertThat("The streamed URL was not as expected", ((URLAudioStream) audioSink.audioStream).getURL(), is(url));
@ -169,7 +168,7 @@ public class AudioConsoleTest extends AbstractAudioServletTest {
@Test @Test
public void audioConsoleListsSinks() { public void audioConsoleListsSinks() {
String[] args = new String[] { AudioConsoleCommandExtension.SUBCMD_SINKS }; String[] args = { AudioConsoleCommandExtension.SUBCMD_SINKS };
audioConsoleCommandExtension.execute(args, consoleMock); audioConsoleCommandExtension.execute(args, consoleMock);
waitForAssert(() -> assertThat("The listed sink was not as expected", consoleOutput, waitForAssert(() -> assertThat("The listed sink was not as expected", consoleOutput,
@ -182,7 +181,7 @@ public class AudioConsoleTest extends AbstractAudioServletTest {
when(audioSource.getId()).thenReturn("sourceId"); when(audioSource.getId()).thenReturn("sourceId");
audioManager.addAudioSource(audioSource); audioManager.addAudioSource(audioSource);
String[] args = new String[] { AudioConsoleCommandExtension.SUBCMD_SOURCES }; String[] args = { AudioConsoleCommandExtension.SUBCMD_SOURCES };
audioConsoleCommandExtension.execute(args, consoleMock); audioConsoleCommandExtension.execute(args, consoleMock);
waitForAssert(() -> assertThat("The listed source was not as expected", consoleOutput, waitForAssert(() -> assertThat("The listed source was not as expected", consoleOutput,

View File

@ -111,7 +111,7 @@ public class AudioServletTest extends AbstractAudioServletTest {
@Test @Test
public void requestToMultitimeStreamCannotBeDoneAfterTheTimeoutOfTheStreamHasExipred() throws Exception { public void requestToMultitimeStreamCannotBeDoneAfterTheTimeoutOfTheStreamHasExipred() throws Exception {
final int streamTimeout = 1; final int streamTimeout = 3;
AudioStream audioStream = getByteArrayAudioStream(testByteArray, AudioFormat.CONTAINER_NONE, AudioStream audioStream = getByteArrayAudioStream(testByteArray, AudioFormat.CONTAINER_NONE,
AudioFormat.CODEC_MP3); AudioFormat.CODEC_MP3);

View File

@ -107,7 +107,7 @@ class ScriptFileWatcherTest {
scriptFileWatcher.processWatchEvent(null, ENTRY_CREATE, p); scriptFileWatcher.processWatchEvent(null, ENTRY_CREATE, p);
verify(scriptEngineManager, timeout(1000)).createScriptEngine("js", p.toFile().toURI().toString()); verify(scriptEngineManager, timeout(10000)).createScriptEngine("js", p.toFile().toURI().toString());
} }
@Test @Test
@ -125,7 +125,7 @@ class ScriptFileWatcherTest {
// verify is called when the start level increases // verify is called when the start level increases
updateStartLevel(100); updateStartLevel(100);
verify(scriptEngineManager, timeout(1000).times(1)).createScriptEngine("js", p.toFile().toURI().toString()); verify(scriptEngineManager, timeout(10000).times(1)).createScriptEngine("js", p.toFile().toURI().toString());
} }
@Test @Test
@ -145,7 +145,7 @@ class ScriptFileWatcherTest {
// verify is called when the start level increases // verify is called when the start level increases
updateStartLevel(100); updateStartLevel(100);
verify(scriptEngineManager, timeout(1000).times(1)).createScriptEngine("js", p.toFile().toURI().toString()); verify(scriptEngineManager, timeout(10000).times(1)).createScriptEngine("js", p.toFile().toURI().toString());
} }
@Test @Test
@ -170,12 +170,12 @@ class ScriptFileWatcherTest {
updateStartLevel(60); updateStartLevel(60);
verify(scriptEngineManager, timeout(1000).times(1)).createScriptEngine("js", p2.toFile().toURI().toString()); verify(scriptEngineManager, timeout(10000).times(1)).createScriptEngine("js", p2.toFile().toURI().toString());
verify(scriptEngineManager, never()).createScriptEngine(anyString(), eq(p1.toFile().toURI().toString())); verify(scriptEngineManager, never()).createScriptEngine(anyString(), eq(p1.toFile().toURI().toString()));
updateStartLevel(80); updateStartLevel(80);
verify(scriptEngineManager, timeout(1000).times(1)).createScriptEngine("js", p1.toFile().toURI().toString()); verify(scriptEngineManager, timeout(10000).times(1)).createScriptEngine("js", p1.toFile().toURI().toString());
} }
@Test @Test
@ -200,12 +200,12 @@ class ScriptFileWatcherTest {
updateStartLevel(60); updateStartLevel(60);
verify(scriptEngineManager, timeout(1000).times(1)).createScriptEngine("js", p2.toFile().toURI().toString()); verify(scriptEngineManager, timeout(10000).times(1)).createScriptEngine("js", p2.toFile().toURI().toString());
verify(scriptEngineManager, never()).createScriptEngine(anyString(), eq(p1.toFile().toURI().toString())); verify(scriptEngineManager, never()).createScriptEngine(anyString(), eq(p1.toFile().toURI().toString()));
updateStartLevel(80); updateStartLevel(80);
verify(scriptEngineManager, timeout(1000).times(1)).createScriptEngine("js", p1.toFile().toURI().toString()); verify(scriptEngineManager, timeout(10000).times(1)).createScriptEngine("js", p1.toFile().toURI().toString());
} }
@Test @Test
@ -236,7 +236,7 @@ class ScriptFileWatcherTest {
scheduledTask.getValue().run(); scheduledTask.getValue().run();
// verify script has now been processed // verify script has now been processed
verify(scriptEngineManager, timeout(1000).times(1)).createScriptEngine("js", p.toFile().toURI().toString()); verify(scriptEngineManager, timeout(10000).times(1)).createScriptEngine("js", p.toFile().toURI().toString());
} }
@Test @Test
@ -258,11 +258,11 @@ class ScriptFileWatcherTest {
InOrder inOrder = inOrder(scriptEngineManager); InOrder inOrder = inOrder(scriptEngineManager);
inOrder.verify(scriptEngineManager, timeout(1000).times(1)).createScriptEngine("js", inOrder.verify(scriptEngineManager, timeout(10000).times(1)).createScriptEngine("js",
p64.toFile().toURI().toString()); p64.toFile().toURI().toString());
inOrder.verify(scriptEngineManager, timeout(1000).times(1)).createScriptEngine("js", inOrder.verify(scriptEngineManager, timeout(10000).times(1)).createScriptEngine("js",
p65.toFile().toURI().toString()); p65.toFile().toURI().toString());
inOrder.verify(scriptEngineManager, timeout(1000).times(1)).createScriptEngine("js", inOrder.verify(scriptEngineManager, timeout(10000).times(1)).createScriptEngine("js",
p66.toFile().toURI().toString()); p66.toFile().toURI().toString());
} }
@ -281,7 +281,7 @@ class ScriptFileWatcherTest {
scriptFileWatcher.onDependencyChange(p.toFile().toURI().toString()); scriptFileWatcher.onDependencyChange(p.toFile().toURI().toString());
verify(scriptEngineManager, timeout(1000).times(2)).createScriptEngine("js", p.toFile().toURI().toString()); verify(scriptEngineManager, timeout(10000).times(2)).createScriptEngine("js", p.toFile().toURI().toString());
} }
@Test @Test
@ -344,6 +344,6 @@ class ScriptFileWatcherTest {
scriptFileWatcher.processWatchEvent(null, ENTRY_MODIFY, p); scriptFileWatcher.processWatchEvent(null, ENTRY_MODIFY, p);
verify(scriptEngineManager).removeEngine(p.toFile().toURI().toString()); verify(scriptEngineManager).removeEngine(p.toFile().toURI().toString());
verify(scriptEngineManager, timeout(1000).times(2)).createScriptEngine("js", p.toFile().toURI().toString()); verify(scriptEngineManager, timeout(10000).times(2)).createScriptEngine("js", p.toFile().toURI().toString());
} }
} }

View File

@ -27,6 +27,8 @@ import org.junit.jupiter.api.Test;
@NonNullByDefault @NonNullByDefault
public class ExecUtilTest { public class ExecUtilTest {
private static final Duration TIMEOUT = Duration.ofSeconds(10);
@Test @Test
public void testBasicExecuteCommandLine() { public void testBasicExecuteCommandLine() {
if (isWindowsSystem()) { if (isWindowsSystem()) {
@ -40,9 +42,9 @@ public class ExecUtilTest {
public void testBasicExecuteCommandLineAndWaitResponse() { public void testBasicExecuteCommandLineAndWaitResponse() {
final String result; final String result;
if (isWindowsSystem()) { if (isWindowsSystem()) {
result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofSeconds(1), "cmd", "/c", "dir"); result = ExecUtil.executeCommandLineAndWaitResponse(TIMEOUT, "cmd", "/c", "dir");
} else { } else {
result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofSeconds(1), "ls"); result = ExecUtil.executeCommandLineAndWaitResponse(TIMEOUT, "ls");
} }
assertNotNull(result); assertNotNull(result);
assertNotEquals("", result); assertNotEquals("", result);
@ -52,9 +54,9 @@ public class ExecUtilTest {
public void testExecuteCommandLineAndWaitResponseWithArguments() { public void testExecuteCommandLineAndWaitResponseWithArguments() {
final String result; final String result;
if (isWindowsSystem()) { if (isWindowsSystem()) {
result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofSeconds(1), "cmd", "/c", "echo", "test"); result = ExecUtil.executeCommandLineAndWaitResponse(TIMEOUT, "cmd", "/c", "echo", "test");
} else { } else {
result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofSeconds(1), "echo", "'test'"); result = ExecUtil.executeCommandLineAndWaitResponse(TIMEOUT, "echo", "'test'");
} }
assertNotNull(result); assertNotNull(result);
assertNotEquals("test", result); assertNotEquals("test", result);
@ -69,10 +71,9 @@ public class ExecUtilTest {
public void testExecuteCommandLineAndWaitStdErrRedirection() { public void testExecuteCommandLineAndWaitStdErrRedirection() {
final String result; final String result;
if (isWindowsSystem()) { if (isWindowsSystem()) {
result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofSeconds(1), "cmd", "/c", "dir", "xxx.xxx", result = ExecUtil.executeCommandLineAndWaitResponse(TIMEOUT, "cmd", "/c", "dir", "xxx.xxx", "1>", "nul");
"1>", "nul");
} else { } else {
result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofSeconds(1), "ls", "xxx.xxx"); result = ExecUtil.executeCommandLineAndWaitResponse(TIMEOUT, "ls", "xxx.xxx");
} }
assertNotNull(result); assertNotNull(result);
assertNotEquals("", result); assertNotEquals("", result);

View File

@ -71,7 +71,7 @@ public class TimerImplTest {
assertThat(subject.hasTerminated(), is(false)); assertThat(subject.hasTerminated(), is(false));
assertThat(subject.isCancelled(), is(false)); assertThat(subject.isCancelled(), is(false));
Thread.sleep(TimeUnit.SECONDS.toMillis(DEFAULT_TIMEOUT_SECONDS + DEFAULT_RUNTIME_SECONDS + 1)); Thread.sleep(TimeUnit.SECONDS.toMillis(DEFAULT_TIMEOUT_SECONDS + DEFAULT_RUNTIME_SECONDS + 3));
assertThat(subject.isActive(), is(false)); assertThat(subject.isActive(), is(false));
assertThat(subject.hasTerminated(), is(true)); assertThat(subject.hasTerminated(), is(true));
assertThat(subject.isCancelled(), is(false)); assertThat(subject.isCancelled(), is(false));
@ -79,17 +79,17 @@ public class TimerImplTest {
@Test @Test
public void testTimerHasTerminatedAndReschedule() throws InterruptedException { public void testTimerHasTerminatedAndReschedule() throws InterruptedException {
Thread.sleep(TimeUnit.SECONDS.toMillis(DEFAULT_TIMEOUT_SECONDS + DEFAULT_RUNTIME_SECONDS + 1)); Thread.sleep(TimeUnit.SECONDS.toMillis(DEFAULT_TIMEOUT_SECONDS + DEFAULT_RUNTIME_SECONDS + 3));
assertThat(subject.isActive(), is(false)); assertThat(subject.isActive(), is(false));
assertThat(subject.hasTerminated(), is(true)); assertThat(subject.hasTerminated(), is(true));
assertThat(subject.isCancelled(), is(false)); assertThat(subject.isCancelled(), is(false));
subject.reschedule(ZonedDateTime.now().plusSeconds(DEFAULT_TIMEOUT_SECONDS)); subject.reschedule(ZonedDateTime.now().plusSeconds(DEFAULT_TIMEOUT_SECONDS));
assertThat(subject.isActive(), is(true)); assertThat(subject.isActive(), is(true));
assertThat(subject.hasTerminated(), is(false)); assertThat(subject.hasTerminated(), is(false));
assertThat(subject.isCancelled(), is(false)); assertThat(subject.isCancelled(), is(false));
Thread.sleep(TimeUnit.SECONDS.toMillis(DEFAULT_TIMEOUT_SECONDS + DEFAULT_RUNTIME_SECONDS + 1)); Thread.sleep(TimeUnit.SECONDS.toMillis(DEFAULT_TIMEOUT_SECONDS + DEFAULT_RUNTIME_SECONDS + 3));
assertThat(subject.isActive(), is(false)); assertThat(subject.isActive(), is(false));
assertThat(subject.hasTerminated(), is(true)); assertThat(subject.hasTerminated(), is(true));
assertThat(subject.isCancelled(), is(false)); assertThat(subject.isCancelled(), is(false));
@ -106,7 +106,7 @@ public class TimerImplTest {
assertThat(subject.hasTerminated(), is(false)); assertThat(subject.hasTerminated(), is(false));
assertThat(subject.isCancelled(), is(false)); assertThat(subject.isCancelled(), is(false));
Thread.sleep(TimeUnit.SECONDS.toMillis(DEFAULT_RUNTIME_SECONDS + 1)); Thread.sleep(TimeUnit.SECONDS.toMillis(DEFAULT_RUNTIME_SECONDS + 3));
assertThat(subject.isRunning(), is(false)); assertThat(subject.isRunning(), is(false));
assertThat(subject.hasTerminated(), is(true)); assertThat(subject.hasTerminated(), is(true));
assertThat(subject.isCancelled(), is(false)); assertThat(subject.isCancelled(), is(false));

View File

@ -22,12 +22,30 @@
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<configuration> <configuration>
<!-- This bundle has many tests so run them in forks to reduce the build time --> <!-- This bundle has many tests so run them in forks to reduce the build time -->
<forkCount>4</forkCount> <forkCount>${cpu.count}</forkCount>
<reuseForks>false</reuseForks> <reuseForks>false</reuseForks>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
</pluginManagement> </pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>get-cpu-count</id>
<goals>
<goal>cpu-count</goal>
</goals>
<configuration>
<cpuCount>cpu.count</cpuCount>
<factor>0.5</factor>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build> </build>
</project> </project>

View File

@ -62,7 +62,7 @@ public class SafeCallerImplTest extends JavaTest {
private static final int THREAD_POOL_SIZE = 3; private static final int THREAD_POOL_SIZE = 3;
// the duration that the called object should block for // the duration that the called object should block for
private static final int BLOCK = 1000; private static final int BLOCK = 5000;
// the standard timeout for the safe-caller used in most tests // the standard timeout for the safe-caller used in most tests
private static final int TIMEOUT = 500; private static final int TIMEOUT = 500;
@ -197,7 +197,7 @@ public class SafeCallerImplTest extends JavaTest {
assertDurationBetween(TIMEOUT - GRACE, BLOCK - GRACE, () -> { assertDurationBetween(TIMEOUT - GRACE, BLOCK - GRACE, () -> {
safeCaller.create(mock, Runnable.class).withTimeout(TIMEOUT).build().run(); safeCaller.create(mock, Runnable.class).withTimeout(TIMEOUT).build().run();
}); });
assertDurationBelow(GRACE, () -> { assertDurationBelow(2 * GRACE, () -> {
safeCaller.create(mock, Runnable.class).withTimeout(TIMEOUT).build().run(); safeCaller.create(mock, Runnable.class).withTimeout(TIMEOUT).build().run();
}); });
assertDurationBetween(TIMEOUT - GRACE, BLOCK + GRACE, () -> { assertDurationBetween(TIMEOUT - GRACE, BLOCK + GRACE, () -> {
@ -214,17 +214,17 @@ public class SafeCallerImplTest extends JavaTest {
configureSingleThread(); configureSingleThread();
spawn(() -> { spawn(() -> {
assertDurationBetween(TIMEOUT - GRACE, BLOCK - GRACE, () -> { assertDurationBetween(0, BLOCK - GRACE, () -> {
safeCaller.create(mock, Runnable.class).withTimeout(TIMEOUT).build().run(); safeCaller.create(mock, Runnable.class).withTimeout(TIMEOUT).build().run();
}); });
}); });
sleep(GRACE); // give it a chance to start sleep(GRACE); // give it a chance to start
spawn(() -> { spawn(() -> {
assertDurationBelow(GRACE, () -> { assertDurationBelow(3 * GRACE, () -> {
safeCaller.create(mock, Runnable.class).withTimeout(TIMEOUT).build().run(); safeCaller.create(mock, Runnable.class).withTimeout(TIMEOUT).build().run();
}); });
}); });
assertDurationBetween(BLOCK - 2 * GRACE, BLOCK + TIMEOUT + GRACE, () -> { assertDurationBetween(BLOCK - 2 * GRACE, BLOCK + TIMEOUT + 4 * GRACE, () -> {
waitForAssert(() -> { waitForAssert(() -> {
verify(mock, times(2)).run(); verify(mock, times(2)).run();
}); });
@ -273,7 +273,7 @@ public class SafeCallerImplTest extends JavaTest {
assertDurationBetween(TIMEOUT - GRACE, BLOCK - GRACE, () -> { assertDurationBetween(TIMEOUT - GRACE, BLOCK - GRACE, () -> {
safeCaller.create(mock1, Runnable.class).withTimeout(TIMEOUT).withIdentifier("id").build().run(); safeCaller.create(mock1, Runnable.class).withTimeout(TIMEOUT).withIdentifier("id").build().run();
}); });
assertDurationBelow(GRACE, () -> { assertDurationBelow(4 * GRACE, () -> {
safeCaller.create(mock2, Runnable.class).withTimeout(TIMEOUT).withIdentifier("id").build().run(); safeCaller.create(mock2, Runnable.class).withTimeout(TIMEOUT).withIdentifier("id").build().run();
}); });
} }
@ -369,9 +369,8 @@ public class SafeCallerImplTest extends JavaTest {
thingHandler.method(); thingHandler.method();
}, Runnable.class).build().run(); }, Runnable.class).build().run();
Object res = safeCaller.create((Function<String, String>) name -> { Object res = safeCaller.create((Function<String, String>) name -> ("Hello " + name + "!"), Function.class)
return "Hello " + name + "!"; .build().apply("World");
}, Function.class).build().apply("World");
assertThat(res, is("Hello World!")); assertThat(res, is("Hello World!"));
} }
@ -382,7 +381,7 @@ public class SafeCallerImplTest extends JavaTest {
assertDurationBelow(GRACE, () -> { assertDurationBelow(GRACE, () -> {
safeCaller.create(mock1, Runnable.class).withTimeout(TIMEOUT).withAsync().build().run(); safeCaller.create(mock1, Runnable.class).withTimeout(TIMEOUT).withAsync().build().run();
}); });
waitForAssert(() -> verify(mock1, times(1)).run()); waitForAssert(() -> verify(mock1, timeout(1000).times(1)).run());
} }
@Test @Test
@ -406,7 +405,7 @@ public class SafeCallerImplTest extends JavaTest {
Runnable mock1 = mock(Runnable.class); Runnable mock1 = mock(Runnable.class);
doThrow(RuntimeException.class).when(mock1).run(); doThrow(RuntimeException.class).when(mock1).run();
assertDurationBelow(GRACE, () -> { assertDurationBelow(2 * GRACE, () -> {
safeCaller.create(mock1, Runnable.class).withTimeout(TIMEOUT).withAsync().withIdentifier(identifier) safeCaller.create(mock1, Runnable.class).withTimeout(TIMEOUT).withAsync().withIdentifier(identifier)
.onTimeout(timeoutHandlerMock).onException(errorHandlerMock).build().run(); .onTimeout(timeoutHandlerMock).onException(errorHandlerMock).build().run();
}); });
@ -431,6 +430,7 @@ public class SafeCallerImplTest extends JavaTest {
}); });
waitForAssert(() -> verify(mock1, times(1)).run()); waitForAssert(() -> verify(mock1, times(1)).run());
waitForAssert(() -> verify(mock2, times(1)).run()); waitForAssert(() -> verify(mock2, times(1)).run());
sleep(GRACE);
verifyNoMoreInteractions(mock1, mock2); verifyNoMoreInteractions(mock1, mock2);
} }
@ -498,7 +498,7 @@ public class SafeCallerImplTest extends JavaTest {
.build().run(); .build().run();
}); });
} }
assertDurationBetween(BLOCK - GRACE, BLOCK + TIMEOUT + GRACE, () -> { assertDurationBetween(BLOCK - GRACE, BLOCK + TIMEOUT + THREAD_POOL_SIZE * 2 * GRACE, () -> {
waitForAssert(() -> { waitForAssert(() -> {
verify(mock, times(THREAD_POOL_SIZE * 2)).run(); verify(mock, times(THREAD_POOL_SIZE * 2)).run();
}); });

View File

@ -144,7 +144,7 @@ class ExpireManagerTest {
expireManager.receive(event); expireManager.receive(event);
Thread.sleep(1500L); Thread.sleep(1500L);
verify(eventPublisherMock, never()).post(any()); verify(eventPublisherMock, never()).post(any());
Thread.sleep(2000L); Thread.sleep(2500L);
verify(eventPublisherMock, times(1)).post(any()); verify(eventPublisherMock, times(1)).post(any());
} }
@ -162,7 +162,7 @@ class ExpireManagerTest {
expireManager.receive(event); expireManager.receive(event);
Thread.sleep(1500L); Thread.sleep(1500L);
verify(eventPublisherMock, never()).post(any()); verify(eventPublisherMock, never()).post(any());
Thread.sleep(2000L); Thread.sleep(2500L);
verify(eventPublisherMock, times(1)).post(any()); verify(eventPublisherMock, times(1)).post(any());
} }
@ -178,7 +178,7 @@ class ExpireManagerTest {
Thread.sleep(1500L); Thread.sleep(1500L);
event = ItemEventFactory.createStateEvent(ITEMNAME, new DecimalType(1)); event = ItemEventFactory.createStateEvent(ITEMNAME, new DecimalType(1));
expireManager.receive(event); expireManager.receive(event);
Thread.sleep(1500L); Thread.sleep(2500L);
verify(eventPublisherMock, times(1)).post(any()); verify(eventPublisherMock, times(1)).post(any());
} }

View File

@ -39,11 +39,11 @@ public class PeriodicSchedulerImplTest {
private final PeriodicSchedulerImpl periodicScheduler = new PeriodicSchedulerImpl(new SchedulerImpl()); private final PeriodicSchedulerImpl periodicScheduler = new PeriodicSchedulerImpl(new SchedulerImpl());
@Test @Test
@Timeout(value = 5, unit = TimeUnit.SECONDS) @Timeout(value = 10, unit = TimeUnit.SECONDS)
public void testSchedule() throws InterruptedException, IOException { public void testSchedule() throws InterruptedException, IOException {
Queue<Long> times = new ArrayDeque<>(); Queue<Long> times = new ArrayDeque<>();
Semaphore semaphore = new Semaphore(0); Semaphore semaphore = new Semaphore(0);
Duration[] delays = { Duration.ofMillis(100), Duration.ofMillis(200), Duration.ofMillis(300) }; Duration[] delays = { Duration.ofMillis(100), Duration.ofMillis(200), Duration.ofMillis(400) };
final long now = System.currentTimeMillis(); final long now = System.currentTimeMillis();
ScheduledCompletableFuture<Object> future = periodicScheduler.schedule(() -> { ScheduledCompletableFuture<Object> future = periodicScheduler.schedule(() -> {
@ -54,11 +54,11 @@ public class PeriodicSchedulerImplTest {
future.cancel(true); future.cancel(true);
// Because starting scheduler takes some time and we don't know how long // Because starting scheduler takes some time and we don't know how long
// the first time set is the offset on which we check the next values. // the first time set is the offset on which we check the next values.
long offset = times.poll(); long offset = times.poll().longValue();
long[] expectedResults = { 200, 300, 300, 300, 300 }; long[] expectedResults = { 200, 400, 400, 400, 400 };
for (long expectedResult : expectedResults) { for (long expectedResult : expectedResults) {
long actualValue = times.poll().longValue(); long actualValue = times.poll().longValue();
assertEquals((offset + expectedResult) / 100.0, actualValue / 100.0, 0.9, assertEquals((offset + expectedResult) / 100.0, actualValue / 100.0, 1.5,
"Expected periodic time, total: " + actualValue); "Expected periodic time, total: " + actualValue);
offset = actualValue; offset = actualValue;
} }

View File

@ -35,8 +35,10 @@ import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout; import org.junit.jupiter.api.Timeout;
import org.openhab.core.JavaTest;
import org.openhab.core.scheduler.ScheduledCompletableFuture; import org.openhab.core.scheduler.ScheduledCompletableFuture;
import org.openhab.core.scheduler.SchedulerRunnable; import org.openhab.core.scheduler.SchedulerRunnable;
import org.openhab.core.scheduler.SchedulerTemporalAdjuster; import org.openhab.core.scheduler.SchedulerTemporalAdjuster;
@ -50,11 +52,16 @@ import org.openhab.core.scheduler.SchedulerTemporalAdjuster;
* @author Hilbrand Bouwkamp - moved cron and periodic scheduling to it's their own interfaces * @author Hilbrand Bouwkamp - moved cron and periodic scheduling to it's their own interfaces
*/ */
@NonNullByDefault @NonNullByDefault
public class SchedulerImplTest { public class SchedulerImplTest extends JavaTest {
private SchedulerImpl scheduler = new SchedulerImpl(); private @NonNullByDefault({}) SchedulerImpl scheduler;
@BeforeEach
public void beforeEach() {
scheduler = new SchedulerImpl();
}
@Test @Test
@Timeout(value = 500, unit = TimeUnit.MILLISECONDS) @Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testAfterCancelled() throws InterruptedException, InvocationTargetException, ExecutionException { public void testAfterCancelled() throws InterruptedException, InvocationTargetException, ExecutionException {
AtomicBoolean check = new AtomicBoolean(); AtomicBoolean check = new AtomicBoolean();
Callable<Boolean> callable = () -> check.getAndSet(true); Callable<Boolean> callable = () -> check.getAndSet(true);
@ -68,7 +75,7 @@ public class SchedulerImplTest {
} }
@Test @Test
@Timeout(value = 300, unit = TimeUnit.MILLISECONDS) @Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testAfterResolved() throws InterruptedException, ExecutionException { public void testAfterResolved() throws InterruptedException, ExecutionException {
AtomicInteger check = new AtomicInteger(); AtomicInteger check = new AtomicInteger();
Callable<Integer> callable = () -> { Callable<Integer> callable = () -> {
@ -76,21 +83,21 @@ public class SchedulerImplTest {
return 5; return 5;
}; };
ScheduledCompletableFuture<Integer> after = scheduler.after(callable, Duration.ofMillis(100)); ScheduledCompletableFuture<Integer> after = scheduler.after(callable, Duration.ofMillis(100));
Thread.sleep(200); Thread.sleep(500);
assertTrue(after.isDone(), "Scheduled job should finish done"); assertTrue(after.isDone(), "Scheduled job should finish done");
assertEquals(10, check.get(), "After CompletableFuture should return correct value"); assertEquals(10, check.get(), "After CompletableFuture should return correct value");
assertEquals(5, after.get().intValue(), "After CompletableFuture should return correct value"); assertEquals(5, after.get().intValue(), "After CompletableFuture should return correct value");
} }
@Test @Test
@Timeout(value = 300, unit = TimeUnit.MILLISECONDS) @Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testAfterResolvedWithException() throws InterruptedException { public void testAfterResolvedWithException() throws InterruptedException {
Callable<Void> callable = () -> { Callable<Void> callable = () -> {
// Pass a exception not very likely thrown by the scheduler it self to avoid missing real exceptions. // Pass a exception not very likely thrown by the scheduler it self to avoid missing real exceptions.
throw new FileNotFoundException("testBeforeTimeoutException"); throw new FileNotFoundException("testBeforeTimeoutException");
}; };
ScheduledCompletableFuture<Void> after = scheduler.after(callable, Duration.ofMillis(100)); ScheduledCompletableFuture<Void> after = scheduler.after(callable, Duration.ofMillis(100));
Thread.sleep(200); Thread.sleep(500);
assertTrue(after.isDone(), "Scheduled job should be done"); assertTrue(after.isDone(), "Scheduled job should be done");
assertTrue(after.getPromise().isCompletedExceptionally(), assertTrue(after.getPromise().isCompletedExceptionally(),
"After CompletableFuture should have completed with an exception"); "After CompletableFuture should have completed with an exception");
@ -105,11 +112,11 @@ public class SchedulerImplTest {
} }
@Test @Test
@Timeout(value = 300, unit = TimeUnit.MILLISECONDS) @Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testBeforeTimeoutException() throws InterruptedException, ExecutionException { public void testBeforeTimeoutException() throws InterruptedException, ExecutionException {
CompletableFuture<Integer> d = new CompletableFuture<>(); CompletableFuture<Integer> d = new CompletableFuture<>();
ScheduledCompletableFuture<Integer> before = scheduler.before(d, Duration.ofMillis(100)); ScheduledCompletableFuture<Integer> before = scheduler.before(d, Duration.ofMillis(100));
Thread.sleep(200); Thread.sleep(500);
d.complete(3); d.complete(3);
d.get(); d.get();
assertTrue(before.isDone(), "Scheduled job should be done"); assertTrue(before.isDone(), "Scheduled job should be done");
@ -125,7 +132,7 @@ public class SchedulerImplTest {
} }
@Test @Test
@Timeout(value = 300, unit = TimeUnit.MILLISECONDS) @Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testBeforeCancelled() throws InterruptedException, InvocationTargetException, ExecutionException { public void testBeforeCancelled() throws InterruptedException, InvocationTargetException, ExecutionException {
CompletableFuture<Integer> d = new CompletableFuture<>(); CompletableFuture<Integer> d = new CompletableFuture<>();
ScheduledCompletableFuture<Integer> before = scheduler.before(d, Duration.ofMillis(100)); ScheduledCompletableFuture<Integer> before = scheduler.before(d, Duration.ofMillis(100));
@ -137,7 +144,7 @@ public class SchedulerImplTest {
} }
@Test @Test
@Timeout(value = 300, unit = TimeUnit.MILLISECONDS) @Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testBeforeResolved() throws InterruptedException, ExecutionException { public void testBeforeResolved() throws InterruptedException, ExecutionException {
CompletableFuture<Boolean> d = new CompletableFuture<>(); CompletableFuture<Boolean> d = new CompletableFuture<>();
ScheduledCompletableFuture<Boolean> before = scheduler.before(d, Duration.ofMillis(100)); ScheduledCompletableFuture<Boolean> before = scheduler.before(d, Duration.ofMillis(100));
@ -147,7 +154,7 @@ public class SchedulerImplTest {
} }
@Test @Test
@Timeout(value = 300, unit = TimeUnit.MILLISECONDS) @Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testBeforeResolvedWithException() throws InterruptedException { public void testBeforeResolvedWithException() throws InterruptedException {
CompletableFuture<Integer> d = new CompletableFuture<>(); CompletableFuture<Integer> d = new CompletableFuture<>();
ScheduledCompletableFuture<Integer> before = scheduler.before(d, Duration.ofMillis(100)); ScheduledCompletableFuture<Integer> before = scheduler.before(d, Duration.ofMillis(100));
@ -166,11 +173,11 @@ public class SchedulerImplTest {
} }
@Test @Test
@Timeout(value = 300, unit = TimeUnit.MILLISECONDS) @Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testAfterTimeoutException() throws InterruptedException, ExecutionException { public void testAfterTimeoutException() throws InterruptedException, ExecutionException {
CompletableFuture<Integer> d = new CompletableFuture<>(); CompletableFuture<Integer> d = new CompletableFuture<>();
ScheduledCompletableFuture<Integer> before = scheduler.before(d, Duration.ofMillis(100)); ScheduledCompletableFuture<Integer> before = scheduler.before(d, Duration.ofMillis(100));
Thread.sleep(200); Thread.sleep(500);
d.complete(3); d.complete(3);
d.get(); d.get();
assertTrue(before.isDone(), "Scheduled job should be done"); assertTrue(before.isDone(), "Scheduled job should be done");
@ -186,34 +193,33 @@ public class SchedulerImplTest {
} }
@Test @Test
@Timeout(value = 1, unit = TimeUnit.SECONDS) @Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testSchedule() throws InterruptedException { public void testSchedule() throws InterruptedException {
Semaphore s = new Semaphore(0); Semaphore s = new Semaphore(0);
TestSchedulerWithCounter temporalAdjuster = new TestSchedulerWithCounter(); TestSchedulerWithCounter temporalAdjuster = new TestSchedulerWithCounter();
scheduler.schedule(s::release, temporalAdjuster); scheduler.schedule(s::release, temporalAdjuster);
s.acquire(3); s.acquire(3);
Thread.sleep(300); // wait a little longer to see if not more are scheduled. Thread.sleep(1000); // wait a little longer to see if not more are scheduled.
assertEquals(0, s.availablePermits(), "Scheduler should not have released more after done"); assertEquals(0, s.availablePermits(), "Scheduler should not have released more after done");
assertEquals(3, temporalAdjuster.getCount(), "Scheduler should have run 3 times"); assertEquals(3, temporalAdjuster.getCount(), "Scheduler should have run 3 times");
} }
@Test @Test
@Timeout(value = 1, unit = TimeUnit.SECONDS) @Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testScheduleCancel() throws InterruptedException { public void testScheduleCancel() throws InterruptedException {
Semaphore s = new Semaphore(0); Semaphore s = new Semaphore(0);
TestSchedulerWithCounter temporalAdjuster = new TestSchedulerWithCounter(); TestSchedulerWithCounter temporalAdjuster = new TestSchedulerWithCounter();
ScheduledCompletableFuture<Void> schedule = scheduler.schedule(s::release, temporalAdjuster); ScheduledCompletableFuture<Void> schedule = scheduler.schedule(s::release, temporalAdjuster);
s.acquire(1); s.acquire(1);
Thread.sleep(50);
schedule.cancel(true); schedule.cancel(true);
Thread.sleep(300); // wait a little longer to see if not more are scheduled. waitForAssert(() -> assertEquals(1, temporalAdjuster.getCount(), "Scheduler should have run 1 time"));
Thread.sleep(1000); // wait a little longer to see if not more are scheduled.
assertEquals(0, s.availablePermits(), "Scheduler should not have released more after cancel"); assertEquals(0, s.availablePermits(), "Scheduler should not have released more after cancel");
assertEquals(1, temporalAdjuster.getCount(), "Scheduler should have run 1 time");
} }
@Test @Test
@Timeout(value = 1, unit = TimeUnit.SECONDS) @Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testScheduleException() throws InterruptedException { public void testScheduleException() throws InterruptedException {
Semaphore s = new Semaphore(0); Semaphore s = new Semaphore(0);
TestSchedulerWithCounter temporalAdjuster = new TestSchedulerWithCounter(); TestSchedulerWithCounter temporalAdjuster = new TestSchedulerWithCounter();
@ -230,25 +236,25 @@ public class SchedulerImplTest {
return null; return null;
}); });
s.acquire(1); s.acquire(1);
Thread.sleep(300); // wait a little longer to see if not more are scheduled. Thread.sleep(1000); // wait a little longer to see if not more are scheduled.
assertEquals(0, s.availablePermits(), "Scheduler should not have released more after cancel"); assertEquals(0, s.availablePermits(), "Scheduler should not have released more after cancel");
assertEquals(0, temporalAdjuster.getCount(), "Scheduler should have run 0 time"); assertEquals(0, temporalAdjuster.getCount(), "Scheduler should have run 0 time");
} }
@Test @Test
@Timeout(value = 300, unit = TimeUnit.MILLISECONDS) @Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testNegative() throws InterruptedException { public void testNegative() throws InterruptedException {
Semaphore s = new Semaphore(0); Semaphore s = new Semaphore(0);
scheduler.after(() -> { scheduler.after(() -> {
s.release(1); s.release(1);
return null; return null;
}, Duration.ofMillis(-1000)); }, Duration.ofMillis(-1000));
Thread.sleep(200); waitForAssert(
assertEquals(1, s.availablePermits(), "Negative value should mean after finished right away"); () -> assertEquals(1, s.availablePermits(), "Negative value should mean after finished right away"));
} }
@Test @Test
@Timeout(value = 1, unit = TimeUnit.SECONDS) @Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testDelay() throws InterruptedException { public void testDelay() throws InterruptedException {
long duration = 5000; long duration = 5000;
ScheduledCompletableFuture<Instant> future = scheduler.after(Duration.ofMillis(duration)); ScheduledCompletableFuture<Instant> future = scheduler.after(Duration.ofMillis(duration));
@ -260,7 +266,7 @@ public class SchedulerImplTest {
} }
@Test @Test
@Timeout(value = 1, unit = TimeUnit.SECONDS) @Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testCompareTo() throws InterruptedException { public void testCompareTo() throws InterruptedException {
long duration = 5000; long duration = 5000;
ScheduledCompletableFuture<Instant> future1 = scheduler.after(Duration.ofMillis(duration)); ScheduledCompletableFuture<Instant> future1 = scheduler.after(Duration.ofMillis(duration));
@ -305,7 +311,7 @@ public class SchedulerImplTest {
@Override @Override
public Temporal adjustInto(@NonNullByDefault({}) Temporal arg0) { public Temporal adjustInto(@NonNullByDefault({}) Temporal arg0) {
return arg0.plus(100, ChronoUnit.MILLIS); return arg0.plus(600, ChronoUnit.MILLIS);
} }
@Override @Override

View File

@ -24,6 +24,7 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -79,7 +80,7 @@ import org.osgi.service.component.ComponentContext;
@NonNullByDefault @NonNullByDefault
public class InboxOSGiTest extends JavaOSGiTest { public class InboxOSGiTest extends JavaOSGiTest {
class DiscoveryService1 extends AbstractDiscoveryService { static class DiscoveryService1 extends AbstractDiscoveryService {
public DiscoveryService1() { public DiscoveryService1() {
super(5); super(5);
} }
@ -89,7 +90,7 @@ public class InboxOSGiTest extends JavaOSGiTest {
} }
} }
class DiscoveryService2 extends AbstractDiscoveryService { static class DiscoveryService2 extends AbstractDiscoveryService {
public DiscoveryService2() { public DiscoveryService2() {
super(5); super(5);
} }
@ -195,12 +196,44 @@ public class InboxOSGiTest extends JavaOSGiTest {
@AfterEach @AfterEach
public void cleanUp() { public void cleanUp() {
discoveryResults.forEach((thingUID, discoveryResult) -> inbox.remove(thingUID)); Set<String> inboxThingUIDsToRemove = inbox.getAll().stream().map(DiscoveryResult::getThingUID)
inboxListeners.forEach(listener -> inbox.removeInboxListener(listener)); .map(ThingUID::toString).collect(Collectors.toSet());
discoveryResults.clear(); Set<String> removedInboxThingUIDs = new HashSet<>();
inboxListeners.clear();
EventSubscriber inboxEventSubscriber = new EventSubscriber() {
@Override
public void receive(Event event) {
if (event instanceof InboxRemovedEvent) {
removedInboxThingUIDs.add(((InboxRemovedEvent) event).getDiscoveryResult().thingUID);
}
}
@Override
public Set<String> getSubscribedEventTypes() {
return Set.of(InboxRemovedEvent.TYPE);
}
@Override
public @Nullable EventFilter getEventFilter() {
return null;
}
};
registerService(inboxEventSubscriber);
registry.remove(BRIDGE_THING_UID); registry.remove(BRIDGE_THING_UID);
managedThingProvider.getAll().forEach(thing -> managedThingProvider.remove(thing.getUID())); managedThingProvider.getAll().forEach(thing -> managedThingProvider.remove(thing.getUID()));
inboxListeners.forEach(listener -> inbox.removeInboxListener(listener));
inbox.getAll().stream().forEach(discoveryResult -> inbox.remove(discoveryResult.getThingUID()));
discoveryResults.clear();
inboxListeners.clear();
// wait for the resulting events to be handled so they do not cause the next test to fail
waitForAssert(() -> assertTrue(removedInboxThingUIDs.containsAll(inboxThingUIDsToRemove)));
unregisterService(inboxEventSubscriber);
} }
private boolean addDiscoveryResult(DiscoveryResult discoveryResult) { private boolean addDiscoveryResult(DiscoveryResult discoveryResult) {
@ -963,7 +996,7 @@ public class InboxOSGiTest extends JavaOSGiTest {
assertTrue(thingProperty.equals(descResultParam)); assertTrue(thingProperty.equals(descResultParam));
} }
services.forEach(obj -> unregisterService(obj)); services.forEach(this::unregisterService);
} }
@Test @Test

View File

@ -16,6 +16,7 @@ import static java.util.stream.Collectors.*;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize; import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.math.BigDecimal; import java.math.BigDecimal;
@ -41,6 +42,7 @@ import org.openhab.core.items.GenericItem;
import org.openhab.core.items.GroupFunction; import org.openhab.core.items.GroupFunction;
import org.openhab.core.items.GroupItem; import org.openhab.core.items.GroupItem;
import org.openhab.core.items.Item; import org.openhab.core.items.Item;
import org.openhab.core.items.ItemNotFoundException;
import org.openhab.core.items.ItemProvider; import org.openhab.core.items.ItemProvider;
import org.openhab.core.items.ItemRegistry; import org.openhab.core.items.ItemRegistry;
import org.openhab.core.items.Metadata; import org.openhab.core.items.Metadata;
@ -107,9 +109,9 @@ public class GenericItemProviderTest extends JavaOSGiTest {
*/ */
@AfterEach @AfterEach
public void tearDown() { public void tearDown() {
Collection<Item> itemsToRemove = itemRegistry.getAll(); Set<String> itemNamesToRemove = itemRegistry.getAll().stream().map(Item::getName).collect(Collectors.toSet());
List<String> modelNamesToRemove = TESTMODEL_NAMES.stream() Set<String> modelNamesToRemove = TESTMODEL_NAMES.stream().filter(name -> modelRepository.getModel(name) != null)
.filter(name -> modelRepository.getModel(name) != null).collect(Collectors.toList()); .collect(Collectors.toSet());
if (!modelNamesToRemove.isEmpty()) { if (!modelNamesToRemove.isEmpty()) {
Set<String> removedModelNames = new HashSet<>(); Set<String> removedModelNames = new HashSet<>();
@ -123,18 +125,18 @@ public class GenericItemProviderTest extends JavaOSGiTest {
} }
}; };
List<AbstractItemRegistryEvent> removedItemEvents = new ArrayList<>(); List<String> removedItemNames = new ArrayList<>();
EventSubscriber itemEventSubscriber = new EventSubscriber() { EventSubscriber itemEventSubscriber = new EventSubscriber() {
@Override @Override
public void receive(Event event) { public void receive(Event event) {
logger.debug("Received event: {}", event); logger.debug("Received event: {}", event);
removedItemEvents.add((AbstractItemRegistryEvent) event); removedItemNames.add(((AbstractItemRegistryEvent) event).getItem().name);
} }
@Override @Override
public Set<String> getSubscribedEventTypes() { public Set<String> getSubscribedEventTypes() {
return Stream.of(ItemRemovedEvent.TYPE).collect(toSet()); return Set.of(ItemRemovedEvent.TYPE);
} }
@Override @Override
@ -149,7 +151,8 @@ public class GenericItemProviderTest extends JavaOSGiTest {
modelNamesToRemove.forEach(modelRepository::removeModel); modelNamesToRemove.forEach(modelRepository::removeModel);
waitForAssert(() -> { waitForAssert(() -> {
assertThat(removedItemEvents, hasSize(itemsToRemove.size())); // removedItemNames can have more item names due to queued events generated in the test
assertTrue(removedItemNames.containsAll(itemNamesToRemove));
assertThat(removedModelNames, hasSize(modelNamesToRemove.size())); assertThat(removedModelNames, hasSize(modelNamesToRemove.size()));
}); });
@ -240,22 +243,17 @@ public class GenericItemProviderTest extends JavaOSGiTest {
String model = "String test1 \"Test Item [%s]\" { channel=\"test:test:test:test\" }\n" String model = "String test1 \"Test Item [%s]\" { channel=\"test:test:test:test\" }\n"
+ "String test2 \"Test Item [%s]\" { channel=\"test:test:test:test\" }"; + "String test2 \"Test Item [%s]\" { channel=\"test:test:test:test\" }";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes())); modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
waitForAssert(() -> assertThat(itemRegistry.getAll(), hasSize(2)));
assertThat(itemRegistry.getAll(), hasSize(2));
model = "String test1 \"Test Item [%s]\" { channel=\"test:test:test:test\" }\n" model = "String test1 \"Test Item [%s]\" { channel=\"test:test:test:test\" }\n"
+ "String {something is wrong} test \"Test Item [%s]\" { channel=\"test:test:test:test\" }"; + "String {something is wrong} test \"Test Item [%s]\" { channel=\"test:test:test:test\" }";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes())); modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
waitForAssert(() -> assertThat(itemRegistry.getAll(), hasSize(0)));
waitForAssert(() -> {
assertThat(itemRegistry.getAll(), hasSize(0));
});
model = "String test1 \"Test Item [%s]\" { channel=\"test:test:test:test\" }\n" model = "String test1 \"Test Item [%s]\" { channel=\"test:test:test:test\" }\n"
+ "String test2 \"Test Item [%s]\" { channel=\"test:test:test:test\" }"; + "String test2 \"Test Item [%s]\" { channel=\"test:test:test:test\" }";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes())); modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
waitForAssert(() -> assertThat(itemRegistry.getAll(), hasSize(2)));
assertThat(itemRegistry.getAll(), hasSize(2));
} }
@Test @Test
@ -392,23 +390,34 @@ public class GenericItemProviderTest extends JavaOSGiTest {
+ "String test2 \"Test Item [%s]\" { channel=\"test:test:test:test\" }\n" + "String test2 \"Test Item [%s]\" { channel=\"test:test:test:test\" }\n"
+ "String test3 \"Test Item [%s]\" { channel=\"test:test:test:test\" }"; + "String test3 \"Test Item [%s]\" { channel=\"test:test:test:test\" }";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes())); modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
assertThat(itemRegistry.getAll(), hasSize(3)); waitForAssert(() -> assertThat(itemRegistry.getAll(), hasSize(3)));
Item unchangedItem = itemRegistry.getItem("test1"); Item unchangedItem = itemRegistry.getItem("test1");
model = "String test1 \"Test Item [%s]\" { channel=\"test:test:test:test\" }\n" model = "String test1 \"Test Item [%s]\" { channel=\"test:test:test:test\" }\n"
+ "String test2 \"Test Item Changed [%s]\" { channel=\"test:test:test:test\" }"; + "String test2 \"Test Item Changed [%s]\" { channel=\"test:test:test:test\" }";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes())); modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
assertThat(itemRegistry.getAll(), hasSize(2)); waitForAssert(() -> {
assertThat(itemRegistry.getItem("test1"), is(unchangedItem)); assertThat(itemRegistry.getAll(), hasSize(2));
try {
assertThat(itemRegistry.getItem("test1"), is(unchangedItem));
} catch (ItemNotFoundException e) {
}
});
model = ""; model = "";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes())); modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
assertThat(itemRegistry.getAll(), hasSize(0)); waitForAssert(() -> assertThat(itemRegistry.getAll(), hasSize(0)));
model = "String test1 \"Test Item [%s]\" { channel=\"test:test:test:test\" }"; model = "String test1 \"Test Item [%s]\" { channel=\"test:test:test:test\" }";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes())); modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
assertThat(itemRegistry.getAll(), hasSize(1)); waitForAssert(() -> {
assertThat(itemRegistry.getItem("test1"), is(unchangedItem)); assertThat(itemRegistry.getAll(), hasSize(1));
try {
assertThat(itemRegistry.getItem("test1"), is(unchangedItem));
} catch (ItemNotFoundException e) {
}
});
} }
@Test @Test

View File

@ -72,7 +72,6 @@ public class ScriptEngineOSGiTest extends JavaOSGiTest {
assertNotNull(itemRegistry); assertNotNull(itemRegistry);
itemProvider = new ItemProvider() { itemProvider = new ItemProvider() {
@Override @Override
public void addProviderChangeListener(ProviderChangeListener<Item> listener) { public void addProviderChangeListener(ProviderChangeListener<Item> listener) {
} }
@ -90,6 +89,8 @@ public class ScriptEngineOSGiTest extends JavaOSGiTest {
registerService(itemProvider); registerService(itemProvider);
waitForAssert(() -> assertThat(itemRegistry.getAll().size(), is(itemProvider.getAll().size())));
ScriptServiceUtil scriptServiceUtil = getService(ScriptServiceUtil.class); ScriptServiceUtil scriptServiceUtil = getService(ScriptServiceUtil.class);
assertNotNull(scriptServiceUtil); assertNotNull(scriptServiceUtil);
scriptEngine = ScriptServiceUtil.getScriptEngine(); scriptEngine = ScriptServiceUtil.getScriptEngine();

View File

@ -24,7 +24,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemRegistry; import org.openhab.core.items.ItemRegistry;
import org.openhab.core.model.core.ModelRepository; import org.openhab.core.model.core.ModelRepository;
import org.openhab.core.test.java.JavaOSGiTest; import org.openhab.core.test.java.JavaOSGiTest;
@ -85,27 +84,25 @@ public class GenericItemChannelLinkProviderTest extends JavaOSGiTest {
"}"; "}";
modelRepository.addOrRefreshModel(THINGS_TESTMODEL_NAME, new ByteArrayInputStream(thingsModel.getBytes())); modelRepository.addOrRefreshModel(THINGS_TESTMODEL_NAME, new ByteArrayInputStream(thingsModel.getBytes()));
Collection<Thing> actualThings = thingRegistry.getAll();
assertThat(actualThings.size(), is(3)); waitForAssert(() -> {
assertThat(thingRegistry.getAll().size(), is(3));
Collection<Item> items = itemRegistry.getItems(); assertThat(itemRegistry.getItems().size(), is(0));
assertThat(items.size(), is(0)); assertThat(itemChannelLinkRegistry.getAll().size(), is(0));
});
Collection<ItemChannelLink> itemChannelLinks = itemChannelLinkRegistry.getAll();
assertThat(itemChannelLinks.size(), is(0));
String itemsModel = "Color Light3Color \"Light3 Color\" { channel=\"hue:LCT001:huebridge:bulb3:color\" }"; String itemsModel = "Color Light3Color \"Light3 Color\" { channel=\"hue:LCT001:huebridge:bulb3:color\" }";
modelRepository.addOrRefreshModel(ITEMS_TESTMODEL_NAME, new ByteArrayInputStream(itemsModel.getBytes())); modelRepository.addOrRefreshModel(ITEMS_TESTMODEL_NAME, new ByteArrayInputStream(itemsModel.getBytes()));
Collection<Item> actualItems = itemRegistry.getItems();
assertThat(actualItems.size(), is(1)); waitForAssert(() -> {
assertThat(itemRegistry.getItems().size(), is(1));
List<ItemChannelLink> actualItemChannelLinks = new ArrayList<>(itemChannelLinkRegistry.getAll()); List<ItemChannelLink> actualItemChannelLinks = new ArrayList<>(itemChannelLinkRegistry.getAll());
assertThat(actualItemChannelLinks.size(), is(1)); assertThat(actualItemChannelLinks.size(), is(1));
assertThat(actualItemChannelLinks.get(0).toString(), assertThat(actualItemChannelLinks.get(0).toString(),
is(equalTo("Light3Color -> hue:LCT001:huebridge:bulb3:color"))); is(equalTo("Light3Color -> hue:LCT001:huebridge:bulb3:color")));
});
} }
@Test @Test
@ -120,28 +117,26 @@ public class GenericItemChannelLinkProviderTest extends JavaOSGiTest {
"}"; "}";
modelRepository.addOrRefreshModel(THINGS_TESTMODEL_NAME, new ByteArrayInputStream(thingsModel.getBytes())); modelRepository.addOrRefreshModel(THINGS_TESTMODEL_NAME, new ByteArrayInputStream(thingsModel.getBytes()));
Collection<Thing> actualThings = thingRegistry.getAll();
assertThat(actualThings.size(), is(3)); waitForAssert(() -> {
assertThat(thingRegistry.getAll().size(), is(3));
Collection<Item> items = itemRegistry.getItems(); assertThat(itemRegistry.getItems().size(), is(0));
assertThat(items.size(), is(0)); assertThat(itemChannelLinkRegistry.getAll().size(), is(0));
});
Collection<ItemChannelLink> itemChannelLinks = itemChannelLinkRegistry.getAll();
assertThat(itemChannelLinks.size(), is(0));
String itemsModel = "Color Light3Color \"Light3 Color\" { channel=\"hue:LCT001:huebridge:bulb3:color, hue:LCT001:huebridge:bulb4:color\" }"; String itemsModel = "Color Light3Color \"Light3 Color\" { channel=\"hue:LCT001:huebridge:bulb3:color, hue:LCT001:huebridge:bulb4:color\" }";
modelRepository.addOrRefreshModel(ITEMS_TESTMODEL_NAME, new ByteArrayInputStream(itemsModel.getBytes())); modelRepository.addOrRefreshModel(ITEMS_TESTMODEL_NAME, new ByteArrayInputStream(itemsModel.getBytes()));
Collection<Item> actualItems = itemRegistry.getItems();
assertThat(actualItems.size(), is(1)); waitForAssert(() -> {
assertThat(itemRegistry.getItems().size(), is(1));
List<ItemChannelLink> actualItemChannelLinks = new ArrayList<>(itemChannelLinkRegistry.getAll()); List<ItemChannelLink> actualItemChannelLinks = new ArrayList<>(itemChannelLinkRegistry.getAll());
assertThat(actualItemChannelLinks.size(), is(2)); assertThat(actualItemChannelLinks.size(), is(2));
assertThat(actualItemChannelLinks.get(0).toString(), assertThat(actualItemChannelLinks.get(0).toString(),
is(equalTo("Light3Color -> hue:LCT001:huebridge:bulb3:color"))); is(equalTo("Light3Color -> hue:LCT001:huebridge:bulb3:color")));
assertThat(actualItemChannelLinks.get(1).toString(), assertThat(actualItemChannelLinks.get(1).toString(),
is(equalTo("Light3Color -> hue:LCT001:huebridge:bulb4:color"))); is(equalTo("Light3Color -> hue:LCT001:huebridge:bulb4:color")));
});
} }
} }

View File

@ -495,7 +495,11 @@ public class GenericThingProviderTest extends JavaOSGiTest {
@Override @Override
public void receive(Event event) { public void receive(Event event) {
receivedEvents.add((AbstractThingRegistryEvent) event); AbstractThingRegistryEvent registryEvent = (AbstractThingRegistryEvent) event;
if (List.of("hue:bridge:my1234Bridge", "hue:LCT001:my1234Bridge:myKitchenBulb1")
.contains(registryEvent.getThing().UID)) {
receivedEvents.add(registryEvent);
}
} }
}; };
@ -503,8 +507,8 @@ public class GenericThingProviderTest extends JavaOSGiTest {
assertThat(thingRegistry.getAll().size(), is(0)); assertThat(thingRegistry.getAll().size(), is(0));
String model = "Bridge hue:bridge:myBridge [ ip = \"1.2.3.4\", username = \"123\" ] {" + // String model = "Bridge hue:bridge:my1234Bridge [ ip = \"1.2.3.4\", username = \"123\" ] {" + //
" LCT001 bulb1 [ lightId = \"1\" ] { Switch : notification }" + // " LCT001 myKitchenBulb1 [ lightId = \"1\" ] { Switch : notification }" + //
"}"; "}";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes())); modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
@ -515,8 +519,8 @@ public class GenericThingProviderTest extends JavaOSGiTest {
}); });
receivedEvents.clear(); receivedEvents.clear();
String newModel = "Bridge hue:bridge:myBridge [ ip = \"1.2.3.4\", username = \"123\" ] {" + // String newModel = "Bridge hue:bridge:my1234Bridge [ ip = \"1.2.3.4\", username = \"123\" ] {" + //
" LCT001 bulb1 [ lightId = \"2\" ] { Switch : notification }" + // " LCT001 myKitchenBulb1 [ lightId = \"2\" ] { Switch : notification }" + //
"}"; "}";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(newModel.getBytes())); modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(newModel.getBytes()));
@ -527,7 +531,7 @@ public class GenericThingProviderTest extends JavaOSGiTest {
Event event = receivedEvents.get(0); Event event = receivedEvents.get(0);
assertEquals(ThingUpdatedEvent.class, event.getClass()); assertEquals(ThingUpdatedEvent.class, event.getClass());
ThingUpdatedEvent thingUpdatedEvent = (ThingUpdatedEvent) event; ThingUpdatedEvent thingUpdatedEvent = (ThingUpdatedEvent) event;
assertEquals("hue:LCT001:myBridge:bulb1", thingUpdatedEvent.getThing().UID.toString()); assertEquals("hue:LCT001:my1234Bridge:myKitchenBulb1", thingUpdatedEvent.getThing().UID);
}); });
} }

View File

@ -21,6 +21,7 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
@ -208,13 +209,8 @@ public class ChannelCommandDescriptionProviderOSGiTest extends JavaOSGiTest {
registerService(new TestDynamicCommandDescriptionProvider(), DynamicCommandDescriptionProvider.class.getName()); registerService(new TestDynamicCommandDescriptionProvider(), DynamicCommandDescriptionProvider.class.getName());
Thing thing = thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"), new ThingUID("hue:lamp:lamp1"), Thing thing = Objects.requireNonNull(thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"),
null, "test thing", new Configuration()); new ThingUID("hue:lamp:lamp1"), null, "test thing", new Configuration()));
assertNotNull(thing);
if (thing == null) {
throw new IllegalStateException("thing is null");
}
managedThingProvider.add(thing); managedThingProvider.add(thing);
ItemChannelLink link = new ItemChannelLink("TestItem1", getChannel(thing, "1").getUID()); ItemChannelLink link = new ItemChannelLink("TestItem1", getChannel(thing, "1").getUID());
@ -230,9 +226,10 @@ public class ChannelCommandDescriptionProviderOSGiTest extends JavaOSGiTest {
Item item = itemRegistry.getItem("TestItem1"); Item item = itemRegistry.getItem("TestItem1");
assertEquals(CoreItemFactory.NUMBER, item.getType()); assertEquals(CoreItemFactory.NUMBER, item.getType());
CommandDescription command = item.getCommandDescription(); final Item finalItem = item;
assertNotNull(command); waitForAssert(() -> assertNotNull(finalItem.getCommandDescription()));
CommandDescription command = Objects.requireNonNull(item.getCommandDescription());
List<CommandOption> opts = command.getCommandOptions(); List<CommandOption> opts = command.getCommandOptions();
assertNotNull(opts); assertNotNull(opts);
assertEquals(1, opts.size()); assertEquals(1, opts.size());
@ -280,12 +277,8 @@ public class ChannelCommandDescriptionProviderOSGiTest extends JavaOSGiTest {
DynamicCommandDescriptionProvider.class.getName()); DynamicCommandDescriptionProvider.class.getName());
registerService(new TestDynamicCommandDescriptionProvider(), DynamicCommandDescriptionProvider.class.getName()); registerService(new TestDynamicCommandDescriptionProvider(), DynamicCommandDescriptionProvider.class.getName());
Thing thing = thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"), new ThingUID("hue:lamp:lamp1"), Thing thing = Objects.requireNonNull(thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"),
null, "test thing", new Configuration()); new ThingUID("hue:lamp:lamp1"), null, "test thing", new Configuration()));
assertNotNull(thing);
if (thing == null) {
throw new IllegalStateException("thing is null");
}
managedThingProvider.add(thing); managedThingProvider.add(thing);
ItemChannelLink link = new ItemChannelLink("TestItem7_2", getChannel(thing, "7_2").getUID()); ItemChannelLink link = new ItemChannelLink("TestItem7_2", getChannel(thing, "7_2").getUID());
@ -294,11 +287,10 @@ public class ChannelCommandDescriptionProviderOSGiTest extends JavaOSGiTest {
final Collection<Item> items = itemRegistry.getItems(); final Collection<Item> items = itemRegistry.getItems();
assertFalse(items.isEmpty()); assertFalse(items.isEmpty());
Item item = itemRegistry.getItem("TestItem7_2"); final Item item = itemRegistry.getItem("TestItem7_2");
waitForAssert(() -> assertNotNull(item.getCommandDescription()));
CommandDescription command = item.getCommandDescription();
assertNotNull(command);
CommandDescription command = Objects.requireNonNull(item.getCommandDescription());
List<CommandOption> opts = command.getCommandOptions(); List<CommandOption> opts = command.getCommandOptions();
assertNotNull(opts); assertNotNull(opts);
assertEquals(1, opts.size()); assertEquals(1, opts.size());
@ -310,7 +302,7 @@ public class ChannelCommandDescriptionProviderOSGiTest extends JavaOSGiTest {
/* /*
* Helper * Helper
*/ */
class TestDynamicCommandDescriptionProvider extends BaseDynamicCommandDescriptionProvider { static class TestDynamicCommandDescriptionProvider extends BaseDynamicCommandDescriptionProvider {
final CommandDescription newCommand = CommandDescriptionBuilder.create() final CommandDescription newCommand = CommandDescriptionBuilder.create()
.withCommandOption(new CommandOption("NEW COMMAND", "My new command.")).build(); .withCommandOption(new CommandOption("NEW COMMAND", "My new command.")).build();
@ -326,7 +318,7 @@ public class ChannelCommandDescriptionProviderOSGiTest extends JavaOSGiTest {
} }
} }
class MalfunctioningDynamicCommandDescriptionProvider extends BaseDynamicCommandDescriptionProvider { static class MalfunctioningDynamicCommandDescriptionProvider extends BaseDynamicCommandDescriptionProvider {
@Override @Override
public @Nullable CommandDescription getCommandDescription(Channel channel, public @Nullable CommandDescription getCommandDescription(Channel channel,
@Nullable CommandDescription originalCommandDescription, @Nullable Locale locale) { @Nullable CommandDescription originalCommandDescription, @Nullable Locale locale) {
@ -362,7 +354,7 @@ public class ChannelCommandDescriptionProviderOSGiTest extends JavaOSGiTest {
} }
} }
class TestItemProvider implements ItemProvider { static class TestItemProvider implements ItemProvider {
private final Collection<Item> items; private final Collection<Item> items;
TestItemProvider(Collection<Item> items) { TestItemProvider(Collection<Item> items) {
@ -383,7 +375,7 @@ public class ChannelCommandDescriptionProviderOSGiTest extends JavaOSGiTest {
} }
} }
private abstract class AbstractThingHandler extends BaseThingHandler { private abstract static class AbstractThingHandler extends BaseThingHandler {
public AbstractThingHandler(Thing thing) { public AbstractThingHandler(Thing thing) {
super(thing); super(thing);
} }

View File

@ -14,15 +14,18 @@ package org.openhab.core.thing.internal;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
@ -36,7 +39,13 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import org.openhab.core.common.registry.RegistryChangeListener; import org.openhab.core.common.registry.RegistryChangeListener;
import org.openhab.core.events.Event;
import org.openhab.core.events.EventFilter;
import org.openhab.core.events.EventSubscriber;
import org.openhab.core.items.Item;
import org.openhab.core.items.ManagedItemProvider; import org.openhab.core.items.ManagedItemProvider;
import org.openhab.core.items.events.AbstractItemRegistryEvent;
import org.openhab.core.items.events.ItemRemovedEvent;
import org.openhab.core.library.CoreItemFactory; import org.openhab.core.library.CoreItemFactory;
import org.openhab.core.library.items.NumberItem; import org.openhab.core.library.items.NumberItem;
import org.openhab.core.test.java.JavaOSGiTest; import org.openhab.core.test.java.JavaOSGiTest;
@ -53,15 +62,23 @@ import org.openhab.core.thing.binding.BaseThingHandler;
import org.openhab.core.thing.binding.ThingHandlerFactory; import org.openhab.core.thing.binding.ThingHandlerFactory;
import org.openhab.core.thing.binding.builder.ChannelBuilder; import org.openhab.core.thing.binding.builder.ChannelBuilder;
import org.openhab.core.thing.binding.builder.ThingBuilder; import org.openhab.core.thing.binding.builder.ThingBuilder;
import org.openhab.core.thing.events.AbstractThingRegistryEvent;
import org.openhab.core.thing.events.ThingRemovedEvent;
import org.openhab.core.thing.link.AbstractLink;
import org.openhab.core.thing.link.ItemChannelLink; import org.openhab.core.thing.link.ItemChannelLink;
import org.openhab.core.thing.link.ItemChannelLinkRegistry; import org.openhab.core.thing.link.ItemChannelLinkRegistry;
import org.openhab.core.thing.link.ManagedItemChannelLinkProvider; import org.openhab.core.thing.link.ManagedItemChannelLinkProvider;
import org.openhab.core.thing.link.dto.ItemChannelLinkDTO;
import org.openhab.core.thing.link.events.AbstractItemChannelLinkRegistryEvent;
import org.openhab.core.thing.link.events.ItemChannelLinkRemovedEvent;
import org.openhab.core.thing.type.ChannelKind; import org.openhab.core.thing.type.ChannelKind;
import org.openhab.core.thing.type.ChannelTypeUID; import org.openhab.core.thing.type.ChannelTypeUID;
import org.openhab.core.thing.util.ThingHandlerHelper; import org.openhab.core.thing.util.ThingHandlerHelper;
import org.openhab.core.types.Command; import org.openhab.core.types.Command;
import org.openhab.core.util.BundleResolver; import org.openhab.core.util.BundleResolver;
import org.osgi.framework.Bundle; import org.osgi.framework.Bundle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* Tests {@link ChannelLinkNotifier}. * Tests {@link ChannelLinkNotifier}.
@ -76,6 +93,8 @@ public class ChannelLinkNotifierOSGiTest extends JavaOSGiTest {
private static final ChannelTypeUID CHANNEL_TYPE_UID = new ChannelTypeUID("binding:channelType"); private static final ChannelTypeUID CHANNEL_TYPE_UID = new ChannelTypeUID("binding:channelType");
private static final int CHANNEL_COUNT = 5; private static final int CHANNEL_COUNT = 5;
private final Logger logger = LoggerFactory.getLogger(ChannelLinkNotifierOSGiTest.class);
private int thingCount; private int thingCount;
private @NonNullByDefault({}) ItemChannelLinkRegistry itemChannelLinkRegistry; private @NonNullByDefault({}) ItemChannelLinkRegistry itemChannelLinkRegistry;
@ -178,12 +197,59 @@ public class ChannelLinkNotifierOSGiTest extends JavaOSGiTest {
@AfterEach @AfterEach
public void afterEach() throws Exception { public void afterEach() throws Exception {
Set<String> itemChannelLinkUIDsToRemove = managedItemChannelLinkProvider.getAll().stream()
.map(ItemChannelLink::getUID).collect(Collectors.toSet());
Set<String> itemNamesToRemove = managedItemProvider.getAll().stream().map(Item::getName)
.collect(Collectors.toSet());
Set<String> thingUIDsToRemove = managedThingProvider.getAll().stream().map(Thing::getUID)
.map(ThingUID::toString).collect(Collectors.toSet());
Set<String> removedItemNames = new HashSet<>();
Set<String> removedItemChannelLinkUIDs = new HashSet<>();
Set<String> removedThingUIDs = new HashSet<>();
EventSubscriber itemEventSubscriber = new EventSubscriber() {
@Override
public void receive(Event event) {
logger.debug("Received event: {}", event);
if (event instanceof AbstractItemChannelLinkRegistryEvent) {
ItemChannelLinkDTO link = ((AbstractItemChannelLinkRegistryEvent) event).getLink();
removedItemChannelLinkUIDs
.add(AbstractLink.getIDFor(link.itemName, new ChannelUID(link.channelUID)));
} else if (event instanceof AbstractItemRegistryEvent) {
removedItemNames.add(((AbstractItemRegistryEvent) event).getItem().name);
} else if (event instanceof AbstractThingRegistryEvent) {
removedThingUIDs.add(((AbstractThingRegistryEvent) event).getThing().UID);
}
}
@Override
public Set<String> getSubscribedEventTypes() {
return Set.of(ItemChannelLinkRemovedEvent.TYPE, ItemRemovedEvent.TYPE, ThingRemovedEvent.TYPE);
}
@Override
public @Nullable EventFilter getEventFilter() {
return null;
}
};
registerService(itemEventSubscriber);
Thread.sleep(300);
managedItemChannelLinkProvider.getAll() managedItemChannelLinkProvider.getAll()
.forEach(itemChannelLink -> managedItemChannelLinkProvider.remove(itemChannelLink.getUID())); .forEach(itemChannelLink -> managedItemChannelLinkProvider.remove(itemChannelLink.getUID()));
managedItemProvider.getAll().forEach(item -> managedItemProvider.remove(item.getUID())); managedItemProvider.getAll().forEach(item -> managedItemProvider.remove(item.getUID()));
managedThingProvider.getAll().forEach(thing -> managedThingProvider.remove(thing.getUID())); managedThingProvider.getAll().forEach(thing -> managedThingProvider.remove(thing.getUID()));
thingCount = 0; // wait for the resulting events to be handled so they do not cause the next test to fail
waitForAssert(() -> {
assertTrue(removedItemChannelLinkUIDs.containsAll(itemChannelLinkUIDsToRemove));
assertTrue(removedItemNames.containsAll(itemNamesToRemove));
assertTrue(removedThingUIDs.containsAll(thingUIDsToRemove));
});
unregisterService(itemEventSubscriber);
} }
private Thing addThing(@Nullable ThingStatus thingStatus) { private Thing addThing(@Nullable ThingStatus thingStatus) {
@ -232,11 +298,38 @@ public class ChannelLinkNotifierOSGiTest extends JavaOSGiTest {
} }
private void addItemsAndLinks(Thing thing, String itemSuffix) { private void addItemsAndLinks(Thing thing, String itemSuffix) {
Set<String> linkUIDsToAdd = new HashSet<>();
Set<String> addedLinkUIDs = new HashSet<>();
RegistryChangeListener<ItemChannelLink> changeListener = new RegistryChangeListener<>() {
@Override
public void added(ItemChannelLink element) {
addedLinkUIDs.add(element.getUID());
}
@Override
public void updated(ItemChannelLink oldElement, ItemChannelLink element) {
}
@Override
public void removed(ItemChannelLink element) {
}
};
itemChannelLinkRegistry.addRegistryChangeListener(changeListener);
forEachThingChannelUID(thing, channelUID -> { forEachThingChannelUID(thing, channelUID -> {
String itemName = getItemName(thing, channelUID, itemSuffix); String itemName = getItemName(thing, channelUID, itemSuffix);
managedItemProvider.add(new NumberItem(itemName)); managedItemProvider.add(new NumberItem(itemName));
managedItemChannelLinkProvider.add(new ItemChannelLink(itemName, channelUID)); ItemChannelLink itemChannelLink = new ItemChannelLink(itemName, channelUID);
managedItemChannelLinkProvider.add(itemChannelLink);
linkUIDsToAdd.add(itemChannelLink.getUID());
}); });
// wait for the resulting events to be handled so they do not cause further assertions fail
waitForAssert(() -> assertTrue(addedLinkUIDs.containsAll(linkUIDsToAdd)));
itemChannelLinkRegistry.removeRegistryChangeListener(changeListener);
} }
private void removeItemsAndLinks(Thing thing, String itemSuffix) { private void removeItemsAndLinks(Thing thing, String itemSuffix) {
@ -270,9 +363,11 @@ public class ChannelLinkNotifierOSGiTest extends JavaOSGiTest {
private void assertAllChannelsLinkedBasedOnEvents(Thing thing, int eventCount) { private void assertAllChannelsLinkedBasedOnEvents(Thing thing, int eventCount) {
TestHandler handler = getHandler(thing); TestHandler handler = getHandler(thing);
forEachThingChannelUID(thing, channelUID -> { waitForAssert(() -> {
assertThat(handler.isLinkedBasedOnEvent(channelUID), is(true)); forEachThingChannelUID(thing, channelUID -> {
assertThat(handler.getChannelLinkEvents(channelUID).size(), is(eventCount)); assertThat(handler.isLinkedBasedOnEvent(channelUID), is(true));
assertThat(handler.getChannelLinkEvents(channelUID).size(), is(eventCount));
});
}); });
} }

View File

@ -21,6 +21,7 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
@ -237,13 +238,8 @@ public class ChannelStateDescriptionProviderOSGiTest extends JavaOSGiTest {
registerService(new TestDynamicStateDescriptionProvider(), DynamicStateDescriptionProvider.class.getName()); registerService(new TestDynamicStateDescriptionProvider(), DynamicStateDescriptionProvider.class.getName());
Thing thing = thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"), new ThingUID("hue:lamp:lamp1"), Thing thing = Objects.requireNonNull(thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"),
null, "test thing", new Configuration()); new ThingUID("hue:lamp:lamp1"), null, "test thing", new Configuration()));
assertNotNull(thing);
if (thing == null) {
throw new IllegalStateException("thing is null");
}
managedThingProvider.add(thing); managedThingProvider.add(thing);
ItemChannelLink link = new ItemChannelLink("TestItem", getChannel(thing, "1").getUID()); ItemChannelLink link = new ItemChannelLink("TestItem", getChannel(thing, "1").getUID());
@ -269,9 +265,10 @@ public class ChannelStateDescriptionProviderOSGiTest extends JavaOSGiTest {
Item item = itemRegistry.getItem("TestItem"); Item item = itemRegistry.getItem("TestItem");
assertEquals(CoreItemFactory.NUMBER, item.getType()); assertEquals(CoreItemFactory.NUMBER, item.getType());
StateDescription state = item.getStateDescription(); final Item finalItem = item;
assertNotNull(state); waitForAssert(() -> assertNotNull(finalItem.getStateDescription()));
StateDescription state = Objects.requireNonNull(item.getStateDescription());
assertEquals(BigDecimal.ZERO, state.getMinimum()); assertEquals(BigDecimal.ZERO, state.getMinimum());
assertEquals(BigDecimal.valueOf(100), state.getMaximum()); assertEquals(BigDecimal.valueOf(100), state.getMaximum());
assertEquals(BigDecimal.TEN, state.getStep()); assertEquals(BigDecimal.TEN, state.getStep());
@ -384,13 +381,8 @@ public class ChannelStateDescriptionProviderOSGiTest extends JavaOSGiTest {
DynamicStateDescriptionProvider.class.getName()); DynamicStateDescriptionProvider.class.getName());
registerService(new TestDynamicStateDescriptionProvider(), DynamicStateDescriptionProvider.class.getName()); registerService(new TestDynamicStateDescriptionProvider(), DynamicStateDescriptionProvider.class.getName());
Thing thing = thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"), new ThingUID("hue:lamp:lamp1"), Thing thing = Objects.requireNonNull(thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"),
null, "test thing", new Configuration()); new ThingUID("hue:lamp:lamp1"), null, "test thing", new Configuration()));
assertNotNull(thing);
if (thing == null) {
throw new IllegalStateException("thing is null");
}
managedThingProvider.add(thing); managedThingProvider.add(thing);
ItemChannelLink link = new ItemChannelLink("TestItem7_2", getChannel(thing, "7_2").getUID()); ItemChannelLink link = new ItemChannelLink("TestItem7_2", getChannel(thing, "7_2").getUID());
@ -401,9 +393,10 @@ public class ChannelStateDescriptionProviderOSGiTest extends JavaOSGiTest {
Item item = itemRegistry.getItem("TestItem7_2"); Item item = itemRegistry.getItem("TestItem7_2");
StateDescription state = item.getStateDescription(); final Item finalItem = item;
assertNotNull(state); waitForAssert(() -> assertNotNull(finalItem.getStateDescription()));
StateDescription state = Objects.requireNonNull(item.getStateDescription());
assertEquals(BigDecimal.valueOf(1), state.getMinimum()); assertEquals(BigDecimal.valueOf(1), state.getMinimum());
assertEquals(BigDecimal.valueOf(101), state.getMaximum()); assertEquals(BigDecimal.valueOf(101), state.getMaximum());
assertEquals(BigDecimal.valueOf(20), state.getStep()); assertEquals(BigDecimal.valueOf(20), state.getStep());
@ -421,7 +414,7 @@ public class ChannelStateDescriptionProviderOSGiTest extends JavaOSGiTest {
/* /*
* Helper * Helper
*/ */
class TestDynamicStateDescriptionProvider extends BaseDynamicStateDescriptionProvider { static class TestDynamicStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {
final @Nullable StateDescription newState = StateDescriptionFragmentBuilder.create() final @Nullable StateDescription newState = StateDescriptionFragmentBuilder.create()
.withMinimum(BigDecimal.valueOf(10)).withMaximum(BigDecimal.valueOf(100)) .withMinimum(BigDecimal.valueOf(10)).withMaximum(BigDecimal.valueOf(100))
.withStep(BigDecimal.valueOf(5)).withPattern("VALUE %d").withReadOnly(false) .withStep(BigDecimal.valueOf(5)).withPattern("VALUE %d").withReadOnly(false)
@ -450,7 +443,7 @@ public class ChannelStateDescriptionProviderOSGiTest extends JavaOSGiTest {
} }
} }
class TestMalfunctioningDynamicStateDescriptionProvider extends BaseDynamicStateDescriptionProvider { static class TestMalfunctioningDynamicStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {
@Override @Override
public @Nullable StateDescription getStateDescription(Channel channel, @Nullable StateDescription original, public @Nullable StateDescription getStateDescription(Channel channel, @Nullable StateDescription original,
@Nullable Locale locale) { @Nullable Locale locale) {
@ -486,7 +479,7 @@ public class ChannelStateDescriptionProviderOSGiTest extends JavaOSGiTest {
} }
} }
class TestItemProvider implements ItemProvider { static class TestItemProvider implements ItemProvider {
private final Collection<Item> items; private final Collection<Item> items;
TestItemProvider(Collection<Item> items) { TestItemProvider(Collection<Item> items) {
@ -507,7 +500,7 @@ public class ChannelStateDescriptionProviderOSGiTest extends JavaOSGiTest {
} }
} }
private abstract class AbstractThingHandler extends BaseThingHandler { private abstract static class AbstractThingHandler extends BaseThingHandler {
public AbstractThingHandler(Thing thing) { public AbstractThingHandler(Thing thing) {
super(thing); super(thing);
} }

View File

@ -804,6 +804,7 @@ public class ThingManagerOSGiTest extends JavaOSGiTest {
String itemName = "name"; String itemName = "name";
managedThingProvider.add(thing); managedThingProvider.add(thing);
managedItemChannelLinkProvider.add(new ItemChannelLink(itemName, CHANNEL_UID)); managedItemChannelLinkProvider.add(new ItemChannelLink(itemName, CHANNEL_UID));
waitForAssert(() -> assertThat(itemChannelLinkRegistry.getLinks(itemName).size(), is(1)));
registerService(thingHandlerFactory); registerService(thingHandlerFactory);
Item item = new StringItem(itemName); Item item = new StringItem(itemName);
@ -870,12 +871,12 @@ public class ThingManagerOSGiTest extends JavaOSGiTest {
managedThingProvider.add(thing); managedThingProvider.add(thing);
managedItemChannelLinkProvider.add(new ItemChannelLink(itemName, CHANNEL_UID)); managedItemChannelLinkProvider.add(new ItemChannelLink(itemName, CHANNEL_UID));
waitForAssert(() -> assertThat(itemChannelLinkRegistry.getLinks(itemName).size(), is(1)));
state.callback.statusUpdated(thing, ThingStatusInfoBuilder.create(ThingStatus.ONLINE).build()); state.callback.statusUpdated(thing, ThingStatusInfoBuilder.create(ThingStatus.ONLINE).build());
final List<Event> receivedEvents = new ArrayList<>(); final List<Event> receivedEvents = new ArrayList<>();
@NonNullByDefault
EventSubscriber itemUpdateEventSubscriber = new EventSubscriber() { EventSubscriber itemUpdateEventSubscriber = new EventSubscriber() {
@Override @Override
public void receive(Event event) { public void receive(Event event) {
@ -990,7 +991,7 @@ public class ThingManagerOSGiTest extends JavaOSGiTest {
} }
@Test @Test
public void thingManagerIgnoresRestoringOfThingStatusFromRemoving() { public void thingManagerIgnoresRestoringOfThingStatusFromRemoving() throws Exception {
class ThingHandlerState { class ThingHandlerState {
@Nullable @Nullable
ThingHandlerCallback callback; ThingHandlerCallback callback;
@ -1028,7 +1029,8 @@ public class ThingManagerOSGiTest extends JavaOSGiTest {
assertThat(thing.getStatusInfo(), is(removingNone)); assertThat(thing.getStatusInfo(), is(removingNone));
// handleRemoval is called when thing is fully initialized and shall be removed // handleRemoval is called asynchronously when thing is fully initialized and shall be removed
Thread.sleep(500);
verify(thingHandler).handleRemoval(); verify(thingHandler).handleRemoval();
} }
@ -1125,6 +1127,7 @@ public class ThingManagerOSGiTest extends JavaOSGiTest {
managedThingProvider.add(thing); managedThingProvider.add(thing);
managedItemChannelLinkProvider.add(new ItemChannelLink(itemName, CHANNEL_UID)); managedItemChannelLinkProvider.add(new ItemChannelLink(itemName, CHANNEL_UID));
waitForAssert(() -> assertThat(itemChannelLinkRegistry.getLinks(itemName).size(), is(1)));
class ThingHandlerState { class ThingHandlerState {
@Nullable @Nullable
@ -1218,7 +1221,6 @@ public class ThingManagerOSGiTest extends JavaOSGiTest {
final List<Event> receivedEvents = new ArrayList<>(); final List<Event> receivedEvents = new ArrayList<>();
@NonNullByDefault
EventSubscriber thingStatusEventSubscriber = new EventSubscriber() { EventSubscriber thingStatusEventSubscriber = new EventSubscriber() {
@Override @Override
public Set<String> getSubscribedEventTypes() { public Set<String> getSubscribedEventTypes() {
@ -1315,7 +1317,6 @@ public class ThingManagerOSGiTest extends JavaOSGiTest {
final List<ThingStatusInfoChangedEvent> infoChangedEvents = new ArrayList<>(); final List<ThingStatusInfoChangedEvent> infoChangedEvents = new ArrayList<>();
@NonNullByDefault
EventSubscriber thingStatusEventSubscriber = new EventSubscriber() { EventSubscriber thingStatusEventSubscriber = new EventSubscriber() {
@Override @Override
public Set<String> getSubscribedEventTypes() { public Set<String> getSubscribedEventTypes() {
@ -1398,7 +1399,7 @@ public class ThingManagerOSGiTest extends JavaOSGiTest {
thingStatusInfoI18nLocalizationService.setBundleResolver(bundleResolver); thingStatusInfoI18nLocalizationService.setBundleResolver(bundleResolver);
final List<ThingStatusInfoEvent> infoEvents = new ArrayList<>(); final List<ThingStatusInfoEvent> infoEvents = new ArrayList<>();
@NonNullByDefault
EventSubscriber thingStatusInfoEventSubscriber = new EventSubscriber() { EventSubscriber thingStatusInfoEventSubscriber = new EventSubscriber() {
@Override @Override
public Set<String> getSubscribedEventTypes() { public Set<String> getSubscribedEventTypes() {
@ -1418,7 +1419,7 @@ public class ThingManagerOSGiTest extends JavaOSGiTest {
registerService(thingStatusInfoEventSubscriber); registerService(thingStatusInfoEventSubscriber);
final List<ThingStatusInfoChangedEvent> infoChangedEvents = new ArrayList<>(); final List<ThingStatusInfoChangedEvent> infoChangedEvents = new ArrayList<>();
@NonNullByDefault
EventSubscriber thingStatusInfoChangedEventSubscriber = new EventSubscriber() { EventSubscriber thingStatusInfoChangedEventSubscriber = new EventSubscriber() {
@Override @Override
public Set<String> getSubscribedEventTypes() { public Set<String> getSubscribedEventTypes() {

View File

@ -159,8 +159,7 @@ public class VoiceManagerImplTest extends JavaOSGiTest {
} }
@Test @Test
public void interpretSomethingWhenTheDefaultHliIsSetAndItIsARegisteredService() public void interpretSomethingWhenTheDefaultHliIsSetAndItIsARegisteredService() throws Exception {
throws IOException, InterpretationException {
hliStub = new HumanLanguageInterpreterStub(); hliStub = new HumanLanguageInterpreterStub();
registerService(hliStub); registerService(hliStub);
@ -171,10 +170,7 @@ public class VoiceManagerImplTest extends JavaOSGiTest {
configuration.update(voiceConfig); configuration.update(voiceConfig);
// Wait some time to be sure that the configuration will be updated // Wait some time to be sure that the configuration will be updated
try { Thread.sleep(2000);
Thread.sleep(1000);
} catch (InterruptedException e) {
}
String result = voiceManager.interpret("something", null); String result = voiceManager.interpret("something", null);
assertThat(result, is("Interpreted text")); assertThat(result, is("Interpreted text"));
@ -368,7 +364,7 @@ public class VoiceManagerImplTest extends JavaOSGiTest {
} }
@Test @Test
public void startDialogWithoutPassingAnyParameters() throws IOException, InterruptedException { public void startDialogWithoutPassingAnyParameters() throws Exception {
sttService = new STTServiceStub(); sttService = new STTServiceStub();
ksService = new KSServiceStub(); ksService = new KSServiceStub();
hliStub = new HumanLanguageInterpreterStub(); hliStub = new HumanLanguageInterpreterStub();
@ -390,10 +386,7 @@ public class VoiceManagerImplTest extends JavaOSGiTest {
configuration.update(config); configuration.update(config);
// Wait some time to be sure that the configuration will be updated // Wait some time to be sure that the configuration will be updated
try { Thread.sleep(2000);
Thread.sleep(1000);
} catch (InterruptedException e) {
}
voiceManager.startDialog(); voiceManager.startDialog();

View File

@ -63,18 +63,20 @@ public class BundleInfoReader {
private void readBindingInfo(Path ohinfPath, BundleInfo bundleInfo) throws IOException { private void readBindingInfo(Path ohinfPath, BundleInfo bundleInfo) throws IOException {
BindingInfoReader reader = new BindingInfoReader(); BindingInfoReader reader = new BindingInfoReader();
xmlPathStream(ohinfPath, "binding").forEach(path -> { try (Stream<Path> xmlPathStream = xmlPathStream(ohinfPath, "binding")) {
log.info("Reading: " + path); xmlPathStream.forEach(path -> {
try { log.info("Reading: " + path);
BindingInfoXmlResult bindingInfoXml = reader.readFromXML(path.toUri().toURL()); try {
if (bindingInfoXml != null) { BindingInfoXmlResult bindingInfoXml = reader.readFromXML(path.toUri().toURL());
bundleInfo.setBindingId(bindingInfoXml.getBindingInfo().getUID()); if (bindingInfoXml != null) {
bundleInfo.setBindingInfoXml(bindingInfoXml); bundleInfo.setBindingId(bindingInfoXml.getBindingInfo().getUID());
bundleInfo.setBindingInfoXml(bindingInfoXml);
}
} catch (ConversionException | MalformedURLException e) {
log.warn("Exception while reading binding info from: " + path, e);
} }
} catch (ConversionException | MalformedURLException e) { });
log.warn("Exception while reading binding info from: " + path, e); }
}
});
} }
private void readConfigInfo(Path ohinfPath, BundleInfo bundleInfo) throws IOException { private void readConfigInfo(Path ohinfPath, BundleInfo bundleInfo) throws IOException {

View File

@ -17,19 +17,22 @@ import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.openhab.core.tools.i18n.plugin.DefaultTranslationsGenerationMode.*; import static org.openhab.core.tools.i18n.plugin.DefaultTranslationsGenerationMode.*;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.UncheckedIOException; import java.io.UncheckedIOException;
import java.nio.file.DirectoryStream; import java.nio.file.DirectoryStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Comparator;
import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.SystemStreamLog; import org.apache.maven.plugin.logging.SystemStreamLog;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
/** /**
* Tests {@link GenerateDefaultTranslationsMojo}. * Tests {@link GenerateDefaultTranslationsMojo}.
@ -39,7 +42,6 @@ import org.junit.jupiter.api.io.TempDir;
@NonNullByDefault @NonNullByDefault
public class GenerateDefaultTranslationsMojoTest { public class GenerateDefaultTranslationsMojoTest {
@TempDir
public @NonNullByDefault({}) Path tempPath; public @NonNullByDefault({}) Path tempPath;
public @NonNullByDefault({}) Path tempI18nPath; public @NonNullByDefault({}) Path tempI18nPath;
@ -92,7 +94,8 @@ public class GenerateDefaultTranslationsMojoTest {
} }
@BeforeEach @BeforeEach
public void before() { public void before() throws IOException {
tempPath = Files.createTempDirectory("i18n-");
tempI18nPath = tempPath.resolve("OH-INF/i18n"); tempI18nPath = tempPath.resolve("OH-INF/i18n");
mojo = new GenerateDefaultTranslationsMojo(); mojo = new GenerateDefaultTranslationsMojo();
@ -101,9 +104,21 @@ public class GenerateDefaultTranslationsMojoTest {
mojo.setTargetDirectory(tempI18nPath.toFile()); mojo.setTargetDirectory(tempI18nPath.toFile());
} }
@AfterEach
public void afterEach() {
try {
Files.walk(tempPath).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
} catch (IOException e) {
// XStream does not close the HierarchicalStreamReader created in XStream.fromXML(URL)
// which causes issues when deleting the temporary path on Windows.
// See: https://github.com/x-stream/xstream/pull/287
tempPath.toFile().deleteOnExit();
}
}
private void assertSameProperties(Path expectedPath, Path actualPath) throws IOException { private void assertSameProperties(Path expectedPath, Path actualPath) throws IOException {
String expected = Files.readString(expectedPath); String expected = Files.readAllLines(expectedPath).stream().collect(Collectors.joining(System.lineSeparator()));
String actual = Files.readString(actualPath); String actual = Files.readAllLines(actualPath).stream().collect(Collectors.joining(System.lineSeparator()));
assertThat(expected, equalTo(actual)); assertThat(expected, equalTo(actual));
} }