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

View File

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

View File

@ -107,7 +107,7 @@ class ScriptFileWatcherTest {
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
@ -125,7 +125,7 @@ class ScriptFileWatcherTest {
// verify is called when the start level increases
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
@ -145,7 +145,7 @@ class ScriptFileWatcherTest {
// verify is called when the start level increases
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
@ -170,12 +170,12 @@ class ScriptFileWatcherTest {
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()));
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
@ -200,12 +200,12 @@ class ScriptFileWatcherTest {
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()));
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
@ -236,7 +236,7 @@ class ScriptFileWatcherTest {
scheduledTask.getValue().run();
// 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
@ -258,11 +258,11 @@ class ScriptFileWatcherTest {
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());
inOrder.verify(scriptEngineManager, timeout(1000).times(1)).createScriptEngine("js",
inOrder.verify(scriptEngineManager, timeout(10000).times(1)).createScriptEngine("js",
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());
}
@ -281,7 +281,7 @@ class ScriptFileWatcherTest {
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
@ -344,6 +344,6 @@ class ScriptFileWatcherTest {
scriptFileWatcher.processWatchEvent(null, ENTRY_MODIFY, p);
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
public class ExecUtilTest {
private static final Duration TIMEOUT = Duration.ofSeconds(10);
@Test
public void testBasicExecuteCommandLine() {
if (isWindowsSystem()) {
@ -40,9 +42,9 @@ public class ExecUtilTest {
public void testBasicExecuteCommandLineAndWaitResponse() {
final String result;
if (isWindowsSystem()) {
result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofSeconds(1), "cmd", "/c", "dir");
result = ExecUtil.executeCommandLineAndWaitResponse(TIMEOUT, "cmd", "/c", "dir");
} else {
result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofSeconds(1), "ls");
result = ExecUtil.executeCommandLineAndWaitResponse(TIMEOUT, "ls");
}
assertNotNull(result);
assertNotEquals("", result);
@ -52,9 +54,9 @@ public class ExecUtilTest {
public void testExecuteCommandLineAndWaitResponseWithArguments() {
final String result;
if (isWindowsSystem()) {
result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofSeconds(1), "cmd", "/c", "echo", "test");
result = ExecUtil.executeCommandLineAndWaitResponse(TIMEOUT, "cmd", "/c", "echo", "test");
} else {
result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofSeconds(1), "echo", "'test'");
result = ExecUtil.executeCommandLineAndWaitResponse(TIMEOUT, "echo", "'test'");
}
assertNotNull(result);
assertNotEquals("test", result);
@ -69,10 +71,9 @@ public class ExecUtilTest {
public void testExecuteCommandLineAndWaitStdErrRedirection() {
final String result;
if (isWindowsSystem()) {
result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofSeconds(1), "cmd", "/c", "dir", "xxx.xxx",
"1>", "nul");
result = ExecUtil.executeCommandLineAndWaitResponse(TIMEOUT, "cmd", "/c", "dir", "xxx.xxx", "1>", "nul");
} else {
result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofSeconds(1), "ls", "xxx.xxx");
result = ExecUtil.executeCommandLineAndWaitResponse(TIMEOUT, "ls", "xxx.xxx");
}
assertNotNull(result);
assertNotEquals("", result);

View File

@ -71,7 +71,7 @@ public class TimerImplTest {
assertThat(subject.hasTerminated(), 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.hasTerminated(), is(true));
assertThat(subject.isCancelled(), is(false));
@ -79,7 +79,7 @@ public class TimerImplTest {
@Test
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.hasTerminated(), is(true));
assertThat(subject.isCancelled(), is(false));
@ -89,7 +89,7 @@ public class TimerImplTest {
assertThat(subject.hasTerminated(), 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.hasTerminated(), is(true));
assertThat(subject.isCancelled(), is(false));
@ -106,7 +106,7 @@ public class TimerImplTest {
assertThat(subject.hasTerminated(), 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.hasTerminated(), is(true));
assertThat(subject.isCancelled(), is(false));

View File

@ -22,12 +22,30 @@
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- 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>
</configuration>
</plugin>
</plugins>
</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>
</project>

View File

@ -62,7 +62,7 @@ public class SafeCallerImplTest extends JavaTest {
private static final int THREAD_POOL_SIZE = 3;
// 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
private static final int TIMEOUT = 500;
@ -197,7 +197,7 @@ public class SafeCallerImplTest extends JavaTest {
assertDurationBetween(TIMEOUT - GRACE, BLOCK - GRACE, () -> {
safeCaller.create(mock, Runnable.class).withTimeout(TIMEOUT).build().run();
});
assertDurationBelow(GRACE, () -> {
assertDurationBelow(2 * GRACE, () -> {
safeCaller.create(mock, Runnable.class).withTimeout(TIMEOUT).build().run();
});
assertDurationBetween(TIMEOUT - GRACE, BLOCK + GRACE, () -> {
@ -214,17 +214,17 @@ public class SafeCallerImplTest extends JavaTest {
configureSingleThread();
spawn(() -> {
assertDurationBetween(TIMEOUT - GRACE, BLOCK - GRACE, () -> {
assertDurationBetween(0, BLOCK - GRACE, () -> {
safeCaller.create(mock, Runnable.class).withTimeout(TIMEOUT).build().run();
});
});
sleep(GRACE); // give it a chance to start
spawn(() -> {
assertDurationBelow(GRACE, () -> {
assertDurationBelow(3 * GRACE, () -> {
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(() -> {
verify(mock, times(2)).run();
});
@ -273,7 +273,7 @@ public class SafeCallerImplTest extends JavaTest {
assertDurationBetween(TIMEOUT - GRACE, BLOCK - GRACE, () -> {
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();
});
}
@ -369,9 +369,8 @@ public class SafeCallerImplTest extends JavaTest {
thingHandler.method();
}, Runnable.class).build().run();
Object res = safeCaller.create((Function<String, String>) name -> {
return "Hello " + name + "!";
}, Function.class).build().apply("World");
Object res = safeCaller.create((Function<String, String>) name -> ("Hello " + name + "!"), Function.class)
.build().apply("World");
assertThat(res, is("Hello World!"));
}
@ -382,7 +381,7 @@ public class SafeCallerImplTest extends JavaTest {
assertDurationBelow(GRACE, () -> {
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
@ -406,7 +405,7 @@ public class SafeCallerImplTest extends JavaTest {
Runnable mock1 = mock(Runnable.class);
doThrow(RuntimeException.class).when(mock1).run();
assertDurationBelow(GRACE, () -> {
assertDurationBelow(2 * GRACE, () -> {
safeCaller.create(mock1, Runnable.class).withTimeout(TIMEOUT).withAsync().withIdentifier(identifier)
.onTimeout(timeoutHandlerMock).onException(errorHandlerMock).build().run();
});
@ -431,6 +430,7 @@ public class SafeCallerImplTest extends JavaTest {
});
waitForAssert(() -> verify(mock1, times(1)).run());
waitForAssert(() -> verify(mock2, times(1)).run());
sleep(GRACE);
verifyNoMoreInteractions(mock1, mock2);
}
@ -498,7 +498,7 @@ public class SafeCallerImplTest extends JavaTest {
.build().run();
});
}
assertDurationBetween(BLOCK - GRACE, BLOCK + TIMEOUT + GRACE, () -> {
assertDurationBetween(BLOCK - GRACE, BLOCK + TIMEOUT + THREAD_POOL_SIZE * 2 * GRACE, () -> {
waitForAssert(() -> {
verify(mock, times(THREAD_POOL_SIZE * 2)).run();
});

View File

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

View File

@ -39,11 +39,11 @@ public class PeriodicSchedulerImplTest {
private final PeriodicSchedulerImpl periodicScheduler = new PeriodicSchedulerImpl(new SchedulerImpl());
@Test
@Timeout(value = 5, unit = TimeUnit.SECONDS)
@Timeout(value = 10, unit = TimeUnit.SECONDS)
public void testSchedule() throws InterruptedException, IOException {
Queue<Long> times = new ArrayDeque<>();
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();
ScheduledCompletableFuture<Object> future = periodicScheduler.schedule(() -> {
@ -54,11 +54,11 @@ public class PeriodicSchedulerImplTest {
future.cancel(true);
// 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.
long offset = times.poll();
long[] expectedResults = { 200, 300, 300, 300, 300 };
long offset = times.poll().longValue();
long[] expectedResults = { 200, 400, 400, 400, 400 };
for (long expectedResult : expectedResults) {
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);
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.Nullable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.openhab.core.JavaTest;
import org.openhab.core.scheduler.ScheduledCompletableFuture;
import org.openhab.core.scheduler.SchedulerRunnable;
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
*/
@NonNullByDefault
public class SchedulerImplTest {
private SchedulerImpl scheduler = new SchedulerImpl();
public class SchedulerImplTest extends JavaTest {
private @NonNullByDefault({}) SchedulerImpl scheduler;
@BeforeEach
public void beforeEach() {
scheduler = new SchedulerImpl();
}
@Test
@Timeout(value = 500, unit = TimeUnit.MILLISECONDS)
@Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testAfterCancelled() throws InterruptedException, InvocationTargetException, ExecutionException {
AtomicBoolean check = new AtomicBoolean();
Callable<Boolean> callable = () -> check.getAndSet(true);
@ -68,7 +75,7 @@ public class SchedulerImplTest {
}
@Test
@Timeout(value = 300, unit = TimeUnit.MILLISECONDS)
@Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testAfterResolved() throws InterruptedException, ExecutionException {
AtomicInteger check = new AtomicInteger();
Callable<Integer> callable = () -> {
@ -76,21 +83,21 @@ public class SchedulerImplTest {
return 5;
};
ScheduledCompletableFuture<Integer> after = scheduler.after(callable, Duration.ofMillis(100));
Thread.sleep(200);
Thread.sleep(500);
assertTrue(after.isDone(), "Scheduled job should finish done");
assertEquals(10, check.get(), "After CompletableFuture should return correct value");
assertEquals(5, after.get().intValue(), "After CompletableFuture should return correct value");
}
@Test
@Timeout(value = 300, unit = TimeUnit.MILLISECONDS)
@Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testAfterResolvedWithException() throws InterruptedException {
Callable<Void> callable = () -> {
// Pass a exception not very likely thrown by the scheduler it self to avoid missing real exceptions.
throw new FileNotFoundException("testBeforeTimeoutException");
};
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.getPromise().isCompletedExceptionally(),
"After CompletableFuture should have completed with an exception");
@ -105,11 +112,11 @@ public class SchedulerImplTest {
}
@Test
@Timeout(value = 300, unit = TimeUnit.MILLISECONDS)
@Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testBeforeTimeoutException() throws InterruptedException, ExecutionException {
CompletableFuture<Integer> d = new CompletableFuture<>();
ScheduledCompletableFuture<Integer> before = scheduler.before(d, Duration.ofMillis(100));
Thread.sleep(200);
Thread.sleep(500);
d.complete(3);
d.get();
assertTrue(before.isDone(), "Scheduled job should be done");
@ -125,7 +132,7 @@ public class SchedulerImplTest {
}
@Test
@Timeout(value = 300, unit = TimeUnit.MILLISECONDS)
@Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testBeforeCancelled() throws InterruptedException, InvocationTargetException, ExecutionException {
CompletableFuture<Integer> d = new CompletableFuture<>();
ScheduledCompletableFuture<Integer> before = scheduler.before(d, Duration.ofMillis(100));
@ -137,7 +144,7 @@ public class SchedulerImplTest {
}
@Test
@Timeout(value = 300, unit = TimeUnit.MILLISECONDS)
@Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testBeforeResolved() throws InterruptedException, ExecutionException {
CompletableFuture<Boolean> d = new CompletableFuture<>();
ScheduledCompletableFuture<Boolean> before = scheduler.before(d, Duration.ofMillis(100));
@ -147,7 +154,7 @@ public class SchedulerImplTest {
}
@Test
@Timeout(value = 300, unit = TimeUnit.MILLISECONDS)
@Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testBeforeResolvedWithException() throws InterruptedException {
CompletableFuture<Integer> d = new CompletableFuture<>();
ScheduledCompletableFuture<Integer> before = scheduler.before(d, Duration.ofMillis(100));
@ -166,11 +173,11 @@ public class SchedulerImplTest {
}
@Test
@Timeout(value = 300, unit = TimeUnit.MILLISECONDS)
@Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testAfterTimeoutException() throws InterruptedException, ExecutionException {
CompletableFuture<Integer> d = new CompletableFuture<>();
ScheduledCompletableFuture<Integer> before = scheduler.before(d, Duration.ofMillis(100));
Thread.sleep(200);
Thread.sleep(500);
d.complete(3);
d.get();
assertTrue(before.isDone(), "Scheduled job should be done");
@ -186,34 +193,33 @@ public class SchedulerImplTest {
}
@Test
@Timeout(value = 1, unit = TimeUnit.SECONDS)
@Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testSchedule() throws InterruptedException {
Semaphore s = new Semaphore(0);
TestSchedulerWithCounter temporalAdjuster = new TestSchedulerWithCounter();
scheduler.schedule(s::release, temporalAdjuster);
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(3, temporalAdjuster.getCount(), "Scheduler should have run 3 times");
}
@Test
@Timeout(value = 1, unit = TimeUnit.SECONDS)
@Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testScheduleCancel() throws InterruptedException {
Semaphore s = new Semaphore(0);
TestSchedulerWithCounter temporalAdjuster = new TestSchedulerWithCounter();
ScheduledCompletableFuture<Void> schedule = scheduler.schedule(s::release, temporalAdjuster);
s.acquire(1);
Thread.sleep(50);
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(1, temporalAdjuster.getCount(), "Scheduler should have run 1 time");
}
@Test
@Timeout(value = 1, unit = TimeUnit.SECONDS)
@Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testScheduleException() throws InterruptedException {
Semaphore s = new Semaphore(0);
TestSchedulerWithCounter temporalAdjuster = new TestSchedulerWithCounter();
@ -230,25 +236,25 @@ public class SchedulerImplTest {
return null;
});
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, temporalAdjuster.getCount(), "Scheduler should have run 0 time");
}
@Test
@Timeout(value = 300, unit = TimeUnit.MILLISECONDS)
@Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testNegative() throws InterruptedException {
Semaphore s = new Semaphore(0);
scheduler.after(() -> {
s.release(1);
return null;
}, Duration.ofMillis(-1000));
Thread.sleep(200);
assertEquals(1, s.availablePermits(), "Negative value should mean after finished right away");
waitForAssert(
() -> assertEquals(1, s.availablePermits(), "Negative value should mean after finished right away"));
}
@Test
@Timeout(value = 1, unit = TimeUnit.SECONDS)
@Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testDelay() throws InterruptedException {
long duration = 5000;
ScheduledCompletableFuture<Instant> future = scheduler.after(Duration.ofMillis(duration));
@ -260,7 +266,7 @@ public class SchedulerImplTest {
}
@Test
@Timeout(value = 1, unit = TimeUnit.SECONDS)
@Timeout(value = 15, unit = TimeUnit.SECONDS)
public void testCompareTo() throws InterruptedException {
long duration = 5000;
ScheduledCompletableFuture<Instant> future1 = scheduler.after(Duration.ofMillis(duration));
@ -305,7 +311,7 @@ public class SchedulerImplTest {
@Override
public Temporal adjustInto(@NonNullByDefault({}) Temporal arg0) {
return arg0.plus(100, ChronoUnit.MILLIS);
return arg0.plus(600, ChronoUnit.MILLIS);
}
@Override

View File

@ -24,6 +24,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
@ -79,7 +80,7 @@ import org.osgi.service.component.ComponentContext;
@NonNullByDefault
public class InboxOSGiTest extends JavaOSGiTest {
class DiscoveryService1 extends AbstractDiscoveryService {
static class DiscoveryService1 extends AbstractDiscoveryService {
public DiscoveryService1() {
super(5);
}
@ -89,7 +90,7 @@ public class InboxOSGiTest extends JavaOSGiTest {
}
}
class DiscoveryService2 extends AbstractDiscoveryService {
static class DiscoveryService2 extends AbstractDiscoveryService {
public DiscoveryService2() {
super(5);
}
@ -195,12 +196,44 @@ public class InboxOSGiTest extends JavaOSGiTest {
@AfterEach
public void cleanUp() {
discoveryResults.forEach((thingUID, discoveryResult) -> inbox.remove(thingUID));
inboxListeners.forEach(listener -> inbox.removeInboxListener(listener));
discoveryResults.clear();
inboxListeners.clear();
Set<String> inboxThingUIDsToRemove = inbox.getAll().stream().map(DiscoveryResult::getThingUID)
.map(ThingUID::toString).collect(Collectors.toSet());
Set<String> removedInboxThingUIDs = new HashSet<>();
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);
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) {
@ -963,7 +996,7 @@ public class InboxOSGiTest extends JavaOSGiTest {
assertTrue(thingProperty.equals(descResultParam));
}
services.forEach(obj -> unregisterService(obj));
services.forEach(this::unregisterService);
}
@Test

View File

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

View File

@ -72,7 +72,6 @@ public class ScriptEngineOSGiTest extends JavaOSGiTest {
assertNotNull(itemRegistry);
itemProvider = new ItemProvider() {
@Override
public void addProviderChangeListener(ProviderChangeListener<Item> listener) {
}
@ -90,6 +89,8 @@ public class ScriptEngineOSGiTest extends JavaOSGiTest {
registerService(itemProvider);
waitForAssert(() -> assertThat(itemRegistry.getAll().size(), is(itemProvider.getAll().size())));
ScriptServiceUtil scriptServiceUtil = getService(ScriptServiceUtil.class);
assertNotNull(scriptServiceUtil);
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.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.model.core.ModelRepository;
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()));
Collection<Thing> actualThings = thingRegistry.getAll();
assertThat(actualThings.size(), is(3));
Collection<Item> items = itemRegistry.getItems();
assertThat(items.size(), is(0));
Collection<ItemChannelLink> itemChannelLinks = itemChannelLinkRegistry.getAll();
assertThat(itemChannelLinks.size(), is(0));
waitForAssert(() -> {
assertThat(thingRegistry.getAll().size(), is(3));
assertThat(itemRegistry.getItems().size(), is(0));
assertThat(itemChannelLinkRegistry.getAll().size(), is(0));
});
String itemsModel = "Color Light3Color \"Light3 Color\" { channel=\"hue:LCT001:huebridge:bulb3:color\" }";
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());
assertThat(actualItemChannelLinks.size(), is(1));
assertThat(actualItemChannelLinks.get(0).toString(),
is(equalTo("Light3Color -> hue:LCT001:huebridge:bulb3:color")));
});
}
@Test
@ -120,22 +117,19 @@ public class GenericItemChannelLinkProviderTest extends JavaOSGiTest {
"}";
modelRepository.addOrRefreshModel(THINGS_TESTMODEL_NAME, new ByteArrayInputStream(thingsModel.getBytes()));
Collection<Thing> actualThings = thingRegistry.getAll();
assertThat(actualThings.size(), is(3));
Collection<Item> items = itemRegistry.getItems();
assertThat(items.size(), is(0));
Collection<ItemChannelLink> itemChannelLinks = itemChannelLinkRegistry.getAll();
assertThat(itemChannelLinks.size(), is(0));
waitForAssert(() -> {
assertThat(thingRegistry.getAll().size(), is(3));
assertThat(itemRegistry.getItems().size(), is(0));
assertThat(itemChannelLinkRegistry.getAll().size(), is(0));
});
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()));
Collection<Item> actualItems = itemRegistry.getItems();
assertThat(actualItems.size(), is(1));
waitForAssert(() -> {
assertThat(itemRegistry.getItems().size(), is(1));
List<ItemChannelLink> actualItemChannelLinks = new ArrayList<>(itemChannelLinkRegistry.getAll());
assertThat(actualItemChannelLinks.size(), is(2));
@ -143,5 +137,6 @@ public class GenericItemChannelLinkProviderTest extends JavaOSGiTest {
is(equalTo("Light3Color -> hue:LCT001:huebridge:bulb3:color")));
assertThat(actualItemChannelLinks.get(1).toString(),
is(equalTo("Light3Color -> hue:LCT001:huebridge:bulb4:color")));
});
}
}

View File

@ -495,7 +495,11 @@ public class GenericThingProviderTest extends JavaOSGiTest {
@Override
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));
String model = "Bridge hue:bridge:myBridge [ ip = \"1.2.3.4\", username = \"123\" ] {" + //
" LCT001 bulb1 [ lightId = \"1\" ] { Switch : notification }" + //
String model = "Bridge hue:bridge:my1234Bridge [ ip = \"1.2.3.4\", username = \"123\" ] {" + //
" LCT001 myKitchenBulb1 [ lightId = \"1\" ] { Switch : notification }" + //
"}";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
@ -515,8 +519,8 @@ public class GenericThingProviderTest extends JavaOSGiTest {
});
receivedEvents.clear();
String newModel = "Bridge hue:bridge:myBridge [ ip = \"1.2.3.4\", username = \"123\" ] {" + //
" LCT001 bulb1 [ lightId = \"2\" ] { Switch : notification }" + //
String newModel = "Bridge hue:bridge:my1234Bridge [ ip = \"1.2.3.4\", username = \"123\" ] {" + //
" LCT001 myKitchenBulb1 [ lightId = \"2\" ] { Switch : notification }" + //
"}";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(newModel.getBytes()));
@ -527,7 +531,7 @@ public class GenericThingProviderTest extends JavaOSGiTest {
Event event = receivedEvents.get(0);
assertEquals(ThingUpdatedEvent.class, event.getClass());
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.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
@ -208,13 +209,8 @@ public class ChannelCommandDescriptionProviderOSGiTest extends JavaOSGiTest {
registerService(new TestDynamicCommandDescriptionProvider(), DynamicCommandDescriptionProvider.class.getName());
Thing thing = thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"), new ThingUID("hue:lamp:lamp1"),
null, "test thing", new Configuration());
assertNotNull(thing);
if (thing == null) {
throw new IllegalStateException("thing is null");
}
Thing thing = Objects.requireNonNull(thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"),
new ThingUID("hue:lamp:lamp1"), null, "test thing", new Configuration()));
managedThingProvider.add(thing);
ItemChannelLink link = new ItemChannelLink("TestItem1", getChannel(thing, "1").getUID());
@ -230,9 +226,10 @@ public class ChannelCommandDescriptionProviderOSGiTest extends JavaOSGiTest {
Item item = itemRegistry.getItem("TestItem1");
assertEquals(CoreItemFactory.NUMBER, item.getType());
CommandDescription command = item.getCommandDescription();
assertNotNull(command);
final Item finalItem = item;
waitForAssert(() -> assertNotNull(finalItem.getCommandDescription()));
CommandDescription command = Objects.requireNonNull(item.getCommandDescription());
List<CommandOption> opts = command.getCommandOptions();
assertNotNull(opts);
assertEquals(1, opts.size());
@ -280,12 +277,8 @@ public class ChannelCommandDescriptionProviderOSGiTest extends JavaOSGiTest {
DynamicCommandDescriptionProvider.class.getName());
registerService(new TestDynamicCommandDescriptionProvider(), DynamicCommandDescriptionProvider.class.getName());
Thing thing = thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"), new ThingUID("hue:lamp:lamp1"),
null, "test thing", new Configuration());
assertNotNull(thing);
if (thing == null) {
throw new IllegalStateException("thing is null");
}
Thing thing = Objects.requireNonNull(thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"),
new ThingUID("hue:lamp:lamp1"), null, "test thing", new Configuration()));
managedThingProvider.add(thing);
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();
assertFalse(items.isEmpty());
Item item = itemRegistry.getItem("TestItem7_2");
CommandDescription command = item.getCommandDescription();
assertNotNull(command);
final Item item = itemRegistry.getItem("TestItem7_2");
waitForAssert(() -> assertNotNull(item.getCommandDescription()));
CommandDescription command = Objects.requireNonNull(item.getCommandDescription());
List<CommandOption> opts = command.getCommandOptions();
assertNotNull(opts);
assertEquals(1, opts.size());
@ -310,7 +302,7 @@ public class ChannelCommandDescriptionProviderOSGiTest extends JavaOSGiTest {
/*
* Helper
*/
class TestDynamicCommandDescriptionProvider extends BaseDynamicCommandDescriptionProvider {
static class TestDynamicCommandDescriptionProvider extends BaseDynamicCommandDescriptionProvider {
final CommandDescription newCommand = CommandDescriptionBuilder.create()
.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
public @Nullable CommandDescription getCommandDescription(Channel channel,
@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;
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) {
super(thing);
}

View File

@ -14,15 +14,18 @@ package org.openhab.core.thing.internal;
import static org.hamcrest.CoreMatchers.*;
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.eq;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@ -36,7 +39,13 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
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.events.AbstractItemRegistryEvent;
import org.openhab.core.items.events.ItemRemovedEvent;
import org.openhab.core.library.CoreItemFactory;
import org.openhab.core.library.items.NumberItem;
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.builder.ChannelBuilder;
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.ItemChannelLinkRegistry;
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.ChannelTypeUID;
import org.openhab.core.thing.util.ThingHandlerHelper;
import org.openhab.core.types.Command;
import org.openhab.core.util.BundleResolver;
import org.osgi.framework.Bundle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 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 int CHANNEL_COUNT = 5;
private final Logger logger = LoggerFactory.getLogger(ChannelLinkNotifierOSGiTest.class);
private int thingCount;
private @NonNullByDefault({}) ItemChannelLinkRegistry itemChannelLinkRegistry;
@ -178,12 +197,59 @@ public class ChannelLinkNotifierOSGiTest extends JavaOSGiTest {
@AfterEach
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()
.forEach(itemChannelLink -> managedItemChannelLinkProvider.remove(itemChannelLink.getUID()));
managedItemProvider.getAll().forEach(item -> managedItemProvider.remove(item.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) {
@ -232,11 +298,38 @@ public class ChannelLinkNotifierOSGiTest extends JavaOSGiTest {
}
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 -> {
String itemName = getItemName(thing, channelUID, itemSuffix);
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) {
@ -270,10 +363,12 @@ public class ChannelLinkNotifierOSGiTest extends JavaOSGiTest {
private void assertAllChannelsLinkedBasedOnEvents(Thing thing, int eventCount) {
TestHandler handler = getHandler(thing);
waitForAssert(() -> {
forEachThingChannelUID(thing, channelUID -> {
assertThat(handler.isLinkedBasedOnEvent(channelUID), is(true));
assertThat(handler.getChannelLinkEvents(channelUID).size(), is(eventCount));
});
});
}
private void assertAllChannelsUnlinkedBasedOnEvents(Thing thing, int eventCount) {

View File

@ -21,6 +21,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
@ -237,13 +238,8 @@ public class ChannelStateDescriptionProviderOSGiTest extends JavaOSGiTest {
registerService(new TestDynamicStateDescriptionProvider(), DynamicStateDescriptionProvider.class.getName());
Thing thing = thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"), new ThingUID("hue:lamp:lamp1"),
null, "test thing", new Configuration());
assertNotNull(thing);
if (thing == null) {
throw new IllegalStateException("thing is null");
}
Thing thing = Objects.requireNonNull(thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"),
new ThingUID("hue:lamp:lamp1"), null, "test thing", new Configuration()));
managedThingProvider.add(thing);
ItemChannelLink link = new ItemChannelLink("TestItem", getChannel(thing, "1").getUID());
@ -269,9 +265,10 @@ public class ChannelStateDescriptionProviderOSGiTest extends JavaOSGiTest {
Item item = itemRegistry.getItem("TestItem");
assertEquals(CoreItemFactory.NUMBER, item.getType());
StateDescription state = item.getStateDescription();
assertNotNull(state);
final Item finalItem = item;
waitForAssert(() -> assertNotNull(finalItem.getStateDescription()));
StateDescription state = Objects.requireNonNull(item.getStateDescription());
assertEquals(BigDecimal.ZERO, state.getMinimum());
assertEquals(BigDecimal.valueOf(100), state.getMaximum());
assertEquals(BigDecimal.TEN, state.getStep());
@ -384,13 +381,8 @@ public class ChannelStateDescriptionProviderOSGiTest extends JavaOSGiTest {
DynamicStateDescriptionProvider.class.getName());
registerService(new TestDynamicStateDescriptionProvider(), DynamicStateDescriptionProvider.class.getName());
Thing thing = thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"), new ThingUID("hue:lamp:lamp1"),
null, "test thing", new Configuration());
assertNotNull(thing);
if (thing == null) {
throw new IllegalStateException("thing is null");
}
Thing thing = Objects.requireNonNull(thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"),
new ThingUID("hue:lamp:lamp1"), null, "test thing", new Configuration()));
managedThingProvider.add(thing);
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");
StateDescription state = item.getStateDescription();
assertNotNull(state);
final Item finalItem = item;
waitForAssert(() -> assertNotNull(finalItem.getStateDescription()));
StateDescription state = Objects.requireNonNull(item.getStateDescription());
assertEquals(BigDecimal.valueOf(1), state.getMinimum());
assertEquals(BigDecimal.valueOf(101), state.getMaximum());
assertEquals(BigDecimal.valueOf(20), state.getStep());
@ -421,7 +414,7 @@ public class ChannelStateDescriptionProviderOSGiTest extends JavaOSGiTest {
/*
* Helper
*/
class TestDynamicStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {
static class TestDynamicStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {
final @Nullable StateDescription newState = StateDescriptionFragmentBuilder.create()
.withMinimum(BigDecimal.valueOf(10)).withMaximum(BigDecimal.valueOf(100))
.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
public @Nullable StateDescription getStateDescription(Channel channel, @Nullable StateDescription original,
@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;
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) {
super(thing);
}

View File

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

View File

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

View File

@ -63,7 +63,8 @@ public class BundleInfoReader {
private void readBindingInfo(Path ohinfPath, BundleInfo bundleInfo) throws IOException {
BindingInfoReader reader = new BindingInfoReader();
xmlPathStream(ohinfPath, "binding").forEach(path -> {
try (Stream<Path> xmlPathStream = xmlPathStream(ohinfPath, "binding")) {
xmlPathStream.forEach(path -> {
log.info("Reading: " + path);
try {
BindingInfoXmlResult bindingInfoXml = reader.readFromXML(path.toUri().toURL());
@ -76,6 +77,7 @@ public class BundleInfoReader {
}
});
}
}
private void readConfigInfo(Path ohinfPath, BundleInfo bundleInfo) throws IOException {
ConfigDescriptionReader reader = new ConfigDescriptionReader();

View File

@ -17,19 +17,22 @@ import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.openhab.core.tools.i18n.plugin.DefaultTranslationsGenerationMode.*;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.SystemStreamLog;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
/**
* Tests {@link GenerateDefaultTranslationsMojo}.
@ -39,7 +42,6 @@ import org.junit.jupiter.api.io.TempDir;
@NonNullByDefault
public class GenerateDefaultTranslationsMojoTest {
@TempDir
public @NonNullByDefault({}) Path tempPath;
public @NonNullByDefault({}) Path tempI18nPath;
@ -92,7 +94,8 @@ public class GenerateDefaultTranslationsMojoTest {
}
@BeforeEach
public void before() {
public void before() throws IOException {
tempPath = Files.createTempDirectory("i18n-");
tempI18nPath = tempPath.resolve("OH-INF/i18n");
mojo = new GenerateDefaultTranslationsMojo();
@ -101,9 +104,21 @@ public class GenerateDefaultTranslationsMojoTest {
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 {
String expected = Files.readString(expectedPath);
String actual = Files.readString(actualPath);
String expected = Files.readAllLines(expectedPath).stream().collect(Collectors.joining(System.lineSeparator()));
String actual = Files.readAllLines(actualPath).stream().collect(Collectors.joining(System.lineSeparator()));
assertThat(expected, equalTo(actual));
}