From 08edd3bf85bcb52d89731f4f2ed5d88759dd62ba Mon Sep 17 00:00:00 2001 From: Rex Hoffman Date: Sun, 24 Sep 2023 16:49:31 -0700 Subject: [PATCH] Try different Test structer --- .../freedesktop/FreedesktopKeyringBackend.java | 3 +++ .../internal/freedesktop/SimpleCollection.java | 4 ++++ .../javakeyring/internal/kde/KWalletBackend.java | 8 -------- .../internal/KeyringBackendFactoryTest.java | 12 +++++++++--- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/java-keyring/src/main/java/com/github/javakeyring/internal/freedesktop/FreedesktopKeyringBackend.java b/java-keyring/src/main/java/com/github/javakeyring/internal/freedesktop/FreedesktopKeyringBackend.java index d205391..4b5b0d8 100755 --- a/java-keyring/src/main/java/com/github/javakeyring/internal/freedesktop/FreedesktopKeyringBackend.java +++ b/java-keyring/src/main/java/com/github/javakeyring/internal/freedesktop/FreedesktopKeyringBackend.java @@ -52,6 +52,9 @@ public class FreedesktopKeyringBackend implements KeyringBackend { public FreedesktopKeyringBackend() throws BackendNotSupportedException { try { collection = new SimpleCollection(); + if (!collection.isConnected()) { + throw new BackendNotSupportedException("Error connecting to dbus"); + } } catch (IOException ex) { throw new BackendNotSupportedException("Error connecting to dbus", ex); } diff --git a/java-keyring/src/main/java/com/github/javakeyring/internal/freedesktop/SimpleCollection.java b/java-keyring/src/main/java/com/github/javakeyring/internal/freedesktop/SimpleCollection.java index 8efebb8..57f0c2b 100755 --- a/java-keyring/src/main/java/com/github/javakeyring/internal/freedesktop/SimpleCollection.java +++ b/java-keyring/src/main/java/com/github/javakeyring/internal/freedesktop/SimpleCollection.java @@ -82,6 +82,10 @@ public SimpleCollection() throws IOException { unlock(); } + public boolean isConnected() { + return this.session.getConnection().isConnected(); + } + /* * A user specified collection. * diff --git a/java-keyring/src/main/java/com/github/javakeyring/internal/kde/KWalletBackend.java b/java-keyring/src/main/java/com/github/javakeyring/internal/kde/KWalletBackend.java index b127156..91bebe4 100644 --- a/java-keyring/src/main/java/com/github/javakeyring/internal/kde/KWalletBackend.java +++ b/java-keyring/src/main/java/com/github/javakeyring/internal/kde/KWalletBackend.java @@ -47,14 +47,6 @@ public class KWalletBackend implements KeyringBackend { public KWalletBackend() throws BackendNotSupportedException { try { - // connection = DBusConnectionBuilder.forAddress(BusAddress.of(System.getProperty("DBUS_TCP_SESSION"))) - // .withRegisterSelf(true) - // .withShared(true) - // .transportConfig() - // .withAdditionalConfig("TIMEOUT", 10000) - // .back() - // .build(); - connection = DBusConnectionBuilder.forSessionBus().build(); wallet = connection.getRemoteObject("org.kde.kwalletd5", "/modules/kwalletd5", KWallet.class, true); wallet.localWallet(); //attempt connection to wallet diff --git a/java-keyring/src/test/java/com/github/javakeyring/internal/KeyringBackendFactoryTest.java b/java-keyring/src/test/java/com/github/javakeyring/internal/KeyringBackendFactoryTest.java index f7fad93..e5cf7ed 100644 --- a/java-keyring/src/test/java/com/github/javakeyring/internal/KeyringBackendFactoryTest.java +++ b/java-keyring/src/test/java/com/github/javakeyring/internal/KeyringBackendFactoryTest.java @@ -27,18 +27,24 @@ package com.github.javakeyring.internal; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.catchThrowable; +import static org.junit.Assume.assumeTrue; import org.junit.Test; import com.github.javakeyring.BackendNotSupportedException; +import com.github.javakeyring.internal.freedesktop.FreedesktopKeyringBackend; +import com.github.javakeyring.internal.kde.KWalletBackend; +import com.sun.jna.Platform; + public class KeyringBackendFactoryTest { @Test public void testKeyringBackendFactory() throws BackendNotSupportedException { - assertThatThrownBy(() -> KeyringBackendFactory.create(null)).isInstanceOf(BackendNotSupportedException.class); - assertThat(KeyringBackendFactory.create()).isNotNull(); + assumeTrue(Platform.isLinux()); + assumeTrue(catchThrowable(KWalletBackend::new) instanceof BackendNotSupportedException); + assertThat(catchThrowable(FreedesktopKeyringBackend::new)).as("Setup should succeed").doesNotThrowAnyException(); } }